diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/markdown-parse.ts b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/markdown-parse.ts index b7f29ab4ea5..677c89b78b8 100644 --- a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/markdown-parse.ts +++ b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/markdown-parse.ts @@ -216,6 +216,25 @@ export function splitMarkdownBlocks(body: string): string[] { return blocks } +/** Detect clipboard formatting with the same grammar and bounded blocks used to open files. */ +export function hasMarkdownFormatting(body: string): boolean { + const markdown = markdownManager().instance + const normalized = body.replace(/\r\n?/g, '\n') + const blocks = NON_CHUNKABLE.test(normalized) ? [normalized] : splitMarkdownBlocks(normalized) + for (const block of blocks) { + let formatted = false + markdown.walkTokens(markdown.lexer(block), (token) => { + // Native paste can add autolinks separately, retaining the text if formatting exceeds its budget. + if (token.type === 'link' && token.raw === token.text) return + // Bare HTML can be ordinary text such as List; explicit Markdown bypasses detection. + if (token.type === 'html') return + if (!['paragraph', 'text', 'space', 'escape', 'def'].includes(token.type)) formatted = true + }) + if (formatted) return true + } + return false +} + /** * Parse a markdown body into a ProseMirror doc by splitting it into top-level blocks and parsing each * independently, then assembling the results. diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/markdown-paste.test.ts b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/markdown-paste.test.ts index d6520dd2b1f..9e65f6b0075 100644 --- a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/markdown-paste.test.ts +++ b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/markdown-paste.test.ts @@ -94,6 +94,19 @@ describe('markdown paste', () => { expect(editor.getText()).toBe('') }) + it.each(['List', 'List', 'Use for emphasis'])( + 'preserves ordinary angle-bracket text %s on plain clipboard paste', + (text) => { + const editor = mount() + dispatchPaste(editor, text) + expect(editor.state.doc.textContent).toBe(text) + expect(editor.getJSON().content?.[0].content).toEqual([{ type: 'text', text }]) + const reopened = mount() + reopened.commands.setContent(editor.getMarkdown(), { contentType: 'markdown' }) + expect(reopened.getJSON()).toEqual(editor.getJSON()) + } + ) + it('does not flatten a rich table containing literal Markdown-shaped cell text', () => { const editor = mount() dispatchPaste( @@ -115,6 +128,44 @@ describe('markdown paste', () => { expect(structural).toEqual(['heading', 'bulletList', 'blockquote']) }) + it.each([ + ['setext heading', 'Paste title\n===========', 'heading', 'Paste title'], + ['tilde fence', '~~~js\nconst answer = 42\n~~~', 'codeBlock', 'const answer = 42'], + ['indented code', ' const answer = 42', 'codeBlock', 'const answer = 42'], + ['parenthesized ordered list', '1) one\n2) two', 'orderedList', 'onetwo'], + ['thematic break', '---', 'horizontalRule', ''], + ['reference link', '[Guide][ref]\n\n[ref]: https://example.com', 'paragraph', 'Guide'], + ])( + 'renders pasted %s as Markdown and preserves it after reopening', + (_, text, type, expectedText) => { + const editor = mount() + dispatchPaste(editor, text) + const first = editor.state.doc.firstChild + expect(first?.type.name).toBe(type) + if (type === 'paragraph') expect(editor.getHTML()).toContain('href="https://example.com"') + expect(first?.textContent).toBe(expectedText) + const reopened = mount() + reopened.commands.setContent(editor.getMarkdown(), { contentType: 'markdown' }) + expect(reopened.getJSON()).toEqual(editor.getJSON()) + } + ) + + it.each([ + ['Paste title', 'Paste title\n===========', '

Paste title

'], + ['', 'Paste title\n===========', '

Paste title

'], + ['HTML source', 'Declared emphasis', 'Declared emphasis'], + ])( + 'honors explicit Markdown provenance with plain fallback %j', + (fallback, markdown, expectedHtml) => { + const editor = mount() + dispatchPaste(editor, fallback, 'Wrong representation', { + 'text/markdown': markdown, + }) + expect(editor.getHTML()).toContain(expectedHtml) + expect(editor.getText()).not.toContain('Wrong representation') + } + ) + it('strips