Skip to content

Commit

Permalink
fix: fix instance options sent to lexer and parser (#3073)
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Nov 10, 2023
1 parent daa24ee commit f9d08cc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Instance.ts
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
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');
});
});

1 comment on commit f9d08cc

@vercel
Copy link

@vercel vercel bot commented on f9d08cc Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.