fix: handle whitespace in code block language lookup

This commit is contained in:
hazre committed 2026-03-27 12:31:55 +01:00
1 parent f804f07a3c
commit bfdda68483
2 files changed
+4 -4

No files matched your search

@@ -51,7 +51,7 @@ const renderMessage = (html: string) =>
describe('getReactCustomHtmlParser code blocks', () => {
it('renders the Arborium renderer inside the existing code block shell for explicit data-lang metadata', () => {
renderMessage(
`<pre><code data-lang="rust">fn main() {\nlet value = 1;\nlet next = 2;\nlet third = 3;\nlet fourth = 4;\nlet fifth = 5;\nlet sixth = 6;\nlet seventh = 7;\nlet eighth = 8;\nlet ninth = 9;\nlet tenth = 10;\nlet eleventh = 11;\nlet twelfth = 12;\nlet thirteenth = 13;\nlet fourteenth = 14;\nlet fifteenth = 15;\n}</code></pre>`
`<pre>\n <code data-lang="rust">fn main() {\nlet value = 1;\nlet next = 2;\nlet third = 3;\nlet fourth = 4;\nlet fifth = 5;\nlet sixth = 6;\nlet seventh = 7;\nlet eighth = 8;\nlet ninth = 9;\nlet tenth = 10;\nlet eleventh = 11;\nlet twelfth = 12;\nlet thirteenth = 13;\nlet fourteenth = 14;\nlet fifteenth = 15;\n}</code>\n</pre>`
);
expect(screen.getByText('rust')).toBeInTheDocument();
+3 -3
View File
@@ -244,8 +244,8 @@ const getCodeBlockLanguage = (
children: ChildNode[],
attribs?: Record<string, string | undefined>
): string | undefined => {
const code = children[0];
const codeAttribs = code instanceof Element && code.name === 'code' ? code.attribs : undefined;
const code = children.find((child) => child instanceof Element && child.name === 'code');
const codeAttribs = code instanceof Element ? code.attribs : undefined;
return (
codeAttribs?.['data-lang'] ??
@@ -477,7 +477,7 @@ export const getReactCustomHtmlParser = (
if (name === 'code') {
if (parent && 'name' in parent && parent.name === 'pre') {
const language = getCodeBlockLanguage(
[domNode],
parent instanceof Element ? parent.children : [],
parent instanceof Element ? parent.attribs : undefined
);
return (