markdown-it-highlightjs icon indicating copy to clipboard operation
markdown-it-highlightjs copied to clipboard

Only the first codeblock is has hljs class added to it

Open danishcake opened this issue 5 months ago • 1 comments

Typical highlightjs output has the structure

<pre>
<code>
</code>
</pre>

This extension adds the 'hljs' class to the code tags using the wrapCodeRenderer.

There's nothing stopping you from having multiple code blocks inside the <pre> tags. It's a bit odd, but I did it recently to overlay a SVG on top of some hex. In this case, hljs is only added to the first block, and this means the second code block doesn't receive the same styles.

This can be fixed by changing

  return function wrappedRenderer (...args) {
    return renderer(...args)
      .replace('<code class="', '<code class="hljs ')
      .replace('<code>', '<code class="hljs">')
  }

to

  return function wrappedRenderer (...args) {
    return renderer(...args)
      .replaceAll('<code class="', '<code class="hljs ')
      .replaceAll('<code>', '<code class="hljs">')
  }

danishcake avatar Sep 18 '24 05:09 danishcake