mdz_helpers.ts

Shared constants and pure helper functions for mdz parsers.

Used by both the synchronous parser (mdz_lexer.ts + mdz_token_parser.ts) and the streaming parser (mdz_stream_parser.ts).

view source

Declarations
#

11 declarations

mdz_extract_single_tag
#

mdz_helpers.ts view source

(nodes: MdzNode[]): MdzNodeElement | MdzNodeComponent | null import {mdz_extract_single_tag} from '@fuzdev/mdz/mdz_helpers.js';

Extracts a single tag (component or element) if it's the only non-whitespace content.

nodes

type MdzNode[]

returns

MdzNodeElement | MdzNodeComponent | null

the tag node if paragraph wrapping should be skipped (MDX convention), or null if the content should be wrapped in a paragraph

mdz_heading_id
#

mdz_helpers.ts view source

(nodes: MdzNode[]): string import {mdz_heading_id} from '@fuzdev/mdz/mdz_helpers.js';

Generates a lowercase slug id for a heading from its child nodes. Follows standard markdown conventions (GitHub, etc.) where heading IDs are lowercased.

nodes

type MdzNode[]

returns

string

mdz_heading_id_from_text
#

mdz_helpers.ts view source

(text: string): string import {mdz_heading_id_from_text} from '@fuzdev/mdz/mdz_helpers.js';

Generates a lowercase slug id for a heading from plain text content. Used by the streaming parser which tracks text content directly rather than building MdzNode[] trees.

text

type string

returns

string

mdz_is_safe_reference
#

mdz_helpers.ts view source

(reference: string): boolean import {mdz_is_safe_reference} from '@fuzdev/mdz/mdz_helpers.js';

reference

type string

returns

boolean

mdz_is_url
#

mdz_helpers.ts view source

(s: string): boolean import {mdz_is_url} from '@fuzdev/mdz/mdz_helpers.js';

s

type string

returns

boolean

mdz_is_void_element
#

mdz_helpers.ts view source

(name: string): boolean import {mdz_is_void_element} from '@fuzdev/mdz/mdz_helpers.js';

Check if an element name is an HTML void element (br, img, hr, ...). Void elements render self-closing; any parsed children are dropped.

name

type string

returns

boolean

mdz_merge_adjacent_text
#

mdz_helpers.ts view source

(nodes: MdzNode[]): MdzNode[] import {mdz_merge_adjacent_text} from '@fuzdev/mdz/mdz_helpers.js';

Return a new array with adjacent Text nodes merged into single nodes. Fast path: returns the original array when no merging is needed.

nodes

type MdzNode[]

returns

MdzNode[]

mdz_push_merging_text
#

mdz_helpers.ts view source

(dest: MdzNode[], node: MdzNode): void import {mdz_push_merging_text} from '@fuzdev/mdz/mdz_helpers.js';

Push a node into a children array, coalescing with the previous Text node. Mutates prev.content and prev.end when both are Text, avoiding array growth and an extra allocation. Callers must own dest and not retain references to the prior last element across the call.

dest

type MdzNode[]

node

type MdzNode

returns

void

mdz_resolve_relative_path
#

mdz_helpers.ts view source

(reference: string, base: string): string import {mdz_resolve_relative_path} from '@fuzdev/mdz/mdz_helpers.js';

Resolves a relative path (./ or ../) against a base path. The base is treated as a directory regardless of trailing slash ('/docs/usage' and '/docs/usage/' behave identically). Handles embedded . and .. segments within the reference (e.g., './a/../b' → navigates up then down). Clamps at root — excess .. segments stop at / rather than escaping.

reference

a relative path starting with ./ or ../

type string

base

An absolute base path (e.g., '/docs/usage/'). Empty string is treated as root.

type string

returns

string

an absolute resolved path (e.g., '/docs/usage/grammar')

mdz_text_content
#

mdz_helpers.ts view source

(nodes: MdzNode[]): string import {mdz_text_content} from '@fuzdev/mdz/mdz_helpers.js';

Extracts plain text content from an array of mdz nodes, recursing into children.

nodes

type MdzNode[]

returns

string

mdz_trim_url_punctuation
#

mdz_helpers.ts view source

(url: string): string import {mdz_trim_url_punctuation} from '@fuzdev/mdz/mdz_helpers.js';

Trim trailing punctuation from URL/path per RFC 3986 and GFM rules.

  • Trims simple trailing: .,;:!?]
  • Balanced logic for () only (valid in path components)

Note on ]: the mdz parsers (mdz.ts, mdz_stream_parser_url.ts, mdz_lexer.ts) scan URL/path chars through is_valid_path_char, which already rejects ] — so it can never reach this function via parser flow. The ] branch here is for external callers using this helper directly on arbitrary URL-ish input as part of the published @fuzdev/mdz surface.

Optimized to avoid O(n²) string slicing - tracks end index and slices once at the end.

url

type string

returns

string

Depends on
#

Imported by
#