@markup-carve/carve-wasm 0.1.4 → 0.1.5

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/README.md CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  WebAssembly build of the [Carve](https://markup-carve.github.io/carve/) markup
4
4
  renderer, compiled from
5
- [carve-rs](https://github.com/markup-carve/carve-rs). Lets the Rust
6
- implementation run client-side in the browser and backs the **Rust (WASM)**
7
- engine in the [Carve playground](https://markup-carve.github.io/carve/playground).
5
+ [carve-rs](https://github.com/markup-carve/carve-rs). It runs the Rust
6
+ implementation in browsers and powers the **Rust (WASM)** engine in the
7
+ [Carve playground](https://markup-carve.github.io/carve/playground).
8
8
 
9
9
  ## Install
10
10
 
@@ -12,716 +12,95 @@ engine in the [Carve playground](https://markup-carve.github.io/carve/playground
12
12
  npm install @markup-carve/carve-wasm
13
13
  ```
14
14
 
15
- > Publishing to npm is pending. Until the package is live you can build locally
16
- > (see [Build](#build) below) or consume the `pkg/` output directly.
15
+ > Publishing to npm is pending. Until the package is live, build it locally
16
+ > using the [build guide](docs/building.md) or consume the generated `pkg/`
17
+ > directory directly.
17
18
 
18
- ## Usage
19
+ ## Render Carve
19
20
 
20
- ### HTML migration
21
-
22
- `htmlToCarve(html, mode)` returns `{ value, report }`, using the same HTML5
23
- import policy and canonical writer as carve-rs. Modes are `safe` (default),
24
- `semantic`, and trusted-only `roundtrip`.
25
-
26
- ```js
27
- const { value, report } = htmlToCarve('<p>Hello <strong>world</strong></p>', 'safe')
28
- ```
29
-
30
- `htmlToAst(html, mode)` is the same importer returning the TREE instead of the
31
- source, so a host that wants a tree does not re-parse what the importer just
32
- wrote. It returns the same `{ value, report }` shape, with `value` holding AST
33
- JSON.
34
-
35
- ```js
36
- const { value, report } = htmlToAst('<p>Hello <b>world</b></p>', 'safe')
37
- astJsonToHtml(value)
38
- ```
39
-
40
- > **The report is not always identical, and the difference is the point.**
41
- > A loss only a WRITER takes is not reported by `htmlToAst`, because no writer
42
- > ran (PART 12 §16). A `<figure>` wrapping a table, or a table with an explicit
43
- > head/body/foot grouping, has no Carve spelling: `htmlToCarve` reports
44
- > `structure-unspellable` for it and `htmlToAst` does not, because the tree
45
- > keeps the thing the source would have lost.
46
-
47
- Portable migration code can use `fromHtml(html, mode)` and
48
- `fromMarkdown(markdown)`. Both return `{ value, report }`. Version 2 reports use
49
- the shared fidelity vocabulary. Markdown, Djot, and BBCode conservatively emit
50
- `fidelity-unverified` as `dropped` / `fallback` until their importers expose
51
- construct-level outcomes; an empty diagnostic list is therefore never used to
52
- imply fidelity that was not assessed.
53
-
54
- ### Core renderer
55
-
56
- Renders Carve markup to HTML with no extensions enabled.
57
-
58
- The published package is the **bundler** target (webpack, Vite, Rollup, ...):
59
- the wasm initializes automatically, so the exports are synchronous - no `init()`
60
- call.
61
-
62
- ```js
63
- import { toHtml } from '@markup-carve/carve-wasm'
64
-
65
- // The other core targets are toMarkdown, toPlainText, toAnsi, and toCarve,
66
- // each with a *WithOptions form that takes the options object below.
67
-
68
- const html = toHtml('# Hello, Carve!')
69
- document.body.innerHTML = html
70
- ```
71
-
72
- ### Source-preserving patches
73
-
74
- Tools can prepare canonical formatting as stale-safe UTF-8 byte edits:
75
-
76
- ```js
77
- const patch = toCarvePatch(source)
78
- const formatted = applySourcePatch(source, patch)
79
- ```
80
-
81
- `createSourcePatch(source, replacement, kind, code)` prepares the same wire
82
- shape for another complete replacement. Its ranges are UTF-8 byte offsets. The
83
- fingerprint and byte length catch accidental staleness; they are not a
84
- cryptographic signature. Treat a patch as trusted edit instructions.
85
-
86
- ### Extensions
87
-
88
- `extensions()` reports every extension this build accepts. The list comes from
89
- the engine, so it cannot fall behind what the engine has:
90
-
91
- ```js
92
- import { extensions, toHtmlWithOptions } from '@markup-carve/carve-wasm'
93
-
94
- extensions()
95
- // ['autolink', 'citations', 'code-callouts', 'code-group', ...]
96
-
97
- toHtmlWithOptions(src, { extensions: ['glossary', 'table-of-contents'] })
98
- ```
99
-
100
- Names are kebab-case; snake_case (`math_block`) is accepted too. An unknown name
101
- throws, because an ignored extension renders as missing behavior that looks
102
- like a Carve bug.
103
-
104
- ### Full renderer (preview set)
105
-
106
- `toHtmlFull` enables the preview set the playground uses: tab normalisation,
107
- `<details>` fences, Mermaid diagrams, wikilinks, autolink, list-table, math
108
- blocks, heading permalinks, citations, code callouts, external-link decoration,
109
- code groups, and tabs.
110
-
111
- It is a curated subset rather than everything registered. Extensions that
112
- rewrite a document that never asked - `heading-numbers` numbers every heading,
113
- `table-of-contents` injects a TOC - are wrong for a preview. Name them
114
- explicitly through `toHtmlWithOptions` when you want them.
115
-
116
- ```js
117
- import { toHtmlFull } from '@markup-carve/carve-wasm'
118
-
119
- const html = toHtmlFull('# Hello\n\n``` mermaid\ngraph TD; A-->B\n```\n')
120
- document.body.innerHTML = html
121
- ```
122
-
123
- ### Symbols
124
-
125
- A `:name:` symbol renders its literal `:name:` source unless the name is in the
126
- **symbols map**. Pass one as a plain object (or a `Map`) to `toHtmlWithSymbols`,
127
- or as the optional second argument of `toHtmlFull`:
128
-
129
- ```js
130
- import { toHtmlWithSymbols } from '@markup-carve/carve-wasm'
131
-
132
- toHtmlWithSymbols('Ship it :rocket:', { rocket: '🚀' })
133
- // => '<p>Ship it 🚀</p>'
134
-
135
- toHtmlWithSymbols('Ship it :rocket: :shrug:', { rocket: '🚀' })
136
- // => '<p>Ship it 🚀 :shrug:</p>' (an unmapped name stays literal)
137
- ```
138
-
139
- The word-boundary guard is unaffected by an active map: `a:b:c`, `10:30:` and
140
- `me@example.com` never become symbols. Names and values must both be strings; a
141
- non-string value throws a `TypeError`.
142
-
143
- > **Security: symbol values are TRUSTED RAW output.**
144
- > A mapped value is inserted into the output **unescaped** - the same trust
145
- > class as a static `renderers` callback. `{ b: '<b>x</b>' }` emits a real
146
- > `<b>` element, not escaped text. This is deliberate (processor configuration
147
- > is trusted). **Never build a symbols map out of untrusted / user-supplied
148
- > input.**
149
-
150
- ### Section wrappers
151
-
152
- A top-level heading is wrapped, along with the content following it up to the
153
- next same-or-shallower heading, in a `<section>` carrying the heading's id (spec
154
- PART 9 §13). Only the id moves - `{#install .featured}` gives
155
- `<section id="install"><h2 class="featured">` - and a heading inside a
156
- blockquote, div or list item is not wrapped at all.
157
-
158
- `toHtmlWithOptions` is the general entry point, and `sections: false` renders
159
- headings flat with the id back on the `<h*>`:
160
-
161
- ```js
162
- import { toHtmlWithOptions } from '@markup-carve/carve-wasm'
163
-
164
- toHtmlWithOptions('# A\n\np\n', { sections: false })
165
- // '<h1 id="A">A</h1>\n<p>p</p>'
166
-
167
- toHtmlWithOptions(src, { sections: false, symbols: { rocket: '🚀' }, full: true })
168
- ```
169
-
170
- Every field is optional. Omitting the object, or passing `null`, renders with
171
- defaults, so the three shorthands above remain the zero-config forms.
172
-
173
- | Field | Default | What it does |
174
- |---|---|---|
175
- | `sections` | `true` | Wrap each top-level heading and its content in a `<section>` |
176
- | `symbols` | none | `:name:` to value, TRUSTED-RAW (see above) |
177
- | `extensions` | none | Array of registry names; takes precedence over `full` |
178
- | `full` | `false` | Enable the preview extension set |
179
- | `rawHtml` | `true` | Emit an explicit passthrough as markup; `false` escapes it |
180
- | `profile` | none | `full` / `article` / `comment` / `minimal`; rejection throws |
181
- | `profileBaseHost` | none | The host the profile's link policy counts as internal |
182
- | `mode` | `interactive` | `static` renders the self-contained form: no client scripts |
183
- | `sourceLine` | `false` | Stamp top-level blocks with `data-source-line` |
184
- | `positions` | `false` | Keep source offsets on the nodes |
185
- | `labels` | none | Override engine-written strings, for a page not in English |
186
- | `smartTypography` | `glyph` | `source` keeps the author's run instead of the glyph |
187
- | `lowercaseHeadingIds` | `false` | Lowercase the generated heading ids |
188
- | `asciiHeadingIds` | `off` | `fold` transliterates, `strict` guarantees ASCII |
189
- | `mentionUrl` | none | URL template for `@mention`; `{name}` / `{user}` take the encoded name |
190
- | `tagUrl` | none | URL template for `#tag`; `{name}` takes the encoded name |
191
-
192
- An unrecognized key is ignored, because the object is configuration and a typo
193
- should not break a render. A recognized key with the wrong type throws a
194
- `TypeError` instead of being coerced: JS truthiness would read
195
- `{ sections: 'false' }` as `true`, the opposite of what was written.
196
-
197
- `renderers` is the one recognized key `toHtmlWithOptions` refuses; it belongs to
198
- [`toHtmlWithRenderers`](#static-renderers).
199
-
200
- This exists for a host whose CSS or JS assumes rendered blocks are direct
201
- children of the content container - the `.stack > * + *` spacing idiom,
202
- `:first-child`, `nth-child()` counting, `element.children` walks - all of which
203
- stop matching once a wrapper sits in between. It is the one output change that
204
- breaks a document whose *source* migrated cleanly.
205
-
206
- Nothing else changes: ids, collision dedup, `</#id>` cross-references, implicit
207
- `[Heading][]` references and heading numbering all resolve against the slug
208
- rather than the element carrying it. The endnotes
209
- `<section role="doc-endnotes">` is a separate construct and is still emitted.
210
-
211
- ### Rendering a document you did not write
212
-
213
- `rawHtml: false` renders an explicit passthrough - the `=html` raw block and the
214
- `` `…`{=html} `` inline raw span - as escaped text instead of markup. It is the
215
- switch carve-js spells `allowRawHtml`.
216
-
217
- ```js
218
- toHtmlWithOptions(fromTheReader, { rawHtml: false })
219
- ```
220
-
221
- Reach for it whenever the document comes from somewhere other than the person
222
- running the page: a shared link, a comment field, a pasted file. A passthrough is
223
- the one construct that puts author-controlled markup on your origin, so leaving
224
- it on for a document a reader supplied is a way to run their script.
225
-
226
- The symbols map is unaffected and stays TRUSTED-RAW either way - it is
227
- configuration the host wrote, not content the document carries.
228
-
229
- ### Rendering with a profile
230
-
231
- `rawHtml: false` closes the passthrough vector. It does not cap input length,
232
- deny a construct, or constrain link schemes - that is what a profile is for, and
233
- the four presets match the ones the spec and the sibling bindings describe.
234
-
235
- ```js
236
- toHtmlWithOptions(fromTheReader, { profile: 'comment', rawHtml: false })
237
- ```
238
-
239
- **A rejected document throws.** The engine's infallible entry point turns a
240
- profile rejection - input past `max_length`, or a denied construct when the
241
- action is Error - into an EMPTY STRING, and a caller cannot tell that from a
242
- document that legitimately rendered to nothing. This binding renders through the
243
- fallible one, so a rejection arrives as an `Error` whose `name` is
244
- `ProfileViolationError` and whose `violations` array carries one message per
245
- refused construct.
246
-
247
- ```js
248
- try {
249
- html = toHtmlWithOptions(fromTheReader, { profile: 'comment' })
250
- } catch (error) {
251
- if (error.name === 'ProfileViolationError') report(error.violations)
252
- }
253
- ```
254
-
255
- **Every target takes the profile, not only HTML.** `toMarkdownWithOptions`,
256
- `toPlainTextWithOptions`, `toAnsiWithOptions`, `toCarveWithOptions` and
257
- `parseJsonWithOptions` read the same options object, so a document held to a
258
- profile on its way to HTML is held to it on its way to Markdown or into a stored
259
- tree. Without them a host could export the same untrusted document unfiltered.
21
+ The published package uses the bundler target for webpack, Vite, Rollup, and
22
+ similar tools. WASM initialization is automatic, so its exports are
23
+ synchronous.
260
24
 
261
25
  ```js
262
- toMarkdownWithOptions(fromTheReader, { profile: 'comment' })
263
- ```
264
-
265
- What those targets actually read is narrower than HTML's list: `profile` and
266
- `smartTypography` change their output, extensions run, and `symbols`, `labels`,
267
- `sections`, `sourceLine`, `mode` and the heading-id switches are HTML-side
268
- concerns the engine's other renderers do not consult. `renderers` is refused
269
- there as it is on `toHtmlWithOptions`.
270
-
271
- `toCarveWithOptions` is narrower again and reads `profile` alone. The canonical
272
- writer is parse-only by contract, so extensions and `smartTypography` are inert
273
- there; they are accepted rather than refused so that one options object can be
274
- handed to every target.
275
-
276
- `applyProfile` runs the same filter over an AST-JSON document and hands back the
277
- filtered tree instead of HTML, for a host that wants to store, diff or re-render
278
- what the filter left. `violations` reports what it degraded or stripped, which
279
- the HTML path discards.
280
-
281
- ```js
282
- const { json, violations } = applyProfile(parseJson(fromTheReader), 'comment')
283
- if (violations.length > 0) tell(violations.map((v) => v.message))
284
- const html = astJsonToHtml(json)
285
- ```
286
-
287
- It reads `profileBaseHost` and `smartTypography` from its options object and
288
- nothing else. It does **not** enforce the profile's `max_length`, which the
289
- engine applies to the SOURCE bytes before a parse - a host filtering untrusted
290
- input still needs that bound on the way in.
26
+ import { toHtml, toHtmlFull, toHtmlWithOptions } from '@markup-carve/carve-wasm'
291
27
 
292
- ### Static renderers
293
-
294
- `mode: 'static'` renders the self-contained form, which carries no client
295
- scripts - so a formula in the document has to be typeset while the HTML is being
296
- written, by the host. `toHtmlWithRenderers(source, options)` takes the same
297
- options object plus a `renderers.math` callback and returns
298
- `{ html, rendererErrors }`.
299
-
300
- ```js
301
- import katex from 'katex'
302
- import { toHtmlWithRenderers } from '@markup-carve/carve-wasm'
303
-
304
- const { html, rendererErrors } = toHtmlWithRenderers(source, {
305
- mode: 'static',
306
- extensions: ['math-block'],
307
- renderers: {
308
- math: (tex, display) => katex.renderToString(tex, { displayMode: display }),
309
- },
310
- })
311
- ```
312
-
313
- The callback is called once per ` ```math ` fence, with the TeX source and a
314
- display flag. Without it, static output keeps the `\[…\]` source for a client to
315
- typeset later - never blank.
316
-
317
- > **Security: what a renderer returns is TRUSTED RAW HTML.**
318
- > The string is inserted **unescaped**, the same trust class as a `symbols`
319
- > value. There is one difference worth stating: a symbol value is host
320
- > configuration keyed by a **name**, while a renderer is host configuration that
321
- > is **handed document content** and typically echoes some of it back. A host
322
- > rendering documents it did not author is accepting whatever its renderer makes
323
- > of that input, so the escaping is the renderer's job.
324
-
325
- **The callback must be synchronous.** wasm-bindgen cannot await across it, so an
326
- `async` renderer returns a Promise the engine has no way to resolve. That is
327
- reported as a failure rather than stringified into the document.
328
-
329
- `renderers.diagrams` is the same callback one level down, keyed by the fence's
330
- css class:
331
-
332
- ```js
333
- const { html, rendererErrors } = toHtmlWithRenderers(source, {
334
- mode: 'static',
335
- extensions: ['fenced-render', 'fenced-render-graphviz'],
336
- renderers: {
337
- diagrams: {
338
- mermaid: (source) => prerendered.get(source),
339
- graphviz: (source) => dotToSvg(source),
340
- },
341
- },
342
- })
343
- ```
344
-
345
- Mermaid itself cannot be passed here: its `render` returns a Promise from v10
346
- on. What a browser host can do is render its diagrams beforehand, by whatever
347
- async means it likes, and pass the lookup as the one-line callback above.
348
-
349
- A fence whose class nobody configured degrades to an escaped source block, and
350
- that is reported by nothing - the callback is never called, so the binding
351
- cannot see the miss. A configured key the document never uses is silent for the
352
- same reason. Keys are checked for shape only: an unknown one is accepted,
353
- because validating it would mean keeping the list of fence classes by hand here
354
- until [markup-carve/carve-rs#1670](https://github.com/markup-carve/carve-rs/issues/1670)
355
- puts the class on the registry entry.
356
-
357
- A callback that throws, or returns anything but a string, does not abort the
358
- render. The node it was called for emits nothing and the failure is reported:
359
-
360
- ```js
361
- const { html, rendererErrors } = toHtmlWithRenderers(source, {
362
- mode: 'static',
363
- extensions: ['math-block'],
364
- renderers: { math: () => { throw new Error('bad TeX') } },
365
- })
366
- // rendererErrors: [{ renderer: 'math', display: true, source: 'E = mc^2', message: '…bad TeX' }]
367
- ```
368
-
369
- A diagram failure is an entry in the same array, with `renderer` naming the css
370
- class and no `display` - that flag is the math callback's second argument.
371
-
372
- That is why this is a separate entry point: `toHtmlWithOptions` returns a bare
373
- string with nowhere to report a failing callback, and a silently empty figure is
374
- the degradation `lintCarve` exists to warn about. Every other check is at READ
375
- time, matching the rest of the options object - a non-callable `math`, a
376
- non-object `renderers` and a non-callable `diagrams` value all throw a
377
- `TypeError` before the render starts, so a misconfigured host finds out without
378
- needing a document that happens to contain a formula.
379
-
380
- ### Editing a tree, and reading one back
381
-
382
- `parseJson` serializes a document out. `astJsonToHtml` renders one back, and
383
- `astJsonToCarve` writes one back as source, so a host that reads the tree in
384
- order to change something can display and save the result without a server.
385
-
386
- ```js
387
- const tree = JSON.parse(parseJson(source))
388
- tree.children.unshift({ type: 'heading', level: 1, children: [{ type: 'text', value: 'Added' }] })
389
- const html = astJsonToHtml(JSON.stringify(tree), { full: true })
390
- const carve = astJsonToCarve(JSON.stringify(tree))
391
- ```
392
-
393
- `astJsonToMarkdown`, `astJsonToPlainText` and `astJsonToAnsi` are the same seam
394
- for the other targets. Each takes the options object, and the profile filter and
395
- the `before_render` hooks run as they do on the HTML path. Without them a host
396
- holding a tree reached Markdown through `astJsonToCarve` and `toMarkdown`: a
397
- canonical write, a re-parse and a second render for one answer.
398
-
399
- A tree carrying something no Carve source can spell is refused by
400
- `astJsonToCarve` rather than written approximately, and an invalid tree throws
401
- from either.
402
-
403
- Ordering follows the tree. Section 7 orders collected definitions by source
404
- position, so a tree carrying spans prints its footnote and link definitions in
405
- source order and a tree carrying none prints them in label order.
406
- `toMarkdown` parses with positions on and always gets the first; these entry
407
- points get whatever their producer built.
408
-
409
- `lintCarve` returns the degradation diagnostics as
410
- `{ line, column, rule, message, start, end }`, with the rule ids carve-js and
411
- carve-php use for the same triggers. Offsets are BYTE offsets into the source,
412
- matching the engine.
413
-
414
- ### Include expansion
415
-
416
- `expandIncludes(source, options)` runs the `{{ path }}` pass (spec PART 9 §19)
417
- over a document and hands back the expanded tree as AST JSON, alongside the
418
- warnings and the dependency list a host needs for reporting and file watching.
419
-
420
- ```js
421
- import { expandIncludes, astJsonToHtml } from '@markup-carve/carve-wasm'
422
-
423
- const files = new Map([['child.crv', 'Included body.\n']])
424
-
425
- const { json, warnings, dependencies } = expandIncludes(source, {
426
- resolve: (path, ctx) => files.get(path) ?? null,
28
+ toHtml('# Hello, Carve!')
29
+ toHtmlFull('# Hello\n\n``` mermaid\ngraph TD; A-->B\n```\n')
30
+ toHtmlWithOptions(source, {
31
+ extensions: ['glossary', 'table-of-contents'],
32
+ rawHtml: false,
33
+ sections: true,
427
34
  })
428
- document.body.innerHTML = astJsonToHtml(json)
429
35
  ```
430
36
 
431
- **The resolver has to be synchronous, and that decides who can use this.** A
432
- browser host that would resolve a path over the network cannot: `fetch` returns
433
- a Promise and wasm-bindgen cannot await across the call. A host whose files are
434
- already in memory can - an editor with its open buffers, a bundler, a VFS, a
435
- test harness. An `async` resolver is not silently ignored: the Promise is
436
- reported in `resolverErrors` and the directive stays literal, the same way an
437
- `async` `renderers.math` is handled.
438
-
439
- `resolve` returns the child's source as a string, or `{ source, id }` when it
440
- can name the file canonically. **Supply the id where you have one** - it is what
441
- the cycle guard compares, so two spellings of one file (`b.crv`, `./b.crv`)
442
- defeat it without one and only the depth limit stops the recursion. `null`
443
- refuses the directive as `not-found`, and `{ denial }` refuses it in one of the
444
- classes §19 names: `outside-root`, `not-found`, `no-root`, `include-denied`,
445
- `include-unresolved`. The class reaches the caller on the dependency.
37
+ `toHtml` uses the core renderer. `toHtmlFull` enables the extension set used
38
+ by the playground preview. `toHtmlWithOptions` lets the caller choose
39
+ extensions and rendering policy. Unknown extension names throw instead of
40
+ silently producing incomplete output.
446
41
 
447
- `ctx` carries `{ sourcePath, stack, depth }`. `stack` is the include chain, root
448
- first, and its last entry is the file containing the directive - which is what
449
- relative resolution keys off.
42
+ Other core targets include `toMarkdown`, `toPlainText`, `toAnsi`, and
43
+ `toCarve`. Each has a `*WithOptions` form.
450
44
 
451
- | Field | Default | What it does |
452
- |---|---|---|
453
- | `resolve` | required | `(path, ctx) => source \| { source, id } \| { denial } \| null` |
454
- | `sourcePath` | none | Identity of the root document, for cycle detection and warning attribution |
455
- | `extensions` | none | Registry names to parse each CHILD with; pass the set the parent was parsed with |
456
- | `maxDepth` | `16` | Transitive include depth |
457
- | `maxBytes` | `max(1 MB, 8 x source bytes)` | Expanded-source byte budget |
458
- | `maxResolverCalls` | `1000` | Resolver calls for one expansion |
459
- | `maxWarnings` | `100` | Warnings retained; one per distinct rule always survives |
45
+ ## Convert existing content
460
46
 
461
- The budgets are part of the contract rather than a detail. They bound what a
462
- document of directives can make a host do, and a host serving documents it did
463
- not write is the one that should lower them.
47
+ The package imports HTML, Markdown, Djot, and BBCode. `fromHtml`,
48
+ `fromMarkdown`, `migrateDjot`, and `migrateBbcode` return the converted value
49
+ with a fidelity report. The simpler `fromDjot` and `fromBbcode` helpers return
50
+ only the converted string.
464
51
 
465
- The result is `{ json, warnings, suppressedWarnings, dependencies,
466
- chargedBytes, resolverErrors }`. `dependencies` lists every target touched,
467
- including the ones that did not resolve: a host watching only the files it read
468
- would never learn that a missing target now exists, so the preview would stay
469
- stale at the moment the author fixes it. `chargedBytes` is what the budget was
470
- charged, which is otherwise unobservable from outside.
471
-
472
- The tree carries no positions. Expansion merges nodes from several files, and a
473
- span on a node that came from a child would point into a source the caller
474
- never passed.
475
- ### Re-parsing while someone types
476
-
477
- `parseSnapshot(source)` parses a document and keeps what a later `reparse` needs.
478
- `reparse(source, changes)` applies a batch of edits and parses the result. Both
479
- return one JSON string.
52
+ Markdown, Djot, and BBCode reports currently mark fidelity as unverified. An
53
+ empty diagnostic list does not claim that every construct was preserved.
480
54
 
481
55
  ```js
482
- import { parseSnapshot, reparse } from '@markup-carve/carve-wasm'
483
-
484
- const first = JSON.parse(parseSnapshot('# One\n'))
485
- const next = JSON.parse(reparse(first.source, JSON.stringify([
486
- { range: [2, 5], replacement: 'Two' },
487
- ])))
488
- next.source // '# Two\n'
489
- ```
490
-
491
- Each result is `{ source, document, sourceLayout, changedSource,
492
- reusedPreviousTree }`. `document` is the PART 12 tree, `sourceLayout` is what
493
- `parseSourceLayoutJson` produces, and `changedSource` is the byte ranges this
494
- parse covered - the whole document on a first parse, the edits on a re-parse.
56
+ import { fromHtml, fromMarkdown, htmlToAst, htmlToCarve } from '@markup-carve/carve-wasm'
495
57
 
496
- The snapshot crosses as JSON rather than as a handle. Every other entry point
497
- in this package is a pure function that owns nothing, and the engine's snapshot
498
- holds only the source, so there is no tree kept alive in wasm memory for a
499
- handle to point at.
500
-
501
- > **The offsets are UTF-8 byte offsets.**
502
- > That is what `parseJson` positions and `createSourcePatch` ranges already
503
- > mean. A browser editor counts UTF-16 code units, so a host holding a
504
- > `selectionStart` converts before calling. An offset landing inside a
505
- > multi-byte character is refused rather than guessed at: `é` is two bytes, and
506
- > `[0, 1]` throws.
507
-
508
- A malformed change throws: overlapping ranges, an end past the source, and a
509
- range splitting a code point are all a broken caller contract rather than a
510
- result the caller asked for. An empty change list is not one - it re-parses the
511
- source unchanged, which is what a host batching keystrokes will hit.
512
-
513
- `reusedPreviousTree` is the engine's own answer about whether any of the
514
- previous parse was reused. **The pinned engine always reports `false`**: it
515
- validates and applies the edits and then parses the whole source. Read the flag
516
- rather than assuming work was saved.
517
- ### Tree patches
518
-
519
- `createAstPatch(before, after)` is the difference between two PART 12 trees,
520
- and `applyAstPatch(ast, patch)` replays it. Trees and patches both cross as
521
- JSON strings, the same way `parseJson` and `astJsonToHtml` hand a tree across.
522
-
523
- ```js
524
- import { parseJson, createAstPatch, applyAstPatch } from '@markup-carve/carve-wasm'
525
-
526
- const patch = createAstPatch(parseJson('# One\n'), parseJson('# Two\n'))
527
- applyAstPatch(parseJson('# One\n'), patch) // the '# Two' tree, as JSON
528
- ```
529
-
530
- The operations are the position-independent `{ op, path, value }` wire shape,
531
- so a patch stays meaningful against a tree that moved underneath it. That is
532
- the opposite trade from `createSourcePatch`, which pins byte ranges and
533
- fingerprints them against staleness.
534
-
535
- The engine's `ast_patch_to_json` and `ast_patch_from_json` have no binding of
536
- their own. With the patch crossing as JSON they are exactly this pair's
537
- encoding, and calling them would convert JSON a host already holds.
538
-
539
- `createReversibleAstPatch(before, after)` returns
540
- `{ forward, inverse, beforeFingerprint, afterFingerprint }` as one JSON string,
541
- and `applyReversibleAstPatch(ast, patch, inverse)` replays either direction.
542
-
543
- ```js
544
- const step = createReversibleAstPatch(before, after)
545
- const redone = applyReversibleAstPatch(before, step)
546
- const undone = applyReversibleAstPatch(after, step, true)
547
- ```
548
-
549
- **The undo stack is yours.** Every entry point here is a pure function that
550
- owns nothing, so there is no history kept in this package to undo against, and
551
- a browser host already has somewhere to keep one. What the reversible pair adds
552
- over two `createAstPatch` calls is the precondition: applying a step to a tree
553
- whose fingerprint is not the one it was made against throws, so an undo cannot
554
- land on a document that has moved on.
555
-
556
- ### Three-way merge
557
-
558
- `mergeAst(base, ours, theirs, options)` merges two edits of one document. Two
559
- people editing one document is a browser problem, so the audience is here.
560
-
561
- ```js
562
- import { parseJson, mergeAst } from '@markup-carve/carve-wasm'
563
-
564
- const { ok, ast, conflicts } = JSON.parse(mergeAst(base, ours, theirs))
565
- ```
566
-
567
- **A conflict is a value, not a throw.** It is the result you asked for: `ok` is
568
- false, `ast` is null, and `conflicts` says where and why. A throw is for a
569
- contract you broke, and an unparseable tree still gets one, naming which of the
570
- three it was.
571
-
572
- Each conflict is `{ path, reason, base, ours, theirs }`, with `reason` one of
573
- `both-changed`, `delete-edit` or `concurrent-sequence-edit`. That shape and
574
- those names are carve-js's, so a host merging with either engine reads one
575
- contract. The Rust engine carries no `deleted` flags, so carve-js's optional
576
- `deleted` field is absent here rather than guessed at.
577
-
578
- `options.resolve` answers conflicts while the merge runs:
579
-
580
- ```js
581
- const merged = JSON.parse(mergeAst(base, ours, theirs, {
582
- resolve: (conflict) => (conflict.path.startsWith('/children/0') ? 'ours' : null),
583
- }))
584
- ```
585
-
586
- It returns `'base'`, `'ours'`, `'theirs'`, `{ value }` to replace the field
587
- outright, or `null` to leave that conflict standing.
588
-
589
- > **The resolver must be synchronous.**
590
- > A resolver that asks a server, or asks the user, returns a Promise the merge
591
- > cannot await. That is reported in `resolverErrors` with the conflict's path,
592
- > and the conflict is left unresolved - never swallowed, and never stringified
593
- > into the tree. Returning `null` is the supported way to decline; a Promise is
594
- > a mistake.
595
-
596
- ### ProseMirror
597
-
598
- `toProseMirror(source)` converts Carve to the ProseMirror document shape, and
599
- `fromProseMirror(doc)` writes one back as canonical Carve source. ProseMirror
600
- runs in a browser and nowhere else, so this binding is the whole audience for
601
- the engine's bridge - without it a host wiring a Carve editor either round-trips
602
- to a server or reimplements the node mapping in JS, where it drifts from the
603
- engine's.
604
-
605
- ```js
606
- const { json, dropped, degraded } = toProseMirror(source)
607
- editor.commands.setContent(JSON.parse(json))
608
- const saved = fromProseMirror(JSON.stringify(editor.getJSON()))
609
- ```
610
-
611
- `json` is a JSON string, the same choice `parseJson` makes. The two maps are
612
- `Carve node type -> reason`: `dropped` where the content is gone (an
613
- abbreviation definition has no editor node), `degraded` where the text survives
614
- without its node type (a soft break becomes whitespace, smart typography
615
- resolves to the glyph). Both are empty for a document the model holds exactly.
616
-
617
- A payload the schema map does not describe is refused rather than written
618
- approximately. Round-tripping normalizes the source the way `toCarve` does, and
619
- the reported degradations do not come back - `a ... b` returns as `a … b`.
620
-
621
- ### TypeScript
622
-
623
- The package ships `.d.ts` declarations. Types are inferred automatically when
624
- imported from `@markup-carve/carve-wasm`.
625
-
626
- ```ts
627
- import { toHtml, toHtmlFull, version } from '@markup-carve/carve-wasm'
58
+ const { value, report } = htmlToCarve('<p>Hello <strong>world</strong></p>', 'safe')
59
+ const { value: ast } = htmlToAst('<p>Hello <b>world</b></p>', 'safe')
628
60
 
629
- console.log(`carve-wasm v${version()}`)
630
- const html: string = toHtml('_Hello_')
61
+ fromHtml(html, 'semantic')
62
+ fromMarkdown(markdown)
631
63
  ```
632
64
 
633
- ## API
65
+ HTML modes are `safe` (the default), `semantic`, and trusted-only
66
+ `roundtrip`. `htmlToAst` returns the serialized Carve AST without passing it
67
+ through the source writer, so its report can differ from `htmlToCarve` when a
68
+ construct is representable in the tree but not in Carve source.
634
69
 
635
- | Export | Signature | Description |
636
- |--------|-----------|-------------|
637
- | `toHtml` | `(source: string) => string` | Core renderer, no extensions |
638
- | `toHtmlWithSymbols` | `(source: string, symbols?: object \| null) => string` | Core renderer + a `:name:` -> value symbols map (values are raw, see above) |
639
- | `toHtmlFull` | `(source: string, symbols?: object \| null) => string` | Core + common extensions (matches playground), optional symbols map |
640
- | `toHtmlWithOptions` | `(source: string, options?: object \| null) => string` | General form; see the options table above. Throws `ProfileViolationError` when a profile rejects the document |
641
- | `toHtmlWithRenderers` | `(source: string, options?: object \| null) => StaticRenderResult` | The options object plus `renderers.math` and `renderers.diagrams`, for `mode: 'static'`; returns `{ html, rendererErrors }` |
642
- | `toMarkdownWithOptions` | `(source: string, options?: object \| null) => string` | Markdown under the same options object. Throws `ProfileViolationError` when a profile rejects the document |
643
- | `toPlainTextWithOptions` | `(source: string, options?: object \| null) => string` | Plain text under the same options object |
644
- | `toAnsiWithOptions` | `(source: string, options?: object \| null) => string` | ANSI text under the same options object |
645
- | `toCarveWithOptions` | `(source: string, options?: object \| null) => string` | Canonical Carve under the same options object; reads `profile` only |
646
- | `parseJsonWithOptions` | `(source: string, options?: object \| null) => string` | The AST as JSON under the same options object; positions are always on |
647
- | `toHtmlWithReport` | `(source: string, strict?: boolean, maximum?: number) => RenderResult` | HTML plus bounded `raw-format-dropped` losses; strict mode throws `RenderLossError` |
648
- | `toMarkdownWithReport` | `(source: string, strict?: boolean, maximum?: number) => RenderResult` | Checked Markdown render |
649
- | `toPlainTextWithReport` | `(source: string, strict?: boolean, maximum?: number) => RenderResult` | Checked plain-text render |
650
- | `toAnsiWithReport` | `(source: string, strict?: boolean, maximum?: number) => RenderResult` | Checked ANSI render |
651
- | `toCarveWithReport` | `(source: string, strict?: boolean, maximum?: number) => RenderResult` | Checked canonical Carve render (lossless) |
652
- | `parseJson` | `(source: string) => string` | The parsed AST as JSON (PART 12 exchange shape) |
653
- | `astJsonToHtml` | `(json: string, options?: object \| null) => string` | Render an AST-JSON document; takes the same options object |
654
- | `astJsonToCarve` | `(json: string) => string` | Write an AST-JSON document back as canonical Carve source |
655
- | `astJsonToMarkdown` | `(json: string, options?: object \| null) => string` | Render an AST-JSON document to Markdown; takes the same options object |
656
- | `astJsonToPlainText` | `(json: string, options?: object \| null) => string` | Render an AST-JSON document to plain text |
657
- | `astJsonToAnsi` | `(json: string, options?: object \| null) => string` | Render an AST-JSON document to ANSI-styled text |
658
- | `applyProfile` | `(json: string, profile: string, options?: object \| null) => ProfileFilterResult` | Filter an AST-JSON document through a profile, keeping the tree and what the filter did |
659
- | `lintCarve` | `(source: string) => LintWarning[]` | Degradation diagnostics, with the rule ids carve-js and carve-php share |
660
- | `lintCarveWithOptions` | `(source: string, options?: object \| null) => LintWarning[]` | The same linter for the extension set the host renders with |
661
- | `lintAccessibility` | `(source: string) => AccessibilityDiagnostic[]` | The second diagnostic family, with its own rule ids and a severity |
662
- | `stampCarve` | `(formatted: string, generatedBy: string, form?: "line" \| "block") => string` | Write the provenance marker `readStamp` reads |
663
- | `sanitizeSvg` | `(source: string, options?: object \| null) => SanitizeResult` | Sanitize an SVG document; strict unless an option says otherwise |
664
- | `parseLocator` | `(loc: string) => ParsedLocator` | Parse a citation locator into label, value and suffix |
665
- | `parseSourceLayoutJson` | `(source: string) => string` | The PART 12 §13 source-layout sidecar |
666
- | `markdownToAstJson` | `(source: string) => string` | Import Markdown straight to the tree, skipping the Carve-source round trip |
667
- | `htmlToAst` | `(html: string, mode?: string) => MigrationResult` | Import HTML straight to the tree; `{ value, report }` with the tree in `value` |
668
- | `expandIncludes` | `(source: string, options: object) => IncludeExpansion` | Expand `{{ path }}` through a SYNCHRONOUS `resolve`; returns the tree plus warnings, dependencies and resolver failures |
669
- | `parseSnapshot` | `(source: string) => string` | Parse and keep what a `reparse` needs; JSON `{ source, document, sourceLayout, changedSource, reusedPreviousTree }` |
670
- | `reparse` | `(source: string, changes: string) => string` | Apply a JSON array of `{ range: [start, end], replacement }` in UTF-8 BYTE offsets and re-parse |
671
- | `mergeAst` | `(base: string, ours: string, theirs: string, options?: object) => string` | Three-way merge; JSON `{ ok, ast, conflicts, resolverErrors }`, a conflict being a value |
672
- | `createAstPatch` | `(before: string, after: string) => string` | The difference between two PART 12 trees, as `{ op, path, value }` patch JSON |
673
- | `applyAstPatch` | `(ast: string, patch: string) => string` | Replay patch JSON onto a tree |
674
- | `createReversibleAstPatch` | `(before: string, after: string) => string` | The same difference with its inverse and both fingerprints |
675
- | `applyReversibleAstPatch` | `(ast: string, patch: string, inverse?: boolean) => string` | Replay a reversible patch either way; a fingerprint mismatch throws |
676
- | `toProseMirror` | `(source: string) => ProseMirrorResult` | Convert to the ProseMirror document shape, with what the model could not hold |
677
- | `fromProseMirror` | `(doc: string) => string` | Convert a ProseMirror document back to canonical Carve source |
678
- | `readStamp` | `(source: string) => { version, generatedBy } \| null` | The document's provenance marker, if it carries one |
679
- | `needsReview` | `(source: string, currentVersion: string) => boolean` | Whether the stamp predates `currentVersion`; unstamped counts as yes |
680
- | `fromDjot` | `(source: string) => string` | Convert Djot source to Carve |
681
- | `migrateDjot` | `(source: string) => { value, report }` | Convert Djot with a v2 fidelity report |
682
- | `fromBbcode` | `(source: string) => string` | Convert BBCode source to Carve; throws past the engine's size cap |
683
- | `migrateBbcode` | `(source: string) => { value, report }` | Convert BBCode with a v2 fidelity report; throws past the engine's size cap |
684
- | `createSourcePatch` | `(source, replacement, kind, code) => SourcePatch` | Build a minimal UTF-8 byte-range patch |
685
- | `toCarvePatch` | `(source: string) => SourcePatch` | Preview canonical formatting as a patch |
686
- | `applySourcePatch` | `(source: string, patch: SourcePatch) => string` | Validate and apply trusted patch instructions |
687
- | `version` | `() => string` | Returns the carve-wasm package version |
70
+ ## Common host APIs
688
71
 
689
- ### The parsed AST
72
+ - `extensions()` returns the extension names supported by the bundled engine.
73
+ - `toCarvePatch()` and `applySourcePatch()` create and apply stale-safe UTF-8
74
+ byte edits.
75
+ - `toHtmlWithSymbols()` resolves configured `:name:` symbols. Symbol values
76
+ are trusted raw output and must not come from user input.
77
+ - `parseJson()`, `astJsonToHtml()`, and the other AST functions use the PART 12
78
+ AST JSON shared by Carve implementations.
79
+ - Include expansion calls a synchronous host resolver, records dependencies,
80
+ and enforces depth, byte, and call budgets. The resolver decides which paths
81
+ to refuse.
82
+ - Incremental parsing, tree patches, three-way merge, and ProseMirror helpers
83
+ support browser editors.
690
84
 
691
- `parseJson` returns the document as a JSON string - the [PART 12 exchange
692
- shape](https://markup-carve.github.io/carve/ast-json), the same tree every Carve
693
- engine publishes, so a consumer written against one implementation reads
694
- another's output.
85
+ The [complete API and usage reference](docs/reference.md) documents these
86
+ APIs, renderer profiles, resource limits, includes, AST fields, and the
87
+ TypeScript declarations.
695
88
 
696
- ```js
697
- import { parseJson } from '@markup-carve/carve-wasm'
698
-
699
- const ast = JSON.parse(parseJson('# Title\n\nBody[^a].\n\n[^a]: note\n'))
700
- ast.children.map((n) => n.type) // ['heading', 'paragraph', 'footnote']
701
- ast.children[0].pos // { startLine: 1, startColumn: 1, ... }
702
- ```
89
+ ## Security
703
90
 
704
- The root carries exactly `type`, `children` and `srcByteLength`; frontmatter and
705
- footnote definitions are block nodes inside `children`, not root fields. Every
706
- node except the root carries `pos` when the engine could place it - 1-based
707
- lines and columns, 0-based offsets, ends exclusive, counted in Unicode
708
- **codepoints**, not bytes or UTF-16 units. A node the engine could not place,
709
- such as reassembled table-cell text, carries no `pos` at all rather than an
710
- invented one.
91
+ Use `rawHtml: false` with a restrictive profile such as `comment` for
92
+ untrusted documents. Symbols and static renderer callbacks are trusted
93
+ configuration and may emit unescaped HTML. Treat source patches as trusted
94
+ edit instructions, not as signed data. A profile rejection throws.
711
95
 
712
- A string rather than a JS object: the caller runs `JSON.parse`, which the
713
- browser does natively and faster than building the object graph across the wasm
714
- boundary one property at a time - and it keeps the bytes available for a caller
715
- that stores or forwards them.
96
+ Carve also enforces URL-scheme and attribute hardening in the renderer. See
97
+ the language [security documentation](https://markup-carve.github.io/carve/security)
98
+ for the complete model.
716
99
 
717
100
  ## Development
718
101
 
719
- The reference material a contributor needs lives in `docs/`, so this page can
720
- stay about USING the package:
102
+ Contributor documentation lives under `docs/`:
721
103
 
722
- - [Building](docs/building.md) - the wasm-pack targets, pointing the tests at a
723
- built package, and the rendering-only browser profile.
724
- - [The carve-rs pin](docs/engine-pin.md) - why the engine is pinned to an exact
725
- commit, and how to move it.
726
- - [Releasing](docs/releasing.md) - the gate, what it refuses, and the runbook
727
- for cutting a version.
104
+ - [Building and testing](docs/building.md)
105
+ - [The carve-rs engine pin](docs/engine-pin.md)
106
+ - [Release process](docs/releasing.md)
Binary file
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@markup-carve/carve-wasm",
3
3
  "type": "module",
4
4
  "description": "WASM bindings for the Carve parser and HTML renderer.",
5
- "version": "0.1.4",
5
+ "version": "0.1.5",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",