Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Code>; 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ describe('markdown paste', () => {
expect(editor.getText()).toBe('')
})

it.each(['List<String>', 'List<Code>', 'Use <em> 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(
Expand All @@ -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===========', '<h1>Paste title</h1>'],
['', 'Paste title\n===========', '<h1>Paste title</h1>'],
['HTML source', '<em>Declared emphasis</em>', '<em>Declared emphasis</em>'],
])(
'honors explicit Markdown provenance with plain fallback %j',
(fallback, markdown, expectedHtml) => {
const editor = mount()
dispatchPaste(editor, fallback, '<b>Wrong representation</b>', {
'text/markdown': markdown,
})
expect(editor.getHTML()).toContain(expectedHtml)
expect(editor.getText()).not.toContain('Wrong representation')
}
)

it('strips <style>/<script> from pasted HTML so their text never leaks into the doc', () => {
const editor = mount()
const gsheets =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { type Editor, Extension } from '@tiptap/core'
import { Plugin } from '@tiptap/pm/state'
import { normalizeLinkHref } from '@/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/markdown-fidelity'
import { parseMarkdownToDoc } from '@/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/markdown-parse'
import {
hasMarkdownFormatting,
parseMarkdownToDoc,
} from '@/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/markdown-parse'

interface MarkdownPasteStorage {
pasteWithoutFormatting: boolean
Expand Down Expand Up @@ -40,41 +43,6 @@ function pastedLinkHref(text: string): string | null {
return null
}

/**
* Structural markdown — strong signals the plain text is genuinely markdown (a link, image, badge,
* list, heading, blockquote, fenced block, or GFM table). Only used after clipboard provenance has
* established that the source is plain text or explicitly Markdown.
*/
const STRUCTURAL_MARKDOWN_HINTS: ReadonlyArray<RegExp> = [
/^#{1,6}\s/m,
/\*\*[^*]+\*\*/,
/\[[^\]]*]\([^)]+\)/,
/^\s*[-*+]\s/m,
/^\s*\d+\.\s/m,
/^>\s/m,
/```/,
/^\|.*\|.*\|/m,
]

/**
* Inline marks — weaker markdown signals (`*italic*` / `_italic_`, `~~strike~~`, `` `code` ``) that a
* rich HTML sibling encodes just as well. Parsed for a plain-text-only paste (so markdown copied from a
* terminal or `.md` source renders), but deferred to an HTML sibling: its presence means the source was
* rich, and it may carry structure the plain text can't (a copied table's plain form is tab-separated,
* not a `| … |` grid, so parsing it would flatten the table).
*/
const INLINE_MARK_HINTS: ReadonlyArray<RegExp> = [
/\*[^*\n]+\*/,
/_[^_\n]+_/,
/~~[^~\n]+~~/,
/`[^`\n]+`/,
/==(?:[^=\n]|=(?!=))+==/,
]

function hasAny(hints: ReadonlyArray<RegExp>, text: string): boolean {
return hints.some((hint) => hint.test(text))
}

/**
* VSCode language ids that differ from our code-block language values. `markdown`/`plaintext` map to
* the empty string so they are NOT forced into a code block — markdown copied from VSCode should parse
Expand Down Expand Up @@ -157,8 +125,8 @@ function stripNonContentHtml(html: string): string {
*
* Provenance decides plain-text-vs-HTML: a `text/html` sibling (copied from a browser, Slack, Notion,
* GitHub, or this editor) is the signal the source was rich. Defer to that structure even when literal
* cell text resembles Markdown. VSCode's explicit Markdown mode is the exception: its colored-span
* HTML represents source, not a rendered document. Explicit paste-without-formatting bypasses every
* cell text resembles Markdown. Explicit `text/markdown` and VSCode's Markdown mode prefer source
* over the HTML representation. Explicit paste-without-formatting bypasses every
* transformation, including link wrapping and source-language code blocks.
*
* The strictness of the parse matters: `marked` follows CommonMark flanking rules, so `*text*` becomes
Expand Down Expand Up @@ -221,7 +189,8 @@ export const MarkdownPaste = Extension.create<Record<string, never>, MarkdownPas
return true
}
if (editor.isActive('codeBlock') || editor.isActive('code')) return false
const text = event.clipboardData?.getData('text/plain')
const markdown = event.clipboardData?.getData('text/markdown')
const text = markdown || event.clipboardData?.getData('text/plain')
if (!text) return false
const { selection } = view.state
if (
Expand All @@ -242,10 +211,10 @@ export const MarkdownPaste = Extension.create<Record<string, never>, MarkdownPas
content: [{ type: 'text', text }],
})
}
const isMarkdownSource = mode === 'markdown' || mode === 'md' || mode === 'mdx'
const isMarkdownSource =
Boolean(markdown) || mode === 'markdown' || mode === 'md' || mode === 'mdx'
if (event.clipboardData?.getData('text/html') && !isMarkdownSource) return false
if (!hasAny(STRUCTURAL_MARKDOWN_HINTS, text) && !hasAny(INLINE_MARK_HINTS, text))
return false
if (!isMarkdownSource && !hasMarkdownFormatting(text)) return false
Comment thread
waleedlatif1 marked this conversation as resolved.
Resolved
const doc = parseMarkdownToDoc(text)
if (!doc.content?.length) return false
return editor.commands.insertContent(doc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ afterEach(() => {
editor = null
})

function runPaste(ed: Editor, text: string, html = ''): { handled: boolean; prevented: boolean } {
function runPaste(
ed: Editor,
text: string,
html = '',
extra: Record<string, string> = {}
): { handled: boolean; prevented: boolean } {
let prevented = false
const event = {
clipboardData: {
getData: (type: string) => {
if (type === 'text/plain') return text
if (type === 'text/html') return html
return ''
return extra[type] ?? ''
},
},
preventDefault: () => {
Expand All @@ -38,11 +43,11 @@ function runPaste(ed: Editor, text: string, html = ''): { handled: boolean; prev
return { handled: false, prevented }
}

function dispatchPaste(ed: Editor, text: string) {
function dispatchPaste(ed: Editor, text: string, extra: Record<string, string> = {}) {
const event = new Event('paste', { bubbles: true, cancelable: true })
Object.defineProperty(event, 'clipboardData', {
value: {
getData: (type: string) => (type === 'text/plain' ? text : ''),
getData: (type: string) => (type === 'text/plain' ? text : (extra[type] ?? '')),
files: [],
items: [],
},
Expand Down Expand Up @@ -136,23 +141,62 @@ describe('rich Markdown paste admission', () => {
expect(onRejected).toHaveBeenCalledOnce()
})

it('rejects before downstream paste parsing when projected bytes exceed the document limit', () => {
const onRejected = vi.fn()
editor = new Editor({
extensions: [
...createMarkdownContentExtensions(),
createRichMarkdownPasteAdmission({
maxResultBytes: 10,
getCurrentText: () => '123456',
onRejected,
}),
],
content: '<p>123456</p>',
})
it.each(['text/plain', 'text/markdown'])(
'rejects oversized %s before downstream paste parsing',
(type) => {
const onRejected = vi.fn()
editor = new Editor({
extensions: [
...createMarkdownContentExtensions(),
createRichMarkdownPasteAdmission({
maxResultBytes: 10,
getCurrentText: () => '123456',
onRejected,
}),
],
content: '<p>123456</p>',
})

expect(runPaste(editor, 'abcde')).toEqual({ handled: true, prevented: true })
expect(onRejected).toHaveBeenCalledOnce()
})
expect(
runPaste(editor, type === 'text/plain' ? 'abcde' : 'x', '', { [type]: 'abcde' })
).toEqual({ handled: true, prevented: true })
expect(onRejected).toHaveBeenCalledOnce()
}
)

it.each(['plain shortcut', 'code block', 'inline code'])(
'budgets the consumed plain representation for %s',
(context) => {
const onRejected = vi.fn()
editor = new Editor({
extensions: [
...createMarkdownContentExtensions(),
MarkdownPaste,
createRichMarkdownPasteAdmission({
maxResultBytes: 64,
getCurrentText: () => editor?.getMarkdown() ?? '',
onRejected,
}),
],
enablePasteRules: false,
})
if (context === 'code block') editor.commands.setCodeBlock()
else if (context === 'inline code') editor.commands.setCode()
else
editor.view.dom.dispatchEvent(
new KeyboardEvent('keydown', {
key: 'v',
keyCode: 86,
ctrlKey: true,
shiftKey: true,
bubbles: true,
})
)
dispatchPaste(editor, 'x', { 'text/markdown': '# '.repeat(100) })
expect(editor.state.doc.textContent).toBe('x')
expect(onRejected).not.toHaveBeenCalled()
}
)

it('rejects oversized rich HTML before downstream parsing', () => {
const onRejected = vi.fn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { Extension } from '@tiptap/core'
import { Plugin, type Transaction } from '@tiptap/pm/state'
import { postProcessSerializedMarkdown } from '@/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/markdown-fidelity'
import { isPlainTextPaste } from '@/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/markdown-paste'

export interface RichMarkdownPasteAdmissionOptions {
maxResultBytes: number
Expand Down Expand Up @@ -101,7 +102,14 @@ export function createRichMarkdownPasteAdmission({
props: {
handleDOMEvents: {
paste: (view, event) => {
const pastedText = event.clipboardData?.getData('text/plain') ?? ''
const literal =
isPlainTextPaste(editor) ||
editor.isActive('codeBlock') ||
editor.isActive('code')
const pastedText =
(!literal && event.clipboardData?.getData('text/markdown')) ||
event.clipboardData?.getData('text/plain') ||
''
const pastedHtml = event.clipboardData?.getData('text/html') ?? ''
if (!pastedText && !pastedHtml) return false

Expand Down
Loading
Loading