generated from tanishqkancharla/npm-ts-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
41 lines (33 loc) · 887 Bytes
/
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
import {
isParseFailure,
logResult,
maybe,
Parser,
sequence,
zeroOrMore,
} from "teg-parser";
import { frontMatterParser } from "./blocks/frontmatter";
import { newLine } from "./blocks/newLine";
import { tkBlock, TKBlock } from "./TKBlock";
export { tkBlock, TKBlock };
export type TKMetadata = Record<string, string | string[]>;
export type TKDoc = {
metadata: TKMetadata;
blocks: TKBlock[];
};
export const tkMetadataParser = frontMatterParser;
export const tkParser: Parser<TKDoc> = sequence(
[maybe(frontMatterParser), zeroOrMore(tkBlock)],
newLine
).map(([frontMatterToken, blocks]) => ({
metadata: frontMatterToken?.content || {},
blocks,
}));
export function parseTK(contents: string): TKDoc {
const result = tkParser.run(contents);
if (isParseFailure(result)) {
console.log(logResult(result));
throw new Error(result.value);
}
return result.value;
}