Debian Codegraph @

debian/tests/test_modules/mdgator/src/mdgator.ts

raw symbols SCIP
1 import * as Markdown from 'markdown-it';
2 import { Group } from './group';
3 import { Metadata, Test } from './test';
4
5 export type State = 'none' | 'heading';
6
7 interface IHeading {
8 readonly children: IHeading[];
9 readonly content: Map<string, string[]>;
10 readonly depth: number;
11 readonly line: number;
12 readonly meta: Map<string, Metadata[]>;
13 readonly name: string;
14 }
15
16 export { Group, Metadata, Test };
17
18 export class MDGator {
19 private readonly md = new Markdown({ html: true });
20 private state: State = 'none';
21
22 public parse(text: string): ReadonlyArray<Group> {
23 const tokens = this.md.parse(text, {});
24
25 const roots: IHeading[] = [];
26
27 let metadata: Metadata | undefined;
28 let depth: number | undefined;
29
30 const stack: IHeading[] = [];
31 for (const token of tokens) {
32 const type = token.type;
33
34 if (type === 'heading_open') {
35 this.state = 'heading';
36 metadata = undefined;
37
38 // Leave previous headings
39 depth = parseInt(token.tag.slice(1), 10) - 1;
40 while (stack.length !== 0 && stack[stack.length - 1].depth >= depth) {
41 stack.pop();
42 }
43 } else if (type === 'heading_close') {
44 this.state = 'none';
45 metadata = undefined;
46 } else if (type === 'inline') {
47 if (this.state !== 'heading') {
48 continue;
49 }
50
51 const heading = {
52 children: [],
53 content: new Map(),
54 depth: depth!,
55 line: token.map![0],
56 meta: new Map(),
57 name: token.content,
58 };
59 depth = undefined;
60
61 if (stack.length === 0) {
62 roots.push(heading);
63 } else {
64 stack[stack.length - 1].children.push(heading);
65 }
66
67 stack.push(heading);
68 } else if (type === 'fence' && stack.length > 1) {
69 const current = stack[stack.length - 1];
70
71 if (!current.content.has(token.info)) {
72 current.content.set(token.info, []);
73 }
74 current.content.get(token.info)!.push(token.content);
75
76 // Set metadata
77 if (metadata !== undefined) {
78 if (!current.meta.has(token.info)) {
79 current.meta.set(token.info, []);
80 }
81 current.meta.get(token.info)!.push(metadata);
82 metadata = undefined;
83 }
84 } else if (type === 'html_block' && stack.length >= 1) {
85 const match = token.content.match(/<!--\s+meta=(.*)\s+-->/);
86 if (match === null) {
87 continue;
88 }
89
90 let json: Metadata;
91 try {
92 json = JSON.parse(match[1]);
93 } catch (e) {
94 throw new Error(`Failed to parse JSON metadata: "${match[1]}"`);
95 }
96
97 metadata = json;
98 }
99 }
100
101 return roots.map((root) => this.translate(root));
102 }
103
104 private translate(heading: IHeading): Group {
105 const children = heading.children.filter((child) => {
106 return child.children.length !== 0;
107 }).map((child) => this.translate(child));
108
109 const tests = heading.children.filter((child) => {
110 return child.content.size !== 0;
111 }).map((child) => this.translateTest(child));
112
113 return new Group(heading.name, heading.line, children, tests);
114 }
115
116 private translateTest(heading: IHeading): Test {
117 return new Test(heading.name, heading.line, heading.content, heading.meta);
118 }
119 }