satteri 0.9.4 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/README.md +11 -1
  2. package/dist/binding.browser.d.ts +1 -1
  3. package/dist/binding.browser.js +1 -1
  4. package/dist/binding.d.ts +1 -1
  5. package/dist/binding.js +1 -1
  6. package/dist/byte-writer.d.ts +4 -0
  7. package/dist/byte-writer.js +13 -4
  8. package/dist/child-stub.d.ts +7 -3
  9. package/dist/child-stub.js +18 -7
  10. package/dist/command-buffer.d.ts +36 -50
  11. package/dist/command-buffer.js +102 -49
  12. package/dist/compile.d.ts +80 -32
  13. package/dist/compile.js +295 -70
  14. package/dist/freeze.d.ts +2 -0
  15. package/dist/freeze.js +10 -0
  16. package/dist/generated/wire-constants.d.ts +3 -2
  17. package/dist/generated/wire-constants.js +4 -2
  18. package/dist/hast/child-stub.d.ts +2 -3
  19. package/dist/hast/child-stub.js +18 -11
  20. package/dist/hast/element-props.d.ts +1 -1
  21. package/dist/hast/element-props.js +14 -6
  22. package/dist/hast/hast-materializer.d.ts +6 -2
  23. package/dist/hast/hast-materializer.js +48 -134
  24. package/dist/hast/hast-reader.d.ts +7 -13
  25. package/dist/hast/hast-reader.js +76 -21
  26. package/dist/hast/hast-visitor.d.ts +55 -5
  27. package/dist/hast/hast-visitor.js +320 -154
  28. package/dist/index.d.ts +6 -6
  29. package/dist/index.js +1 -1
  30. package/dist/lazy-child-resolver.d.ts +20 -13
  31. package/dist/lazy-child-resolver.js +66 -38
  32. package/dist/materializer-cache.d.ts +42 -0
  33. package/dist/materializer-cache.js +192 -0
  34. package/dist/mdast/child-stub.d.ts +6 -3
  35. package/dist/mdast/child-stub.js +71 -8
  36. package/dist/mdast/generated/layout.d.ts +2 -3
  37. package/dist/mdast/generated/layout.js +24 -28
  38. package/dist/mdast/generated/node-types.d.ts +4 -0
  39. package/dist/mdast/generated/node-types.js +25 -0
  40. package/dist/mdast/mdast-materializer.d.ts +12 -3
  41. package/dist/mdast/mdast-materializer.js +83 -78
  42. package/dist/mdast/mdast-reader.d.ts +15 -0
  43. package/dist/mdast/mdast-reader.js +55 -10
  44. package/dist/mdast/mdast-visitor.d.ts +79 -32
  45. package/dist/mdast/mdast-visitor.js +305 -122
  46. package/dist/op-stream.d.ts +5 -12
  47. package/dist/op-stream.js +6 -21
  48. package/dist/plugin.d.ts +36 -10
  49. package/dist/plugin.js +50 -2
  50. package/dist/string-pool.d.ts +13 -0
  51. package/dist/string-pool.js +91 -0
  52. package/dist/types.d.ts +44 -1
  53. package/dist/visitor-shared.d.ts +27 -4
  54. package/dist/visitor-shared.js +36 -10
  55. package/dist/wire-read.js +3 -1
  56. package/index.d.ts +130 -7
  57. package/index.js +89 -68
  58. package/package.json +24 -19
  59. package/dist/lazy-props.d.ts +0 -10
  60. package/dist/lazy-props.js +0 -50
  61. package/satteri_napi.wasi-browser.js +0 -83
  62. package/satteri_napi.wasi.cjs +0 -134
  63. package/wasi-worker-browser.mjs +0 -36
  64. package/wasi-worker.mjs +0 -63
@@ -1,6 +1,7 @@
1
1
  import { MdastReader } from "./mdast-reader.js";
2
2
  import { CommandBuffer } from "../command-buffer.js";
3
- import type { MdastNode, Toml, MathNode, InlineMath, Superscript, Subscript, Data } from "../types.js";
3
+ import type { MdastNode, Toml, MathNode, InlineMath, Superscript, Subscript, DescriptionList, DescriptionTerm, DescriptionDetails, Custom, Data, SourceFormat } from "../types.js";
4
+ import { type PluginOptions } from "../visitor-shared.js";
4
5
  import { LazyChildResolver } from "../lazy-child-resolver.js";
5
6
  import type { MdastHandle } from "../handles.js";
6
7
  import type { Blockquote, Break, Code, Definition, Delete, Emphasis, FootnoteDefinition, FootnoteReference, Heading, Html, Image, ImageReference, InlineCode, Link, LinkReference, List, ListItem, Paragraph, Strong, Table, TableRow, TableCell, Text, ThematicBreak, Yaml, Parents as MdastParents, Root as MdastRoot } from "mdast";
@@ -8,14 +9,31 @@ import type { MdxJsxFlowElement, MdxJsxTextElement } from "../mdx-types.js";
8
9
  import type { MdxFlowExpression, MdxTextExpression } from "../mdx-types.js";
9
10
  import type { MdxjsEsm } from "../mdx-types.js";
10
11
  import type { ContainerDirective, LeafDirective, TextDirective } from "../directive-types.js";
11
- /** New content for a structural mutation: a declarative node, or a raw markdown
12
- * / HTML escape hatch. Declarative nodes compile to the op-stream; a type the
13
- * op-stream can't encode is a hard error. */
14
- export type MdastContent = MdastNode | {
12
+ /** A string spliced into the tree, re-parsed as Markdown. Set `mdxExpressions:
13
+ * false` to keep MDX `{…}` literal, needed when injecting generated HTML
14
+ * (KaTeX, highlighters, diagrams) whose braces aren't expressions. Default true. */
15
+ export interface RawMdastContent {
15
16
  raw: string;
16
- } | {
17
+ mdxExpressions?: boolean;
18
+ }
19
+ export interface RawHtmlMdastContent {
20
+ /** @deprecated Use the equivalent `{ raw, mdxExpressions: false }`. */
17
21
  rawHtml: string;
18
- };
22
+ }
23
+ /** New content for a structural mutation: a declarative node, or a raw string
24
+ * escape hatch ({@link RawMdastContent}). Declarative nodes compile to the
25
+ * op-stream; a type the op-stream can't encode is a hard error. */
26
+ export type MdastContent = MdastNode | Custom | RawMdastContent | RawHtmlMdastContent;
27
+ /** An existing node a mutation targets. Includes {@link Custom} so a node
28
+ * reached through the `custom` visitor can be passed straight back in. */
29
+ export type MdastTarget = MdastNode | Custom;
30
+ /** A `wrapNode` wrapper: a built-in parent, or a user-defined node declaring
31
+ * a children array. */
32
+ export type MdastParentContent = Exclude<Extract<MdastNode, {
33
+ children: unknown[];
34
+ }>, MdastRoot> | (Custom & {
35
+ children: NonNullable<Custom["children"]>;
36
+ });
19
37
  export interface MdastDiagnostic {
20
38
  message: string;
21
39
  nodeId?: number | undefined;
@@ -38,33 +56,49 @@ export declare class MdastVisitorContext {
38
56
  * instances. Returned to the caller as `result.data`.
39
57
  */
40
58
  readonly data: Data;
41
- constructor(handle: MdastHandle, getSource: () => string, fileURL: URL | undefined, resolver: LazyChildResolver<MdastReader, MdastNode>, data: Data);
59
+ /**
60
+ * The source format this compile is processing: `"markdown"` for a plain
61
+ * Markdown compile, `"mdx"` for an MDX one. Lets a plugin shared between both
62
+ * pipelines branch on which it is handling.
63
+ */
64
+ readonly sourceFormat: SourceFormat;
65
+ constructor(handle: MdastHandle, getSource: () => string, fileURL: URL | undefined, resolver: LazyChildResolver<MdastReader, MdastNode>, data: Data, sourceFormat: SourceFormat, diagnostics: MdastDiagnostic[]);
42
66
  get source(): string;
43
- removeNode(node: Readonly<MdastNode>): void;
44
- insertBefore(node: Readonly<MdastNode>, newNode: MdastContent | MdastContent[]): void;
45
- insertAfter(node: Readonly<MdastNode>, newNode: MdastContent | MdastContent[]): void;
67
+ removeNode(node: Readonly<MdastTarget>): void;
68
+ insertBefore(node: Readonly<MdastTarget>, newNode: MdastContent | MdastContent[]): void;
69
+ insertAfter(node: Readonly<MdastTarget>, newNode: MdastContent | MdastContent[]): void;
46
70
  /**
47
71
  * Wrap `node` in `parentNode`, making it `parentNode`'s first child. Any
48
- * children `parentNode` declares are kept after it.
72
+ * children `parentNode` declares are kept after it. `parentNode` must be a
73
+ * node type that can hold children, or a raw string parsing to exactly one
74
+ * such block (`{ raw: "> " }` wraps in a blockquote); to surround a node
75
+ * with raw HTML tags, use `replaceNode(node, [openTag, node, closeTag])`
76
+ * instead.
49
77
  */
50
- wrapNode(node: Readonly<MdastNode>, parentNode: MdastContent): void;
51
- prependChild(node: Readonly<MdastNode>, childNode: MdastContent | MdastContent[]): void;
52
- appendChild(node: Readonly<MdastNode>, childNode: MdastContent | MdastContent[]): void;
78
+ wrapNode(node: Readonly<MdastTarget>, parentNode: MdastParentContent | RawMdastContent | RawHtmlMdastContent): void;
79
+ prependChild(node: Readonly<MdastTarget>, childNode: MdastContent | MdastContent[]): void;
80
+ appendChild(node: Readonly<MdastTarget>, childNode: MdastContent | MdastContent[]): void;
53
81
  /** Insert one node or an array at `index`; clamps (`0` or less prepends, past the end appends). */
54
- insertChildAt(node: Readonly<MdastNode>, index: number, childNode: MdastContent | MdastContent[]): void;
82
+ insertChildAt(node: Readonly<MdastTarget>, index: number, childNode: MdastContent | MdastContent[]): void;
55
83
  /** Remove the `index`-th child of `node`; a no-op when there is no such child. */
56
- removeChildAt(node: Readonly<MdastNode>, index: number): void;
57
- replaceNode(node: Readonly<MdastNode>, newNode: MdastContent): void;
58
- setProperty<N extends MdastNode, K extends keyof N & string>(node: Readonly<N>, key: K, value: N[K]): void;
84
+ removeChildAt(node: Readonly<MdastTarget>, index: number): void;
85
+ /**
86
+ * Swap `node` for one node, or for an array of nodes placed in order at its
87
+ * position. An empty array drops the node, the same as `removeNode`.
88
+ * The document root takes a `root`, the one place a `root` is accepted as
89
+ * content, or a raw string, which parses to a root of its own.
90
+ */
91
+ replaceNode(node: Readonly<MdastTarget>, newNode: MdastContent | MdastContent[]): void;
92
+ setProperty<N extends MdastTarget, K extends keyof N & string>(node: Readonly<N>, key: K, value: N[K]): void;
59
93
  /** `children` is structural and every parent accepts it, so the key also
60
94
  * works on node-type unions (e.g. a node returned by `parent()`). */
61
- setProperty(node: Readonly<MdastNode>, key: "children", value: readonly MdastNode[]): void;
95
+ setProperty(node: Readonly<MdastTarget>, key: "children", value: readonly MdastTarget[]): void;
62
96
  /** `data` is an open per-node bag serialized to JSON on the wire, so it
63
97
  * accepts any record (hName/hProperties/custom fields), not just the node's
64
98
  * declared `data` shape. `null` clears it. */
65
- setProperty(node: Readonly<MdastNode>, key: "data", value: Record<string, unknown> | null): void;
99
+ setProperty(node: Readonly<MdastTarget>, key: "data", value: Record<string, unknown> | null): void;
66
100
  /** Collect the concatenated text of all descendant text nodes (like mdast-util-to-string). */
67
- textContent(node: Readonly<MdastNode>, options?: {
101
+ textContent(node: Readonly<MdastTarget>, options?: {
68
102
  includeImageAlt?: boolean;
69
103
  includeHtml?: boolean;
70
104
  }): string;
@@ -74,28 +108,32 @@ export declare class MdastVisitorContext {
74
108
  * by identity.
75
109
  */
76
110
  parent<N extends Exclude<MdastNode, MdastRoot>>(node: Readonly<N>): Readonly<MdastParents>;
77
- parent(node: Readonly<MdastNode>): Readonly<MdastParents> | undefined;
111
+ parent(node: Readonly<MdastTarget>): Readonly<MdastParents> | undefined;
78
112
  /**
79
113
  * Index of `node` within its parent's children, or `undefined` at the root.
80
114
  * Use this rather than `parent.children.indexOf(node)`, which won't find it.
81
115
  */
82
- indexOf(node: Readonly<MdastNode>): number | undefined;
116
+ indexOf(node: Readonly<MdastTarget>): number | undefined;
83
117
  report({ message, node, severity, }: {
84
118
  message: string;
85
- node?: Readonly<MdastNode>;
119
+ node?: Readonly<MdastTarget>;
86
120
  severity?: "error" | "warning" | "info";
87
121
  }): void;
88
122
  /** Get the binary command buffer for all mutations recorded via context methods. */
89
123
  getCommandBuffer(): CommandBuffer;
90
124
  getDiagnostics(): MdastDiagnostic[];
91
125
  }
92
- type MdastVisitorResult = MdastNode | {
93
- raw: string;
94
- } | {
95
- rawHtml: string;
96
- } | undefined | null | void;
97
- type MdastVisitorFn<N extends MdastNode = MdastNode> = (node: Readonly<N>, context: MdastVisitorContext) => MdastVisitorResult | Promise<MdastVisitorResult>;
126
+ type MdastVisitorResult = MdastNode | RawMdastContent | RawHtmlMdastContent | undefined | null | void;
127
+ type MdastVisitorFn<N extends MdastNode | Custom = MdastNode> = (node: Readonly<N>, context: MdastVisitorContext) => MdastVisitorResult | Promise<MdastVisitorResult>;
128
+ export type MdastHookFn = (root: Readonly<MdastRoot>, context: MdastVisitorContext) => void | Promise<void>;
98
129
  export interface MdastPluginInstance {
130
+ /** Plugin-level configuration (e.g. `{ position: true }` to read positions). */
131
+ options?: PluginOptions;
132
+ /** Runs once per document, before the plugin's visitors. Awaited when async. */
133
+ before?: MdastHookFn;
134
+ /** Runs once per document, after the plugin's visitors have settled. Awaited
135
+ * when async. */
136
+ after?: MdastHookFn;
99
137
  paragraph?: MdastVisitorFn<Paragraph>;
100
138
  heading?: MdastVisitorFn<Heading>;
101
139
  thematicBreak?: MdastVisitorFn<ThematicBreak>;
@@ -129,11 +167,17 @@ export interface MdastPluginInstance {
129
167
  textDirective?: MdastVisitorFn<TextDirective>;
130
168
  superscript?: MdastVisitorFn<Superscript>;
131
169
  subscript?: MdastVisitorFn<Subscript>;
170
+ descriptionList?: MdastVisitorFn<DescriptionList>;
171
+ descriptionTerm?: MdastVisitorFn<DescriptionTerm>;
172
+ descriptionDetails?: MdastVisitorFn<DescriptionDetails>;
132
173
  mdxJsxFlowElement?: MdastVisitorFn<MdxJsxFlowElement>;
133
174
  mdxJsxTextElement?: MdastVisitorFn<MdxJsxTextElement>;
134
175
  mdxFlowExpression?: MdastVisitorFn<MdxFlowExpression>;
135
176
  mdxTextExpression?: MdastVisitorFn<MdxTextExpression>;
136
177
  mdxjsEsm?: MdastVisitorFn<MdxjsEsm>;
178
+ /** Fires for every user-defined node (any node created with a `type` outside
179
+ * the built-in set). Discriminate with `node.type`. */
180
+ custom?: MdastVisitorFn<Custom>;
137
181
  }
138
182
  interface MdastVisitResult {
139
183
  /** Binary command buffer containing all mutations. */
@@ -154,4 +198,7 @@ export declare function resolveMdastSubscriptions(plugin: MdastPluginInstance):
154
198
  * Returns MdastVisitResult synchronously if all visitors are sync,
155
199
  * or Promise<MdastVisitResult> if any visitor is async.
156
200
  */
157
- export declare function visitMdastHandle(handle: MdastHandle, plugin: MdastPluginInstance, subs: MdastSubscription[], source: string | (() => string), fileURL: URL | undefined, data?: Data): MdastVisitResult | Promise<MdastVisitResult>;
201
+ export declare function visitMdastHandle(handle: MdastHandle, plugin: MdastPluginInstance, subs: MdastSubscription[], source: string | (() => string), fileURL: URL | undefined, data?: Data, sourceFormat?: SourceFormat, diagnostics?: MdastDiagnostic[]): MdastVisitResult | Promise<MdastVisitResult>;
202
+ /** Its own pass so the caller can apply what `before` queued before the
203
+ * visitors walk, and the visitors' mutations before `after` reads the tree. */
204
+ export declare function visitMdastHook(handle: MdastHandle, plugin: MdastPluginInstance, hook: MdastHookFn, source: string | (() => string), fileURL: URL | undefined, data?: Data, sourceFormat?: SourceFormat, diagnostics?: MdastDiagnostic[]): MdastVisitResult | Promise<MdastVisitResult>;