-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathparser.ts
More file actions
41 lines (35 loc) · 1.13 KB
/
parser.ts
File metadata and controls
41 lines (35 loc) · 1.13 KB
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
import { parse } from "parse5";
import { findChildren } from "./helpers/findChildren";
import { parseMicroformat } from "./microformats/parse";
import { isMicroformatRoot } from "./helpers/nodeMatchers";
import { ParsedDocument, ParserOptions, ParsingOptions } from "./types";
import { validateParsedHtml } from "./validator";
import { documentSetup } from "./helpers/documentSetup";
import { parseMetaformats } from "./helpers/metaformats";
import { isEnabled } from "./helpers/experimental";
export const parser = (
html: string,
options: ParserOptions,
): ParsedDocument => {
const doc = parse(html);
validateParsedHtml(doc);
const { idRefs, rels, relUrls, baseUrl, lang } = documentSetup(doc, options);
const parsingOptions: ParsingOptions = {
...options,
baseUrl,
idRefs,
inherited: { roots: [], lang },
rels,
};
let items = findChildren(doc, isMicroformatRoot).map((mf) =>
parseMicroformat(mf, parsingOptions),
);
if (items.length === 0 && isEnabled(parsingOptions, "metaformats")) {
items = parseMetaformats(doc, parsingOptions);
}
return {
rels,
"rel-urls": relUrls,
items,
};
};