Skip to content

let walkTokens replace the current token with multiple #3844

@calculuschild

Description

@calculuschild

Describe the feature
Currently, walkTokens lets you edit individual token properties as you walk the tree. However, you can't inject new tokens (except as children if the current token has a "tokens" property). I would like to take, say, an HTML token, and break it apart into multiple pieces as their own tokens. For example:

<div>content</div> is parsed into:

[
{type:"html", block:true, raw:"<div>\ncontent\n</div>", pre:false, text:"<div>\ncontent\n</div>"}
]

Which I would like to split into:

[
  {type:"html", block:true, raw:"<div>\n\n", pre:false, text:"<div>\n\n"}
  {type:"text", raw:"content", text:"content", escaped:false}
  {type:"html", block:true, raw:"</div>", pre:false, text:"</div>"}
]

Why is this feature necessary?
walkTokens is a great tool to tweak the token tree, particularly when you want to slightly change the behavior of a built-in token without reimplementing the entire tokenizer along with its regex. But it would be nice to have more control over the tree beyond just changing the existing tokens.

Describe alternatives you've considered
Fully re-implementing the tokenizer for built-in tokens does work, because you can break tokens down as much as you want, but extensions can quickly fall out-of-sync with Marked.js bug fixes, and requires a lot more maintenance work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions