Skip to content

How does one extend built-in parsers/tokenizers? #410

@ghost

Description

For example:

const linksidx = md.inline.ruler.__find__('links')
const oldlinks = md.inline.ruler.__rules__[linksidx].fn
const links = function (state, startLine, endLine, silent) {
    console.log(state)
    const ret = oldlinks(state, silent)
    if (!ret) return false

    console.log(state) /* This just returns the cumulative
        state object, without the ability to modify the
        particular link instance that was just captured. */
    return true
}
md.inline.ruler.at("links", links)

But to add custom attributes, one would need to import all the dependencies in lib/rules_inline/links.js and basically copy/paste the function, but the lib dir does not come with the node package. Also, this API seems to be a roundabout way to override or extend parser rules. Is there a better way?

Example case: add dimensions to images with a custom extension:

[alt text](https://example.com/image.jpg)[500x300]

I guess I will have to add it to the ALT text like:

[@500x300,alt text here](https://example.com/image.jpg)

but this use case could apply to any custom token such as a heading, URL, even a table with attributes, etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions