PUBLIC MODULE · lib/markdown.js
Markdown
lib/markdown.js — Markdown: markdown STRUCTURE for any render target, presentation-free.
This module depends on no higher-level library code. CLI, TUI, and external hosts can use its public functions.
The interface is marked-style CALLBACKS: a renderer is an object of string-in/string-out functions — inline: text, strong, em, codespan, link, br; block: heading, code, blockquote, list, listItem, table, tableRow/tableCell (or marked's tablerow/tablecell), paragraph, hr, space — every one optional (missing ones default to plain text). Whole-text tokenization routes internally: the optional marked package's lexer when it resolves (a guarded dynamic import in lib/markdown/marked.js), or the builtin lexer (lib/markdown/lexer.js). Both produce the marked-shaped tokens accepted by the same walker (lib/markdown/walk.js), so renderers receive the same token contract from either engine.
Render targets hook in two ways: - whole text: renderMarkdown(text, renderer) — async (the marked import is), engine-routed; - incrementally (a still-growing stream, where marked's whole-text requirement can't apply): classifyLine() per complete line + renderInline() for the inline spans — the builtin engine's own primitives, synchronous. The terminal's shared SGR implementation lives in lib/tui-helpers/markdown-ansi.js; each TUI engine can use it or supply its own codes.
parseGitDiff(text) function
Parse one complete unified Git diff synchronously, with exact source-line spans.
async markdownEngine() function
Which engine whole-text rendering routes through: "marked" when the optional package resolved, "builtin" otherwise.
Returns Promise<"marked"|"builtin">
async lexMarkdown(text) function
Tokenize complete markdown text: marked's lexer (gfm, breaks) when available, the builtin lexer otherwise — same token shapes either way, ready for walkTokens.
textstring
Returns Promise<Array<object>> — marked-shaped block tokens
async renderMarkdown(text, renderer = {…}) function
Render complete markdown text through a renderer's callbacks, engine-routed (see lexMarkdown). Rendered tokens join with one "\n"; source blank lines are space tokens, so one blank line remains one blank line in the output.
textstring[renderer]object- marked-style callbacks; missing ones default to plain text
Returns Promise<string>
renderInline(text, renderer = {…}) function
Render one fragment of inline markdown through a renderer's inline callbacks, synchronously, via the builtin tokenizer (engine routing needs whole text — inline fragments don't have it). This is the primitive incremental renderers style complete lines with.
textstring- inline markdown (no block structure)
[renderer]object- inline callbacks; missing ones default to plain text
Returns string
class Markdown class
Canonical markdown module namespace. Static-only by design.
parseInline(text) function
Tokenize inline markdown into flat spans.
textstring- one line/fragment of inline markdown
Returns Array<{type: string, text: string, href?: string}>
Defined in lib/markdown/inline.js
classifyLine(line, state = {…}) function
Classify one complete line of markdown.
linestring- one line, no trailing "\n"
Returns {kind: "fence", lang: string, raw: string — | {kind: "code", raw: string}
| {kind: "hr", width: number, raw: string}
| {kind: "heading", depth: number, text: string, raw: string}
| {kind: "list", indent: string, marker: string, ordered: boolean, text: string, raw: string}
| {kind: "quote", text: string, raw: string}
| {kind: "text", text: string, raw: string}}
`raw` is always the source line; `text` the content with the
block marker stripped; a list's `marker` is the source marker
("-", "*", "2.") — normalization is the renderer's business.
Defined in lib/markdown/line.js
walkTokens(tokens, renderer = {…}) function
Walk block tokens through a renderer (completed internally).
tokensArray<object>- marked-shaped block tokens
[renderer]object- callbacks; missing ones default to plain text
Returns string
Defined in lib/markdown/walk.js