Skip to content

Commit

Permalink
Merge pull request #61 from timlrx/fix/default-language-class
Browse files Browse the repository at this point in the history
Fix - add class info for default language
  • Loading branch information
timlrx authored Jun 25, 2023
2 parents 2bd23c6 + 1afe3ce commit f1555e1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,14 @@ const rehypePrismGenerator = (refractor) => {
} else {
node.properties.className = []
}

let lang = getLanguage(node)
// If no language is set on the code block, use defaultLanguage if specified
if (!lang && options.defaultLanguage) {
lang = options.defaultLanguage
node.properties.className.push(`language-${lang}`)
}
node.properties.className.push('code-highlight')
const lang = getLanguage(node) || options.defaultLanguage

/** @type {Element} */
let refractorRoot
Expand Down
17 changes: 16 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,25 @@ test('with options.defaultLanguage, it adds the correct language class tag', ()
`,
{ defaultLanguage: 'py' }
)
const expected = dedent`<pre class="language-py"><code class="code-highlight"><span class="code-line">x <span class="token operator">=</span> <span class="token number">6</span></span></code></pre>`
const expected = dedent`<pre class="language-py"><code class="language-py code-highlight"><span class="code-line">x <span class="token operator">=</span> <span class="token number">6</span></span></code></pre>`
assert.is(result, expected)
})

test('defaultLanguage should produce the same syntax tree as if manually specified', () => {
const resultDefaultLanguage = processHtml(
dedent`
<pre><code>x = 6</code></pre>
`,
{ defaultLanguage: 'py' }
)
const resultManuallySpecified = processHtml(
dedent`
<pre><code class="language-py">x = 6</code></pre>
`
)
assert.is(resultDefaultLanguage, resultManuallySpecified)
})

test('throws error if options.defaultLanguage is not registered with refractor', () => {
assert.throws(
() =>
Expand Down

0 comments on commit f1555e1

Please sign in to comment.