@fuzdev/fuz_ui 0.185.2 → 0.186.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/dist/Mdz.svelte +5 -0
- package/dist/Mdz.svelte.d.ts +1 -0
- package/dist/Mdz.svelte.d.ts.map +1 -1
- package/dist/MdzNodeView.svelte +19 -8
- package/dist/MdzNodeView.svelte.d.ts +1 -1
- package/dist/MdzNodeView.svelte.d.ts.map +1 -1
- package/dist/mdz.d.ts +13 -0
- package/dist/mdz.d.ts.map +1 -1
- package/dist/mdz.js +73 -280
- package/dist/mdz_components.d.ts +12 -0
- package/dist/mdz_components.d.ts.map +1 -1
- package/dist/mdz_components.js +8 -0
- package/dist/mdz_helpers.d.ts +108 -0
- package/dist/mdz_helpers.d.ts.map +1 -0
- package/dist/mdz_helpers.js +237 -0
- package/dist/mdz_lexer.d.ts +93 -0
- package/dist/mdz_lexer.d.ts.map +1 -0
- package/dist/mdz_lexer.js +727 -0
- package/dist/mdz_to_svelte.d.ts +5 -2
- package/dist/mdz_to_svelte.d.ts.map +1 -1
- package/dist/mdz_to_svelte.js +13 -2
- package/dist/mdz_token_parser.d.ts +14 -0
- package/dist/mdz_token_parser.d.ts.map +1 -0
- package/dist/mdz_token_parser.js +374 -0
- package/dist/svelte_preprocess_mdz.js +23 -7
- package/package.json +3 -2
- package/src/lib/mdz.ts +106 -302
- package/src/lib/mdz_components.ts +9 -0
- package/src/lib/mdz_helpers.ts +251 -0
- package/src/lib/mdz_lexer.ts +1003 -0
- package/src/lib/mdz_to_svelte.ts +15 -2
- package/src/lib/mdz_token_parser.ts +460 -0
- package/src/lib/svelte_preprocess_mdz.ts +23 -7
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared constants and pure helper functions for mdz parsers.
|
|
3
|
+
*
|
|
4
|
+
* Used by both the single-pass parser (`mdz.ts`) and the two-phase
|
|
5
|
+
* lexer+parser (`mdz_lexer.ts` + `mdz_token_parser.ts`).
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type {MdzNode, MdzComponentNode, MdzElementNode} from './mdz.js';
|
|
11
|
+
|
|
12
|
+
// Character codes for performance
|
|
13
|
+
export const BACKTICK = 96; // `
|
|
14
|
+
export const ASTERISK = 42; // *
|
|
15
|
+
export const UNDERSCORE = 95; // _
|
|
16
|
+
export const TILDE = 126; // ~
|
|
17
|
+
export const NEWLINE = 10; // \n
|
|
18
|
+
export const HYPHEN = 45; // -
|
|
19
|
+
export const HASH = 35; // #
|
|
20
|
+
export const SPACE = 32; // (space)
|
|
21
|
+
export const TAB = 9; // \t
|
|
22
|
+
export const LEFT_ANGLE = 60; // <
|
|
23
|
+
export const RIGHT_ANGLE = 62; // >
|
|
24
|
+
export const SLASH = 47; // /
|
|
25
|
+
export const LEFT_BRACKET = 91; // [
|
|
26
|
+
export const RIGHT_BRACKET = 93; // ]
|
|
27
|
+
export const LEFT_PAREN = 40; // (
|
|
28
|
+
export const RIGHT_PAREN = 41; // )
|
|
29
|
+
export const COLON = 58; // :
|
|
30
|
+
export const PERIOD = 46; // .
|
|
31
|
+
export const COMMA = 44; // ,
|
|
32
|
+
export const SEMICOLON = 59; // ;
|
|
33
|
+
export const EXCLAMATION = 33; // !
|
|
34
|
+
export const QUESTION = 63; // ?
|
|
35
|
+
// RFC 3986 URI characters
|
|
36
|
+
export const DOLLAR = 36; // $
|
|
37
|
+
export const PERCENT = 37; // %
|
|
38
|
+
export const AMPERSAND = 38; // &
|
|
39
|
+
export const APOSTROPHE = 39; // '
|
|
40
|
+
export const PLUS = 43; // +
|
|
41
|
+
export const EQUALS = 61; // =
|
|
42
|
+
export const AT = 64; // @
|
|
43
|
+
// Character ranges
|
|
44
|
+
export const A_UPPER = 65; // A
|
|
45
|
+
export const Z_UPPER = 90; // Z
|
|
46
|
+
export const A_LOWER = 97; // a
|
|
47
|
+
export const Z_LOWER = 122; // z
|
|
48
|
+
export const ZERO = 48; // 0
|
|
49
|
+
export const NINE = 57; // 9
|
|
50
|
+
// mdz specification constants
|
|
51
|
+
export const HR_HYPHEN_COUNT = 3; // Horizontal rule requires exactly 3 hyphens
|
|
52
|
+
export const MIN_CODEBLOCK_BACKTICKS = 3; // Code blocks require minimum 3 backticks
|
|
53
|
+
export const MAX_HEADING_LEVEL = 6; // Headings support levels 1-6
|
|
54
|
+
export const HTTPS_PREFIX_LENGTH = 8; // Length of "https://"
|
|
55
|
+
export const HTTP_PREFIX_LENGTH = 7; // Length of "http://"
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Check if character code is a letter (A-Z, a-z).
|
|
59
|
+
*/
|
|
60
|
+
export const is_letter = (char_code: number): boolean =>
|
|
61
|
+
(char_code >= A_UPPER && char_code <= Z_UPPER) || (char_code >= A_LOWER && char_code <= Z_LOWER);
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Check if character code is valid for tag name (letter, number, hyphen, underscore).
|
|
65
|
+
*/
|
|
66
|
+
export const is_tag_name_char = (char_code: number): boolean =>
|
|
67
|
+
is_letter(char_code) ||
|
|
68
|
+
(char_code >= ZERO && char_code <= NINE) ||
|
|
69
|
+
char_code === HYPHEN ||
|
|
70
|
+
char_code === UNDERSCORE;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Check if character is part of a word for word boundary detection.
|
|
74
|
+
* Used to prevent intraword emphasis with `_` and `~` delimiters.
|
|
75
|
+
*
|
|
76
|
+
* Formatting delimiters (`*`, `_`, `~`) are NOT word characters - they're transparent.
|
|
77
|
+
* Only alphanumeric characters (A-Z, a-z, 0-9) are considered word characters.
|
|
78
|
+
*
|
|
79
|
+
* This prevents false positives with snake_case identifiers while allowing
|
|
80
|
+
* adjacent formatting like `**bold**_italic_`.
|
|
81
|
+
*/
|
|
82
|
+
export const is_word_char = (char_code: number): boolean => {
|
|
83
|
+
if (char_code === ASTERISK || char_code === UNDERSCORE || char_code === TILDE) return false;
|
|
84
|
+
return (
|
|
85
|
+
(char_code >= A_UPPER && char_code <= Z_UPPER) ||
|
|
86
|
+
(char_code >= A_LOWER && char_code <= Z_LOWER) ||
|
|
87
|
+
(char_code >= ZERO && char_code <= NINE)
|
|
88
|
+
);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Check if character code is valid in URI path per RFC 3986.
|
|
93
|
+
* Validates against the `pchar` production plus path/query/fragment separators.
|
|
94
|
+
*
|
|
95
|
+
* Valid characters:
|
|
96
|
+
* - unreserved: A-Z a-z 0-9 - . _ ~
|
|
97
|
+
* - sub-delims: ! $ & ' ( ) * + , ; =
|
|
98
|
+
* - path allowed: : @
|
|
99
|
+
* - separators: / ? #
|
|
100
|
+
* - percent-encoding: %
|
|
101
|
+
*/
|
|
102
|
+
export const is_valid_path_char = (char_code: number): boolean =>
|
|
103
|
+
(char_code >= A_UPPER && char_code <= Z_UPPER) ||
|
|
104
|
+
(char_code >= A_LOWER && char_code <= Z_LOWER) ||
|
|
105
|
+
(char_code >= ZERO && char_code <= NINE) ||
|
|
106
|
+
char_code === HYPHEN ||
|
|
107
|
+
char_code === PERIOD ||
|
|
108
|
+
char_code === UNDERSCORE ||
|
|
109
|
+
char_code === TILDE ||
|
|
110
|
+
char_code === EXCLAMATION ||
|
|
111
|
+
char_code === DOLLAR ||
|
|
112
|
+
char_code === AMPERSAND ||
|
|
113
|
+
char_code === APOSTROPHE ||
|
|
114
|
+
char_code === LEFT_PAREN ||
|
|
115
|
+
char_code === RIGHT_PAREN ||
|
|
116
|
+
char_code === ASTERISK ||
|
|
117
|
+
char_code === PLUS ||
|
|
118
|
+
char_code === COMMA ||
|
|
119
|
+
char_code === SEMICOLON ||
|
|
120
|
+
char_code === EQUALS ||
|
|
121
|
+
char_code === COLON ||
|
|
122
|
+
char_code === AT ||
|
|
123
|
+
char_code === SLASH ||
|
|
124
|
+
char_code === QUESTION ||
|
|
125
|
+
char_code === HASH ||
|
|
126
|
+
char_code === PERCENT;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Trim trailing punctuation from URL/path per RFC 3986 and GFM rules.
|
|
130
|
+
* - Trims simple trailing: .,;:!?]
|
|
131
|
+
* - Balanced logic for () only (valid in path components)
|
|
132
|
+
* - Invalid chars like [] {} are already stopped by whitelist, but ] trimmed as fallback
|
|
133
|
+
*
|
|
134
|
+
* Optimized to avoid O(n²) string slicing - tracks end index and slices once at the end.
|
|
135
|
+
*/
|
|
136
|
+
export const trim_trailing_punctuation = (url: string): string => {
|
|
137
|
+
let end = url.length;
|
|
138
|
+
|
|
139
|
+
// Trim simple trailing punctuation (] as fallback - whitelist should prevent it)
|
|
140
|
+
while (end > 0) {
|
|
141
|
+
const last_char = url.charCodeAt(end - 1);
|
|
142
|
+
if (
|
|
143
|
+
last_char === PERIOD ||
|
|
144
|
+
last_char === COMMA ||
|
|
145
|
+
last_char === SEMICOLON ||
|
|
146
|
+
last_char === COLON ||
|
|
147
|
+
last_char === EXCLAMATION ||
|
|
148
|
+
last_char === QUESTION ||
|
|
149
|
+
last_char === RIGHT_BRACKET
|
|
150
|
+
) {
|
|
151
|
+
end--;
|
|
152
|
+
} else {
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Handle balanced parentheses ONLY (parens are valid in URI path components)
|
|
158
|
+
// Count parentheses in the trimmed portion
|
|
159
|
+
let open_count = 0;
|
|
160
|
+
let close_count = 0;
|
|
161
|
+
for (let i = 0; i < end; i++) {
|
|
162
|
+
const char = url.charCodeAt(i);
|
|
163
|
+
if (char === LEFT_PAREN) open_count++;
|
|
164
|
+
if (char === RIGHT_PAREN) close_count++;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Trim unmatched trailing closing parens
|
|
168
|
+
while (end > 0 && close_count > open_count) {
|
|
169
|
+
const last_char = url.charCodeAt(end - 1);
|
|
170
|
+
if (last_char === RIGHT_PAREN) {
|
|
171
|
+
end--;
|
|
172
|
+
close_count--;
|
|
173
|
+
} else {
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Return original string if no trimming, otherwise slice once
|
|
179
|
+
return end === url.length ? url : url.slice(0, end);
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Extract a single tag (component or element) if it's the only non-whitespace content.
|
|
184
|
+
* Returns the tag node if paragraph wrapping should be skipped (MDX convention),
|
|
185
|
+
* or null if the content should be wrapped in a paragraph.
|
|
186
|
+
*/
|
|
187
|
+
/**
|
|
188
|
+
* Check if position in text is the start of an absolute path (starts with `/`).
|
|
189
|
+
* Must be preceded by whitespace or be at the start of the string.
|
|
190
|
+
* Rejects `//` (comments/protocol-relative) and `/ ` (bare slash).
|
|
191
|
+
*/
|
|
192
|
+
export const is_at_absolute_path = (text: string, index: number): boolean => {
|
|
193
|
+
if (text.charCodeAt(index) !== SLASH) return false;
|
|
194
|
+
if (index > 0) {
|
|
195
|
+
const prev_char = text.charCodeAt(index - 1);
|
|
196
|
+
if (prev_char !== SPACE && prev_char !== NEWLINE && prev_char !== TAB) return false;
|
|
197
|
+
}
|
|
198
|
+
if (index + 1 >= text.length) return false;
|
|
199
|
+
const next_char = text.charCodeAt(index + 1);
|
|
200
|
+
return next_char !== SLASH && next_char !== SPACE && next_char !== NEWLINE;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Check if position in text is the start of a relative path (`./` or `../`).
|
|
205
|
+
* Must be preceded by whitespace or be at the start of the string.
|
|
206
|
+
* Requires at least one path character after the prefix.
|
|
207
|
+
*/
|
|
208
|
+
export const is_at_relative_path = (text: string, index: number): boolean => {
|
|
209
|
+
if (text.charCodeAt(index) !== PERIOD) return false;
|
|
210
|
+
if (index > 0) {
|
|
211
|
+
const prev_char = text.charCodeAt(index - 1);
|
|
212
|
+
if (prev_char !== SPACE && prev_char !== NEWLINE && prev_char !== TAB) return false;
|
|
213
|
+
}
|
|
214
|
+
const remaining = text.length - index;
|
|
215
|
+
// Check for ../ (at least 4 chars: ../x)
|
|
216
|
+
if (
|
|
217
|
+
remaining >= 4 &&
|
|
218
|
+
text.charCodeAt(index + 1) === PERIOD &&
|
|
219
|
+
text.charCodeAt(index + 2) === SLASH
|
|
220
|
+
) {
|
|
221
|
+
const after = text.charCodeAt(index + 3);
|
|
222
|
+
return after !== SPACE && after !== NEWLINE && after !== SLASH;
|
|
223
|
+
}
|
|
224
|
+
// Check for ./ (at least 3 chars: ./x)
|
|
225
|
+
if (remaining >= 3 && text.charCodeAt(index + 1) === SLASH) {
|
|
226
|
+
const after = text.charCodeAt(index + 2);
|
|
227
|
+
return after !== SPACE && after !== NEWLINE && after !== SLASH;
|
|
228
|
+
}
|
|
229
|
+
return false;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
export const extract_single_tag = (
|
|
233
|
+
nodes: Array<MdzNode>,
|
|
234
|
+
): MdzComponentNode | MdzElementNode | null => {
|
|
235
|
+
let tag: MdzComponentNode | MdzElementNode | null = null;
|
|
236
|
+
|
|
237
|
+
for (const node of nodes) {
|
|
238
|
+
if (node.type === 'Component' || node.type === 'Element') {
|
|
239
|
+
if (tag) return null; // Multiple tags
|
|
240
|
+
tag = node;
|
|
241
|
+
} else if (node.type === 'Text') {
|
|
242
|
+
// Allow only whitespace-only text nodes
|
|
243
|
+
if (node.content.trim() !== '') return null;
|
|
244
|
+
} else {
|
|
245
|
+
// Any other node type means not a single tag
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return tag;
|
|
251
|
+
};
|