@fuzdev/mdz 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/dist/Mdz.svelte +39 -0
- package/dist/Mdz.svelte.d.ts +17 -0
- package/dist/Mdz.svelte.d.ts.map +1 -0
- package/dist/MdzNodeView.svelte +140 -0
- package/dist/MdzNodeView.svelte.d.ts +9 -0
- package/dist/MdzNodeView.svelte.d.ts.map +1 -0
- package/dist/MdzPrecompiled.svelte +33 -0
- package/dist/MdzPrecompiled.svelte.d.ts +17 -0
- package/dist/MdzPrecompiled.svelte.d.ts.map +1 -0
- package/dist/MdzRoot.svelte +40 -0
- package/dist/MdzRoot.svelte.d.ts +14 -0
- package/dist/MdzRoot.svelte.d.ts.map +1 -0
- package/dist/MdzStream.svelte +37 -0
- package/dist/MdzStream.svelte.d.ts +18 -0
- package/dist/MdzStream.svelte.d.ts.map +1 -0
- package/dist/MdzStreamNodeView.svelte +150 -0
- package/dist/MdzStreamNodeView.svelte.d.ts +9 -0
- package/dist/MdzStreamNodeView.svelte.d.ts.map +1 -0
- package/dist/context_helpers.d.ts +23 -0
- package/dist/context_helpers.d.ts.map +1 -0
- package/dist/context_helpers.js +19 -0
- package/dist/mdz.d.ts +152 -0
- package/dist/mdz.d.ts.map +1 -0
- package/dist/mdz.js +61 -0
- package/dist/mdz_contexts.d.ts +106 -0
- package/dist/mdz_contexts.d.ts.map +1 -0
- package/dist/mdz_contexts.js +42 -0
- package/dist/mdz_helpers.d.ts +321 -0
- package/dist/mdz_helpers.d.ts.map +1 -0
- package/dist/mdz_helpers.js +668 -0
- package/dist/mdz_lexer.d.ts +123 -0
- package/dist/mdz_lexer.d.ts.map +1 -0
- package/dist/mdz_lexer.js +1405 -0
- package/dist/mdz_opcodes.d.ts +193 -0
- package/dist/mdz_opcodes.d.ts.map +1 -0
- package/dist/mdz_opcodes.js +14 -0
- package/dist/mdz_opcodes_to_nodes.d.ts +26 -0
- package/dist/mdz_opcodes_to_nodes.d.ts.map +1 -0
- package/dist/mdz_opcodes_to_nodes.js +410 -0
- package/dist/mdz_stream_parser.d.ts +109 -0
- package/dist/mdz_stream_parser.d.ts.map +1 -0
- package/dist/mdz_stream_parser.js +699 -0
- package/dist/mdz_stream_parser_block.d.ts +99 -0
- package/dist/mdz_stream_parser_block.d.ts.map +1 -0
- package/dist/mdz_stream_parser_block.js +437 -0
- package/dist/mdz_stream_parser_inline.d.ts +98 -0
- package/dist/mdz_stream_parser_inline.d.ts.map +1 -0
- package/dist/mdz_stream_parser_inline.js +481 -0
- package/dist/mdz_stream_parser_link.d.ts +29 -0
- package/dist/mdz_stream_parser_link.d.ts.map +1 -0
- package/dist/mdz_stream_parser_link.js +227 -0
- package/dist/mdz_stream_parser_list.d.ts +106 -0
- package/dist/mdz_stream_parser_list.d.ts.map +1 -0
- package/dist/mdz_stream_parser_list.js +438 -0
- package/dist/mdz_stream_parser_state.d.ts +476 -0
- package/dist/mdz_stream_parser_state.d.ts.map +1 -0
- package/dist/mdz_stream_parser_state.js +651 -0
- package/dist/mdz_stream_parser_text.d.ts +25 -0
- package/dist/mdz_stream_parser_text.d.ts.map +1 -0
- package/dist/mdz_stream_parser_text.js +67 -0
- package/dist/mdz_stream_parser_url.d.ts +76 -0
- package/dist/mdz_stream_parser_url.d.ts.map +1 -0
- package/dist/mdz_stream_parser_url.js +328 -0
- package/dist/mdz_stream_state.svelte.d.ts +53 -0
- package/dist/mdz_stream_state.svelte.d.ts.map +1 -0
- package/dist/mdz_stream_state.svelte.js +360 -0
- package/dist/mdz_to_svelte.d.ts +71 -0
- package/dist/mdz_to_svelte.d.ts.map +1 -0
- package/dist/mdz_to_svelte.js +127 -0
- package/dist/mdz_token_parser.d.ts +20 -0
- package/dist/mdz_token_parser.d.ts.map +1 -0
- package/dist/mdz_token_parser.js +449 -0
- package/dist/svelte_preprocess_mdz.d.ts +80 -0
- package/dist/svelte_preprocess_mdz.d.ts.map +1 -0
- package/dist/svelte_preprocess_mdz.js +543 -0
- package/dist/tsdoc_mdz.d.ts +45 -0
- package/dist/tsdoc_mdz.d.ts.map +1 -0
- package/dist/tsdoc_mdz.js +88 -0
- package/package.json +131 -0
- package/src/lib/context_helpers.ts +52 -0
- package/src/lib/mdz.ts +190 -0
- package/src/lib/mdz_contexts.ts +99 -0
- package/src/lib/mdz_helpers.ts +727 -0
- package/src/lib/mdz_lexer.ts +1761 -0
- package/src/lib/mdz_opcodes.ts +227 -0
- package/src/lib/mdz_opcodes_to_nodes.ts +470 -0
- package/src/lib/mdz_stream_parser.ts +766 -0
- package/src/lib/mdz_stream_parser_block.ts +490 -0
- package/src/lib/mdz_stream_parser_inline.ts +527 -0
- package/src/lib/mdz_stream_parser_link.ts +283 -0
- package/src/lib/mdz_stream_parser_list.ts +525 -0
- package/src/lib/mdz_stream_parser_state.ts +957 -0
- package/src/lib/mdz_stream_parser_text.ts +95 -0
- package/src/lib/mdz_stream_parser_url.ts +386 -0
- package/src/lib/mdz_stream_state.svelte.ts +383 -0
- package/src/lib/mdz_to_svelte.ts +213 -0
- package/src/lib/mdz_token_parser.ts +544 -0
- package/src/lib/svelte_preprocess_mdz.ts +777 -0
- package/src/lib/tsdoc_mdz.ts +97 -0
|
@@ -0,0 +1,727 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared constants and pure helper functions for mdz parsers.
|
|
3
|
+
*
|
|
4
|
+
* Used by both the synchronous parser (`mdz_lexer.ts` + `mdz_token_parser.ts`)
|
|
5
|
+
* and the streaming parser (`mdz_stream_parser.ts`).
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type {MdzNode, MdzNodeText, MdzNodeComponent, MdzNodeElement} from './mdz.js';
|
|
11
|
+
|
|
12
|
+
// Character codes for performance
|
|
13
|
+
/** @nodocs */
|
|
14
|
+
export const BACKTICK = 96; // `
|
|
15
|
+
/** @nodocs */
|
|
16
|
+
export const ASTERISK = 42; // *
|
|
17
|
+
/** @nodocs */
|
|
18
|
+
export const UNDERSCORE = 95; // _
|
|
19
|
+
/** @nodocs */
|
|
20
|
+
export const TILDE = 126; // ~
|
|
21
|
+
/** @nodocs */
|
|
22
|
+
export const NEWLINE = 10; // \n
|
|
23
|
+
/** @nodocs */
|
|
24
|
+
export const HYPHEN = 45; // -
|
|
25
|
+
/** @nodocs */
|
|
26
|
+
export const HASH = 35; // #
|
|
27
|
+
/** @nodocs */
|
|
28
|
+
export const SPACE = 32; // (space)
|
|
29
|
+
/** @nodocs */
|
|
30
|
+
export const TAB = 9; // \t
|
|
31
|
+
/** @nodocs */
|
|
32
|
+
export const CARRIAGE_RETURN = 13; // \r
|
|
33
|
+
/** @nodocs */
|
|
34
|
+
export const LEFT_ANGLE = 60; // <
|
|
35
|
+
/** @nodocs */
|
|
36
|
+
export const RIGHT_ANGLE = 62; // >
|
|
37
|
+
/** @nodocs */
|
|
38
|
+
export const SLASH = 47; // /
|
|
39
|
+
/** @nodocs */
|
|
40
|
+
export const LEFT_BRACKET = 91; // [
|
|
41
|
+
/** @nodocs */
|
|
42
|
+
export const RIGHT_BRACKET = 93; // ]
|
|
43
|
+
/** @nodocs */
|
|
44
|
+
export const LEFT_PAREN = 40; // (
|
|
45
|
+
/** @nodocs */
|
|
46
|
+
export const RIGHT_PAREN = 41; // )
|
|
47
|
+
/** @nodocs */
|
|
48
|
+
export const COLON = 58; // :
|
|
49
|
+
/** @nodocs */
|
|
50
|
+
export const PERIOD = 46; // .
|
|
51
|
+
/** @nodocs */
|
|
52
|
+
export const COMMA = 44; // ,
|
|
53
|
+
/** @nodocs */
|
|
54
|
+
export const SEMICOLON = 59; // ;
|
|
55
|
+
/** @nodocs */
|
|
56
|
+
export const EXCLAMATION = 33; // !
|
|
57
|
+
/** @nodocs */
|
|
58
|
+
export const QUESTION = 63; // ?
|
|
59
|
+
// RFC 3986 URI characters
|
|
60
|
+
/** @nodocs */
|
|
61
|
+
export const DOLLAR = 36; // $
|
|
62
|
+
/** @nodocs */
|
|
63
|
+
export const PERCENT = 37; // %
|
|
64
|
+
/** @nodocs */
|
|
65
|
+
export const AMPERSAND = 38; // &
|
|
66
|
+
/** @nodocs */
|
|
67
|
+
export const APOSTROPHE = 39; // '
|
|
68
|
+
/** @nodocs */
|
|
69
|
+
export const PLUS = 43; // +
|
|
70
|
+
/** @nodocs */
|
|
71
|
+
export const EQUALS = 61; // =
|
|
72
|
+
/** @nodocs */
|
|
73
|
+
export const AT = 64; // @
|
|
74
|
+
// Character ranges
|
|
75
|
+
/** @nodocs */
|
|
76
|
+
export const A_UPPER = 65; // A
|
|
77
|
+
/** @nodocs */
|
|
78
|
+
export const Z_UPPER = 90; // Z
|
|
79
|
+
/** @nodocs */
|
|
80
|
+
export const A_LOWER = 97; // a
|
|
81
|
+
/** @nodocs */
|
|
82
|
+
export const Z_LOWER = 122; // z
|
|
83
|
+
/** @nodocs */
|
|
84
|
+
export const ZERO = 48; // 0
|
|
85
|
+
/** @nodocs */
|
|
86
|
+
export const ONE = 49; // 1
|
|
87
|
+
/** @nodocs */
|
|
88
|
+
export const NINE = 57; // 9
|
|
89
|
+
// mdz specification constants
|
|
90
|
+
/** @nodocs */
|
|
91
|
+
export const HR_HYPHEN_COUNT = 3; // Horizontal rule requires exactly 3 hyphens
|
|
92
|
+
/** @nodocs */
|
|
93
|
+
export const MIN_CODEBLOCK_BACKTICKS = 3; // Code blocks require minimum 3 backticks
|
|
94
|
+
/** @nodocs */
|
|
95
|
+
export const MAX_LIST_NUMBER_DIGITS = 9; // Ordered list markers cap at 9 digits (CommonMark's cap)
|
|
96
|
+
/** @nodocs */
|
|
97
|
+
export const MAX_HEADING_LEVEL = 6; // Headings support levels 1-6
|
|
98
|
+
/** @nodocs */
|
|
99
|
+
export const HTTPS_PREFIX_LENGTH = 8; // Length of "https://"
|
|
100
|
+
/** @nodocs */
|
|
101
|
+
export const HTTP_PREFIX_LENGTH = 7; // Length of "http://"
|
|
102
|
+
/** @nodocs */
|
|
103
|
+
export const H_LOWER = 104; // h
|
|
104
|
+
/** @nodocs */
|
|
105
|
+
export const H_UPPER = 72; // H
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Whether a char code is whitespace within a line — space, tab, or `\r` —
|
|
109
|
+
* everything but the newline itself. A line consisting only of these counts
|
|
110
|
+
* as blank (paragraph breaks), and trailing runs of them are tolerated but
|
|
111
|
+
* never significant (HR and fence lines). `\r` is included so CRLF input
|
|
112
|
+
* gets blank-line and end-of-line behavior for free; mdz does no other CRLF
|
|
113
|
+
* handling.
|
|
114
|
+
*
|
|
115
|
+
* @nodocs
|
|
116
|
+
*/
|
|
117
|
+
export const is_line_whitespace = (char_code: number): boolean =>
|
|
118
|
+
char_code === SPACE || char_code === TAB || char_code === CARRIAGE_RETURN;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Whether `text` contains any non-whitespace character — the
|
|
122
|
+
* whitespace-only-paragraph drop shared by both parsers. Hot path in the
|
|
123
|
+
* streaming parser (called from `emit` on every text emission); the char
|
|
124
|
+
* loop avoids the regex allocation `/\S/.test()` incurs per call.
|
|
125
|
+
*
|
|
126
|
+
* Deliberately ASCII-only (space, tab, LF, CR, FF, VT), matching the
|
|
127
|
+
* grammar's blank-line definition: a line of Unicode-only whitespace
|
|
128
|
+
* (U+00A0 NBSP, U+3000, etc.) is content, not blank — CommonMark agrees.
|
|
129
|
+
* `/\S/` and `String.prototype.trim()` would recognize Unicode whitespace
|
|
130
|
+
* and silently drop such paragraphs.
|
|
131
|
+
*
|
|
132
|
+
* @nodocs
|
|
133
|
+
*/
|
|
134
|
+
export const has_non_whitespace = (text: string): boolean => {
|
|
135
|
+
for (let i = 0; i < text.length; i++) {
|
|
136
|
+
const c = text.charCodeAt(i);
|
|
137
|
+
// 0x09=tab, 0x0a=LF, 0x0b=VT, 0x0c=FF, 0x0d=CR, 0x20=space
|
|
138
|
+
if (c !== 0x20 && (c < 0x09 || c > 0x0d)) return true;
|
|
139
|
+
}
|
|
140
|
+
return false;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A matched blockquote prefix: a run of `>` markers — tight (`>>`) or
|
|
145
|
+
* separated by single spaces (`> >`), interchangeably — terminated by exactly
|
|
146
|
+
* one space or the end of the line. Depth is the count of `>`.
|
|
147
|
+
*
|
|
148
|
+
* @nodocs
|
|
149
|
+
*/
|
|
150
|
+
export interface MdzBlockquotePrefix {
|
|
151
|
+
/** Count of `>` markers. */
|
|
152
|
+
depth: number;
|
|
153
|
+
/**
|
|
154
|
+
* Index just past the prefix: after the terminating space, or at the
|
|
155
|
+
* line-end whitespace/newline for a bare (empty) prefix line.
|
|
156
|
+
*/
|
|
157
|
+
content_start: number;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Match a blockquote prefix at `i` (the position of a `>`). Shared by both
|
|
162
|
+
* parsers — the sync lexer calls with `forced` true (its text is complete);
|
|
163
|
+
* the streaming parser gets `'pending'` when the buffer ends before the
|
|
164
|
+
* prefix can resolve.
|
|
165
|
+
*
|
|
166
|
+
* Returns null when the line has no valid prefix: a `>` followed directly by
|
|
167
|
+
* content (`>a`), a tab instead of the marker space (`>\ta`), or a tight run
|
|
168
|
+
* with trailing content (`>>x`). The backtrack pin: a `>` not followed by
|
|
169
|
+
* space, end-of-line, or a continuing run is content, and the separator space
|
|
170
|
+
* before it re-serves as the terminator — `> >x` is a depth-1 prefix with
|
|
171
|
+
* content `>x`. A `>` followed by only whitespace to the line end is a valid
|
|
172
|
+
* bare prefix (trailing whitespace tolerated, never significant).
|
|
173
|
+
*
|
|
174
|
+
* @nodocs
|
|
175
|
+
*/
|
|
176
|
+
export const mdz_match_blockquote_prefix = (
|
|
177
|
+
text: string,
|
|
178
|
+
i: number,
|
|
179
|
+
forced: boolean,
|
|
180
|
+
): MdzBlockquotePrefix | 'pending' | null => {
|
|
181
|
+
let depth = 0;
|
|
182
|
+
let pos = i;
|
|
183
|
+
while (true) {
|
|
184
|
+
// only reachable at entry — separators are consumed only when a `>` follows
|
|
185
|
+
if (text.charCodeAt(pos) !== RIGHT_ANGLE) return null;
|
|
186
|
+
depth++;
|
|
187
|
+
pos++;
|
|
188
|
+
if (pos >= text.length) {
|
|
189
|
+
return forced ? {depth, content_start: pos} : 'pending';
|
|
190
|
+
}
|
|
191
|
+
const c = text.charCodeAt(pos);
|
|
192
|
+
if (c === RIGHT_ANGLE) continue; // tight run
|
|
193
|
+
if (c === SPACE) {
|
|
194
|
+
if (pos + 1 >= text.length) {
|
|
195
|
+
// could be a separator (next chunk starts with `>`) or the terminator
|
|
196
|
+
return forced ? {depth, content_start: pos + 1} : 'pending';
|
|
197
|
+
}
|
|
198
|
+
if (text.charCodeAt(pos + 1) === RIGHT_ANGLE) {
|
|
199
|
+
pos++; // separator — continue the run at the next `>`
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
return {depth, content_start: pos + 1}; // terminating space
|
|
203
|
+
}
|
|
204
|
+
if (c === NEWLINE) {
|
|
205
|
+
return {depth, content_start: pos}; // bare prefix line
|
|
206
|
+
}
|
|
207
|
+
if (is_line_whitespace(c)) {
|
|
208
|
+
// tab/CR is not the marker space — valid only as tolerated trailing
|
|
209
|
+
// whitespace on a bare prefix line
|
|
210
|
+
let j = pos;
|
|
211
|
+
while (j < text.length && is_line_whitespace(text.charCodeAt(j))) {
|
|
212
|
+
j++;
|
|
213
|
+
}
|
|
214
|
+
if (j >= text.length) {
|
|
215
|
+
return forced ? {depth, content_start: pos} : 'pending';
|
|
216
|
+
}
|
|
217
|
+
if (text.charCodeAt(j) === NEWLINE) {
|
|
218
|
+
return {depth, content_start: pos};
|
|
219
|
+
}
|
|
220
|
+
// trailing content after non-space whitespace — fall through to backtrack
|
|
221
|
+
}
|
|
222
|
+
// invalid char directly after a `>`: this `>` is content; the separator
|
|
223
|
+
// space before it (if any) re-serves as the terminator
|
|
224
|
+
if (depth >= 2 && text.charCodeAt(pos - 2) === SPACE) {
|
|
225
|
+
return {depth: depth - 1, content_start: pos - 1};
|
|
226
|
+
}
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Whether the line at a quote prefix's `content_start` has non-whitespace
|
|
233
|
+
* content before its end — the no-empty-start probe for quote opens
|
|
234
|
+
* (contentless prefix lines never start a quote). `'pending'` when the buffer
|
|
235
|
+
* ends inside the trailing whitespace run while streaming.
|
|
236
|
+
*
|
|
237
|
+
* @nodocs
|
|
238
|
+
*/
|
|
239
|
+
export const mdz_blockquote_line_has_content = (
|
|
240
|
+
text: string,
|
|
241
|
+
content_start: number,
|
|
242
|
+
forced: boolean,
|
|
243
|
+
): boolean | 'pending' => {
|
|
244
|
+
let i = content_start;
|
|
245
|
+
while (i < text.length && is_line_whitespace(text.charCodeAt(i))) {
|
|
246
|
+
i++;
|
|
247
|
+
}
|
|
248
|
+
if (i >= text.length) return forced ? false : 'pending';
|
|
249
|
+
return text.charCodeAt(i) !== NEWLINE;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* The one-level strip width for a quote line: the first `>` plus its single
|
|
254
|
+
* following space when present. Stripping one level leaves any deeper prefix
|
|
255
|
+
* in the content for the nested document to parse — `>> a` strips to `> a`.
|
|
256
|
+
*
|
|
257
|
+
* @nodocs
|
|
258
|
+
*/
|
|
259
|
+
export const mdz_blockquote_strip_width = (text: string, i: number): number =>
|
|
260
|
+
i + 1 < text.length && text.charCodeAt(i + 1) === SPACE ? 2 : 1;
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Map a position in a blockquote's stripped inner document back to its
|
|
264
|
+
* source offset. Each segment covers one quote line's content plus its
|
|
265
|
+
* terminating newline — contiguous in both coordinate spaces, so the map is
|
|
266
|
+
* affine per segment. A binary search for the rightmost segment at or before
|
|
267
|
+
* `p` always lands correctly, including the end-exclusive position past the
|
|
268
|
+
* final line.
|
|
269
|
+
*
|
|
270
|
+
* @nodocs
|
|
271
|
+
*/
|
|
272
|
+
export const mdz_remap_segments = (
|
|
273
|
+
segments: Array<{inner_start: number; source_start: number}>,
|
|
274
|
+
p: number,
|
|
275
|
+
): number => {
|
|
276
|
+
let lo = 0;
|
|
277
|
+
let hi = segments.length - 1;
|
|
278
|
+
while (lo < hi) {
|
|
279
|
+
const mid = (lo + hi + 1) >> 1;
|
|
280
|
+
if (segments[mid]!.inner_start <= p) {
|
|
281
|
+
lo = mid;
|
|
282
|
+
} else {
|
|
283
|
+
hi = mid - 1;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
const seg = segments[lo]!;
|
|
287
|
+
return seg.source_start + (p - seg.inner_start);
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Lowercase an ASCII letter code; passes other chars through.
|
|
292
|
+
* Mirrors the `[A-Z] → [a-z]` shift without touching non-ASCII.
|
|
293
|
+
*
|
|
294
|
+
* @nodocs
|
|
295
|
+
*/
|
|
296
|
+
export const ascii_to_lower = (char_code: number): number =>
|
|
297
|
+
char_code >= A_UPPER && char_code <= Z_UPPER ? char_code + 32 : char_code;
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Case-insensitive URL scheme prefix check. Returns the prefix length
|
|
301
|
+
* (8 for `https://`, 7 for `http://`) or 0 if neither matches at `pos`.
|
|
302
|
+
*
|
|
303
|
+
* URI schemes are case-insensitive per RFC 3986 §3.1, and mobile keyboards
|
|
304
|
+
* routinely auto-capitalize the first letter of a sentence, producing
|
|
305
|
+
* `Https://` etc. The original case is preserved in the emitted reference —
|
|
306
|
+
* browsers normalize schemes themselves.
|
|
307
|
+
*
|
|
308
|
+
* @nodocs
|
|
309
|
+
*/
|
|
310
|
+
export const match_url_prefix_case_insensitive = (s: string, pos: number): 0 | 7 | 8 => {
|
|
311
|
+
if (pos + HTTP_PREFIX_LENGTH > s.length) return 0;
|
|
312
|
+
// h | H
|
|
313
|
+
if (ascii_to_lower(s.charCodeAt(pos)) !== H_LOWER) return 0;
|
|
314
|
+
// 'ttps://' (7 chars) or 'ttp://' (6 chars)
|
|
315
|
+
if (
|
|
316
|
+
ascii_to_lower(s.charCodeAt(pos + 1)) === 116 /* t */ &&
|
|
317
|
+
ascii_to_lower(s.charCodeAt(pos + 2)) === 116 /* t */ &&
|
|
318
|
+
ascii_to_lower(s.charCodeAt(pos + 3)) === 112 /* p */
|
|
319
|
+
) {
|
|
320
|
+
if (
|
|
321
|
+
pos + HTTPS_PREFIX_LENGTH <= s.length &&
|
|
322
|
+
ascii_to_lower(s.charCodeAt(pos + 4)) === 115 /* s */ &&
|
|
323
|
+
s.charCodeAt(pos + 5) === COLON &&
|
|
324
|
+
s.charCodeAt(pos + 6) === SLASH &&
|
|
325
|
+
s.charCodeAt(pos + 7) === SLASH
|
|
326
|
+
) {
|
|
327
|
+
return HTTPS_PREFIX_LENGTH;
|
|
328
|
+
}
|
|
329
|
+
if (
|
|
330
|
+
s.charCodeAt(pos + 4) === COLON &&
|
|
331
|
+
s.charCodeAt(pos + 5) === SLASH &&
|
|
332
|
+
s.charCodeAt(pos + 6) === SLASH
|
|
333
|
+
) {
|
|
334
|
+
return HTTP_PREFIX_LENGTH;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return 0;
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Check if character code is a letter (A-Z, a-z).
|
|
342
|
+
*
|
|
343
|
+
* @nodocs
|
|
344
|
+
*/
|
|
345
|
+
export const is_letter = (char_code: number): boolean =>
|
|
346
|
+
(char_code >= A_UPPER && char_code <= Z_UPPER) || (char_code >= A_LOWER && char_code <= Z_LOWER);
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Check if character code is valid for tag name (letter, number, hyphen, underscore).
|
|
350
|
+
*
|
|
351
|
+
* @nodocs
|
|
352
|
+
*/
|
|
353
|
+
export const is_tag_name_char = (char_code: number): boolean =>
|
|
354
|
+
is_letter(char_code) ||
|
|
355
|
+
(char_code >= ZERO && char_code <= NINE) ||
|
|
356
|
+
char_code === HYPHEN ||
|
|
357
|
+
char_code === UNDERSCORE;
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Check if character is part of a word for word boundary detection.
|
|
361
|
+
* Used to prevent intraword emphasis with the `_` delimiter.
|
|
362
|
+
*
|
|
363
|
+
* Only alphanumeric characters (A-Z, a-z, 0-9) are word characters.
|
|
364
|
+
* Formatting delimiters (`*`, `_`, `~`) fall outside all three ranges,
|
|
365
|
+
* so they're naturally excluded without explicit checks.
|
|
366
|
+
*
|
|
367
|
+
* This prevents false positives with snake_case identifiers while allowing
|
|
368
|
+
* adjacent formatting like `**bold**_italic_`.
|
|
369
|
+
*
|
|
370
|
+
* @nodocs
|
|
371
|
+
*/
|
|
372
|
+
export const is_word_char = (char_code: number): boolean =>
|
|
373
|
+
(char_code >= A_UPPER && char_code <= Z_UPPER) ||
|
|
374
|
+
(char_code >= A_LOWER && char_code <= Z_LOWER) ||
|
|
375
|
+
(char_code >= ZERO && char_code <= NINE);
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Lookup table for valid URI path characters per RFC 3986.
|
|
379
|
+
* Replaces a 16-comparison chain with a single array access.
|
|
380
|
+
*
|
|
381
|
+
* Valid characters:
|
|
382
|
+
* - unreserved: A-Z a-z 0-9 - . _ ~
|
|
383
|
+
* - sub-delims: ! $ & ' ( ) * + , ; =
|
|
384
|
+
* - path allowed: : @
|
|
385
|
+
* - separators: / ? #
|
|
386
|
+
* - percent-encoding: %
|
|
387
|
+
*/
|
|
388
|
+
const PATH_CHAR_TABLE: Uint8Array = (() => {
|
|
389
|
+
const t = new Uint8Array(128);
|
|
390
|
+
for (let i = A_UPPER; i <= Z_UPPER; i++) t[i] = 1;
|
|
391
|
+
for (let i = A_LOWER; i <= Z_LOWER; i++) t[i] = 1;
|
|
392
|
+
for (let i = ZERO; i <= NINE; i++) t[i] = 1;
|
|
393
|
+
// unreserved: - . _ ~
|
|
394
|
+
t[HYPHEN] = 1;
|
|
395
|
+
t[PERIOD] = 1;
|
|
396
|
+
t[UNDERSCORE] = 1;
|
|
397
|
+
t[TILDE] = 1;
|
|
398
|
+
// sub-delims: ! $ & ' ( ) * + , ; =
|
|
399
|
+
t[EXCLAMATION] = 1;
|
|
400
|
+
t[DOLLAR] = 1;
|
|
401
|
+
t[AMPERSAND] = 1;
|
|
402
|
+
t[APOSTROPHE] = 1;
|
|
403
|
+
t[LEFT_PAREN] = 1;
|
|
404
|
+
t[RIGHT_PAREN] = 1;
|
|
405
|
+
t[ASTERISK] = 1;
|
|
406
|
+
t[PLUS] = 1;
|
|
407
|
+
t[COMMA] = 1;
|
|
408
|
+
t[SEMICOLON] = 1;
|
|
409
|
+
t[EQUALS] = 1;
|
|
410
|
+
// path allowed: : @
|
|
411
|
+
t[COLON] = 1;
|
|
412
|
+
t[AT] = 1;
|
|
413
|
+
// separators: / ? #
|
|
414
|
+
t[SLASH] = 1;
|
|
415
|
+
t[QUESTION] = 1;
|
|
416
|
+
t[HASH] = 1;
|
|
417
|
+
// percent-encoding: %
|
|
418
|
+
t[PERCENT] = 1;
|
|
419
|
+
return t;
|
|
420
|
+
})();
|
|
421
|
+
|
|
422
|
+
/** @nodocs */
|
|
423
|
+
export const is_valid_path_char = (char_code: number): boolean =>
|
|
424
|
+
char_code < 128 && PATH_CHAR_TABLE[char_code] === 1;
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Trim trailing punctuation from URL/path per RFC 3986 and GFM rules.
|
|
428
|
+
* - Trims simple trailing: .,;:!?]
|
|
429
|
+
* - Balanced logic for () only (valid in path components)
|
|
430
|
+
*
|
|
431
|
+
* Note on `]`: the mdz parsers (`mdz.ts`, `mdz_stream_parser_url.ts`,
|
|
432
|
+
* `mdz_lexer.ts`) scan URL/path chars through `is_valid_path_char`, which
|
|
433
|
+
* already rejects `]` — so it can never reach this function via parser flow.
|
|
434
|
+
* The `]` branch here is for external callers using this helper directly on
|
|
435
|
+
* arbitrary URL-ish input as part of the published `@fuzdev/mdz` surface.
|
|
436
|
+
*
|
|
437
|
+
* Optimized to avoid O(n²) string slicing - tracks end index and slices once at the end.
|
|
438
|
+
*/
|
|
439
|
+
export const mdz_trim_url_punctuation = (url: string): string => {
|
|
440
|
+
let end = url.length;
|
|
441
|
+
|
|
442
|
+
while (end > 0) {
|
|
443
|
+
const last_char = url.charCodeAt(end - 1);
|
|
444
|
+
if (
|
|
445
|
+
last_char === PERIOD ||
|
|
446
|
+
last_char === COMMA ||
|
|
447
|
+
last_char === SEMICOLON ||
|
|
448
|
+
last_char === COLON ||
|
|
449
|
+
last_char === EXCLAMATION ||
|
|
450
|
+
last_char === QUESTION ||
|
|
451
|
+
last_char === RIGHT_BRACKET
|
|
452
|
+
) {
|
|
453
|
+
end--;
|
|
454
|
+
} else {
|
|
455
|
+
break;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// Handle balanced parentheses ONLY (parens are valid in URI path components)
|
|
460
|
+
// Only count parens when the trimmed URL ends with ')' — otherwise the
|
|
461
|
+
// trailing-paren loop below can't trim anything.
|
|
462
|
+
if (end > 0 && url.charCodeAt(end - 1) === RIGHT_PAREN) {
|
|
463
|
+
let open_count = 0;
|
|
464
|
+
let close_count = 0;
|
|
465
|
+
for (let i = 0; i < end; i++) {
|
|
466
|
+
const char = url.charCodeAt(i);
|
|
467
|
+
if (char === LEFT_PAREN) open_count++;
|
|
468
|
+
if (char === RIGHT_PAREN) close_count++;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// Trim unmatched trailing closing parens
|
|
472
|
+
while (end > 0 && close_count > open_count) {
|
|
473
|
+
const last_char = url.charCodeAt(end - 1);
|
|
474
|
+
if (last_char === RIGHT_PAREN) {
|
|
475
|
+
end--;
|
|
476
|
+
close_count--;
|
|
477
|
+
} else {
|
|
478
|
+
break;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// Return original string if no trimming, otherwise slice once
|
|
484
|
+
return end === url.length ? url : url.slice(0, end);
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Check if position in text is the start of an absolute path (starts with `/`).
|
|
489
|
+
* Must be preceded by whitespace or be at the start of the string.
|
|
490
|
+
* Rejects `//` (comments/protocol-relative) and slash followed by whitespace.
|
|
491
|
+
*
|
|
492
|
+
* @nodocs
|
|
493
|
+
*/
|
|
494
|
+
export const is_at_absolute_path = (text: string, index: number): boolean => {
|
|
495
|
+
if (text.charCodeAt(index) !== SLASH) return false;
|
|
496
|
+
if (index > 0) {
|
|
497
|
+
const prev_char = text.charCodeAt(index - 1);
|
|
498
|
+
if (prev_char !== SPACE && prev_char !== NEWLINE && prev_char !== TAB) return false;
|
|
499
|
+
}
|
|
500
|
+
if (index + 1 >= text.length) return false;
|
|
501
|
+
const next_char = text.charCodeAt(index + 1);
|
|
502
|
+
return next_char !== SLASH && next_char !== SPACE && next_char !== NEWLINE && next_char !== TAB;
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Check if position in text is the start of a relative path (`./` or `../`).
|
|
507
|
+
* Must be preceded by whitespace or be at the start of the string.
|
|
508
|
+
* Rejects prefix followed by whitespace, slash, or end of string.
|
|
509
|
+
*
|
|
510
|
+
* @nodocs
|
|
511
|
+
*/
|
|
512
|
+
export const is_at_relative_path = (text: string, index: number): boolean => {
|
|
513
|
+
if (text.charCodeAt(index) !== PERIOD) return false;
|
|
514
|
+
if (index > 0) {
|
|
515
|
+
const prev_char = text.charCodeAt(index - 1);
|
|
516
|
+
if (prev_char !== SPACE && prev_char !== NEWLINE && prev_char !== TAB) return false;
|
|
517
|
+
}
|
|
518
|
+
const remaining = text.length - index;
|
|
519
|
+
// Check for ../ (at least 4 chars: ../x)
|
|
520
|
+
if (
|
|
521
|
+
remaining >= 4 &&
|
|
522
|
+
text.charCodeAt(index + 1) === PERIOD &&
|
|
523
|
+
text.charCodeAt(index + 2) === SLASH
|
|
524
|
+
) {
|
|
525
|
+
const after = text.charCodeAt(index + 3);
|
|
526
|
+
return after !== SPACE && after !== NEWLINE && after !== TAB && after !== SLASH;
|
|
527
|
+
}
|
|
528
|
+
// Check for ./ (at least 3 chars: ./x)
|
|
529
|
+
if (remaining >= 3 && text.charCodeAt(index + 1) === SLASH) {
|
|
530
|
+
const after = text.charCodeAt(index + 2);
|
|
531
|
+
return after !== SPACE && after !== NEWLINE && after !== TAB && after !== SLASH;
|
|
532
|
+
}
|
|
533
|
+
return false;
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Extracts plain text content from an array of mdz nodes, recursing into children.
|
|
538
|
+
*/
|
|
539
|
+
export const mdz_text_content = (nodes: Array<MdzNode>): string =>
|
|
540
|
+
nodes
|
|
541
|
+
.map((n) => ('children' in n ? mdz_text_content(n.children) : 'content' in n ? n.content : ''))
|
|
542
|
+
.join('');
|
|
543
|
+
|
|
544
|
+
// lowercase slug for heading ids: lowercase, strip chars outside word/space/hyphen,
|
|
545
|
+
// then collapse whitespace and hyphen runs into a single `-` (empty segments dropped)
|
|
546
|
+
const slugify = (str: string): string =>
|
|
547
|
+
str
|
|
548
|
+
.toLowerCase()
|
|
549
|
+
.replace(/[^\s\w-]/g, '')
|
|
550
|
+
.split(/[\s-]+/g)
|
|
551
|
+
.filter(Boolean)
|
|
552
|
+
.join('-');
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Generates a lowercase slug id for a heading from its child nodes.
|
|
556
|
+
* Follows standard markdown conventions (GitHub, etc.) where heading IDs are lowercased.
|
|
557
|
+
*/
|
|
558
|
+
export const mdz_heading_id = (nodes: Array<MdzNode>): string => slugify(mdz_text_content(nodes));
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* Generates a lowercase slug id for a heading from plain text content.
|
|
562
|
+
* Used by the streaming parser which tracks text content directly
|
|
563
|
+
* rather than building `MdzNode[]` trees.
|
|
564
|
+
*/
|
|
565
|
+
export const mdz_heading_id_from_text = (text: string): string => slugify(text);
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* Check if a string is a URL (`https://` or `http://`).
|
|
569
|
+
* Requires at least one valid character after the protocol.
|
|
570
|
+
* Rejects whitespace and characters that can't start a valid hostname.
|
|
571
|
+
*/
|
|
572
|
+
const URL_PATTERN = /^https?:\/\/[^\s)\]}<>.,:/?#!]/i;
|
|
573
|
+
export const mdz_is_url = (s: string): boolean => URL_PATTERN.test(s);
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* Check if a link reference is safe to use as an `href` attribute.
|
|
577
|
+
* References without a colon are always safe (paths, fragments, queries).
|
|
578
|
+
* References with a colon must use `http(s)://` — rejects `javascript:`, `data:`, etc.
|
|
579
|
+
*/
|
|
580
|
+
const SAFE_PROTOCOL_PATTERN = /^https?:\/\//i;
|
|
581
|
+
export const mdz_is_safe_reference = (reference: string): boolean =>
|
|
582
|
+
!reference.includes(':') || SAFE_PROTOCOL_PATTERN.test(reference);
|
|
583
|
+
|
|
584
|
+
const VOID_ELEMENTS = new Set([
|
|
585
|
+
'area',
|
|
586
|
+
'base',
|
|
587
|
+
'br',
|
|
588
|
+
'col',
|
|
589
|
+
'embed',
|
|
590
|
+
'hr',
|
|
591
|
+
'img',
|
|
592
|
+
'input',
|
|
593
|
+
'link',
|
|
594
|
+
'meta',
|
|
595
|
+
'source',
|
|
596
|
+
'track',
|
|
597
|
+
'wbr',
|
|
598
|
+
]);
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* Check if an element name is an HTML void element (`br`, `img`, `hr`, ...).
|
|
602
|
+
* Void elements render self-closing; any parsed children are dropped.
|
|
603
|
+
*/
|
|
604
|
+
// TODO the parsers don't know void-ness — `<br>text</br>` parses as an open/close
|
|
605
|
+
// pair with children that renderers silently drop; consider treating void tags as
|
|
606
|
+
// implicitly self-closing at parse time, or rejecting the open/close form
|
|
607
|
+
export const mdz_is_void_element = (name: string): boolean => VOID_ELEMENTS.has(name);
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Resolves a relative path (`./` or `../`) against a base path.
|
|
611
|
+
* The base is treated as a directory regardless of trailing slash
|
|
612
|
+
* (`'/docs/usage'` and `'/docs/usage/'` behave identically).
|
|
613
|
+
* Handles embedded `.` and `..` segments within the reference
|
|
614
|
+
* (e.g., `'./a/../b'` → navigates up then down).
|
|
615
|
+
* Clamps at root — excess `..` segments stop at `/` rather than escaping.
|
|
616
|
+
*
|
|
617
|
+
* @param reference - a relative path starting with `./` or `../`
|
|
618
|
+
* @param base - An absolute base path (e.g., `'/docs/usage/'`). Empty string is treated as root.
|
|
619
|
+
* @returns an absolute resolved path (e.g., `'/docs/usage/grammar'`)
|
|
620
|
+
*/
|
|
621
|
+
export const mdz_resolve_relative_path = (reference: string, base: string): string => {
|
|
622
|
+
const segments = base.split('/');
|
|
623
|
+
// Remove trailing empty from split (e.g., '/docs/usage/' → ['', 'docs', 'mdz', ''])
|
|
624
|
+
// but keep the root segment ([''] from '' base or ['', ''] from '/').
|
|
625
|
+
if (segments.length > 1 && segments.at(-1) === '') segments.pop();
|
|
626
|
+
const trailing = reference.endsWith('/');
|
|
627
|
+
for (const segment of reference.split('/')) {
|
|
628
|
+
if (segment === '.' || segment === '') continue;
|
|
629
|
+
if (segment === '..') {
|
|
630
|
+
if (segments.length > 1) segments.pop(); // clamp at root
|
|
631
|
+
} else {
|
|
632
|
+
segments.push(segment);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
if (trailing) segments.push('');
|
|
636
|
+
return segments.join('/');
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
/**
|
|
640
|
+
* Push a node into a children array, coalescing with the previous Text node.
|
|
641
|
+
* Mutates `prev.content` and `prev.end` when both are Text, avoiding array growth
|
|
642
|
+
* and an extra allocation. Callers must own `dest` and not retain references to
|
|
643
|
+
* the prior last element across the call.
|
|
644
|
+
*/
|
|
645
|
+
export const mdz_push_merging_text = (dest: Array<MdzNode>, node: MdzNode): void => {
|
|
646
|
+
if (node.type === 'Text') {
|
|
647
|
+
const last = dest[dest.length - 1];
|
|
648
|
+
if (last?.type === 'Text') {
|
|
649
|
+
last.content += node.content;
|
|
650
|
+
last.end = node.end;
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
dest.push(node);
|
|
655
|
+
};
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Return a new array with adjacent Text nodes merged into single nodes.
|
|
659
|
+
* Fast path: returns the original array when no merging is needed.
|
|
660
|
+
*/
|
|
661
|
+
export const mdz_merge_adjacent_text = (nodes: Array<MdzNode>): Array<MdzNode> => {
|
|
662
|
+
if (nodes.length <= 1) return nodes;
|
|
663
|
+
|
|
664
|
+
let needs_merge = false;
|
|
665
|
+
for (let i = 1; i < nodes.length; i++) {
|
|
666
|
+
if (nodes[i - 1]!.type === 'Text' && nodes[i]!.type === 'Text') {
|
|
667
|
+
needs_merge = true;
|
|
668
|
+
break;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
if (!needs_merge) return nodes;
|
|
672
|
+
|
|
673
|
+
const merged: Array<MdzNode> = [];
|
|
674
|
+
let pending: MdzNodeText | null = null;
|
|
675
|
+
|
|
676
|
+
for (const node of nodes) {
|
|
677
|
+
if (node.type === 'Text') {
|
|
678
|
+
if (pending) {
|
|
679
|
+
pending = {
|
|
680
|
+
type: 'Text',
|
|
681
|
+
content: pending.content + node.content,
|
|
682
|
+
start: pending.start,
|
|
683
|
+
end: node.end,
|
|
684
|
+
};
|
|
685
|
+
} else {
|
|
686
|
+
pending = {...node};
|
|
687
|
+
}
|
|
688
|
+
} else {
|
|
689
|
+
if (pending) {
|
|
690
|
+
merged.push(pending);
|
|
691
|
+
pending = null;
|
|
692
|
+
}
|
|
693
|
+
merged.push(node);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
if (pending) merged.push(pending);
|
|
698
|
+
|
|
699
|
+
return merged;
|
|
700
|
+
};
|
|
701
|
+
|
|
702
|
+
/**
|
|
703
|
+
* Extracts a single tag (component or element) if it's the only non-whitespace content.
|
|
704
|
+
*
|
|
705
|
+
* @returns the tag node if paragraph wrapping should be skipped (MDX convention),
|
|
706
|
+
* or `null` if the content should be wrapped in a paragraph
|
|
707
|
+
*/
|
|
708
|
+
export const mdz_extract_single_tag = (
|
|
709
|
+
nodes: Array<MdzNode>,
|
|
710
|
+
): MdzNodeComponent | MdzNodeElement | null => {
|
|
711
|
+
let tag: MdzNodeComponent | MdzNodeElement | null = null;
|
|
712
|
+
|
|
713
|
+
for (const node of nodes) {
|
|
714
|
+
if (node.type === 'Component' || node.type === 'Element') {
|
|
715
|
+
if (tag) return null; // Multiple tags
|
|
716
|
+
tag = node;
|
|
717
|
+
} else if (node.type === 'Text') {
|
|
718
|
+
// Allow only whitespace-only text nodes
|
|
719
|
+
if (node.content.trim() !== '') return null;
|
|
720
|
+
} else {
|
|
721
|
+
// Any other node type means not a single tag
|
|
722
|
+
return null;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
return tag;
|
|
727
|
+
};
|