summaryrefslogtreecommitdiff
path: root/includes/external/addressbook/node_modules/open-graph-scraper/dist/lib/extract.js
blob: cd4b802334ff50f98e5c750181509e1ab188e742 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cheerio_1 = require("cheerio");
const fallback_1 = require("./fallback");
const fields_1 = require("./fields");
const media = require("./media");
const utils = require("./utils");
/**
 * extract all of the meta tags needed for ogs
 *
 * @param {sting} body - the body of the got request
 * @param {object} options - options for ogs
 * @return {object} object with ogs results
 *
 */
function extractMetaTags(body, options, rawBody) {
    let ogObject = {};
    const $ = (0, cheerio_1.load)(body);
    const metaFields = fields_1.default.concat(options.customMetaTags);
    // find all of the open graph info in the meta tags
    $('meta').each((index, meta) => {
        if (!meta.attribs || (!meta.attribs.property && !meta.attribs.name))
            return;
        const property = meta.attribs.property || meta.attribs.name;
        const content = meta.attribs.content || meta.attribs.value;
        metaFields.forEach((item) => {
            if (item && property.toLowerCase() === item.property.toLowerCase()) {
                if (!item.multiple) {
                    ogObject[item.fieldName] = content;
                }
                else if (!ogObject[item.fieldName]) {
                    ogObject[item.fieldName] = [content];
                }
                else if (Array.isArray(ogObject[item.fieldName])) {
                    ogObject[item.fieldName].push(content);
                }
            }
        });
    });
    // set ogImage to ogImageSecureURL/ogImageURL if there is no ogImage
    if (!ogObject.ogImage && ogObject.ogImageSecureURL) {
        ogObject.ogImage = ogObject.ogImageSecureURL;
    }
    else if (!ogObject.ogImage && ogObject.ogImageURL) {
        ogObject.ogImage = ogObject.ogImageURL;
    }
    // formats the multiple media values
    ogObject = media.mediaSetup(ogObject, options);
    // if onlyGetOpenGraphInfo isn't set, run the open graph fallbacks
    if (!options.onlyGetOpenGraphInfo) {
        ogObject = (0, fallback_1.default)(ogObject, options, $, rawBody);
    }
    // TODO: Is this still needed?
    // removes any undefs
    ogObject = utils.removeNestedUndefinedValues(ogObject);
    return ogObject;
}
exports.default = extractMetaTags;