Skip to content

Commit

Permalink
Merge pull request #34 from danishcake/task/33-only-first-code-tag-ge…
Browse files Browse the repository at this point in the history
…ts-hljs-class

fix: Add 'hljs' class to all <code> tags, not just the first
  • Loading branch information
valeriangalliat authored Sep 18, 2024
2 parents cafa778 + 153e4ea commit e9fa48e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ function highlightAuto (md: MarkdownIt, hljs: HLJSApi, ignoreIllegals: boolean,
function wrapCodeRenderer (renderer: Renderer.RenderRule): Renderer.RenderRule {
return function wrappedRenderer (...args) {
return renderer(...args)
.replace('<code class="', '<code class="hljs ')
.replace('<code>', '<code class="hljs">')
.replace(/<code class="/g, '<code class="hljs ')
.replace(/<code>/g, '<code class="hljs">')
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,24 @@ describe('markdown-it-highlightjs', () => {
md().use(highlightjs, { inline: true }).use(attrs).renderInline('`console.log(42)`{.js}'),
'<code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">42</span>)</code>')
})

it('applies hljs class to all code tags', () => {
// Define a mock plugin that handles the 'multicode' language
const multicodeHlJsPlugin = (md) => {
const oldHighlight = md.options.highlight
md.options.highlight = (code, lang, attrs) => {
if (lang && lang === 'multicode') {
return '<pre><code class="language-multicode">A</code><code class="language-multicode-overlay">B</code></pre>'
}

return oldHighlight(code, lang, attrs)
}
}

const markdownIt = md()
markdownIt.use(highlightjs)
markdownIt.use(multicodeHlJsPlugin)

assert.equal(markdownIt.render('```multicode\n```'), '<pre><code class="hljs language-multicode">A</code><code class="hljs language-multicode-overlay">B</code></pre>\n')
})
})

0 comments on commit e9fa48e

Please sign in to comment.