Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse function requires provideLexer and provideParser hooks to be defined when using custom hooks #3641

Open
lukasmatta opened this issue Mar 17, 2025 · 1 comment

Comments

@lukasmatta
Copy link

Marked version:
15.0.7

Describe the bug
When providing only subset of hooks it fails on:

const lexer = opt.hooks ? opt.hooks.provideLexer() : (blockType ? _Lexer.lex : _Lexer.lexInline);
(if hooks is defined it expects provideLexer and provideParser functions to be defined as well)

To Reproduce

import { marked } from "marked";

const options = {
  hooks: {
    postprocess: (html) => html,
  },
};

// Run marked
console.log(marked.parse("# Test", options)); // TypeError: opt.hooks.provideLexer is not a function

Note that when providing options using the use function, it works:

import { marked } from "marked";

const options = {
  hooks: {
    postprocess: (html) => html,
  },
};

marked.use(options);

console.log(marked("# Test")); // <h1>Test</h1>

Expected behavior
I think it should be possible to provide only subset of hooks when using parse function as well.

lukasmatta added a commit to AbsaOSS/cps-mdoc-viewer that referenced this issue Mar 17, 2025
@UziTech
Copy link
Member

UziTech commented Mar 17, 2025

We really want to get away from people adding options to parse. The same thing will happen if you try to pass a partial tokenizer or renderer object to parse. The reason is that the logic in marked.use to combine all of these partial objects and extensions is very slow so we want people to do it once at the beginning rather than in a loop when they call parse on different markdown strings.

If your goal is to pass different hooks depending on the markdown string you can use different marked instances

import { Marked } from 'marked';

const marked1 = new Marked(someOptions);
const marked2 = new Marked(otherOptions);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants