mdz_opcodes.ts

Opcode types for the mdz streaming parser.

Opcodes are serializable rendering instructions emitted by MdzStreamParser. They tell a renderer what to do next — open a container, append text, close it, or revert an optimistic assumption. Target-agnostic: works for HTML, Svelte, PDF, etc.

The parser makes optimistic assumptions about ambiguous syntax (e.g., ** is probably bold) and emits revert opcodes to correct when wrong. This enables true streaming rendering without ever re-parsing.

view source

Declarations
#

14 declarations

MdzNodeId
#

mdz_opcodes.ts view source

number import type {MdzNodeId} from '@fuzdev/mdz/mdz_opcodes.js';

Unique monotonic identifier for each node created by the parser. IDs are never reused within a parser instance.

MdzNodeType
#

mdz_opcodes.ts view source

MdzNodeType import type {MdzNodeType} from '@fuzdev/mdz/mdz_opcodes.js';

All node types that can appear in the mdz tree.

MdzNodeTypeContainer
#

mdz_opcodes.ts view source

MdzNodeTypeContainer import type {MdzNodeTypeContainer} from '@fuzdev/mdz/mdz_opcodes.js';

Node types that can be opened as containers.

Code appears here *and* in MdzNodeTypeText: inline code resolved within one chunk emits as a single text opcode, while a backtick with no closer visible yet opens an optimistic Code container whose content streams as text children (collapsed back to content by consumers at close).

MdzNodeTypeText
#

MdzNodeTypeVoid
#

mdz_opcodes.ts view source

"Hr" import type {MdzNodeTypeVoid} from '@fuzdev/mdz/mdz_opcodes.js';

Node types for self-contained leaf elements.

MdzOpcode
#

mdz_opcodes.ts view source

MdzOpcode import type {MdzOpcode} from '@fuzdev/mdz/mdz_opcodes.js';

Discriminated union of all mdz opcodes.

MdzOpcodeAppendText
#

mdz_opcodes.ts view source

MdzOpcodeAppendText import type {MdzOpcodeAppendText} from '@fuzdev/mdz/mdz_opcodes.js';

Append content to an existing text node. Streaming optimization — avoids creating a new node per chunk during plain text runs.

type

type 'append_text'

id

type MdzNodeId

content

type string

end?

Byte offset immediately after the appended content in the full input. When absent, consumers extend the node's end by content.length; present when the two differ — list continuation lines strip structural indent from content, so the source span outgrows the content length.

type number

MdzOpcodeClose
#

mdz_opcodes.ts view source

MdzOpcodeClose import type {MdzOpcodeClose} from '@fuzdev/mdz/mdz_opcodes.js';

Close a previously opened container node. Carries deferred metadata that wasn't known at open time.

type

type 'close'

id

type MdzNodeId

end

Byte offset in the full input immediately after the closing delimiter.

type number

reference?

Link URL/path, resolved when ](url) completes.

type string

link_type?

Link type, resolved alongside reference.

type 'external' | 'internal'

heading_id?

Heading slug, computed from full heading content.

type string

discard?

If true, consumer drops this node and its descendants from the tree. Used for whitespace-only paragraphs that match nothing in mdz_parse's output — the streaming parser emits open/text speculatively, then retroactively drops the empty wrapper at close.

type boolean

MdzOpcodeOpen
#

mdz_opcodes.ts view source

MdzOpcodeOpen import type {MdzOpcodeOpen} from '@fuzdev/mdz/mdz_opcodes.js';

Open a container node. The renderer starts a new element/wrapper. Children are subsequent opcodes until the matching close.

type

type 'open'

id

type MdzNodeId

node_type

type MdzNodeTypeContainer

start

Byte offset in the full input where the opening delimiter begins.

type number

level?

Heading level (1-6). Present when node_type is 'Heading'.

type 1 | 2 | 3 | 4 | 5 | 6

name?

Tag name. Present when node_type is 'Element' or 'Component'.

type string

lang?

Language hint. Present when node_type is 'Codeblock'.

type string | null

ordered?

Whether the list is ordered. Present when node_type is 'List'.

type boolean

start_number?

First item's authored number — the <ol start> attribute. Present for ordered 'List'.

type number

number?

Authored item number. Present when node_type is 'ListItem' in an ordered list.

type number

MdzOpcodeRevert
#

mdz_opcodes.ts view source

MdzOpcodeRevert import type {MdzOpcodeRevert} from '@fuzdev/mdz/mdz_opcodes.js';

Undo an optimistic open. Removes the container wrapper, inserts replacement_text as literal text at the container's position, and re-parents the container's children to the grandparent.

Only inline containers revert — block structure (paragraphs, headings, codeblocks, lists, blockquotes) is decided from bounded input before opening, so block opens are never speculative.

type

type 'revert'

id

type MdzNodeId

replacement_text

The delimiter text to emit as literal content (e.g., "**", "[", "<Tag>").

type string

start

Byte offset of the original opening delimiter in the full input.

type number

MdzOpcodeText
#

mdz_opcodes.ts view source

MdzOpcodeText import type {MdzOpcodeText} from '@fuzdev/mdz/mdz_opcodes.js';

Create a leaf text or code node. The parent is implicit — the innermost open container on the renderer's stack.

type

type 'text'

id

type MdzNodeId

content

type string

text_type

type MdzNodeTypeText

start

Byte offset where this node begins (for Code, the opening backtick).

type number

end

Byte offset immediately after this node ends (for Code, after the closing backtick).

type number

MdzOpcodeTrimText
#

mdz_opcodes.ts view source

MdzOpcodeTrimText import type {MdzOpcodeTrimText} from '@fuzdev/mdz/mdz_opcodes.js';

Trim count characters from the end of an existing text node. If trimming empties the node, the consumer removes it from its parent.

Used by paragraph/codeblock close to drop the trailing newline that separates inline content from the block boundary. Emitted instead of retroactively mutating the prior text/append_text opcode, so the opcode stream is append-only.

type

type 'trim_text'

id

type MdzNodeId

count

type number

MdzOpcodeVoid
#

mdz_opcodes.ts view source

MdzOpcodeVoid import type {MdzOpcodeVoid} from '@fuzdev/mdz/mdz_opcodes.js';

Create a self-contained leaf node (e.g., horizontal rule). Inserted as a child of the innermost open container, or at root level.

type

type 'void'

id

type MdzNodeId

node_type

type MdzNodeTypeVoid

start

Byte offset in the full input where this element begins.

type number

end

Byte offset immediately after this element ends.

type number

MdzOpcodeWrap
#

mdz_opcodes.ts view source

MdzOpcodeWrap import type {MdzOpcodeWrap} from '@fuzdev/mdz/mdz_opcodes.js';

Retroactively wrap an existing text node in a container. Used for text-first auto-links: URL/path text streams as plain text, then gets wrapped in a Link when the URL boundary is found.

Specific to text-first auto-detected content — not a general retroactive-container mechanism. Delimiter-paired inlines (bold, italic, strikethrough) use optimistic open + revert instead: their opening delimiter is an explicit open event, and the delimiters are discarded rather than retained as content.

When trim_end is set, trailing characters (punctuation) are trimmed from the target text node and placed in a new sibling Text node after the Link wrapper, identified by trim_id.

type

type 'wrap'

id

ID for the new Link container node.

type MdzNodeId

node_type

Container type to wrap in (always 'Link' for now).

type 'Link'

target_id

ID of the existing text node to wrap.

type MdzNodeId

reference

Resolved URL or path reference.

type string

link_type

Whether the link is external (URL) or internal (path).

type 'external' | 'internal'

start

Byte offset where the URL/path begins.

type number

end

Byte offset immediately after the URL/path (before any trimmed punctuation).

type number

trim_end?

Number of trailing chars to trim from target and place after the link.

type number

trim_id?

ID for the trimmed-text sibling node. Required when trim_end > 0.

type MdzNodeId

Imported by
#