Feature: Convert pasted plain-text Markdown using the markdown transformers
Description
Pasting plain-text Markdown into a rich-text editor inserts it literally. Markdown shortcut transformers only run while you type. For example, pasting this from a text editor, a terminal or an LLM chat:
# Heading
**bold** text
> quotegives four plain paragraphs (# Heading, empty, **bold** text, > quote). If you type # in the same editor, it turns into a heading. You can see this in the playground, which registers registerMarkdownShortcuts(editor, PLAYGROUND_TRANSFORMERS).
This is how things work now, not a regression:
registerMarkdownShortcuts(@lexical/markdown) and the@lexical/mdastshortcuts both run from an update listener. They only act when the caret moved forward by exactly one typed character.MdastShortcuts.tsexplicitly skips paste and drop so that transforms that change content don't fire by mistake.- The default
text/plainhandler inClipboardImportExtensioninserts the text line by line withselection.insertRawText. It never checks for Markdown.
So there's currently no built-in path from a plain-text paste to the Markdown importers. Right now each app has to write its own paste plugin. That was hard until #8789 added $generateNodesFromMarkdownString (see #7663, where someone was building exactly this plugin). #2070 asked for this in 2022 and was closed as "already present in the playground", but the playground only has the Markdown toggle, not paste conversion.
Proposal
Add an opt-in extension, e.g. MarkdownPasteExtension in @lexical/markdown (and/or the same for @lexical/mdast), that adds a text/plain handler to ClipboardImportExtension.$importMimeType:
- On a regular paste it only handles
text/plain.application/x-lexical-editorandtext/htmlkeep priority, so copying from a web page or from Lexical itself works as before. - It parses with
$generateNodesFromMarkdownString(text, transformers)and inserts with$insertGeneratedNodes. If the text has no Markdown syntax, or the selection is inside a code block (CodeNode) or another context where Markdown shouldn't be interpreted, it hands off to the default handler with$next(). - Its config takes
transformers, an optionalshouldImport(text, selection)predicate (e.g. to require "looks like Markdown", or to paste some text literally), and adisabledsignal, following the extension conventions. - Paste as plain text (Cmd/Ctrl+Shift+V) imports the clipboard's
text/plainpayload as Markdown, even when HTML or Lexical content is also on the clipboard. Editors and chat apps usually copy a rendered HTML version along with the Markdown source, and a regular paste picks the HTML, so this shortcut is how a user chooses the Markdown. Text without Markdown is still inserted as plain text.
Then the playground can enable it alongside PlaygroundMarkdownShortcutsExtension.
Alternatives considered
- Running the shortcut transformers on pasted text. They're written for one line at the caret, so multi-line blocks such as lists, fenced code and tables wouldn't import correctly. The full importer handles those.
- Leaving it to user land. That's possible now with
$generateNodesFromMarkdownString, but people clearly want it (#2070, #7663, #6305 and #6177 all describe paste workflows), and the details (priority against HTML, code blocks, selection placement) are easy to get wrong.
Impact
Anyone using Markdown shortcuts has probably tried pasting Markdown and expected it to convert, especially text copied from LLM chat output, READMEs, and notes apps that put only text/plain on the clipboard. Because it's opt-in, existing editors aren't affected.
Source: facebook/lexical