From bfdda6848355f2e476dc758169a91f77cc9203c0 Mon Sep 17 00:00:00 2001 From: hazre Date: Fri, 27 Mar 2026 11:05:32 +0100 Subject: [PATCH] fix: handle whitespace in code block language lookup --- src/app/plugins/react-custom-html-parser.test.tsx | 2 +- src/app/plugins/react-custom-html-parser.tsx | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app/plugins/react-custom-html-parser.test.tsx b/src/app/plugins/react-custom-html-parser.test.tsx index 00fb0b2c..0ecd248f 100644 --- a/src/app/plugins/react-custom-html-parser.test.tsx +++ b/src/app/plugins/react-custom-html-parser.test.tsx @@ -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( - `
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}
` + `
\n  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}\n
` ); expect(screen.getByText('rust')).toBeInTheDocument(); diff --git a/src/app/plugins/react-custom-html-parser.tsx b/src/app/plugins/react-custom-html-parser.tsx index c4698d34..0f1c54c9 100644 --- a/src/app/plugins/react-custom-html-parser.tsx +++ b/src/app/plugins/react-custom-html-parser.tsx @@ -244,8 +244,8 @@ const getCodeBlockLanguage = ( children: ChildNode[], attribs?: Record ): 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 (