@mdsvex/parse 1.0.0-next.4 → 1.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/PLUGINS.md CHANGED
@@ -267,6 +267,7 @@ const toc = {
267
267
  | `ctx.read(path)`, `ctx.resolve(specifier)` | read and resolve with what the host passes |
268
268
  | `ctx.cached(key, load)` | a load shared by the documents of a compiler |
269
269
  | `ctx.id(base)` | an id no element of the document has, see [Ids](#ids) |
270
+ | `ctx.text(node)` | the text of a node as the page shows it, see [`ctx.text`](#ctxtext) |
270
271
 
271
272
  A handler bound to a directive gets the same members beside `directive`, `index` and `state`.
272
273
 
@@ -289,6 +290,29 @@ The parser knows neither. Whoever runs the parse tells the dispatcher, see [For
289
290
 
290
291
  In `mdsvex` these join the `warnings` of the compile, with `plugin` set to the name of the plugin and `start` as a line and a column. The Vite plugin reports them like any other warning.
291
292
 
293
+ ### `ctx.text`
294
+
295
+ `node.text_content` is the text as the author wrote it. `ctx.text(node)` is the text as the page shows it, which is what a table of contents, a search index or an excerpt wants.
296
+
297
+ ```js
298
+ const plugin = {
299
+ name: 'toc',
300
+ heading: {
301
+ parse(node, ctx) {
302
+ return () => ctx.add('headings', { depth: node.depth, text: ctx.text(node) });
303
+ },
304
+ },
305
+ };
306
+ ```
307
+
308
+ - **A text node gives its text, a code span its code, any other node the text of its children.** An image is its alt and a line end is a space. A comment, a `{expression}`, a component and a directive inside the node add nothing.
309
+ - **A character reference html needs is its character:** a numeric one, and `&`, `<`, `>`, `"`, `'` and ` `. One whose `&` is escaped stays as written.
310
+ - **The smart punctuation of the compile is applied** as the renderer applies it. With `smart_punctuation: true` in `mdsvex`, `## Let's go` gives `Let’s go`. Code, the url of an autolink, an alt and an escaped character stay as written, as they do on the page.
311
+ - **Text a plugin added is as it was added.**
312
+ - **Read it once the block that holds the node has closed.** A quote looks at the text on both sides of it, so in an open handler the answer can change. A delimiter that never closes, such as the `**` of `a **b`, is still an open emphasis in the close callback of a paragraph, so its characters are missing there, from `text_content` too. A heading has settled by its close callback, and the document hook reads every block as it ends up.
313
+
314
+ A caller with a dispatcher of its own passes the option as `smart_punctuation` of `set_document`. Without it the punctuation is as written.
315
+
292
316
  ### `ctx.set`, `ctx.add` and `ctx.data`
293
317
 
294
318
  A plugin hands something back from a parse by writing it: a table of contents, a reading time, an excerpt.
package/WIRE_FORMAT.md CHANGED
@@ -209,7 +209,7 @@ Paragraph {
209
209
 
210
210
  The key insight for streaming renderers:
211
211
 
212
- 1. **Block-level stability.** The root node's children are top-level blocks (headings, paragraphs, code fences, etc.). Once a block is closed, it never changes. Your renderer can cache closed blocks and skip re-rendering them.
212
+ 1. **Block-level stability.** The root node's children are top-level blocks (headings, paragraphs, code fences, etc.). Once a block is closed, it never changes, but for a link or image in it whose definition comes later, which gets its `href` or `src` then. Your renderer can cache closed blocks and skip re-rendering them, and drops the cached block of a node that gets an attr.
213
213
 
214
214
  2. **Only the last block is active.** During streaming, only the last block in the document is typically open and receiving updates. All prior blocks are closed.
215
215
 
@@ -266,7 +266,9 @@ These appear inside paragraphs, headings, list items, and other inline container
266
266
 
267
267
  A `code_span` whose `extra` is `1` is inline math, written ``$`x`$``. Its content is the LaTeX, without the dollars. A client with no math renderer shows it as it shows any other code span.
268
268
 
269
- A `definition` is a link reference definition, in a block quote or a list item too. Its attrs are the label as written and the `href` and `title` the links that use it carry, `title` only when it is not empty. It renders nothing, the links already hold its url.
269
+ A `definition` is a link reference definition, in a block quote or a list item too. Its attrs are the label as written and the `href` and `title` the links that use it carry, `title` only when it is not empty. It renders nothing, the links hold its url.
270
+
271
+ A link or image written as a reference, `[text][label]` or `[label][]`, is a `link` or `image` by its syntax alone. When its definition came first it has its `href` or `src` before it closes. When the definition comes later, or never, it closes with neither, and the `A` ops for `href` or `src` and `title` arrive with the definition, after the node and usually its block have closed. A client shows such a link as text with no target until then, and renders its block again when the attr arrives.
270
272
 
271
273
  A `footnote_reference` is a leaf with the attr `label` and no content. A `footnote_definition` is a block with the same attr, and its content is the blocks of the note. Nothing on the wire numbers them or moves a definition: a client shows the label where the reference is and the definition where it was written, or numbers the labels by first reference and collects the definitions at the end, as the compile does.
272
274
 
package/dist/buf-utils.js CHANGED
@@ -1,150 +1,183 @@
1
- function S(t, e) {
2
- const n = [];
3
- let o = t.first_child_at(e);
4
- for (; o !== 4294967295; ) {
5
- n.push(o);
6
- const s = t.next_at(o);
7
- if (s === 4294967295 || t.parent_at(s) !== e)
1
+ function P(t, o) {
2
+ const e = [];
3
+ let n = t.first_child_at(o);
4
+ for (; n !== 4294967295; ) {
5
+ e.push(n);
6
+ const s = t.next_at(n);
7
+ if (s === 4294967295 || t.parent_at(s) !== o)
8
8
  break;
9
- o = s;
9
+ n = s;
10
10
  }
11
- return n;
11
+ return e;
12
12
  }
13
- function v(t, e, n) {
14
- const o = t._strings[e];
15
- if (o !== void 0)
16
- return o;
17
- const s = t.value_start_at(e), r = t.value_end_at(e);
18
- return s === 4294967295 || r === 4294967295 || r <= s ? "" : n.slice(s, r);
13
+ function M(t, o, e) {
14
+ const n = t._strings[o];
15
+ if (n !== void 0)
16
+ return n;
17
+ const s = t.value_start_at(o), i = t.value_end_at(o);
18
+ return s === 4294967295 || i === 4294967295 || i <= s ? "" : e.slice(s, i);
19
19
  }
20
- function M(t, e, n) {
21
- const o = v(t, e, n);
22
- if (o)
23
- return o;
24
- let s = "", r = t.first_child_at(e);
25
- for (; r !== 4294967295; ) {
26
- s += M(t, r, n);
27
- const l = t.next_at(r);
28
- if (l === 4294967295 || t.parent_at(l) !== e)
20
+ function x(t, o, e) {
21
+ const n = M(t, o, e);
22
+ if (n)
23
+ return n;
24
+ let s = "", i = t.first_child_at(o);
25
+ for (; i !== 4294967295; ) {
26
+ s += x(t, i, e);
27
+ const r = t.next_at(i);
28
+ if (r === 4294967295 || t.parent_at(r) !== o)
29
29
  break;
30
- r = l;
30
+ i = r;
31
31
  }
32
32
  return s;
33
33
  }
34
- function F(t, e, n) {
35
- const o = R(t, e, n), s = t.first_child_at(e);
36
- return s === 4294967295 ? o : o + k(t, e, s, n);
34
+ function A(t, o, e) {
35
+ const n = F(t, o, e), s = t.first_child_at(o);
36
+ return s === 4294967295 ? n : n + T(t, o, s, e);
37
37
  }
38
- function R(t, e, n) {
39
- const o = t._strings[e];
40
- if (o !== void 0)
41
- return o;
42
- const s = t.value_start_at(e), r = t.value_end_at(e);
43
- return s === 4294967295 || r === 4294967295 || r <= s ? "" : n.slice(s, r);
38
+ function F(t, o, e) {
39
+ const n = t._strings[o];
40
+ if (n !== void 0)
41
+ return n;
42
+ const s = t.value_start_at(o), i = t.value_end_at(o);
43
+ return s === 4294967295 || i === 4294967295 || i <= s ? "" : e.slice(s, i);
44
44
  }
45
- function k(t, e, n, o) {
45
+ function T(t, o, e, n) {
46
46
  let s = "";
47
- for (; n !== 4294967295 && t.parent_at(n) === e; ) {
48
- const r = t.kind_at(n);
49
- if (r === D || r === x || r === y)
47
+ for (; e !== 4294967295 && t.parent_at(e) === o; ) {
48
+ const i = t.kind_at(e);
49
+ if (i === m || i === y || i === D)
50
50
  s += `
51
51
  `;
52
- else if (r === A || r === I || r === C)
53
- s += F(t, n, o);
54
- else if (r !== B) {
55
- const l = t.first_child_at(n);
56
- l !== 4294967295 && (s += k(t, n, l, o));
52
+ else if (i === I || i === C || i === R)
53
+ s += A(t, e, n);
54
+ else if (i !== K) {
55
+ const r = t.first_child_at(e);
56
+ r !== 4294967295 && (s += T(t, e, r, n));
57
57
  }
58
- n = t.next_at(n);
58
+ e = t.next_at(e);
59
59
  }
60
60
  return s;
61
61
  }
62
- const A = 1, C = 5, D = 6, I = 8, x = 17, y = 18, B = 26, K = 36, m = 37;
63
- function H(t) {
62
+ const I = 1, R = 5, m = 6, C = 8, y = 17, D = 18, K = 26, B = 12, w = 13, L = 36, H = 37;
63
+ function X(t, o) {
64
+ const e = [];
65
+ let n = t.first_child_at(0);
66
+ for (; n !== 4294967295; ) {
67
+ const s = t.kind_at(n);
68
+ if ((s === B || s === w) && t.end_at(n) !== 4294967295) {
69
+ const r = t.metadata_at(n), _ = s === w ? "src" : "href";
70
+ if (r == null || r[_] === void 0) {
71
+ const a = t.value_end_at(n), E = t.end_at(n), d = E - a === 3 ? o.slice(t.value_start_at(n), a) : o.slice(a + 2, E - 1);
72
+ e.push({ label: d, node: n, image: s === w });
73
+ }
74
+ }
75
+ const i = t.first_child_at(n);
76
+ if (i !== 4294967295) {
77
+ n = i;
78
+ continue;
79
+ }
80
+ for (; ; ) {
81
+ const r = t.next_at(n), _ = t.parent_at(n);
82
+ if (r !== 4294967295 && t.parent_at(r) === _) {
83
+ n = r;
84
+ break;
85
+ }
86
+ if (_ === 0 || _ === 4294967295) {
87
+ n = 4294967295;
88
+ break;
89
+ }
90
+ n = _;
91
+ }
92
+ }
93
+ return e;
94
+ }
95
+ function S(t) {
64
96
  return "fn-" + t;
65
97
  }
66
- function L(t, e) {
67
- return e === 1 ? "fnref-" + t : "fnref-" + t + "-" + e;
98
+ function G(t, o) {
99
+ return o === 1 ? "fnref-" + t : "fnref-" + t + "-" + o;
68
100
  }
69
- function P(t) {
70
- const e = [];
71
- for (let n = 0; n < t.labels.length; n++) {
72
- t.definitions[n] !== 4294967295 && e.push(H(t.labels[n]));
73
- for (let o = 1; o <= t.counts[n]; o++)
74
- e.push(L(n + 1, o));
101
+ function j(t) {
102
+ const o = [];
103
+ for (let e = 0; e < t.labels.length; e++) {
104
+ t.definitions[e] !== 4294967295 && o.push(S(t.labels[e]));
105
+ for (let n = 1; n <= t.counts[e]; n++)
106
+ o.push(G(e + 1, n));
75
107
  }
76
- return e;
108
+ return o;
77
109
  }
78
- function T(t, e) {
79
- var o;
80
- const n = (o = t.metadata_at(e)) == null ? void 0 : o.label;
81
- return typeof n == "string" ? n : "";
110
+ function v(t, o) {
111
+ var n;
112
+ const e = (n = t.metadata_at(o)) == null ? void 0 : n.label;
113
+ return typeof e == "string" ? e : "";
82
114
  }
83
- function X(t, e = !1) {
84
- const n = /* @__PURE__ */ new Map(), o = [], s = /* @__PURE__ */ new Map(), r = [];
85
- let l = 4294967295, E = !1, f = t.first_child_at(0);
86
- for (; f !== 4294967295; ) {
87
- const c = t.kind_at(f);
88
- let a = t.first_child_at(f);
89
- if (c === K) {
90
- E = !0;
91
- const i = s.get(l);
92
- i === void 0 ? s.set(l, [f]) : i.push(f);
93
- } else if (c === m) {
94
- E = !0;
95
- const i = T(t, f);
96
- n.has(i) ? (e || o.push({ code: "footnote_duplicate", label: i, node: f }), a = 4294967295) : (n.set(i, f), a !== 4294967295 ? (r.push(l), l = f) : e || o.push({ code: "footnote_empty", label: i, node: f }));
115
+ function q(t, o = !1) {
116
+ const e = /* @__PURE__ */ new Map(), n = [], s = /* @__PURE__ */ new Map(), i = [];
117
+ let r = 4294967295, _ = !1, a = t.first_child_at(0);
118
+ for (; a !== 4294967295; ) {
119
+ const f = t.kind_at(a);
120
+ let l = t.first_child_at(a);
121
+ if (f === L) {
122
+ _ = !0;
123
+ const c = s.get(r);
124
+ c === void 0 ? s.set(r, [a]) : c.push(a);
125
+ } else if (f === H) {
126
+ _ = !0;
127
+ const c = v(t, a);
128
+ e.has(c) ? (o || n.push({ code: "footnote_duplicate", label: c, node: a }), l = 4294967295) : (e.set(c, a), l !== 4294967295 ? (i.push(r), r = a) : o || n.push({ code: "footnote_empty", label: c, node: a }));
97
129
  }
98
- if (a !== 4294967295) {
99
- f = a;
130
+ if (l !== 4294967295) {
131
+ a = l;
100
132
  continue;
101
133
  }
102
134
  for (; ; ) {
103
- const i = t.next_at(f), _ = t.parent_at(f);
104
- if (i !== 4294967295 && t.parent_at(i) === _) {
105
- f = i;
135
+ const c = t.next_at(a), N = t.parent_at(a);
136
+ if (c !== 4294967295 && t.parent_at(c) === N) {
137
+ a = c;
106
138
  break;
107
139
  }
108
- if (_ === 0 || _ === 4294967295) {
109
- f = 4294967295;
140
+ if (N === 0 || N === 4294967295) {
141
+ a = 4294967295;
110
142
  break;
111
143
  }
112
- f = _, f === l && (l = r.pop());
144
+ a = N, a === r && (r = i.pop());
113
145
  }
114
146
  }
115
- if (!E)
147
+ if (!_)
116
148
  return null;
117
- const h = [], N = [], d = [], w = /* @__PURE__ */ new Map(), O = /* @__PURE__ */ new Map(), g = (c) => {
118
- if (c !== void 0)
119
- for (let a = 0; a < c.length; a++) {
120
- const i = c[a], _ = T(t, i);
121
- let u = O.get(_);
149
+ const E = [], d = [], O = [], k = /* @__PURE__ */ new Map(), h = /* @__PURE__ */ new Map(), g = (f) => {
150
+ if (f !== void 0)
151
+ for (let l = 0; l < f.length; l++) {
152
+ const c = f[l], N = v(t, c);
153
+ let u = h.get(N);
122
154
  if (u === void 0) {
123
- const p = n.get(_);
124
- if (p === void 0 && !e) {
125
- o.push({ code: "footnote_undefined", label: _, node: i });
155
+ const p = e.get(N);
156
+ if (p === void 0 && !o) {
157
+ n.push({ code: "footnote_undefined", label: N, node: c });
126
158
  continue;
127
159
  }
128
- u = h.push(_), N.push(p === void 0 ? 4294967295 : p), d.push(0), O.set(_, u);
160
+ u = E.push(N), d.push(p === void 0 ? 4294967295 : p), O.push(0), h.set(N, u);
129
161
  }
130
- w.set(i, u * 65536 + ++d[u - 1]);
162
+ k.set(c, u * 65536 + ++O[u - 1]);
131
163
  }
132
164
  };
133
165
  g(s.get(4294967295));
134
- for (let c = 0; c < N.length; c++)
135
- N[c] !== 4294967295 && g(s.get(N[c]));
136
- if (!e)
137
- for (const [c, a] of n)
138
- O.has(c) || o.push({ code: "footnote_unused", label: c, node: a });
139
- return { labels: h, definitions: N, counts: d, references: w, problems: o };
166
+ for (let f = 0; f < d.length; f++)
167
+ d[f] !== 4294967295 && g(s.get(d[f]));
168
+ if (!o)
169
+ for (const [f, l] of e)
170
+ h.has(f) || n.push({ code: "footnote_unused", label: f, node: l });
171
+ return { labels: E, definitions: d, counts: O, references: k, problems: n };
140
172
  }
141
173
  export {
142
- S as buf_children,
143
- F as buf_code,
144
- v as buf_text,
145
- M as buf_text_content,
146
- H as footnote_id,
147
- P as footnote_ids,
148
- X as footnote_index,
149
- L as footnote_ref_id
174
+ P as buf_children,
175
+ A as buf_code,
176
+ M as buf_text,
177
+ x as buf_text_content,
178
+ S as footnote_id,
179
+ j as footnote_ids,
180
+ q as footnote_index,
181
+ G as footnote_ref_id,
182
+ X as undefined_references
150
183
  };
@@ -24,6 +24,19 @@ export declare function buf_code(buf: NodeBuffer, idx: number, source: SourceSli
24
24
  export interface SourceSlice {
25
25
  slice(start: number, end: number): string;
26
26
  }
27
+ /** a link or image reference whose label has no definition */
28
+ export interface UndefinedReference {
29
+ /** the label as written, for `[label][]` the text between the brackets */
30
+ label: string;
31
+ node: number;
32
+ image: boolean;
33
+ }
34
+ /**
35
+ * the references of a document that no definition resolved, in its order, a
36
+ * reference is one by its syntax alone and the parser gives it no url, a link
37
+ * or image a plugin made has no end and is not one
38
+ */
39
+ export declare function undefined_references(buf: NodeBuffer, source: string): UndefinedReference[];
27
40
  /** what a document gets wrong about its footnotes, node is the reference or the definition */
28
41
  export interface FootnoteProblem {
29
42
  /**
@@ -43,6 +43,8 @@ export declare class HeadingIds {
43
43
  private given;
44
44
  /** slots below this were looked at for ids already fixed */
45
45
  private scanned;
46
+ /** generation at last scan, skips walking the buffer when neither html headings nor footnotes entered */
47
+ private scan_gen;
46
48
  /** the heading closed last, a second close of it only fixes an id a plugin gave it since */
47
49
  private last;
48
50
  /** the id made for last, null for none */
package/dist/main.d.ts CHANGED
@@ -5,6 +5,7 @@ import { NodeBuffer, ErrorCollector } from './utils.js';
5
5
  export type { ParseOptions, ParseResult, SyntaxOptions } from './types.js';
6
6
  export type { ParsePlugin, PluginContext, NodeHandler, DirectiveContext, DirectiveEntry, DirectiveHandler, DirectiveHandlers, DirectiveTable, DataChange, DocumentContext, Json, NamedDirectiveHandlers, PluginData, PluginDocument, PluginWarning, TaskContext, } from './plugin_types.js';
7
7
  export { PluginCache, PluginDataError } from './plugin_data.js';
8
+ export { reader_text } from './reader_text.js';
8
9
  export { PluginAsyncError, PluginTaskError } from './plugin_tasks.js';
9
10
  export type { NodeView, WrapperView, Point, Position } from './node_view.js';
10
11
  export { NodeKind, NodeBuffer, take_src } from './utils.js';
@@ -159,6 +160,7 @@ export declare class PFMParser {
159
160
  private frontmatter_failed;
160
161
  private imports_allowed;
161
162
  private ref_map;
163
+ private ref_waiting;
162
164
  private link_text_start;
163
165
  private directive_colon_counts;
164
166
  private directive_text_ids;
@@ -485,7 +487,7 @@ export declare class PFMParser {
485
487
  private lead_columns;
486
488
  /**
487
489
  * true once enough input follows the linefeed at `pos` for a block level decision,
488
- * a first char that may start a block waits for its whole line, in a block quote
490
+ * a first char that may start a block waits until the line can no longer be that block, in a block quote
489
491
  * the line is read after its markers and an unmarked line decides at once
490
492
  */
491
493
  private can_decide_after_lf;
@@ -802,6 +804,8 @@ export declare class PFMParser {
802
804
  * node under parent.
803
805
  */
804
806
  private try_parse_link_ref_definition;
807
+ /** the references that were read before this definition get its url and title */
808
+ private fill_references;
805
809
  /**
806
810
  * parse a title string starting at pos. handles "...", '...', (...)
807
811
  * including multi-line titles (but not across blank lines).
@@ -872,6 +876,8 @@ export declare class PFMParser {
872
876
  private dedent_line;
873
877
  /** sets the code from from to to without the fence indent as its text, when a line had some to take off */
874
878
  private fence_code;
879
+ /** opens the paragraph of a line that starts with a code span, right under a list item tight or loose decides its wrapper when the list closes */
880
+ private span_line_para;
875
881
  /** finished parse, one or two backticks starting a block open a paragraph and take the inline code span step */
876
882
  private span_para;
877
883
  /** incremental fence opener once the info line and the char after it are visible, emits what the start and info states would */
@@ -908,6 +914,12 @@ export declare class PFMParser {
908
914
  private _run_superscript;
909
915
  private _run_subscript;
910
916
  private _run_link_text;
917
+ /**
918
+ * closes the link or image whose text ends at the cursor as a reference, it is one
919
+ * by its syntax alone, a label with no definition yet waits for one
920
+ * @returns false when the label can name no definition, the caller revokes
921
+ */
922
+ private close_reference;
911
923
  /**
912
924
  * true when the paragraph ends at this linefeed, in a block quote a continuation line
913
925
  * is taken instead, a heading ends at every linefeed