-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
91 lines (76 loc) · 2.31 KB
/
parser.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Includes code from https://github.com/mozilla/page-metadata-parser
* Copyright 2014-2022 Mozilla / Mozilla Public License 2.0
**/
const { metadataRuleSets } = require("./ruleSets");
/**
* @param {import("./types").MetadataRuleset} ruleSet
* @returns {(doc: Document, context: import("./types").PageMetadataContext) => string | null}
**/
function buildRuleSet(ruleSet) {
return (doc, context) => {
let maxScore = 0;
let maxValue;
for (let currRule = 0; currRule < ruleSet.rules.length; currRule++) {
const [query, handler] = ruleSet.rules[currRule];
/** @type {HTMLElement[]} */
const elements = Array.from(doc.querySelectorAll(query));
if (elements.length) {
for (const element of elements) {
let score = ruleSet.rules.length - currRule;
if (ruleSet.scorers) {
for (const scorer of ruleSet.scorers) {
const newScore = scorer(element, score);
if (newScore) {
score = newScore;
}
}
}
if (score > maxScore) {
maxScore = score;
maxValue = handler(element);
}
}
}
}
if (!maxValue && ruleSet.defaultValue) {
maxValue = ruleSet.defaultValue(context);
}
if (maxValue) {
if (ruleSet.processors) {
for (const processor of ruleSet.processors) {
maxValue = processor(maxValue, context);
}
}
if (maxValue.trim) {
maxValue = maxValue.trim();
}
return maxValue;
}
};
}
/**
* @param {Document} doc
* @param {string} url
* @param {Record<keyof import("./types").PageMetadata, import("./types").MetadataRuleset>} customRuleSets
* @returns {import("./types").PageMetadata}
**/
export function getMetadata(doc, url, customRuleSets) {
/** @type {import("./types").PageMetadata} */
// @ts-ignore
const metadata = {};
const context = {
url,
};
const ruleSets = customRuleSets || metadataRuleSets;
/** @type {(keyof import("./types").PageMetadata)[]} */
// @ts-ignore
const keys = Object.keys(ruleSets);
keys.map((ruleSetKey) => {
const ruleSet = ruleSets[ruleSetKey];
const builtRuleSet = buildRuleSet(ruleSet);
// @ts-ignore
metadata[ruleSetKey] = builtRuleSet(doc, context);
});
return metadata;
}