@fuzdev/mdz 0.3.0 → 0.5.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.
Files changed (81) hide show
  1. package/LICENSE +10 -17
  2. package/README.md +6 -3
  3. package/dist/Mdz.svelte +8 -7
  4. package/dist/Mdz.svelte.d.ts.map +1 -1
  5. package/dist/MdzNodeView.svelte +74 -30
  6. package/dist/MdzNodeView.svelte.d.ts +1 -2
  7. package/dist/MdzNodeView.svelte.d.ts.map +1 -1
  8. package/dist/MdzPrecompiled.svelte +3 -3
  9. package/dist/MdzPrecompiled.svelte.d.ts.map +1 -1
  10. package/dist/MdzRoot.svelte +3 -3
  11. package/dist/MdzRoot.svelte.d.ts.map +1 -1
  12. package/dist/MdzStream.svelte +3 -3
  13. package/dist/MdzStream.svelte.d.ts.map +1 -1
  14. package/dist/MdzStreamNodeView.svelte +79 -31
  15. package/dist/MdzStreamNodeView.svelte.d.ts +1 -2
  16. package/dist/MdzStreamNodeView.svelte.d.ts.map +1 -1
  17. package/dist/context_helpers.js +1 -1
  18. package/dist/mdz.d.ts +12 -1
  19. package/dist/mdz.d.ts.map +1 -1
  20. package/dist/mdz.js +1 -1
  21. package/dist/mdz_contexts.d.ts +33 -6
  22. package/dist/mdz_contexts.d.ts.map +1 -1
  23. package/dist/mdz_debug_work.d.ts.map +1 -1
  24. package/dist/mdz_helpers.d.ts +31 -3
  25. package/dist/mdz_helpers.d.ts.map +1 -1
  26. package/dist/mdz_helpers.js +104 -12
  27. package/dist/mdz_lexer.d.ts +3 -1
  28. package/dist/mdz_lexer.d.ts.map +1 -1
  29. package/dist/mdz_lexer.js +115 -31
  30. package/dist/mdz_opcodes.d.ts +3 -1
  31. package/dist/mdz_opcodes.d.ts.map +1 -1
  32. package/dist/mdz_opcodes_to_nodes.d.ts.map +1 -1
  33. package/dist/mdz_opcodes_to_nodes.js +25 -17
  34. package/dist/mdz_stream_parser.d.ts.map +1 -1
  35. package/dist/mdz_stream_parser.js +8 -8
  36. package/dist/mdz_stream_parser_block.js +5 -5
  37. package/dist/mdz_stream_parser_inline.js +4 -4
  38. package/dist/mdz_stream_parser_link.d.ts.map +1 -1
  39. package/dist/mdz_stream_parser_link.js +83 -11
  40. package/dist/mdz_stream_parser_list.d.ts.map +1 -1
  41. package/dist/mdz_stream_parser_list.js +13 -7
  42. package/dist/mdz_stream_parser_state.d.ts.map +1 -1
  43. package/dist/mdz_stream_parser_state.js +7 -7
  44. package/dist/mdz_stream_parser_table.d.ts.map +1 -1
  45. package/dist/mdz_stream_parser_table.js +16 -9
  46. package/dist/mdz_stream_parser_text.js +2 -2
  47. package/dist/mdz_stream_parser_url.js +7 -7
  48. package/dist/mdz_stream_state.svelte.d.ts +3 -1
  49. package/dist/mdz_stream_state.svelte.d.ts.map +1 -1
  50. package/dist/mdz_stream_state.svelte.js +4 -0
  51. package/dist/mdz_to_svelte.d.ts +17 -4
  52. package/dist/mdz_to_svelte.d.ts.map +1 -1
  53. package/dist/mdz_to_svelte.js +22 -9
  54. package/dist/mdz_token_parser.d.ts.map +1 -1
  55. package/dist/mdz_token_parser.js +25 -11
  56. package/dist/svelte_preprocess_mdz.d.ts +6 -3
  57. package/dist/svelte_preprocess_mdz.d.ts.map +1 -1
  58. package/dist/svelte_preprocess_mdz.js +11 -11
  59. package/package.json +25 -40
  60. package/src/lib/context_helpers.ts +2 -2
  61. package/src/lib/mdz.ts +15 -3
  62. package/src/lib/mdz_contexts.ts +41 -11
  63. package/src/lib/mdz_debug_work.ts +1 -1
  64. package/src/lib/mdz_helpers.ts +164 -43
  65. package/src/lib/mdz_lexer.ts +173 -80
  66. package/src/lib/mdz_opcodes.ts +3 -1
  67. package/src/lib/mdz_opcodes_to_nodes.ts +40 -30
  68. package/src/lib/mdz_stream_parser.ts +17 -17
  69. package/src/lib/mdz_stream_parser_block.ts +14 -14
  70. package/src/lib/mdz_stream_parser_inline.ts +18 -18
  71. package/src/lib/mdz_stream_parser_link.ts +88 -18
  72. package/src/lib/mdz_stream_parser_list.ts +31 -25
  73. package/src/lib/mdz_stream_parser_state.ts +27 -27
  74. package/src/lib/mdz_stream_parser_table.ts +38 -31
  75. package/src/lib/mdz_stream_parser_text.ts +3 -3
  76. package/src/lib/mdz_stream_parser_url.ts +11 -11
  77. package/src/lib/mdz_stream_state.svelte.ts +11 -8
  78. package/src/lib/mdz_to_svelte.ts +71 -27
  79. package/src/lib/mdz_token_parser.ts +52 -38
  80. package/src/lib/svelte_preprocess_mdz.ts +66 -60
  81. package/src/lib/tsdoc_mdz.ts +1 -1
@@ -7,7 +7,18 @@
7
7
  * @module
8
8
  */
9
9
 
10
- import type {MdzNode, MdzNodeText, MdzNodeComponent, MdzNodeElement, MdzTableAlign} from './mdz.ts';
10
+ import { DEV } from 'esm-env';
11
+ import type { Component } from 'svelte';
12
+
13
+ import type {
14
+ MdzAttribute,
15
+ MdzNode,
16
+ MdzNodeText,
17
+ MdzNodeComponent,
18
+ MdzNodeElement,
19
+ MdzTableAlign
20
+ } from './mdz.ts';
21
+ import type { MdzComponentConfig, MdzComponentProps } from './mdz_contexts.ts';
11
22
 
12
23
  // Character codes for performance
13
24
  /** @nodocs */
@@ -70,6 +81,8 @@ export const AMPERSAND = 38; // &
70
81
  /** @nodocs */
71
82
  export const APOSTROPHE = 39; // '
72
83
  /** @nodocs */
84
+ export const DOUBLE_QUOTE = 34; // "
85
+ /** @nodocs */
73
86
  export const PLUS = 43; // +
74
87
  /** @nodocs */
75
88
  export const EQUALS = 61; // =
@@ -197,7 +210,7 @@ export interface MdzBlockquotePrefix {
197
210
  export const mdz_match_blockquote_prefix = (
198
211
  text: string,
199
212
  i: number,
200
- forced: boolean,
213
+ forced: boolean
201
214
  ): MdzBlockquotePrefix | 'pending' | null => {
202
215
  let depth = 0;
203
216
  let pos = i;
@@ -207,23 +220,23 @@ export const mdz_match_blockquote_prefix = (
207
220
  depth++;
208
221
  pos++;
209
222
  if (pos >= text.length) {
210
- return forced ? {depth, content_start: pos} : 'pending';
223
+ return forced ? { depth, content_start: pos } : 'pending';
211
224
  }
212
225
  const c = text.charCodeAt(pos);
213
226
  if (c === RIGHT_ANGLE) continue; // tight run
214
227
  if (c === SPACE) {
215
228
  if (pos + 1 >= text.length) {
216
229
  // could be a separator (next chunk starts with `>`) or the terminator
217
- return forced ? {depth, content_start: pos + 1} : 'pending';
230
+ return forced ? { depth, content_start: pos + 1 } : 'pending';
218
231
  }
219
232
  if (text.charCodeAt(pos + 1) === RIGHT_ANGLE) {
220
233
  pos++; // separator — continue the run at the next `>`
221
234
  continue;
222
235
  }
223
- return {depth, content_start: pos + 1}; // terminating space
236
+ return { depth, content_start: pos + 1 }; // terminating space
224
237
  }
225
238
  if (c === NEWLINE) {
226
- return {depth, content_start: pos}; // bare prefix line
239
+ return { depth, content_start: pos }; // bare prefix line
227
240
  }
228
241
  if (is_line_whitespace(c)) {
229
242
  // tab/CR is not the marker space — valid only as tolerated trailing
@@ -233,17 +246,17 @@ export const mdz_match_blockquote_prefix = (
233
246
  j++;
234
247
  }
235
248
  if (j >= text.length) {
236
- return forced ? {depth, content_start: pos} : 'pending';
249
+ return forced ? { depth, content_start: pos } : 'pending';
237
250
  }
238
251
  if (text.charCodeAt(j) === NEWLINE) {
239
- return {depth, content_start: pos};
252
+ return { depth, content_start: pos };
240
253
  }
241
254
  // trailing content after non-space whitespace — fall through to backtrack
242
255
  }
243
256
  // invalid char directly after a `>`: this `>` is content; the separator
244
257
  // space before it (if any) re-serves as the terminator
245
258
  if (depth >= 2 && text.charCodeAt(pos - 2) === SPACE) {
246
- return {depth: depth - 1, content_start: pos - 1};
259
+ return { depth: depth - 1, content_start: pos - 1 };
247
260
  }
248
261
  return null;
249
262
  }
@@ -260,7 +273,7 @@ export const mdz_match_blockquote_prefix = (
260
273
  export const mdz_blockquote_line_has_content = (
261
274
  text: string,
262
275
  content_start: number,
263
- forced: boolean,
276
+ forced: boolean
264
277
  ): boolean | 'pending' => {
265
278
  let i = content_start;
266
279
  while (i < text.length && is_line_whitespace(text.charCodeAt(i))) {
@@ -291,8 +304,8 @@ export const mdz_blockquote_strip_width = (text: string, i: number): number =>
291
304
  * @nodocs
292
305
  */
293
306
  export const mdz_remap_segments = (
294
- segments: Array<{inner_start: number; source_start: number}>,
295
- p: number,
307
+ segments: Array<{ inner_start: number; source_start: number }>,
308
+ p: number
296
309
  ): number => {
297
310
  let lo = 0;
298
311
  let hi = segments.length - 1;
@@ -314,13 +327,13 @@ export const mdz_remap_segments = (
314
327
  const trim_table_cell = (
315
328
  text: string,
316
329
  start: number,
317
- end: number,
318
- ): {start: number; end: number} => {
330
+ end: number
331
+ ): { start: number; end: number } => {
319
332
  let s = start;
320
333
  let e = end;
321
334
  while (s < e && is_line_whitespace(text.charCodeAt(s))) s++;
322
335
  while (e > s && is_line_whitespace(text.charCodeAt(e - 1))) e--;
323
- return {start: s, end: e};
336
+ return { start: s, end: e };
324
337
  };
325
338
 
326
339
  /**
@@ -343,10 +356,10 @@ const trim_table_cell = (
343
356
  export const mdz_split_table_row = (
344
357
  text: string,
345
358
  line_start: number,
346
- line_end: number,
347
- ): Array<{start: number; end: number}> | null => {
359
+ line_end: number
360
+ ): Array<{ start: number; end: number }> | null => {
348
361
  if (line_start >= line_end || text.charCodeAt(line_start) !== PIPE) return null;
349
- const cells: Array<{start: number; end: number}> = [];
362
+ const cells: Array<{ start: number; end: number }> = [];
350
363
  let i = line_start + 1; // past the leading pipe
351
364
  let cell_start = i;
352
365
  while (i < line_end) {
@@ -415,7 +428,7 @@ const table_delimiter_align = (text: string, start: number, end: number): MdzTab
415
428
  export const mdz_parse_table_delimiter = (
416
429
  text: string,
417
430
  line_start: number,
418
- line_end: number,
431
+ line_end: number
419
432
  ): Array<MdzTableAlign> | null => {
420
433
  const cells = mdz_split_table_row(text, line_start, line_end);
421
434
  if (cells === null) return null;
@@ -769,7 +782,7 @@ const VOID_ELEMENTS = new Set([
769
782
  'meta',
770
783
  'source',
771
784
  'track',
772
- 'wbr',
785
+ 'wbr'
773
786
  ]);
774
787
 
775
788
  /**
@@ -781,6 +794,93 @@ const VOID_ELEMENTS = new Set([
781
794
  // implicitly self-closing at parse time, or rejecting the open/close form
782
795
  export const mdz_is_void_element = (name: string): boolean => VOID_ELEMENTS.has(name);
783
796
 
797
+ /**
798
+ * The closed, global set of attribute names allowed on rendered HTML elements.
799
+ * Every name is inert on any element (no URL, no script, no focus or DOM
800
+ * clobbering), so element safety is independent of which element names a
801
+ * consumer registers. Anything outside this set is dropped by
802
+ * `mdz_filter_element_attributes`.
803
+ */
804
+ export const ALLOWED_ELEMENT_ATTRIBUTES: ReadonlySet<string> = new Set([
805
+ 'class',
806
+ 'title',
807
+ 'lang',
808
+ 'dir',
809
+ 'role',
810
+ 'aria-label',
811
+ 'aria-hidden',
812
+ 'aria-describedby',
813
+ 'aria-labelledby'
814
+ ]);
815
+
816
+ // filters an element's parsed attributes against the allowlist, returning a
817
+ // name→value object ready to spread onto `<svelte:element {...}>`. Disallowed
818
+ // names are dropped (DEV-warned). Used identically by MdzNodeView,
819
+ // MdzStreamNodeView, and mdz_to_svelte, so precompiled and runtime output match.
820
+ export const mdz_filter_element_attributes = (
821
+ element_name: string,
822
+ attributes: Array<MdzAttribute>,
823
+ allowlist: ReadonlySet<string>
824
+ ): Record<string, string | true> => {
825
+ const filtered: Record<string, string | true> = {};
826
+ for (const attr of attributes) {
827
+ if (allowlist.has(attr.name)) {
828
+ filtered[attr.name] = attr.value;
829
+ } else if (DEV) {
830
+ console.warn(
831
+ `mdz: dropped disallowed attribute \`${attr.name}\` on <${element_name}> — not in the element attribute allowlist`
832
+ );
833
+ }
834
+ }
835
+ return filtered;
836
+ };
837
+
838
+ // filters a component's parsed attributes against its registered `props`,
839
+ // returning a name→value props object spread onto `<Component {...props}>`.
840
+ // Omitted `props` passes nothing, `'all'` passes everything; dropped names are
841
+ // DEV-warned. Used identically by MdzNodeView, MdzStreamNodeView, and
842
+ // mdz_to_svelte, so precompiled and runtime output match.
843
+ export const mdz_filter_component_attributes = (
844
+ component_name: string,
845
+ attributes: Array<MdzAttribute>,
846
+ props: MdzComponentProps | undefined
847
+ ): Record<string, string | true> => {
848
+ const filtered: Record<string, string | true> = {};
849
+ for (const attr of attributes) {
850
+ if (props === 'all' || props?.includes(attr.name)) {
851
+ filtered[attr.name] = attr.value;
852
+ } else if (DEV) {
853
+ console.warn(
854
+ `mdz: dropped attribute \`${attr.name}\` on <${component_name}> — not in its registered props`
855
+ );
856
+ }
857
+ }
858
+ return filtered;
859
+ };
860
+
861
+ // normalizes a component registry entry to its config form
862
+ export const mdz_component_config = (
863
+ entry: Component<any, any> | MdzComponentConfig
864
+ ): MdzComponentConfig => (typeof entry === 'function' ? { component: entry } : entry);
865
+
866
+ // reconstructs authored attribute text (` name="value"` / ` name`) for the
867
+ // unregistered-tag literal placeholder. The quote char is chosen so a value
868
+ // containing one quote type stays balanced — a parsed value holds at most one
869
+ // (a `"`-delimited value can't contain `"`, a `'`-delimited one can't contain
870
+ // `'`). The caller interpolates the result as Svelte text (auto-escaped).
871
+ export const mdz_format_unregistered_attributes = (attributes: Array<MdzAttribute>): string => {
872
+ let text = '';
873
+ for (const attr of attributes) {
874
+ if (attr.value === true) {
875
+ text += ` ${attr.name}`;
876
+ } else {
877
+ const quote = attr.value.includes('"') ? "'" : '"';
878
+ text += ` ${attr.name}=${quote}${attr.value}${quote}`;
879
+ }
880
+ }
881
+ return text;
882
+ };
883
+
784
884
  /**
785
885
  * Resolves a relative path (`./` or `../`) against a base path.
786
886
  * The base is treated as a directory regardless of trailing slash
@@ -811,6 +911,20 @@ export const mdz_resolve_relative_path = (reference: string, base: string): stri
811
911
  return segments.join('/');
812
912
  };
813
913
 
914
+ // `/a` → `a`, `//a` → `a` — the pathname form `resolve()` takes; `\` counts as a
915
+ // slash too, and tab/LF/CR are dropped, since browsers read `/\host` and
916
+ // `/<tab>/host` as protocol-relative
917
+ const strip_leading_slashes = (path: string): string => {
918
+ let i = 0;
919
+ for (;;) {
920
+ const c = path.charCodeAt(i);
921
+ if (c !== SLASH && c !== BACKSLASH && c !== TAB && c !== NEWLINE && c !== CARRIAGE_RETURN)
922
+ break;
923
+ i++;
924
+ }
925
+ return path.slice(i);
926
+ };
927
+
814
928
  /**
815
929
  * How a `Link` node's `reference` should render, shared by all three renderers
816
930
  * (`MdzNodeView`, `MdzStreamNodeView`, `mdz_to_svelte`) so the safety gate and
@@ -819,42 +933,49 @@ export const mdz_resolve_relative_path = (reference: string, base: string): stri
819
933
  */
820
934
  export type MdzLinkRender =
821
935
  /** Unsafe protocol — render children only, no `<a>`. */
822
- | {kind: 'unsafe'}
823
- /** Route/relative path — wrap in `resolve()` (needs `$app/paths`). */
824
- | {kind: 'resolve'; href: string}
936
+ | { kind: 'unsafe' }
937
+ /**
938
+ * Absolute path — render `resolve(path)` (needs `$app/paths`). `path` is the
939
+ * reference minus its leading slash, SvelteKit's pathname form, which only
940
+ * prepends the base path (or `#` under hash routing; a relative `./`/`../`
941
+ * prefix under `paths.relative` SSR) and keeps the rest verbatim.
942
+ */
943
+ | { kind: 'resolve'; path: string }
825
944
  /** Internal fragment/query/relative/bare ref — raw `href`, no `resolve()`. */
826
- | {kind: 'internal'; href: string}
945
+ | { kind: 'internal'; href: string }
827
946
  /** External link — raw `href` plus `target="_blank" rel="noopener"`. */
828
- | {kind: 'external'; href: string};
947
+ | { kind: 'external'; href: string };
829
948
 
830
949
  /**
831
950
  * Classify a `Link` reference into how it should render. Pure — no rendering,
832
951
  * no context; `base` is the resolved base path (or `undefined`).
952
+ *
953
+ * Absolute references are authored URLs, not SvelteKit route ids: they classify
954
+ * as `resolve` with the base-relative pathname, never a leading-slash route id,
955
+ * whose resolution would drop `(group)` and empty segments, decode `[x+nn]`
956
+ * escapes, and throw on `[param]` segments. A leading `/` followed by any `/`
957
+ * or `\` run collapses to one `/`, so `//host` and `/\host` stay under the base
958
+ * rather than becoming protocol-relative.
833
959
  * @nodocs
834
960
  */
835
961
  export const mdz_classify_link = (
836
962
  reference: string,
837
963
  link_type: 'internal' | 'external' | undefined,
838
- base: string | undefined,
964
+ base: string | undefined
839
965
  ): MdzLinkRender => {
840
- if (!mdz_is_safe_reference(reference)) return {kind: 'unsafe'};
966
+ if (!mdz_is_safe_reference(reference)) return { kind: 'unsafe' };
841
967
  if (link_type === 'internal') {
842
968
  if (reference.startsWith('.') && base) {
843
- return {kind: 'resolve', href: mdz_resolve_relative_path(reference, base)};
844
- }
845
- // fragment/query/relative/bare — `resolve()` only accepts absolute paths or
846
- // route ids and throws on anything else
847
- if (
848
- reference.startsWith('#') ||
849
- reference.startsWith('?') ||
850
- reference.startsWith('.') ||
851
- !reference.startsWith('/')
852
- ) {
853
- return {kind: 'internal', href: reference};
969
+ return {
970
+ kind: 'resolve',
971
+ path: strip_leading_slashes(mdz_resolve_relative_path(reference, base))
972
+ };
854
973
  }
855
- return {kind: 'resolve', href: reference};
974
+ // fragment/query/relative/bare — not under the base, so a raw `href`
975
+ if (!reference.startsWith('/')) return { kind: 'internal', href: reference };
976
+ return { kind: 'resolve', path: strip_leading_slashes(reference) };
856
977
  }
857
- return {kind: 'external', href: reference};
978
+ return { kind: 'external', href: reference };
858
979
  };
859
980
 
860
981
  /**
@@ -901,10 +1022,10 @@ export const mdz_merge_adjacent_text = (nodes: Array<MdzNode>): Array<MdzNode> =
901
1022
  type: 'Text',
902
1023
  content: pending.content + node.content,
903
1024
  start: pending.start,
904
- end: node.end,
1025
+ end: node.end
905
1026
  };
906
1027
  } else {
907
- pending = {...node};
1028
+ pending = { ...node };
908
1029
  }
909
1030
  } else {
910
1031
  if (pending) {
@@ -927,7 +1048,7 @@ export const mdz_merge_adjacent_text = (nodes: Array<MdzNode>): Array<MdzNode> =
927
1048
  * or `null` if the content should be wrapped in a paragraph
928
1049
  */
929
1050
  export const mdz_extract_single_tag = (
930
- nodes: Array<MdzNode>,
1051
+ nodes: Array<MdzNode>
931
1052
  ): MdzNodeComponent | MdzNodeElement | null => {
932
1053
  let tag: MdzNodeComponent | MdzNodeElement | null = null;
933
1054