-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
65 lines (57 loc) · 2.16 KB
/
index.ts
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
59
60
61
62
63
64
65
import * as cheerio from 'cheerio';
import { fields } from './fields';
import { mediaSetup } from './media';
import { removeNestedUndefinedValues } from './utils';
import { fallback } from './fallback';
import Root = cheerio.Root;
import TagElement = cheerio.TagElement;
/*
* extract meta tags from html string
* @param string body - html string
* @param string options - options the user has set
*/
export function extractOpenGraph(
body: string | Buffer,
options?: {
customMetaTags?: ConcatArray<{ multiple: boolean; property: string; fieldName: string }>;
allMedia?: boolean;
onlyGetOpenGraphInfo?: boolean;
ogImageFallback?: boolean;
},
) {
let ogObject: any = {};
const $: Root = cheerio.load(body);
const metaFields = fields.concat(options?.customMetaTags ?? []);
// find all the open graph info in the meta tags
$('meta').each((index, meta: TagElement) => {
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 = mediaSetup(ogObject, options);
// if onlyGetOpenGraphInfo isn't set, run the open graph fallbacks
if (!options?.onlyGetOpenGraphInfo) {
ogObject = fallback(ogObject, options, $);
}
// removes any undef
ogObject = removeNestedUndefinedValues(ogObject);
return ogObject;
}