From f9d08cc1ef5294b1866705860242f78528f8d508 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Fri, 10 Nov 2023 00:41:04 -0700 Subject: [PATCH] fix: fix instance options sent to lexer and parser (#3073) --- src/Instance.ts | 10 ++++++++-- test/unit/instance-spec.js | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Instance.ts b/src/Instance.ts index 9012da42f5..d2d52745cd 100644 --- a/src/Instance.ts +++ b/src/Instance.ts @@ -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 => { const origOpt = { ...options }; diff --git a/test/unit/instance-spec.js b/test/unit/instance-spec.js index bfcbd00ee9..f4c12232a0 100644 --- a/test/unit/instance-spec.js +++ b/test/unit/instance-spec.js @@ -72,4 +72,19 @@ describe('Marked', () => { expect(marked2.parse('# header')).toBe('im marked2'); expect(marked.parse('# header')).toBe('

header

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