Marked: id attribute in heading is not show in the right way when they contains some Chinese

Created on 31 Aug 2018  ·  5Comments  ·  Source: markedjs/marked

I have a markdown string contains some Chinese like this:

# 中文English中文

use marked I got this:

<h1 id="-Englist-">中文English中文</h1>
````
I wish I cloud get this:

中文English中文

```
id attribute in headings (h1,h2,h3,etc) is not show in the right way

L2 - annoying headings

Most helpful comment

I also encountered this problem, try custom renderer

  const renderer = new marked.Renderer();
  renderer.heading = (text, level, raw) => {
    const id = raw.toLowerCase().replace(/[^a-zA-Z0-9\u4e00-\u9fa5]+/g, '-');
    return `<h${level} id=${id}>${text}</h${level}>\n`;
  };

  const result = marked(src, { renderer });

All 5 comments

Related: #1280

It looks like github-slugger could be used to solve this issue too.

I also encountered this problem, try custom renderer

  const renderer = new marked.Renderer();
  renderer.heading = (text, level, raw) => {
    const id = raw.toLowerCase().replace(/[^a-zA-Z0-9\u4e00-\u9fa5]+/g, '-');
    return `<h${level} id=${id}>${text}</h${level}>\n`;
  };

  const result = marked(src, { renderer });

@jinasonlin
It works!Thank you!

Closing as resolved.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

raguay picture raguay  ·  4Comments

camwiegert picture camwiegert  ·  4Comments

priyesh-diukar picture priyesh-diukar  ·  3Comments

amejiarosario picture amejiarosario  ·  3Comments

pigtooter picture pigtooter  ·  4Comments