marked icon indicating copy to clipboard operation
marked copied to clipboard

Extension not triggering

Open nrivard opened this issue 6 months ago • 5 comments

Marked version:

Describe the bug I have created an extension to try and detect runs of newlines but neither source function nor tokenizer function ever seem to get called.

I am using like:

export class Markdown {
  #marked = new Marked().use({
    extensions: [newlineExtension]
  });

  // other stuff in my class
}

To Reproduce

  1. Define your extension as follows:
export const newlineExtension: TokenizerExtension = {
    name: 'newline',
    level: 'block',
    start(src) {
        return src.match(/[\n]/)?.index
    },
    tokenizer(src, tokens) {
        const match = src.match(/^[\n]{2,}$/);
        if (match && match.length > 0) {
            return {
                type: 'newline',
                raw: match[0]
            }
        }
        return undefined;
    }
};
  1. Pass your extension on to marked:
export class Markdown {
  #marked = new Marked().use({
    extensions: [newlineExtension]
  });

  // other stuff in my class
}
  1. Pass the following string in:
const multiLine = `
First line





Second line
`

In this case I am still getting space with raw: '\n\n\n\n\n\n\n' as the token type.

Expected behavior I would expect my extension function(s) to get called a single newline token to get created

nrivard avatar Aug 23 '24 13:08 nrivard