Skip to content

docs: show how to supply a custom parser - #4313

Open
friedman-max wants to merge 1 commit into
markedjs:masterfrom
friedman-max:docs-provide-parser-example
Open

friedman-max wants to merge 1 commit into
markedjs:masterfrom
friedman-max:docs-provide-parser-example

Conversation

@friedman-max

Copy link
Copy Markdown

Marked version: 18.4.0 (471f274)

Markdown flavor: n/a (documentation)

Description

Refs #4282. Add a runnable provideParser example that returns rendered HTML alongside each top-level token. Show how to fall back for inline parsing and explain why parser results are not chained. No runtime changes.

Contributor

  • No new tests required for this docs-only change. Built the docs and verified both outputs from the example against the local build.

Disclosure: Prepared with OpenAI Codex assistance.

Copilot AI balanced review requested due to automatic review settings December 5, 2026 16:51
@vercel

vercel Bot commented Dec 5, 2026

Copy link
Copy Markdown

@friedman-max is attempting to deploy a commit to the MarkedJS Team on Vercel.

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@vercel

vercel Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
marked-website Ready Ready Preview Dec 6, 2026 12:08am UTC

Request Review

@UziTech UziTech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding documentation for provideParser! A few accuracy and consistency adjustments before merging:

  1. Keep the return type in the Hooks table (docs/USING_PRO.md, line 294):
    Changing provideParser(): (tokens: Token[], options?: MarkedOptions) => string to provideParser(block?: boolean) drops the return signature. Please keep the return type so readers can see the (tokens, options) callback signature and false fallback, e.g.:
    `provideParser(block?: boolean): ((tokens: Token[], options?: MarkedOptions) => ParserOutput) | false` (or => any).

  2. Include provideLexer in the exception note:
    The new paragraph states that "provideParser hooks are an exception", but both provideLexer and provideParser are non-pass-through provider hooks (neither is in _Hooks.passThroughHooks), both receive block?: boolean, and both fall back to the previous hook on false without chaining results. Please mention both provideLexer and provideParser here (and optionally add block?: boolean to the provideLexer table row for consistency).

  3. Avoid the misleading "interactions between adjacent tokens" note and per-token Parser instantiation:
    Marked's built-in Parser.parse renders each token independently and concatenates the output (reference link definitions are resolved during lexing), so there are no built-in interactions between adjacent tokens during parsing. Calling Parser.parse([token], options) inside tokens.map(...) also constructs a new Parser (and TextRenderer) for every top-level token. Consider reusing a single parser instance:

    const parser = new Parser(options);
    return tokens.map((token) => ({
      type: token.type,
      html: parser.parse([token]),
    }));

    and removing or rewording the "adjacent tokens" sentence (e.g., noting that custom stateful renderers/extensions expecting the full token array may need parser.parse(tokens)).

  4. Style consistency (optional nit):
    Other examples in docs/USING_PRO.md use a separate **Output:** block below the code snippet instead of inline // ... comments, and the Hooks examples roughly follow the table order (preprocess, postprocess, processAllTokens/provideLexer, emStrongMask). Moving this example to the end of the Hooks section with an **Output:** block would match the rest of the page.

This branch was successfully deployed

1 active deployment
Preview — 2de8b66f Deployed Dec 6, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants