marked icon indicating copy to clipboard operation
marked copied to clipboard

Cannot use a custom renderer to override the link renderer.

Open creeper-0910 opened this issue 2 years ago • 23 comments

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.

creeper-0910 avatar Aug 02 '24 14:08 creeper-0910

It seems to work fine in v12, so I assume this problem is due to the new renderer.

creeper-0910 avatar Aug 02 '24 14:08 creeper-0910

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

sernaferna avatar Aug 02 '24 16:08 sernaferna

Have you tried using the parser to parse the text as shown in the docs?

UziTech avatar Aug 03 '24 02:08 UziTech

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.

creeper-0910 avatar Aug 03 '24 03:08 creeper-0910

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>`;

UziTech avatar Aug 03 '24 04:08 UziTech

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;

creeper-0910 avatar Aug 03 '24 04:08 creeper-0910

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)

sevenc-nanashi avatar Aug 03 '24 09:08 sevenc-nanashi

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.

UziTech avatar Aug 03 '24 22:08 UziTech

It would be good to have a way to do super somehow

@sevenc-nanashi if you can create a PR that would be great 😃 👍

UziTech avatar Aug 03 '24 22:08 UziTech

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.

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>

image

creeper-0910 avatar Aug 04 '24 04:08 creeper-0910

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

UziTech avatar Aug 04 '24 04:08 UziTech

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?

creeper-0910 avatar Aug 04 '24 04:08 creeper-0910

No because list is a block element. Marked.parseInline only parses inline elements.

UziTech avatar Aug 04 '24 04:08 UziTech

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...?

creeper-0910 avatar Aug 04 '24 05:08 creeper-0910

marked.parse will parse both

UziTech avatar Aug 04 '24 05:08 UziTech

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.

creeper-0910 avatar Aug 04 '24 05:08 creeper-0910

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

woss avatar Aug 04 '24 09:08 woss

@creeper-0910 @woss To be able to help I will need to know

  1. what extensions and versions you are using
  2. What options you use
  3. What is the input markdown
  4. What is the expected html

UziTech avatar Aug 04 '24 17:08 UziTech

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 avatar Aug 04 '24 17:08 creeper-0910

@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)"));

UziTech avatar Aug 04 '24 20:08 UziTech