Repository navigation
Expand file tree
/
Copy pathinline-parser.ts
More file actions
213 lines (189 loc) · 5.87 KB
/
Copy pathinline-parser.ts
File metadata and controls
213 lines (189 loc) · 5.87 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* inline-parser.ts :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dlesieur <dlesieur@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/28 22:27:46 by dlesieur #+# #+# */
/* Updated: 2026/05/11 22:04:13 by dlesieur ### ########.fr */
/* */
/* ************************************************************************** */
import type {
CodeSpanNode,
EmphasisNode,
InlineNode,
LinkNode,
SourceSpan,
StrongNode,
TextNode,
} from "./types";
import { stableId } from "./utils";
interface Cursor {
source: string;
index: number;
line: number;
globalStartLine: number;
}
function makeSpan(
line: number,
startOffset: number,
endOffset: number,
): SourceSpan {
return {
startLine: line,
endLine: line,
startOffset,
endOffset,
};
}
function textNode(
value: string,
line: number,
start: number,
end: number,
): TextNode {
return {
id: stableId(`txt:${line}:${start}:${value}`),
kind: "text",
value,
span: makeSpan(line, start, end),
};
}
function parseBracketLabel(cursor: Cursor): string | null {
if (cursor.source[cursor.index] !== "[") return null;
let i = cursor.index + 1;
while (i < cursor.source.length && cursor.source[i] !== "]") i++;
if (i >= cursor.source.length) return null;
const label = cursor.source.slice(cursor.index + 1, i);
cursor.index = i + 1;
return label;
}
function parseParenHref(cursor: Cursor): string | null {
if (cursor.source[cursor.index] !== "(") return null;
let i = cursor.index + 1;
while (i < cursor.source.length && cursor.source[i] !== ")") i++;
if (i >= cursor.source.length) return null;
const href = cursor.source.slice(cursor.index + 1, i).trim();
cursor.index = i + 1;
return href || null;
}
export function parseInlines(source: string, line: number): InlineNode[] {
const cursor: Cursor = {
source,
index: 0,
line,
globalStartLine: line,
};
const nodes: InlineNode[] = [];
while (cursor.index < cursor.source.length) {
nodes.push(parseInlineNode(cursor, line));
}
return mergeTextNodes(nodes);
}
function parseInlineNode(cursor: Cursor, line: number): InlineNode {
return (
parseCodeSpanNode(cursor, line) ??
parseStrongNode(cursor, line) ??
parseEmphasisNode(cursor, line) ??
parseLinkNode(cursor, line) ??
parseTextRun(cursor, line)
);
}
function parseCodeSpanNode(cursor: Cursor, line: number): CodeSpanNode | null {
if (cursor.source[cursor.index] !== "`") return null;
const start = cursor.index;
const close = cursor.source.indexOf("`", start + 1);
if (close === -1) return null;
const value = cursor.source.slice(start + 1, close);
cursor.index = close + 1;
return {
id: stableId(`code:${line}:${start}:${value}`),
kind: "code_span",
value,
span: makeSpan(line, start, cursor.index),
};
}
function parseStrongNode(cursor: Cursor, line: number): StrongNode | null {
if (!cursor.source.startsWith("**", cursor.index)) return null;
const start = cursor.index;
const close = cursor.source.indexOf("**", start + 2);
if (close === -1) return null;
const content = cursor.source.slice(start + 2, close);
cursor.index = close + 2;
return {
id: stableId(`strong:${line}:${start}:${content}`),
kind: "strong",
children: parseInlines(content, line),
span: makeSpan(line, start, cursor.index),
};
}
function parseEmphasisNode(cursor: Cursor, line: number): EmphasisNode | null {
if (cursor.source[cursor.index] !== "*") return null;
const start = cursor.index;
const close = cursor.source.indexOf("*", start + 1);
if (close === -1) return null;
const content = cursor.source.slice(start + 1, close);
cursor.index = close + 1;
return {
id: stableId(`em:${line}:${start}:${content}`),
kind: "emphasis",
children: parseInlines(content, line),
span: makeSpan(line, start, cursor.index),
};
}
function parseLinkNode(cursor: Cursor, line: number): LinkNode | null {
if (cursor.source[cursor.index] !== "[") return null;
const start = cursor.index;
const label = parseBracketLabel(cursor);
if (label === null) {
cursor.index = start;
return null;
}
const href = parseParenHref(cursor);
if (href === null) {
cursor.index = start;
return null;
}
return {
id: stableId(`link:${line}:${start}:${href}:${label}`),
kind: "link",
href,
children: parseInlines(label, line),
span: makeSpan(line, start, cursor.index),
};
}
function parseTextRun(cursor: Cursor, line: number): TextNode {
const start = cursor.index;
let end = start + 1;
while (end < cursor.source.length && !isInlineTokenStart(cursor.source[end])) {
end++;
}
const value = cursor.source.slice(start, end);
cursor.index = end;
return textNode(value, line, start, end);
}
function isInlineTokenStart(char: string): boolean {
return char === "`" || char === "[" || char === "*";
}
function mergeTextNodes(nodes: InlineNode[]): InlineNode[] {
if (nodes.length <= 1) return nodes;
const merged: InlineNode[] = [];
for (const node of nodes) {
const prev = merged.at(-1);
if (prev?.kind === "text" && node.kind === "text") {
merged.pop();
merged.push({
...prev,
value: prev.value + node.value,
span: {
...prev.span,
endOffset: node.span.endOffset,
},
});
continue;
}
merged.push(node);
}
return merged;
}