@mdsvex/parse 1.0.0-next.3 → 1.0.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 +4 -2
- package/WIRE_FORMAT.md +23 -8
- package/dist/cursor.d.ts +18 -12
- package/dist/cursor.js +5 -5
- package/dist/main.js +1 -1
- package/dist/opcodes.d.ts +9 -5
- package/dist/types.d.ts +5 -2
- package/package.json +1 -1
package/PLUGINS.md
CHANGED
|
@@ -75,7 +75,8 @@ heading: {
|
|
|
75
75
|
}
|
|
76
76
|
```
|
|
77
77
|
|
|
78
|
-
- **`offset`**
|
|
78
|
+
- **`offset`** is a JavaScript string index, in UTF-16 code units, not a byte offset. In `é🐧\n\n# Hi` the heading starts at 5, where its UTF-8 byte offset would be 8.
|
|
79
|
+
- **`offset` indexes the source the parser read,** in which every `\r\n` and every lone `\r` is a `\n`. A lone `\r` moves no offset, but each `\r\n` moves those after it back by one, so a document written with `\r\n` has other offsets on disk. `raw_offsets(raw)` from `@mdsvex/parse` maps them back: `null` when `raw` has no `\r\n`, else an object whose `to_raw(offset)` is the index in `raw`.
|
|
79
80
|
- **`line`** counts from 1 and **`column`** from 0. Both hold in the document as it was written, `\r\n` or not.
|
|
80
81
|
- **`end` is `undefined` while the node is open**, which is the case in its own open handler. The close callback has it.
|
|
81
82
|
- **`position` is `undefined` for a node a plugin made**, which has no source, **and over the wire**. The wire format carries text and no offsets, so a tree built by `WireTreeBuilder` cannot say where a node was.
|
|
@@ -528,7 +529,7 @@ const { nodes, warnings, data } = parse_markdown_svelte(source, {
|
|
|
528
529
|
});
|
|
529
530
|
```
|
|
530
531
|
|
|
531
|
-
- **`frontmatter` is the parser of the frontmatter,** not its value. The parser does not read YAML, so without the option `ctx.frontmatter` is `undefined`.
|
|
532
|
+
- **`frontmatter` is the parser of the frontmatter,** not its value. The parser does not read YAML, so without the option `ctx.frontmatter` is `undefined`. Its second argument is `{ filename }`, the `filename` option, `undefined` without one.
|
|
532
533
|
- **`warnings` and `data` are `undefined`** when no handler left any, and always without plugins.
|
|
533
534
|
|
|
534
535
|
## Handler Lifecycle
|
|
@@ -574,6 +575,7 @@ code_fence: {
|
|
|
574
575
|
- **It is the whole line after the fence**, the language and what follows it. The parser does not split it. In `mdsvex`, `fence_meta(node.info)` from `mdsvex/compile` reads it the way a compile does: the language, a `[name]` or `title="..."`, a caption, `src=`, and the `key=value` pairs and flags.
|
|
575
576
|
- **While the document streams**, the handlers run once the line of the opening fence is complete. Over the wire a fence with an empty info string is sent none, and its handlers run when its first text or its close arrives.
|
|
576
577
|
- **Any other node has no `info`**, it reads `undefined`.
|
|
578
|
+
- **A plugin that renders a language itself** says so in `mdsvex` with `fences: ['mermaid']` on its bundle, and the highlighter never sees those fences, so they render plain and warn of no unknown language. Its handlers, and those of every other plugin, still see the fence. See the design in `design/plugin-directives.md`, 3.6.
|
|
577
579
|
|
|
578
580
|
### The code of a fence a plugin makes
|
|
579
581
|
|
package/WIRE_FORMAT.md
CHANGED
|
@@ -107,7 +107,7 @@ Appends text to a node's content. Multiple `T` opcodes for the same node should
|
|
|
107
107
|
|
|
108
108
|
The flag says the author escaped the first character of `content` with a backslash. The backslash is not sent. A client that only shows the text ignores the flag. One that rewrites text, as `WireTreeBuilder` does for the text handler of a parse plugin, keeps where those characters are and leaves them alone.
|
|
109
109
|
|
|
110
|
-
Text is always resolved to strings on the wire, the client never sees
|
|
110
|
+
Text is always resolved to strings on the wire, the client never sees source offsets or needs the source markdown.
|
|
111
111
|
|
|
112
112
|
### A, Attr
|
|
113
113
|
|
|
@@ -502,10 +502,10 @@ The parser runs server-side (or in a worker) and produces batches via the `WireE
|
|
|
502
502
|
Parse a complete markdown string in one call:
|
|
503
503
|
|
|
504
504
|
```javascript
|
|
505
|
-
import { PFMParser, WireEmitter } from '@mdsvex/parse';
|
|
505
|
+
import { PFMParser, WireEmitter, normalize_newlines } from '@mdsvex/parse';
|
|
506
506
|
|
|
507
507
|
const emitter = new WireEmitter();
|
|
508
|
-
emitter.set_source(source);
|
|
508
|
+
emitter.set_source(normalize_newlines(source));
|
|
509
509
|
|
|
510
510
|
const parser = new PFMParser(emitter);
|
|
511
511
|
parser.parse(source);
|
|
@@ -519,7 +519,7 @@ const batch = emitter.flush(); // single batch with all opcodes
|
|
|
519
519
|
Feed chunks as they arrive (e.g., from an LLM stream, file read, or network):
|
|
520
520
|
|
|
521
521
|
```javascript
|
|
522
|
-
import { PFMParser, WireEmitter } from '@mdsvex/parse';
|
|
522
|
+
import { PFMParser, WireEmitter, normalize_newlines } from '@mdsvex/parse';
|
|
523
523
|
|
|
524
524
|
const emitter = new WireEmitter();
|
|
525
525
|
const parser = new PFMParser(emitter);
|
|
@@ -529,7 +529,7 @@ let accumulated = '';
|
|
|
529
529
|
|
|
530
530
|
function onChunk(chunk) {
|
|
531
531
|
accumulated += chunk;
|
|
532
|
-
emitter.set_source(accumulated); //
|
|
532
|
+
emitter.set_source(normalize_newlines(accumulated)); // the full source so far
|
|
533
533
|
parser.feed(chunk); // only the new chunk
|
|
534
534
|
const batch = emitter.flush(); // opcodes produced by this chunk
|
|
535
535
|
if (batch.length > 0) {
|
|
@@ -538,7 +538,7 @@ function onChunk(chunk) {
|
|
|
538
538
|
}
|
|
539
539
|
|
|
540
540
|
function onEnd() {
|
|
541
|
-
emitter.set_source(accumulated);
|
|
541
|
+
emitter.set_source(normalize_newlines(accumulated));
|
|
542
542
|
parser.finish(); // finalize all open nodes
|
|
543
543
|
const batch = emitter.flush();
|
|
544
544
|
if (batch.length > 0) {
|
|
@@ -563,11 +563,26 @@ function onEnd() {
|
|
|
563
563
|
|
|
564
564
|
| Method | Description |
|
|
565
565
|
| -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
566
|
-
| `set_source(source)` | Set the **full accumulated source** string. Must be called before each `feed()` and before `finish()`. The emitter uses this to resolve
|
|
566
|
+
| `set_source(source)` | Set the **full accumulated source** string, with its line endings normalized as the parser normalizes them (`normalize_newlines`). Must be called before each `feed()` and before `finish()`. The emitter uses this to resolve source offsets to text content. |
|
|
567
567
|
| `flush()` | Return all accumulated opcodes as a batch (array of tuples) and clear the internal buffer. The first flush includes the `S` (schema) opcode. |
|
|
568
568
|
| `flush_json()` | Convenience, calls `flush()` and returns `JSON.stringify(result)`. |
|
|
569
569
|
| `reset()` | Reset all state for reuse with a new parse. |
|
|
570
570
|
|
|
571
|
+
#### Source offsets
|
|
572
|
+
|
|
573
|
+
The `start` and `end` an `Emitter` gets are JavaScript string indices, UTF-16 code units, not UTF-8 bytes. In `é🐧\n\n# Hi` the heading starts at 5: `é` is one code unit and `🐧` two. Its UTF-8 byte offset would be 8.
|
|
574
|
+
|
|
575
|
+
They index the source after line endings are normalized. `parse()` and `feed()` turn every `\r\n` and every lone `\r` into `\n` before the parser sees them, and `feed()` holds back a chunk's trailing `\r` until the next chunk shows whether a `\n` follows. `normalize_newlines(source)` makes the same string, and it is what `set_source()` needs, or the text the emitter slices drifts by one character for each `\r\n` before it.
|
|
576
|
+
|
|
577
|
+
A lone `\r` becomes one `\n`, so it moves no offset. A `\r\n` becomes one character, so each one moves every offset after it back by one. `raw_offsets(raw)` maps them back: it returns `null` for a source with no `\r\n`, else an object whose `to_raw(offset)` gives the index in the source as written.
|
|
578
|
+
|
|
579
|
+
```javascript
|
|
580
|
+
import { raw_offsets } from '@mdsvex/parse';
|
|
581
|
+
|
|
582
|
+
const map = raw_offsets(raw);
|
|
583
|
+
const at = map === null ? offset : map.to_raw(offset);
|
|
584
|
+
```
|
|
585
|
+
|
|
571
586
|
### How stalling works
|
|
572
587
|
|
|
573
588
|
The parser is a streaming state machine. When fed a chunk, it advances the cursor through confirmed content and **stalls** when it reaches a point where the next chunk might change the interpretation:
|
|
@@ -727,6 +742,6 @@ source.onmessage = (event) => {
|
|
|
727
742
|
|
|
728
743
|
2. **Text nodes are invisible.** The parser uses internal text nodes (kind=1, "text"), but these are suppressed on the wire. Their content is flattened into `T` opcodes on the parent. The client never sees text nodes.
|
|
729
744
|
|
|
730
|
-
3. **No source access needed.** All
|
|
745
|
+
3. **No source access needed.** All source offsets are resolved to strings by the emitter. The client works purely with text content and structural opcodes.
|
|
731
746
|
|
|
732
747
|
4. **Batch-per-chunk.** Each `feed()` call produces exactly one batch. The transport layer decides how to frame batches. The client applies them in order.
|
package/dist/cursor.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* cursor, zero-allocation tree traversal over NodeBuffer.
|
|
3
3
|
*
|
|
4
4
|
* single reusable object that provides tree-traversal semantics
|
|
5
|
-
* (
|
|
5
|
+
* (goto_first_child / goto_next_sibling / goto_parent) while reading
|
|
6
6
|
* directly from the node words underneath. no per-node
|
|
7
7
|
* objects are created, text is lazily sliced from the source
|
|
8
8
|
* string only when requested.
|
|
@@ -11,16 +11,22 @@
|
|
|
11
11
|
*
|
|
12
12
|
* usage:
|
|
13
13
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
14
|
+
* import { PFMParser } from '@mdsvex/parse';
|
|
15
|
+
* import { TreeBuilder } from '@mdsvex/parse/tree-builder';
|
|
16
|
+
* import { Cursor } from '@mdsvex/parse/cursor';
|
|
17
|
+
*
|
|
18
|
+
* const tree = new TreeBuilder(128);
|
|
19
|
+
* const parser = new PFMParser(tree);
|
|
16
20
|
* parser.parse(source);
|
|
17
|
-
* const cursor = new
|
|
21
|
+
* const cursor = new Cursor(tree.get_buffer(), source);
|
|
18
22
|
*
|
|
19
23
|
* // walk tree
|
|
20
|
-
* if (cursor.
|
|
21
|
-
* do { process(cursor); } while (cursor.
|
|
22
|
-
* cursor.
|
|
24
|
+
* if (cursor.goto_first_child()) {
|
|
25
|
+
* do { process(cursor); } while (cursor.goto_next_sibling());
|
|
26
|
+
* cursor.goto_parent();
|
|
23
27
|
* }
|
|
28
|
+
*
|
|
29
|
+
* offsets are utf-16 code unit indices into the source as parsed
|
|
24
30
|
*/
|
|
25
31
|
import { type NodeBuffer } from './utils.js';
|
|
26
32
|
export declare class Cursor {
|
|
@@ -48,13 +54,13 @@ export declare class Cursor {
|
|
|
48
54
|
get pending(): boolean;
|
|
49
55
|
/** parent kind of the current node, -1 if at root. */
|
|
50
56
|
get parent_kind(): number;
|
|
51
|
-
/**
|
|
57
|
+
/** offset where the current node starts in source */
|
|
52
58
|
get start(): number;
|
|
53
|
-
/**
|
|
59
|
+
/** offset where the current node ends in source */
|
|
54
60
|
get end(): number;
|
|
55
|
-
/**
|
|
61
|
+
/** offset where the value content of the current node starts */
|
|
56
62
|
get value_start(): number;
|
|
57
|
-
/**
|
|
63
|
+
/** offset where the value content of the current node ends */
|
|
58
64
|
get value_end(): number;
|
|
59
65
|
/** get text content for the current node. prebuilt strings
|
|
60
66
|
(from wiretreebuilder) are returned directly; otherwise sliced lazily. */
|
|
@@ -66,7 +72,7 @@ export declare class Cursor {
|
|
|
66
72
|
get prebuilt(): string | undefined;
|
|
67
73
|
/** get metadata for the current node, or undefined if none. */
|
|
68
74
|
meta(): Record<string, unknown> | undefined;
|
|
69
|
-
/** slice the source
|
|
75
|
+
/** slice the source by offsets, for metadata offset pairs */
|
|
70
76
|
slice(start: number, end: number): string;
|
|
71
77
|
goto_first_child(): boolean;
|
|
72
78
|
goto_next_sibling(): boolean;
|
package/dist/cursor.js
CHANGED
|
@@ -46,28 +46,28 @@ class r {
|
|
|
46
46
|
/* NodeField.stride */
|
|
47
47
|
] & 255;
|
|
48
48
|
}
|
|
49
|
-
/**
|
|
49
|
+
/** offset where the current node starts in source */
|
|
50
50
|
get start() {
|
|
51
51
|
return this.n[
|
|
52
52
|
this.b + 1
|
|
53
53
|
/* NodeField.start */
|
|
54
54
|
];
|
|
55
55
|
}
|
|
56
|
-
/**
|
|
56
|
+
/** offset where the current node ends in source */
|
|
57
57
|
get end() {
|
|
58
58
|
return this.n[
|
|
59
59
|
this.b + 2
|
|
60
60
|
/* NodeField.end */
|
|
61
61
|
];
|
|
62
62
|
}
|
|
63
|
-
/**
|
|
63
|
+
/** offset where the value content of the current node starts */
|
|
64
64
|
get value_start() {
|
|
65
65
|
return this.n[
|
|
66
66
|
this.b + 3
|
|
67
67
|
/* NodeField.value_start */
|
|
68
68
|
];
|
|
69
69
|
}
|
|
70
|
-
/**
|
|
70
|
+
/** offset where the value content of the current node ends */
|
|
71
71
|
get value_end() {
|
|
72
72
|
return this.n[
|
|
73
73
|
this.b + 4
|
|
@@ -108,7 +108,7 @@ class r {
|
|
|
108
108
|
];
|
|
109
109
|
return t === 0 ? void 0 : this.buf._meta[t - 1];
|
|
110
110
|
}
|
|
111
|
-
/** slice the source
|
|
111
|
+
/** slice the source by offsets, for metadata offset pairs */
|
|
112
112
|
slice(t, s) {
|
|
113
113
|
return this.src.slice(t, s);
|
|
114
114
|
}
|
package/dist/main.js
CHANGED
|
@@ -10550,7 +10550,7 @@ function mn(f, t, s) {
|
|
|
10550
10550
|
return r;
|
|
10551
10551
|
const h = t.get_buffer(), l = h.first_child_at(0);
|
|
10552
10552
|
if (!(l === 4294967295 || h.kind_at(l) !== 33 || h.end_at(l) === 4294967295))
|
|
10553
|
-
return n = !0, r = i(s.slice(h.value_start_at(l), h.value_end_at(l)));
|
|
10553
|
+
return n = !0, r = i(s.slice(h.value_start_at(l), h.value_end_at(l)), { filename: e });
|
|
10554
10554
|
}
|
|
10555
10555
|
};
|
|
10556
10556
|
}
|
package/dist/opcodes.d.ts
CHANGED
|
@@ -7,6 +7,10 @@ export interface IdSource {
|
|
|
7
7
|
/**
|
|
8
8
|
* emitter interface for pfm parser opcodes.
|
|
9
9
|
*
|
|
10
|
+
* offsets are utf-16 code unit indices, not utf-8 bytes, into the source after
|
|
11
|
+
* normalize_newlines, so a crlf source is shorter than the file and RawOffsets
|
|
12
|
+
* maps them back to it
|
|
13
|
+
*
|
|
10
14
|
* the parser calls these methods as it processes input. implementations
|
|
11
15
|
* decide what to do with each opcode:
|
|
12
16
|
* - treebuilder: reconstructs a NodeBuffer (backward compat)
|
|
@@ -20,7 +24,7 @@ export interface Emitter {
|
|
|
20
24
|
* node takes the next id, so a builder can use ids as slot indices
|
|
21
25
|
* @param id monotonic node id (unique within this parse).
|
|
22
26
|
* @param kind node kind (heading, paragraph, emphasis, etc.).
|
|
23
|
-
* @param start source
|
|
27
|
+
* @param start source offset where this node starts
|
|
24
28
|
* @param parent id of the parent node (-1 for root's parent).
|
|
25
29
|
* @param extra kind-specific extra value (e.g. heading depth).
|
|
26
30
|
* @param pending true if this node is speculative (may be revoked).
|
|
@@ -30,7 +34,7 @@ export interface Emitter {
|
|
|
30
34
|
* close a previously opened node, a pending node is final from here on
|
|
31
35
|
* unless it is a paragraph, which a commit or a revoke settles later
|
|
32
36
|
* @param id id of the node to close.
|
|
33
|
-
* @param end source
|
|
37
|
+
* @param end source offset where this node ends
|
|
34
38
|
* @param kind kind at open, a revoke may have rewritten it since, omitting
|
|
35
39
|
* it makes the builder track revokes
|
|
36
40
|
*/
|
|
@@ -48,8 +52,8 @@ export interface Emitter {
|
|
|
48
52
|
* headings, code fences, code spans and html comments get a value range and
|
|
49
53
|
* take no id
|
|
50
54
|
* @param parent id of the parent node.
|
|
51
|
-
* @param start source
|
|
52
|
-
* @param end source
|
|
55
|
+
* @param start source offset of text start
|
|
56
|
+
* @param end source offset of text end
|
|
53
57
|
* @param parent_kind parent kind at open, as for close
|
|
54
58
|
*/
|
|
55
59
|
text(parent: number, start: number, end: number, parent_kind?: NodeKind): void;
|
|
@@ -112,7 +116,7 @@ export interface Emitter {
|
|
|
112
116
|
* report the parser's current cursor position. called at the end of
|
|
113
117
|
* each feed() and finish(). emitters can use this as the boundary
|
|
114
118
|
* for eager text emission during streaming.
|
|
115
|
-
* @param pos current
|
|
119
|
+
* @param pos current offset in the source
|
|
116
120
|
*/
|
|
117
121
|
cursor(pos: number): void;
|
|
118
122
|
/** the incremental finish emitted the last opcode, reserved storage can go */
|
package/dist/types.d.ts
CHANGED
|
@@ -30,9 +30,12 @@ export interface ParseOptions extends SyntaxOptions {
|
|
|
30
30
|
filename?: string;
|
|
31
31
|
/**
|
|
32
32
|
* parses the frontmatter for plugin handlers, called once with the text
|
|
33
|
-
* between the fences when a handler first reads
|
|
33
|
+
* between the fences and the filename when a handler first reads
|
|
34
|
+
* ctx.frontmatter
|
|
34
35
|
*/
|
|
35
|
-
frontmatter?: (text: string
|
|
36
|
+
frontmatter?: (text: string, file: {
|
|
37
|
+
filename: string | undefined;
|
|
38
|
+
}) => Record<string, unknown> | undefined;
|
|
36
39
|
}
|
|
37
40
|
/** structured output produced by the markdown parser. */
|
|
38
41
|
export interface ParseResult {
|