-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemtext.ts
136 lines (130 loc) · 3.82 KB
/
gemtext.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// ====== AST / PARSER
// Fork of https://github.com/bctnry/gemtext
export type Renderer<T> = {
text(content: string): T;
link(url: string, alt: string): T;
preformatted(content: string[], alt: string): T;
heading(level: number, text: string): T;
unorderedList(content: string[]): T;
quote(content: string): T;
};
export enum GemText {
Text = 1,
Link = 2,
Preformatted = 3,
Heading = 4,
List = 5,
Quote = 6,
}
type ParseResultData =
| { _: GemText.Text; val: string }
| { _: GemText.Link; url: string; alt: string }
| { _: GemText.Preformatted; content: string[]; alt: string }
| { _: GemText.Heading; level: number; text: string }
| { _: GemText.List; content: string[] }
| { _: GemText.Quote; content: string };
class ParseResult {
constructor(public data: ParseResultData[]) {}
generate<T>(generator: Renderer<T>) {
return this.data
.map((v) => {
switch (v._) {
case GemText.Text:
return generator.text(v.val);
case GemText.Link:
return generator.link(v.url, v.alt);
case GemText.Preformatted:
return generator.preformatted(v.content, v.alt);
case GemText.Heading:
return generator.heading(v.level, v.text);
case GemText.List:
return generator.unorderedList(v.content);
case GemText.Quote:
return generator.quote(v.content);
}
})
.join(EMPTY);
}
}
const WHITESPACE = " \t\r\n\v\b";
const FENCE = "```";
const CR = "\n";
const EMPTY = "";
const HEADING = "#";
// TODO: Refactor this code to be more readable, but only after
// there is a test suite.
export function parse(source: string): ParseResult {
let res: ParseResultData[] = [];
let preformatting: boolean = false;
let preformattingAlt: string = EMPTY;
let preformattingBuffer: string[] = [];
let listStarted: boolean = false;
let listBuffer: string[] = [];
source
.replace(/\r\n/g, CR)
.split(CR)
.forEach((v) => {
if (preformatting) {
if (v.trim() === FENCE) {
res.push({
_: GemText.Preformatted,
content: preformattingBuffer,
alt: preformattingAlt,
});
preformatting = false;
preformattingBuffer = [];
preformattingAlt = EMPTY;
return;
} else {
preformattingBuffer.push(v);
return;
}
}
if (listStarted && !v.startsWith("* ")) {
res.push({ _: GemText.List, content: listBuffer });
listStarted = false;
listBuffer = [];
}
if (v.startsWith("=>")) {
let x = v.substring(2).trim();
let i = 0;
while (i < x.length && !WHITESPACE.includes(x[i])) {
i++;
}
let url = x.substring(0, i);
x = x.substring(i).trim();
res.push({ _: GemText.Link, url, alt: x });
} else if (v.startsWith(">")) {
res.push({ _: GemText.Quote, content: v.substring(1).trim() });
} else if (v.startsWith(HEADING)) {
let i = 0;
while (v[i] == HEADING) {
i++;
}
let level = i;
res.push({ _: GemText.Heading, level, text: v.substring(i).trim() });
} else if (v.startsWith(FENCE)) {
preformattingAlt = v.substring(3).trim();
preformatting = true;
} else if (v.startsWith("* ")) {
if (!listStarted) {
listStarted = true;
listBuffer = [];
}
listBuffer.push(v.substring(2).trim());
} else {
res.push({ _: GemText.Text, val: v });
}
});
if (preformattingBuffer.length > 0) {
res.push({
_: GemText.Preformatted,
content: preformattingBuffer,
alt: preformattingAlt,
});
}
if (listBuffer.length > 0) {
res.push({ _: GemText.List, content: listBuffer });
}
return new ParseResult(res);
}