forked from microformats/microformats-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.ts
33 lines (28 loc) · 900 Bytes
/
parser.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
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";
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 },
};
return {
rels,
"rel-urls": relUrls,
items: findChildren(doc, isMicroformatRoot).map((mf) =>
parseMicroformat(mf, parsingOptions)
),
};
};