Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array-based className handling to avoid comma separation in child elements #16

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/mapChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { createElement } from 'react'
function mapChild (child, i, depth) {
if (child.tagName) {
const props = Object.assign({ key: 'lo-' + depth + '-' + i }, child.properties)

if (Array.isArray(props.className)) {
props.className = props.className.join(' ')
}

const children = child.children ? child.children.map(mapWithDepth(depth + 1)) : null

return createElement(child.tagName, props, children)
Expand Down
8 changes: 8 additions & 0 deletions test/Lowlight.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ describe('react-lowlight', () => {
].join('')
)
})

it('should properly join array-based class names in child elements', () => {
const code = 'function foo() {}'

expect(render({ value: code, language: 'js' }, { withWrapper: true })).toBe(
'<pre class="lowlight"><code class="hljs js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) {}</code></pre>'
)
})
})

function render (props, options = {}) {
Expand Down
Loading