Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
fix(ok): clipboard source fallback for dollar and fence block math (#2878)
Nick Gomez295bddf270c7
diff --git a/.changeset/clipboard-dollar-math-source-fallback.md b/.changeset/clipboard-dollar-math-source-fallback.mdnew file mode 100644index 000000000..c7d1741c5--- /dev/null+++ b/.changeset/clipboard-dollar-math-source-fallback.md@@ -0,0 +1,7 @@+---+"@inkeep/open-knowledge": patch+---++Copy readable LaTeX source for block math authored as `$$…$$` or ```math.++Copying block math from the WYSIWYG editor now writes readable LaTeX source (`$$\nformula\n$$`) to the `text/html` clipboard flavor for math authored as `$$…$$` or a ```math fence, matching slash-menu-authored math. Previously only slash-menu math got the source fallback while dollar- and fence-authored math (the dominant on-disk forms) pasted a non-portable KaTeX style clone into rich destinations like Gmail, Google Docs, and Notion. Block math that was previously dropped from the clipboard entirely in some editor states is now included as the same readable source too.diff --git a/packages/app/package.json b/packages/app/package.jsonindex 2b5cdc9fa..8c32f53bf 100644--- a/packages/app/package.json+++ b/packages/app/package.json@@ -26,7 +26,7 @@"measure:fuzz": "bash scripts/measure-fuzz.sh","measure:stress": "bash scripts/measure-stress.sh","perf:compare": "bash scripts/perf-compare.sh",- "test:e2e": "playwright test tests/stress/ux-interactions.e2e.ts tests/stress/file-tree-create.e2e.ts tests/stress/file-tree-drag-to-root.e2e.ts tests/stress/file-tree-deselect-to-root.e2e.ts tests/stress/create-then-rename-editable.e2e.ts tests/stress/find-replace.e2e.ts tests/stress/editor-tabs.e2e.ts tests/stress/crdt-stress.e2e.ts tests/stress/slash-command.e2e.ts tests/stress/paste-fidelity.e2e.ts tests/stress/fr-7a-disconnect-source-mode.e2e.ts tests/stress/docs-open.e2e.ts tests/stress/frozen-table-headers.e2e.ts tests/stress/asset-embed.e2e.ts tests/stress/asset-embed-advanced.e2e.ts tests/stress/asset-click-dispatch.e2e.ts tests/stress/handoff.e2e.ts tests/stress/multi-agent-presence.e2e.ts tests/stress/editor-mode-persistence.e2e.ts tests/stress/lint-config-toggle.e2e.ts tests/stress/sidebar-search-pill.e2e.ts tests/stress/command-palette-semantic.e2e.ts tests/stress/agent-activity-panel.e2e.ts tests/stress/drop-pipeline-auto-open.e2e.ts tests/stress/command-palette-flicker.e2e.ts tests/stress/cm6-list-hanging-indent.e2e.ts tests/stress/ng7-rapid-nav-coherence.e2e.ts tests/stress/reveal-on-activate.e2e.ts tests/stress/selection-indicator.e2e.ts tests/stress/new-file-cross-doc-bleed.e2e.ts tests/stress/editor-mode-flip-cross-doc-bleed.e2e.ts tests/stress/editor-area-viewport-resize.e2e.ts tests/stress/qa-sidebar-responsive.e2e.ts tests/stress/prd-6955-reassertion-repro.e2e.ts tests/stress/prd-6955-reassertion-wedge.e2e.ts tests/stress/prd-6914-repro.e2e.ts tests/stress/showall-lazy-tree.e2e.ts tests/stress/tabs-component-strip.e2e.ts tests/stress/source-find-scroll.e2e.ts tests/stress/harness-app-warmth.e2e.ts tests/stress/qa-canary-authoring-both-modes.e2e.ts tests/stress/qa-canary-live-typing.e2e.ts tests/stress/keystroke-cadence-danger-space.e2e.ts tests/stress/jsx-unregistered-backspace-delete.e2e.ts tests/stress/jsx-unregistered-ime-concurrent.e2e.ts tests/stress/jsx-backspace-delete.e2e.ts tests/stress/link-authoring-bytes.e2e.ts tests/stress/link-authoring-apex.e2e.ts tests/stress/command-palette-parity.e2e.ts tests/stress/settings-search.e2e.ts tests/stress/chip-popover-positioning.e2e.ts tests/stress/docs-open-scroll-restore.e2e.ts tests/stress/file-tree-decoration-chip-strip-repro.e2e.ts tests/stress/grip-click-nodeselect.e2e.ts tests/stress/jsx-halo-semantics.e2e.ts tests/stress/jsx-image-body-click.e2e.ts tests/stress/jsx-prop-panel-placeholder.e2e.ts tests/stress/jsx-range-encompass-halo.e2e.ts tests/stress/jsx-wildcard-convert.e2e.ts tests/stress/markdown-lint.e2e.ts tests/stress/unified-problems.e2e.ts tests/stress/mid-type-recovery.e2e.ts tests/stress/observer-a-multi-client.e2e.ts tests/stress/outline-toolbar-occlusion.e2e.ts tests/stress/quiet-tree-hidden-doc.e2e.ts tests/stress/raw-mdx-fallback-onblur-upgrade.e2e.ts tests/stress/rename-noext-probe.e2e.ts tests/stress/show-ok-folders.e2e.ts tests/stress/source-polish.e2e.ts tests/stress/warm-skeleton-scroll-restore.e2e.ts tests/stress/props-toggle-scroll-preserve.e2e.ts tests/stress/yjs-no-dual-import.e2e.ts tests/stress/slash-command-auto-open.e2e.ts tests/stress/graph-panel-surfaces.e2e.ts tests/stress/clipboard-relative-url-source-fallback.e2e.ts tests/stress/asset-embed-real-fidelity.e2e.ts tests/stress/prop-upload.e2e.ts tests/stress/rename-content-preservation.e2e.ts tests/stress/rename-consolidation.e2e.ts tests/stress/outline-navigation.e2e.ts",+ "test:e2e": "playwright test tests/stress/ux-interactions.e2e.ts tests/stress/file-tree-create.e2e.ts tests/stress/file-tree-drag-to-root.e2e.ts tests/stress/file-tree-deselect-to-root.e2e.ts tests/stress/create-then-rename-editable.e2e.ts tests/stress/find-replace.e2e.ts tests/stress/editor-tabs.e2e.ts tests/stress/crdt-stress.e2e.ts tests/stress/slash-command.e2e.ts tests/stress/paste-fidelity.e2e.ts tests/stress/fr-7a-disconnect-source-mode.e2e.ts tests/stress/docs-open.e2e.ts tests/stress/frozen-table-headers.e2e.ts tests/stress/asset-embed.e2e.ts tests/stress/asset-embed-advanced.e2e.ts tests/stress/asset-click-dispatch.e2e.ts tests/stress/handoff.e2e.ts tests/stress/multi-agent-presence.e2e.ts tests/stress/editor-mode-persistence.e2e.ts tests/stress/lint-config-toggle.e2e.ts tests/stress/sidebar-search-pill.e2e.ts tests/stress/command-palette-semantic.e2e.ts tests/stress/agent-activity-panel.e2e.ts tests/stress/drop-pipeline-auto-open.e2e.ts tests/stress/command-palette-flicker.e2e.ts tests/stress/cm6-list-hanging-indent.e2e.ts tests/stress/ng7-rapid-nav-coherence.e2e.ts tests/stress/reveal-on-activate.e2e.ts tests/stress/selection-indicator.e2e.ts tests/stress/new-file-cross-doc-bleed.e2e.ts tests/stress/editor-mode-flip-cross-doc-bleed.e2e.ts tests/stress/editor-area-viewport-resize.e2e.ts tests/stress/qa-sidebar-responsive.e2e.ts tests/stress/prd-6955-reassertion-repro.e2e.ts tests/stress/prd-6955-reassertion-wedge.e2e.ts tests/stress/prd-6914-repro.e2e.ts tests/stress/showall-lazy-tree.e2e.ts tests/stress/tabs-component-strip.e2e.ts tests/stress/source-find-scroll.e2e.ts tests/stress/harness-app-warmth.e2e.ts tests/stress/qa-canary-authoring-both-modes.e2e.ts tests/stress/qa-canary-live-typing.e2e.ts tests/stress/keystroke-cadence-danger-space.e2e.ts tests/stress/jsx-unregistered-backspace-delete.e2e.ts tests/stress/jsx-unregistered-ime-concurrent.e2e.ts tests/stress/jsx-backspace-delete.e2e.ts tests/stress/link-authoring-bytes.e2e.ts tests/stress/link-authoring-apex.e2e.ts tests/stress/command-palette-parity.e2e.ts tests/stress/settings-search.e2e.ts tests/stress/chip-popover-positioning.e2e.ts tests/stress/docs-open-scroll-restore.e2e.ts tests/stress/file-tree-decoration-chip-strip-repro.e2e.ts tests/stress/grip-click-nodeselect.e2e.ts tests/stress/jsx-halo-semantics.e2e.ts tests/stress/jsx-image-body-click.e2e.ts tests/stress/jsx-prop-panel-placeholder.e2e.ts tests/stress/jsx-range-encompass-halo.e2e.ts tests/stress/jsx-wildcard-convert.e2e.ts tests/stress/markdown-lint.e2e.ts tests/stress/unified-problems.e2e.ts tests/stress/mid-type-recovery.e2e.ts tests/stress/observer-a-multi-client.e2e.ts tests/stress/outline-toolbar-occlusion.e2e.ts tests/stress/quiet-tree-hidden-doc.e2e.ts tests/stress/raw-mdx-fallback-onblur-upgrade.e2e.ts tests/stress/rename-noext-probe.e2e.ts tests/stress/show-ok-folders.e2e.ts tests/stress/source-polish.e2e.ts tests/stress/warm-skeleton-scroll-restore.e2e.ts tests/stress/props-toggle-scroll-preserve.e2e.ts tests/stress/yjs-no-dual-import.e2e.ts tests/stress/slash-command-auto-open.e2e.ts tests/stress/graph-panel-surfaces.e2e.ts tests/stress/clipboard-relative-url-source-fallback.e2e.ts tests/stress/asset-embed-real-fidelity.e2e.ts tests/stress/prop-upload.e2e.ts tests/stress/rename-content-preservation.e2e.ts tests/stress/rename-consolidation.e2e.ts tests/stress/outline-navigation.e2e.ts tests/stress/clipboard-dollar-math-source-fallback.e2e.ts","test:e2e:install-browsers": "playwright install chromium webkit firefox","test:visual": "playwright test --config playwright.visual.config.ts","test:visual:update": "playwright test --config playwright.visual.config.ts --update-snapshots",diff --git a/packages/app/src/editor/clipboard/clipboard-walker-fallback-palette.dom-shape.test.ts b/packages/app/src/editor/clipboard/clipboard-walker-fallback-palette.dom-shape.test.tsnew file mode 100644index 000000000..eefc5021e--- /dev/null+++ b/packages/app/src/editor/clipboard/clipboard-walker-fallback-palette.dom-shape.test.ts@@ -0,0 +1,73 @@+// @vitest-environment jsdom+/**+ * DOM-shape tests for the Activity-hidden fallback palette's block-math+ * dispatch. `paletteFor` builds real DOM (`document.createElement`), so this+ * file opts into the jsdom environment via the per-file docblock above. It+ * is NOT a Tier-3 React mount test (no component render, no+ * `@testing-library/react`), so it deliberately does not use the+ * `*.dom.test.tsx` suffix — that suffix routes the RTL mount substrate and+ * carries an RTL-import contract this file must not satisfy.+ *+ * Contract pinned here: block math authored as `$$…$$` (`DollarMath`) or+ * ` ```math ` (`MathFence`) is dispatched to the same source-fallback the+ * canonical `Math` produces — an Activity-hidden copy of dollar/fence math+ * must not silently drop the block.+ *+ * Tier responsibility: this tier proves `paletteFor`'s ROUTING and the+ * `<pre class="mdx-component"><code>` element shape. Byte-level pinning of+ * the `$$\nformula\n$$` source form lives in the no-DOM unit tier+ * (`non-portable-render-source-fallback.test.ts`); compat assertions here+ * are relative to the canonical `Math` output, not absolute byte literals.+ */++import type { Node as PmNode } from '@tiptap/pm/model';+import { describe, expect, test } from 'vitest';+import { paletteFor } from './clipboard-walker-fallback-palette.ts';++/**+ * Stub a top-level `jsxComponent` PM node matching `paletteFor`'s access+ * pattern (`type.name`, `attrs.componentName`, `attrs.props`). Deliberately+ * narrower than the sibling classifier tests' named-args `stubPmNode`+ * (non-portable-render-source-fallback.test.ts): this tier only ever stubs+ * `jsxComponent` nodes, so the type name is fixed and the args stay+ * positional.+ */+function stubPmNode(componentName: string, props: Record<string, unknown>): PmNode {+ return {+ type: { name: 'jsxComponent' },+ attrs: { componentName, props },+ } as unknown as PmNode;+}++/** Pure extractor — structural assertions live in each test body. */+function sourceText(el: Element): string | null {+ return el.querySelector('code')?.textContent ?? null;+}++describe('paletteFor — block-math authored forms', () => {+ test('canonical Math dispatches to the source-fallback pre/code (control)', () => {+ const el = paletteFor(stubPmNode('Math', { formula: 'E = mc^2' }));+ expect(el).not.toBeNull();+ expect(el?.tagName.toLowerCase()).toBe('pre');+ expect(el?.className).toBe('mdx-component');+ expect(sourceText(el as Element)).toBe('$$\nE = mc^2\n$$');+ });++ test('DollarMath ($$…$$ authored) dispatches like canonical Math', () => {+ const canonical = paletteFor(stubPmNode('Math', { formula: 'E = mc^2' }));+ const el = paletteFor(stubPmNode('DollarMath', { formula: 'E = mc^2' }));+ expect(el).not.toBeNull();+ expect(el?.tagName.toLowerCase()).toBe('pre');+ expect(el?.className).toBe('mdx-component');+ expect(sourceText(el as Element)).toBe(sourceText(canonical as Element));+ });++ test('MathFence (```math authored) dispatches like canonical Math', () => {+ const canonical = paletteFor(stubPmNode('Math', { formula: 'a^2 + b^2 = c^2' }));+ const el = paletteFor(stubPmNode('MathFence', { formula: 'a^2 + b^2 = c^2' }));+ expect(el).not.toBeNull();+ expect(el?.tagName.toLowerCase()).toBe('pre');+ expect(el?.className).toBe('mdx-component');+ expect(sourceText(el as Element)).toBe(sourceText(canonical as Element));+ });+});diff --git a/packages/app/src/editor/clipboard/clipboard-walker-fallback-palette.test.ts b/packages/app/src/editor/clipboard/clipboard-walker-fallback-palette.test.tsindex cfcb74fda..aab715474 100644--- a/packages/app/src/editor/clipboard/clipboard-walker-fallback-palette.test.ts+++ b/packages/app/src/editor/clipboard/clipboard-walker-fallback-palette.test.ts@@ -30,6 +30,7 @@ import {TYPE_TO_TONE,toneForType,} from './clipboard-walker-fallback-palette.ts';+import { nonPortableDescriptorNames } from './non-portable-descriptors.test-helper.ts';describe('PALETTE_DESCRIPTOR_NAMES — registry coverage', () => {test('covers every canonical descriptor', () => {@@ -60,7 +61,36 @@ describe('PALETTE_DESCRIPTOR_NAMES — registry coverage', () => {// Hard count anchor. If a descriptor is added or removed, this// failing test becomes the prompt to also update the palette switch// and PALETTE_DESCRIPTOR_NAMES together.- expect(PALETTE_DESCRIPTOR_NAMES.length).toBe(10);+ expect(PALETTE_DESCRIPTOR_NAMES.length).toBe(12);+ });+});++describe('PALETTE_DESCRIPTOR_NAMES — registry-derived non-portable coverage', () => {+ // The non-portable-render descriptors are the ones the source fallback+ // (`sourceFallbackFormFor`) canonicalizes: their live React render is+ // KaTeX / mermaid SVG, which pastes as garbage cross-app. The palette+ // path must cover every descriptor whose RENDER identity is one of those+ // canonicals, not just the canonically-named ones — a compat descriptor+ // authored as `$$…$$` (`DollarMath`) or ` ```math ` (`MathFence`) renders+ // as `<Math>` and so must resolve to a palette entry too. Deriving the+ // expectation from the registry (rather than a frozen literal list) is+ // what makes a newly-added non-portable compat descriptor fail loudly+ // instead of silently dropping out of the Activity-hidden payload.+ // Set + predicate are shared with the walker-path guard via+ // non-portable-descriptors.test-helper.ts.++ test('every descriptor that renders as a non-portable canonical has a palette name', () => {+ const descriptorNames = nonPortableDescriptorNames();++ // Sanity: the registry must actually carry the two math compat rows,+ // otherwise this test would vacuously pass.+ expect(descriptorNames).toEqual(+ expect.arrayContaining(['Math', 'MermaidFence', 'DollarMath', 'MathFence']),+ );++ for (const name of descriptorNames) {+ expect([...PALETTE_DESCRIPTOR_NAMES], name).toContain(name);+ }});});diff --git a/packages/app/src/editor/clipboard/clipboard-walker-fallback-palette.ts b/packages/app/src/editor/clipboard/clipboard-walker-fallback-palette.tsindex 5d524d462..a33d53d6c 100644--- a/packages/app/src/editor/clipboard/clipboard-walker-fallback-palette.ts+++ b/packages/app/src/editor/clipboard/clipboard-walker-fallback-palette.ts@@ -79,6 +79,11 @@ export const PALETTE_DESCRIPTOR_NAMES = [// same `<pre class="mdx-component">` shape regardless of whether the// slice was Activity-mounted or not.'Math',+ // Compat block-math descriptors that render as `<Math>`.+ 'DollarMath',+ 'MathFence',+ // MermaidFence: separate non-portable canonical (mermaid SVG), not a+ // Math compat.'MermaidFence',] as const;@@ -293,7 +298,15 @@ export function paletteFor(node: PmNode): Element | null {return videoPalette(props);case 'audio':return audioPalette(props);+ // `DollarMath` / `MathFence` are compat descriptors that render as+ // `<Math>` but keep their authored componentName; route them through+ // the same source-fallback so the Activity-hidden path doesn't drop+ // block math authored as `$$…$$` or ```math. `MermaidFence` is a+ // separate non-portable canonical (mermaid SVG) sharing the same+ // source-fallback path.case 'Math':+ case 'DollarMath':+ case 'MathFence':case 'MermaidFence':return nonPortableRenderSourceFallback(node, document);default:diff --git a/packages/app/src/editor/clipboard/non-portable-descriptors.test-helper.ts b/packages/app/src/editor/clipboard/non-portable-descriptors.test-helper.tsnew file mode 100644index 000000000..eb1e4da18--- /dev/null+++ b/packages/app/src/editor/clipboard/non-portable-descriptors.test-helper.ts@@ -0,0 +1,39 @@+/**+ * Shared derivation of the non-portable-render descriptor set, used by the+ * registry-derived completeness guards at BOTH clipboard dispatch sites+ * (`sourceFallbackFormFor` in non-portable-render-source-fallback.test.ts and+ * `PALETTE_DESCRIPTOR_NAMES` in clipboard-walker-fallback-palette.test.ts).+ * The two guards express one logical contract — every descriptor whose RENDER+ * identity is a non-portable canonical must have coverage at both sites — so+ * the canonical set and the filter predicate live here once, not as driftable+ * per-file copies.+ */++import { builtInComponents } from '@inkeep/open-knowledge-core';++/**+ * Canonicals whose live React render is KaTeX / mermaid SVG — the renders the+ * clipboard source fallback canonicalizes because they paste as garbage+ * cross-app. Extend this set when a new non-portable canonical is added to+ * the switch in `sourceFallbackFormFor` — the registry-derived guards depend+ * on this set being complete.+ */+const NON_PORTABLE_CANONICALS: ReadonlySet<string> = new Set(['Math', 'MermaidFence']);++/**+ * Every registry descriptor name whose render identity is a non-portable+ * canonical: the canonicals themselves plus every compat descriptor whose+ * `rendersAs` points at one (e.g. `DollarMath` / `MathFence` render as+ * `<Math>`).+ */+export function nonPortableDescriptorNames(): string[] {+ return builtInComponents+ .filter((meta) => {+ const rendersAs = 'rendersAs' in meta ? meta.rendersAs : undefined;+ return (+ NON_PORTABLE_CANONICALS.has(meta.name) ||+ (typeof rendersAs === 'string' && NON_PORTABLE_CANONICALS.has(rendersAs))+ );+ })+ .map((meta) => meta.name);+}diff --git a/packages/app/src/editor/clipboard/non-portable-render-source-fallback.test.ts b/packages/app/src/editor/clipboard/non-portable-render-source-fallback.test.tsindex d12ed06d0..e32e800c6 100644--- a/packages/app/src/editor/clipboard/non-portable-render-source-fallback.test.ts+++ b/packages/app/src/editor/clipboard/non-portable-render-source-fallback.test.ts@@ -7,17 +7,20 @@* without a DOM via `sourceFallbackFormFor` (the inner pure classifier).** Coverage tiers:- * 1. Block jsxComponents (Math, Mermaid) → expected markdown-source- * bytes+ * 1. Block jsxComponents (Math, DollarMath, MathFence, MermaidFence)+ * → expected markdown-source bytes* 2. Preview-active codeBlock → fenced-source bytes; non-preview → null* 3. Falls through (Callout, paragraph, heading, mathInline,* unknown jsxComponent) → null* 4. Edge cases — empty / missing / non-string props+ * 5. Registry-derived completeness — every descriptor rendering as a+ * non-portable canonical yields a source form*/import { MarkdownManager, sharedExtensions } from '@inkeep/open-knowledge-core';import type { Node as PmNode } from '@tiptap/pm/model';import { describe, expect, test } from 'vitest';+import { nonPortableDescriptorNames } from './non-portable-descriptors.test-helper.ts';import { sourceFallbackFormFor } from './non-portable-render-source-fallback.ts';/**@@ -137,6 +140,92 @@ describe('sourceFallbackFormFor — MermaidFence jsxComponent', () => {});});+describe('sourceFallbackFormFor — block-math compat authored forms (DollarMath / MathFence)', () => {+ // `$$…$$`-authored block math parses to a `DollarMath` jsxComponent and+ // ` ```math ` fences parse to `MathFence`; both `rendersAs: 'Math'` so+ // they paste as the same non-portable KaTeX render as canonical `Math`.+ // The source fallback must fire for all three authored forms — every+ // block-math node that renders as KaTeX owes the clipboard the same+ // readable `$$\nformula\n$$` bytes regardless of how it was authored.+ test('DollarMath emits the same `$$\\nformula\\n$$` source as canonical Math', () => {+ const dollar = stubPmNode({+ typeName: 'jsxComponent',+ componentName: 'DollarMath',+ props: { formula: 'E = mc^2' },+ });+ const canonical = stubPmNode({+ typeName: 'jsxComponent',+ componentName: 'Math',+ props: { formula: 'E = mc^2' },+ });+ expect(sourceFallbackFormFor(dollar)).toEqual({ source: '$$\nE = mc^2\n$$' });+ expect(sourceFallbackFormFor(dollar)).toEqual(sourceFallbackFormFor(canonical));+ });++ test('MathFence emits the same `$$\\nformula\\n$$` source as canonical Math', () => {+ const fence = stubPmNode({+ typeName: 'jsxComponent',+ componentName: 'MathFence',+ props: { formula: 'a^2 + b^2 = c^2' },+ });+ const canonical = stubPmNode({+ typeName: 'jsxComponent',+ componentName: 'Math',+ props: { formula: 'a^2 + b^2 = c^2' },+ });+ expect(sourceFallbackFormFor(fence)).toEqual({ source: '$$\na^2 + b^2 = c^2\n$$' });+ expect(sourceFallbackFormFor(fence)).toEqual(sourceFallbackFormFor(canonical));+ });++ test('block-math compat forms preserve the block-vs-inline newlines', () => {+ // Same load-bearing newline invariant the canonical Math case pins:+ // a single-line `$$x$$` re-parses as inline math at the destination.+ for (const componentName of ['DollarMath', 'MathFence']) {+ const node = stubPmNode({+ typeName: 'jsxComponent',+ componentName,+ props: { formula: 'x' },+ });+ expect(sourceFallbackFormFor(node), componentName).toEqual({ source: '$$\nx\n$$' });+ }+ });++ test('missing formula prop on compat forms falls back to empty string', () => {+ for (const componentName of ['DollarMath', 'MathFence']) {+ const node = stubPmNode({ typeName: 'jsxComponent', componentName, props: {} });+ expect(sourceFallbackFormFor(node), componentName).toEqual({ source: '$$\n\n$$' });+ }+ });+});++describe('sourceFallbackFormFor — registry-derived non-portable coverage', () => {+ // Symmetric guard to the palette's registry-derived coverage test. The+ // primary walker path (`sourceFallbackFormFor`) must emit a source form+ // for every descriptor whose RENDER identity is a non-portable canonical+ // (`Math` / `MermaidFence`), not just the canonically-named ones — a+ // compat descriptor authored as `$$…$$` (`DollarMath`) or ` ```math `+ // (`MathFence`) renders as `<Math>` and so owes the same readable source.+ // Deriving the set from the registry makes a newly-added non-portable+ // compat descriptor fail loudly here instead of silently falling through+ // to the KaTeX/SVG live-DOM clone. Set + predicate are shared with the+ // palette guard via non-portable-descriptors.test-helper.ts.++ test('every descriptor that renders as a non-portable canonical yields a source form', () => {+ const descriptorNames = nonPortableDescriptorNames();++ // Sanity: the registry must actually carry the two math compat rows,+ // otherwise this test would vacuously pass.+ expect(descriptorNames).toEqual(+ expect.arrayContaining(['Math', 'MermaidFence', 'DollarMath', 'MathFence']),+ );++ for (const componentName of descriptorNames) {+ const node = stubPmNode({ typeName: 'jsxComponent', componentName, props: {} });+ expect(sourceFallbackFormFor(node), componentName).not.toBeNull();+ }+ });+});+describe('sourceFallbackFormFor — preview-active codeBlock', () => {test('html + preview → fenced source with the authored info-string', () => {const node = stubPmNode({diff --git a/packages/app/src/editor/clipboard/non-portable-render-source-fallback.ts b/packages/app/src/editor/clipboard/non-portable-render-source-fallback.tsindex 63ca84058..d0133a1e9 100644--- a/packages/app/src/editor/clipboard/non-portable-render-source-fallback.ts+++ b/packages/app/src/editor/clipboard/non-portable-render-source-fallback.ts@@ -74,7 +74,13 @@ export function sourceFallbackFormFor(node: PmNode): SourceFallbackForm | null {const props = (node.attrs.props as Record<string, unknown> | undefined) ?? {};switch (componentName) {- case 'Math': {+ // `DollarMath` (`$$…$$`-authored) and `MathFence` (```math-authored)+ // are compat descriptors that render as `<Math>` (KaTeX) but keep+ // their authored componentName; they share the `formula` prop, so+ // all three block-math forms emit the same portable dollar source.+ case 'Math':+ case 'DollarMath':+ case 'MathFence': {// `$$\nformula\n$$` newlines are load-bearing: a single-line// `$$x$$` mid-paragraph parses as inline math, breaking the// block-vs-inline distinction we want to preserve through thediff --git a/packages/app/tests/stress/clipboard-dollar-math-source-fallback.e2e.ts b/packages/app/tests/stress/clipboard-dollar-math-source-fallback.e2e.tsnew file mode 100644index 000000000..4541c28ad--- /dev/null+++ b/packages/app/tests/stress/clipboard-dollar-math-source-fallback.e2e.ts@@ -0,0 +1,115 @@+/**+ * Compositional E2E coverage for the block-math non-portable-render+ * source-fallback on the clipboard `text/html` flavor. Targets the copy+ * journey that bun/jsdom cannot exercise: the full copy-event → walker →+ * `fragment.appendChild` wiring that turns a `sourceFallbackFormFor`+ * result into clipboard bytes.+ *+ * Block math authored as `$$…$$` parses to a `DollarMath` jsxComponent and+ * ` ```math ` fences parse to `MathFence`; both `rendersAs: 'Math'` so they+ * render as the same non-portable KaTeX span-tree as canonical `Math`, and+ * are the dominant on-disk authoring forms. Copying either must write the+ * same readable `$$\nformula\n$$` source to the `text/html` clipboard flavor+ * that canonical `Math` writes — a KaTeX style clone in the payload is a+ * paste-fidelity regression for rich destinations (Gmail, Google Docs,+ * Notion). These tests pin that both authored forms emit the source+ * fallback and that no KaTeX markup survives.+ *+ * Companion to the unit/DOM coverage in+ * non-portable-render-source-fallback.test.ts (node classifier) and+ * clipboard-walker-fallback-palette.dom-shape.test.ts (palette DOM shape).+ * Mirrors the sibling clipboard-relative-url-source-fallback.e2e.ts+ * harness.+ */++import { randomUUID } from 'node:crypto';+import {+ expect,+ simulateCopyAndRead,+ test,+ waitForActiveProviderSynced as waitForProvider,+} from './_helpers';++async function getYText(page: import('@playwright/test').Page): Promise<string> {+ return page.evaluate(() => {+ const provider = window.__activeProvider;+ return provider?.document?.getText('source')?.toString() ?? '';+ });+}++test.describe('block-math source-fallback — dollar / fence authored forms on text/html', () => {+ let docName: string;++ test.beforeEach(async ({ page, api }) => {+ docName = `test-dollarmath-${randomUUID().slice(0, 8)}`;+ await api.createPage(`${docName}.md`);+ await page.goto(`/#/${docName}`);+ await waitForProvider(page);+ await page.waitForSelector('.ProseMirror:not(.composer-prosemirror)');+ });++ test('$$…$$-authored block math (DollarMath) emits readable LaTeX source, not a KaTeX clone', async ({+ page,+ baseURL,+ }) => {+ // `$$…$$` on its own lines parses to a `DollarMath` node that renders+ // as KaTeX. Copying it must write the readable `$$\nformula\n$$` source+ // to text/html so rich destinations (Gmail, Docs, Notion) get LaTeX+ // instead of a broken style clone.+ const formula = 'E = mc^2';+ await fetch(`${baseURL}/api/agent-write-md`, {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({+ docName,+ markdown: `$$\n${formula}\n$$\n\nSurrounding prose.\n`,+ position: 'replace',+ }),+ });+ await expect(async () => {+ expect(await getYText(page)).toContain(formula);+ }).toPass({ timeout: 5_000 });+ await page.click('.ProseMirror:not(.composer-prosemirror)');++ const captured = await simulateCopyAndRead(page, 'wysiwyg');++ // text/html carries the source fallback: `<pre class="mdx-component">+ // <code>$$\nformula\n$$</code></pre>`.+ expect(captured.html).toMatch(/<pre class="mdx-component"[ >]/);+ expect(captured.html).toContain('<code>');+ expect(captured.html).toContain(`$$\n${formula}\n$$`);+ // The non-portable KaTeX render must NOT leak into the payload.+ expect(captured.html).not.toContain('class="katex"');+ });++ test('```math-authored block math (MathFence) emits the same dollar source form', async ({+ page,+ baseURL,+ }) => {+ // ` ```math ` fences parse to a `MathFence` node that also renders as+ // KaTeX; the source fallback maps it to the SAME dollar form as+ // canonical `Math`, matching the render identity rather than the+ // authored fence bytes.+ const formula = 'a^2 + b^2 = c^2';+ await fetch(`${baseURL}/api/agent-write-md`, {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({+ docName,+ markdown: `\`\`\`math\n${formula}\n\`\`\`\n\nSurrounding prose.\n`,+ position: 'replace',+ }),+ });+ await expect(async () => {+ expect(await getYText(page)).toContain(formula);+ }).toPass({ timeout: 5_000 });+ await page.click('.ProseMirror:not(.composer-prosemirror)');++ const captured = await simulateCopyAndRead(page, 'wysiwyg');++ expect(captured.html).toMatch(/<pre class="mdx-component"[ >]/);+ expect(captured.html).toContain('<code>');+ expect(captured.html).toContain(`$$\n${formula}\n$$`);+ expect(captured.html).not.toContain('class="katex"');+ });+});