Cannot use a custom renderer to override the link renderer.
Marked version: v13.0.3 Describe the bug A clear and concise description of what the bug is.
To Reproduce Steps to reproduce the behavior: Override the link renderer with the following code.
const marked = new Marked();
const originalRenderer = new Renderer();
originalRenderer.parser = new Parser();
marked.use({
breaks: true,
useNewRenderer: true,
renderer: {
link({tokens, href, text}) {
console.log(`${tokens}`);
return `<a target="_blank" href="${href}">${text}</a>`;
},
},
});
Expected behavior A clear and concise description of what you expected to happen. The link renderer is overridden and target="_blank" is added to the html.
It seems to work fine in v12, so I assume this problem is due to the new renderer.
I'm having the same issue and it seems to have come up recently. My implementation was pretty much exactly the same as what's listed above, but for troubleshooting purposes I tried this (TypeScript):
export const LinksInNewWindow: Partial<Omit<Renderer, 'options'>> = {
link(tokens) {
const { href, raw, text, type, title } = tokens;
console.log('typeof tokens:', typeof tokens);
console.log('tokens:', tokens);
console.log('tokens.tokens:', tokens.tokens);
console.log('href:', href);
console.log('raw:', raw);
console.log('text:', text);
console.log('type:', type);
console.log('title:', title);
return `<a href="${href}" title="${title}" target="_blank">${text}</a>`;
},
};
Here's what I saw at the console, when using markdown like this: [click here](https://www.google.ca):
typeof tokens: string
tokens: https://www.google.ca
tokens.tokens: undefined
href: undefined
raw: undefined
text: undefined
type: undefined
title: undefined
Have you tried using the parser to parse the text as shown in the docs?
Have you tried using the parser to parse the text as shown in the docs?
Is the code you provided as an example not enough? The heading renderer, can be successfully overridden with the same code, but only the link renderer seems to be unable to be successfully overridden.
import { Marked, Renderer, Parser } from 'marked';
const marked = new Marked();
const originalRenderer = new Renderer();
originalRenderer.parser = new Parser();
marked.use({
breaks: true,
useNewRenderer: true,
renderer: {
link({ tokens, href, text }) {
return `<a target="_blank" href="${href}">${text}</a>`;
},
},
});
console.log(marked.parse('[link](https://example.com)'));
// <p><a target="_blank" href="https://example.com">link</a></p>
seems to work for me. But if you want markdown formatting in the text you will need to use the parser like in the docs
return `<a target="_blank" href="${href}">${this.parser.parseInline(tokens)}</a>`;
There seems to be a problem with the code you are currently using... If I override renderer.text and renderer.link and then use bullet points and links together, it does not seem to work correctly.
const marked = new Marked();
const originalRenderer = new Renderer();
originalRenderer.parser = new Parser();
marked.use({
breaks: true,
useNewRenderer: true,
renderer: {
// headingを一段階下げる
heading({ text, depth }) {
return `<h${depth + 1}>${text}</h${depth + 1}>`;
},
// budouxを適用する
text(token) {
const html = originalRenderer.text(token);
return `<budoux-ja>${html}</budoux-ja>`;
},
link({ tokens, href, text }) {
return `<a target="_blank" href="${href}">${text}</a>`;
},
},
});
const html = marked.parse(`
- [example](https://example.com)
`) as string;
I think the problem is that @creeper-0910 san's text uses originalRenderer, in other words, renderer without custom link to generate htmls inside.
It would be good to have a way to do super somehow, for use cases such as adding some tags around html (budoux, which adds <wbr>s to Japanese/Chinese texts, is a good example)
There seems to be a problem with the code you are currently using...
That is the same code from your first example.
If I override renderer.text and renderer.link and then use bullet points and links together, it does not seem to work correctly.
That is because you are not using parseInline for the text like the docs show.
It would be good to have a way to do super somehow
@sevenc-nanashi if you can create a PR that would be great 😃 👍
There seems to be a problem with the code you are currently using...
That is the same code from your first example.
If I override renderer.text and renderer.link and then use bullet points and links together, it does not seem to work correctly.
That is because you are not using
parseInlinefor the text like the docs show.
If parseInline is used, the output will look like the following and cannot be used because bullets will not work.
<budoux-ja>- </budoux-ja><a target="_blank" href="https://example.com/">link</a>
if parseInline is used, the output will look like the following and cannot be used because bullets will not work.
No using this.parser.parseInline in the renderer functions like the docs show not marked.parseinline
if parseInline is used, the output will look like the following and cannot be used because bullets will not work.
No using this.parser.parseInline in the renderer functions like the docs show not marked.parseinline
I am using marked.perseinline, is this not the correct way?
No because list is a block element. Marked.parseInline only parses inline elements.
No because list is a block element. Marked.parseInline only parses inline elements.
So what should we do if we want to parse both elements...?
marked.parse will parse both
So how can I use marked.parser to parse correctly when using renderer.text and renderer.link...? I would be happy to have an example. This may be a little too difficult for me.
I've spent 1h today moving from 9->13 and the link override doesn't work. When I run the override functions and console log input params they are all undefined
@creeper-0910 @woss To be able to help I will need to know
- what extensions and versions you are using
- What options you use
- What is the input markdown
- What is the expected html
1.marked: 13.0.3,No extensions are used. 2.
marked.use({
breaks: true,
useNewRenderer: true,
renderer: {
// headingを一段階下げる
heading({ text, depth }) {
return `<h${depth + 1}>${text}</h${depth + 1}>`;
},
// budouxを適用する
text(token) {
const html = originalRenderer.text(token);
return `<budoux-ja>${html}</budoux-ja>`;
},
link({ tokens, href, text }) {
return `<a target="_blank" href="${href}">${text}</a>`;
},
},
});
3.- [link](https://example.com)
4.
<budoux-ja>
<ul>
<li><a target="_blank" href="https://example.com/">link</a></li>
</ul>
</budoux-ja>
@creeper-0910 this should work. mostly changing text renderer for list renderer.
import { Marked, Renderer } from 'marked';
const marked = new Marked();
const originalRenderer = new Renderer();
marked.use({
breaks: true,
useNewRenderer: true,
renderer: {
// headingを一段階下げる
heading({ tokens, depth }) {
const text = this.parser.parseInline(tokens);
return `<h${depth + 1}>${text}</h${depth + 1}>`;
},
// budouxを適用する
list(token) {
const html = originalRenderer.list.call(this, token);
return `<budoux-ja>${html}</budoux-ja>`;
},
link({ tokens, href }) {
const text = this.parser.parseInline(tokens);
return `<a target="_blank" href="${href}">${text}</a>`;
},
},
});
console.log(marked.parse("- [link](https://example.com)"));