From 8f1add6059c5efa72232d4bc060c6a5e1acb74e5 Mon Sep 17 00:00:00 2001 From: Ajay Bura <32841439+ajbura@users.noreply.github.com> Date: Sun, 15 Mar 2026 15:37:14 +1100 Subject: [PATCH] fix: prevent codeblock filename drop on edit (#2780) prevent codeblock filename drop on edit --- src/app/components/editor/input.ts | 10 +++++++--- src/app/plugins/markdown/block/rules.ts | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/app/components/editor/input.ts b/src/app/components/editor/input.ts index ad314add..4b6df40f 100644 --- a/src/app/components/editor/input.ts +++ b/src/app/components/editor/input.ts @@ -228,9 +228,13 @@ const parseCodeBlockNode = (node: Element): CodeBlockElement[] | ParagraphElemen children: [{ text }], })); const childCode = node.children[0]; - const className = - isTag(childCode) && childCode.tagName === 'code' ? childCode.attribs.class ?? '' : ''; - const prefix = { text: `${mdSequence}${className.replace('language-', '')}` }; + const attribs = + isTag(childCode) && childCode.tagName === 'code' ? childCode.attribs : undefined; + const languageClass = attribs?.class; + const customLabel = attribs?.['data-label']; + const prefix = { + text: `${mdSequence}${customLabel ?? languageClass?.replace('language-', '') ?? ''}`, + }; const suffix = { text: mdSequence }; return [ { type: BlockType.Paragraph, children: [prefix] }, diff --git a/src/app/plugins/markdown/block/rules.ts b/src/app/plugins/markdown/block/rules.ts index e0a727ee..ad1af37e 100644 --- a/src/app/plugins/markdown/block/rules.ts +++ b/src/app/plugins/markdown/block/rules.ts @@ -18,7 +18,7 @@ export const CodeBlockRule: BlockMDRule = { const [, g1, g2] = match; // use last identifier after dot, e.g. for "example.json" gets us "json" as language code. const langCode = g1 ? g1.substring(g1.lastIndexOf('.') + 1) : null; - const filename = g1 != langCode ? g1 : null; + const filename = g1 !== langCode ? g1 : null; const classNameAtt = langCode ? ` class="language-${langCode}"` : ''; const filenameAtt = filename ? ` data-label="${filename}"` : ''; return `
${g2}`;