@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.
@@ -225,11 +225,11 @@ class $ {
225
225
  this.logs.clear(), this.kind_changes = null, this.active_node = c;
226
226
  }
227
227
  }
228
- function h(a) {
228
+ function l(a) {
229
229
  const t = b(a);
230
230
  return (t === 36 || t === 37) && E(), t;
231
231
  }
232
- const l = 4294967295, O = "a NodeView was used after its document, a view is only valid during the parse that made it", z = [0];
232
+ const h = 4294967295, O = "a NodeView was used after its document, a view is only valid during the parse that made it", z = [0];
233
233
  class W {
234
234
  constructor(t) {
235
235
  this.lines = null, this.source = t;
@@ -239,11 +239,14 @@ class W {
239
239
  }
240
240
  get_string(t) {
241
241
  }
242
+ text() {
243
+ return this.source;
244
+ }
242
245
  escapes(t, e) {
243
246
  if (t._strings[e] !== void 0)
244
247
  return null;
245
248
  const i = t.value_start_at(e), n = t.value_end_at(e);
246
- if (i === l || n === l || n <= i)
249
+ if (i === h || n === h || n <= i)
247
250
  return null;
248
251
  const r = this.source;
249
252
  let s = i;
@@ -339,7 +342,7 @@ function x(a, t, e) {
339
342
  }
340
343
  class L {
341
344
  constructor(t, e, i, n = null) {
342
- this.first = null, this.views = null, this.gen = 0, this.lazy = l, this.headings = null, this.buf = t, this.text_source = e, this.undo = i, this.host = n;
345
+ this.first = null, this.views = null, this.gen = 0, this.lazy = h, this.headings = null, this.buf = t, this.text_source = e, this.undo = i, this.host = n;
343
346
  }
344
347
  /** the view of a wrap_from wrapper, it replaces a plain view of the same node */
345
348
  wrapper(t) {
@@ -354,7 +357,7 @@ class L {
354
357
  }
355
358
  /** get or create a NodeView for the given buffer index. */
356
359
  get(t) {
357
- if (t === l)
360
+ if (t === h)
358
361
  return null;
359
362
  const e = this.first;
360
363
  if (e === null)
@@ -376,11 +379,11 @@ class L {
376
379
  /** @internal a callback reads the attrs of the heading whose id waits, it is made first */
377
380
  make_lazy(t) {
378
381
  const e = this.lazy;
379
- this.lazy = l, this.headings.close(t, e);
382
+ this.lazy = h, this.headings.close(t, e);
380
383
  }
381
384
  /** discard all cached views. */
382
385
  clear() {
383
- this.first = null, this.views !== null && this.views.clear();
386
+ this.first = null, this.views !== null && this.views.size !== 0 && this.views.clear();
384
387
  }
385
388
  /**
386
389
  * the buffer is about to hold another document, a view kept from this one
@@ -474,7 +477,7 @@ class m {
474
477
  return y(this._live().kind_at(this._index));
475
478
  }
476
479
  set type(t) {
477
- const e = h(t);
480
+ const e = l(t);
478
481
  if (e === void 0)
479
482
  return;
480
483
  const i = this._live();
@@ -493,11 +496,11 @@ class m {
493
496
  }
494
497
  get next() {
495
498
  const t = this._live(), e = t.next_at(this._index);
496
- return e === l || t.parent_at(e) !== t.parent_at(this._index) ? null : this._cache.get(e);
499
+ return e === h || t.parent_at(e) !== t.parent_at(this._index) ? null : this._cache.get(e);
497
500
  }
498
501
  get prev() {
499
502
  const t = this._live().prev_at(this._index);
500
- return t === l ? null : this._cache.get(t);
503
+ return t === h ? null : this._cache.get(t);
501
504
  }
502
505
  /**
503
506
  * flattened text of all descendants, the text a reader sees: a line end
@@ -515,12 +518,12 @@ class m {
515
518
  if (s !== void 0)
516
519
  return s;
517
520
  const o = e.value_start_at(t), _ = e.value_end_at(t);
518
- return o === l || _ === l || _ <= o ? "" : this._text_source.slice(o, _);
521
+ return o === h || _ === h || _ <= o ? "" : this._text_source.slice(o, _);
519
522
  }
520
523
  if (i === 5 || i === 8 || i === 26)
521
524
  return A(e, t, this._text_source);
522
525
  let n = "", r = e.first_child_at(t);
523
- for (; r !== l && e.parent_at(r) === t; ) {
526
+ for (; r !== h && e.parent_at(r) === t; ) {
524
527
  const s = e.kind_at(r);
525
528
  s === 18 || s === 17 ? n += " " : s !== 26 && (n += this._collect_text(r)), r = e.next_at(r);
526
529
  }
@@ -605,13 +608,13 @@ class m {
605
608
  const n = t.end_at(e);
606
609
  return {
607
610
  start: i.point(t.start_at(e)),
608
- end: n === l ? void 0 : i.point(n)
611
+ end: n === h ? void 0 : i.point(n)
609
612
  };
610
613
  }
611
614
  /** @internal the offsets a warning about this node carries, NONE for those it has none of */
612
615
  _span() {
613
616
  const t = this._live(), e = this._index;
614
- return this._text_source.point === void 0 || t.synthetic_at(e) ? [l, l] : [t.start_at(e), t.end_at(e)];
617
+ return this._text_source.point === void 0 || t.synthetic_at(e) ? [h, h] : [t.start_at(e), t.end_at(e)];
615
618
  }
616
619
  get attrs() {
617
620
  return this._attrs !== null ? this._attrs : this._attrs = new Proxy(new D(this, this._index, this._undo), I);
@@ -622,7 +625,7 @@ class m {
622
625
  * returns a view for the new wrapper node.
623
626
  */
624
627
  wrap_inner(t, e) {
625
- const i = h(t);
628
+ const i = l(t);
626
629
  if (i === void 0)
627
630
  throw new Error(`Unknown node type: ${t}`);
628
631
  const n = this._live();
@@ -640,7 +643,7 @@ class m {
640
643
  * child of its parent
641
644
  */
642
645
  wrap_from(t, e) {
643
- const i = h(t);
646
+ const i = l(t);
644
647
  if (i === void 0)
645
648
  throw new Error(`Unknown node type: ${t}`);
646
649
  const n = this._live();
@@ -668,17 +671,17 @@ class m {
668
671
  e.remove(t, this._index);
669
672
  }
670
673
  prepend(t, e) {
671
- const i = h(t);
674
+ const i = l(t);
672
675
  if (i === void 0)
673
676
  throw new Error(`Unknown node type: ${t}`);
674
677
  const n = this._live();
675
678
  this._writable("prepend");
676
679
  const r = this._index, s = x(i, t, e), o = n.first_child_at(r);
677
680
  let _;
678
- return o === l ? _ = n.push_synthetic(i, 0, r, 0, s) : (_ = n.push_synthetic(i, 0, l, 0, s), n.set_parent(_, r), n.set_next(_, o), n.set_prev(o, _), n.set_first_child(r, _)), typeof e == "string" && (n.own_strings()[_] = e), this._undo.record_prepend(r, _, o), this._cache.get(_);
681
+ return o === h ? _ = n.push_synthetic(i, 0, r, 0, s) : (_ = n.push_synthetic(i, 0, h, 0, s), n.set_parent(_, r), n.set_next(_, o), n.set_prev(o, _), n.set_first_child(r, _)), typeof e == "string" && (n.own_strings()[_] = e), this._undo.record_prepend(r, _, o), this._cache.get(_);
679
682
  }
680
683
  append(t, e) {
681
- const i = h(t);
684
+ const i = l(t);
682
685
  if (i === void 0)
683
686
  throw new Error(`Unknown node type: ${t}`);
684
687
  const n = this._live();
@@ -21,6 +21,8 @@ export interface Position {
21
21
  export interface TextSource {
22
22
  slice(start: number, end: number): string;
23
23
  get_string(index: number): string | undefined;
24
+ /** the whole source the value ranges index, only a source that has one has it */
25
+ text?(): string;
24
26
  /** the line and column of an offset, only a source that has offsets has it */
25
27
  point?(offset: number): Point;
26
28
  /**
@@ -39,6 +41,7 @@ export declare class SourceTextSource implements TextSource {
39
41
  constructor(source: string);
40
42
  slice(start: number, end: number): string;
41
43
  get_string(_index: number): string | undefined;
44
+ text(): string;
42
45
  escapes(buf: NodeBuffer, index: number): readonly number[] | null;
43
46
  /** update source for incremental parsing. */
44
47
  set_source(source: string): void;
@@ -102,6 +102,7 @@ export declare class PluginDispatcher implements StructureHost, RedirectHost, Da
102
102
  private open_from;
103
103
  /** a closed wrap_from wrapper whose last child was still open, keyed by that child */
104
104
  private close_with;
105
+ private close_with_top;
105
106
  /** the node whose handler or close callback is running */
106
107
  private running;
107
108
  /** the observer whose handler or close callback is running, it changes no node */
@@ -157,6 +158,8 @@ export declare class PluginDispatcher implements StructureHost, RedirectHost, Da
157
158
  on_data(listener: (change: DataChange) => void): () => void;
158
159
  /** @internal */
159
160
  _filename(): string | undefined;
161
+ /** @internal the text of a node for ctx.text */
162
+ _text(node: NodeView): string;
160
163
  /** @internal */
161
164
  _frontmatter(): Record<string, unknown> | undefined;
162
165
  /**
@@ -268,8 +271,7 @@ export declare class PluginDispatcher implements StructureHost, RedirectHost, Da
268
271
  /** a close needs no dispatch */
269
272
  quiet(): boolean;
270
273
  /**
271
- * closes of nodes from this index on have nothing to dispatch, a close
272
- * callback or a wrapper that waits on a child can sit anywhere
274
+ * closes of nodes from this index on have nothing to dispatch
273
275
  */
274
276
  plain_from(): number;
275
277
  /**
@@ -1,5 +1,6 @@
1
1
  import type { NodeView } from './node_view.js';
2
2
  import type { Json, PluginCache } from './plugin_data.js';
3
+ import type { SmartPunctuation } from './reader_text.js';
3
4
  import type { TextRun } from './text_run.js';
4
5
  export type { DataChange, Json } from './plugin_data.js';
5
6
  /** a problem a plugin reported with ctx.warn */
@@ -36,6 +37,8 @@ export interface PluginDocument {
36
37
  resolve?: (specifier: string, importer: string | undefined) => string | null | undefined | Promise<string | null | undefined>;
37
38
  /** what ctx.cached keeps, shared by the documents of one compiler */
38
39
  cache?: PluginCache;
40
+ /** the smart punctuation the document renders with, ctx.text applies it, off when undefined */
41
+ smart_punctuation?: SmartPunctuation;
39
42
  }
40
43
  /** what every handler learns about its document and can hand back */
41
44
  export interface DocumentContext {
@@ -47,6 +50,18 @@ export interface DocumentContext {
47
50
  * runs before it is complete, do not write to it
48
51
  */
49
52
  readonly frontmatter: Record<string, unknown> | undefined;
53
+ /**
54
+ * the text of a node as the page shows it, where text_content is the text
55
+ * as written, an image is its alt, a line end is a space, and a comment,
56
+ * an expression, a component and a directive add nothing, a character
57
+ * reference is its character and the smart_punctuation of the compile is
58
+ * applied, a quote asks the text beside it, so read it once the block
59
+ * that holds the node closed
60
+ *
61
+ * @example
62
+ * heading: { parse: (node, ctx) => () => ctx.add('headings', ctx.text(node)) }
63
+ */
64
+ text(node: NodeView): string;
50
65
  /**
51
66
  * report a problem, at the node when one is given, it is dropped when the
52
67
  * node whose handler reported it is revoked
@@ -0,0 +1,17 @@
1
+ import type { TextSource } from './node_view.js';
2
+ import type { NodeBuffer } from './utils.js';
3
+ /** the opening and closing double quote, then the opening and closing single quote */
4
+ export type SmartQuotes = readonly [string, string, string, string];
5
+ /** each kind is on unless it is false */
6
+ export interface SmartPunctuationOptions {
7
+ /** straight quotes to curly ones, four strings replace the english ones */
8
+ quotes?: boolean | SmartQuotes;
9
+ /** two hyphens to an en dash and three to an em dash */
10
+ dashes?: boolean;
11
+ /** three dots to an ellipsis */
12
+ ellipses?: boolean;
13
+ }
14
+ /** true is every kind with english quotes, false is none */
15
+ export type SmartPunctuation = boolean | SmartPunctuationOptions;
16
+ /** what ctx.text gives, smart is the option of the render, undefined for off */
17
+ export declare function reader_text(buffer: NodeBuffer, source: TextSource, node: number, smart: SmartPunctuation | undefined): string;