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

This commit is contained in:
Tony Brix 2023-11-10 00:41:04 -07:00 committed by GitHub
parent daa24eeca8
commit f9d08cc1ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -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;
@ -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 };

View File

@ -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');
});
});