diff --git a/components/notes/InlineMarkdownEditor.test.tsx b/components/notes/InlineMarkdownEditor.test.tsx index 80c192d07..17286b6bb 100644 --- a/components/notes/InlineMarkdownEditor.test.tsx +++ b/components/notes/InlineMarkdownEditor.test.tsx @@ -419,3 +419,12 @@ test("note preview uses MDX readOnly with code-copy chrome", () => { /\.netcatty-mdx-editor\s+\[class\*="_codeMirrorToolbar_"\][^{]*svg\s*\{[^}]*width:\s*10px\s*!important;/s, ); }); + +test("annotateMathFormulaBlocks handles empty math blocks and avoids reading toolbar text", () => { + const source = readFileSync(new URL("./InlineMarkdownEditor.tsx", import.meta.url), "utf8"); + + assert.match(source, /export const annotateMathFormulaBlocks/); + assert.match(source, /const text = getCodeMirrorBlockText\(wrapper\)\.trim\(\);/); + assert.match(source, /if \(!formulaSource\) \{/); + assert.match(source, /existingPreview\.remove\(\)/); +}); diff --git a/components/notes/InlineMarkdownEditor.tsx b/components/notes/InlineMarkdownEditor.tsx index 95982aeb8..60ba0dde4 100644 --- a/components/notes/InlineMarkdownEditor.tsx +++ b/components/notes/InlineMarkdownEditor.tsx @@ -495,8 +495,21 @@ export const getCodeMirrorBlockText = (wrapper: Element): string => { .join("\n"); } - const content = wrapper.querySelector(".cm-content"); - return content?.textContent?.replace(/\u00a0/g, " ") ?? ""; + const cmContent = wrapper.querySelector(".cm-content"); + if (cmContent) { + return cmContent.textContent?.replace(/\u00a0/g, " ") ?? ""; + } + + const codeEl = wrapper.querySelector("code"); + if (codeEl) { + return codeEl.textContent?.replace(/\u00a0/g, " ") ?? ""; + } + + if (wrapper.tagName.toLowerCase() === "pre") { + return wrapper.textContent?.replace(/\u00a0/g, " ") ?? ""; + } + + return ""; }; const clearNoteCodeBlockCopyResetTimer = (button: HTMLElement): void => { @@ -547,10 +560,7 @@ function createCopyButton( event.stopPropagation(); void (async () => { clearNoteCodeBlockCopyResetTimer(button); - let text = getCodeMirrorBlockText(wrapper); - if (!text) { - text = wrapper.querySelector("code")?.textContent || wrapper.textContent || ""; - } + const text = getCodeMirrorBlockText(wrapper); const ok = await onCopy(text); if (!ok) { toast.error(copyFailedLabel); @@ -691,11 +701,7 @@ export const annotateMathFormulaBlocks = (container: HTMLElement, editorMode: st lang.includes("language-latex") || lang.includes("language-tex"); - let text = getCodeMirrorBlockText(wrapper); - if (!text) { - text = wrapper.querySelector("code")?.textContent || wrapper.textContent || ""; - } - text = text.trim(); + const text = getCodeMirrorBlockText(wrapper).trim(); const isDollarMath = text.startsWith("$$") && text.endsWith("$$") && text.length > 4; if (!isMathBlock && !isDollarMath) { @@ -706,7 +712,12 @@ export const annotateMathFormulaBlocks = (container: HTMLElement, editorMode: st } const formulaSource = isDollarMath ? text.slice(2, -2).trim() : text; - if (!formulaSource) return; + if (!formulaSource) { + const existingPreview = wrapper.querySelector(".netcatty-math-formula-preview"); + if (existingPreview) existingPreview.remove(); + wrapper.classList.remove("netcatty-math-reading-mode"); + return; + } let preview = wrapper.querySelector(".netcatty-math-formula-preview") as HTMLElement | null; if (!preview) {