@devmehq/open-graph-extractor
Version:
Extract Open Graph and Twitter Card info off from html
80 lines (79 loc) • 3.46 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractOpenGraph = void 0;
const cheerio = __importStar(require("cheerio"));
const fields_1 = require("./fields");
const media_1 = require("./media");
const utils_1 = require("./utils");
const fallback_1 = require("./fallback");
/*
* extract meta tags from html string
* @param string body - html string
* @param string options - options the user has set
*/
function extractOpenGraph(body, options) {
var _a;
let ogObject = {};
const $ = cheerio.load(body);
const metaFields = fields_1.fields.concat((_a = options === null || options === void 0 ? void 0 : options.customMetaTags) !== null && _a !== void 0 ? _a : []);
// find all 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 || meta.attribs.itemprop || meta.attribs.itemProp;
const content = meta.attribs.content || meta.attribs.value;
metaFields.forEach((item) => {
if (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 = (0, media_1.mediaSetup)(ogObject, options);
// if onlyGetOpenGraphInfo isn't set, run the open graph fallbacks
if (!(options === null || options === void 0 ? void 0 : options.onlyGetOpenGraphInfo)) {
ogObject = (0, fallback_1.fallback)(ogObject, options, $);
}
// removes any undef
ogObject = (0, utils_1.removeNestedUndefinedValues)(ogObject);
return ogObject;
}
exports.extractOpenGraph = extractOpenGraph;