Skip to content

fix: fix instance options sent to lexer and parser #3073

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

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ export class Marked {
parseInline = this.#parseMarkdown(_Lexer.lexInline, _Parser.parseInline);

Parser = _Parser;
parser = _Parser.parse;
Renderer = _Renderer;
TextRenderer = _TextRenderer;
Lexer = _Lexer;
lexer = _Lexer.lex;
Tokenizer = _Tokenizer;
Hooks = _Hooks;

Expand Down Expand Up @@ -232,6 +230,14 @@ export class Marked {
return this;
}

lexer(src: string, options?: MarkedOptions) {
return _Lexer.lex(src, options ?? this.defaults);
}

parser(tokens: Token[], options?: MarkedOptions) {
return _Parser.parse(tokens, options ?? this.defaults);
}

#parseMarkdown(lexer: (src: string, options?: MarkedOptions) => TokensList | Token[], parser: (tokens: Token[], options?: MarkedOptions) => string) {
return (src: string, options?: MarkedOptions | undefined | null): string | Promise<string> => {
const origOpt = { ...options };
Expand Down
15 changes: 15 additions & 0 deletions test/unit/instance-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,19 @@ describe('Marked', () => {
expect(marked2.parse('# header')).toBe('im marked2');
expect(marked.parse('# header')).toBe('<h1>header</h1>\n');
});

it('should pass defaults to lexer and parser', () => {
const marked1 = new Marked();
marked1.use({
renderer: {
heading() {
return 'test';
}
}
});
const tokens = marked1.lexer('# hi');
const html = marked1.parser(tokens);

expect(html).toBe('test');
});
});