Skip to content

Paste as Markdown

react-smart-copy/markdown turns what people paste into comment boxes, editors and CMS fields into clean Markdown: headings, bold and italic, links, lists (nested and task lists), tables, code blocks and quotes, from Google Docs, Word, Notion, GitHub, web pages and mail. No dependencies, about 7.6 kB.

Copy rich text from anywhere (Google Docs, Word, a web page, or the sample below) and paste it into the editor: it arrives as Markdown.

Launch checklist

Ship on Friday, after the final review. Details are in the repository.

  • Write the changelog
  • Tag the release
StepOwner
ReviewAsha
PublishCI
npm install react-smart-copy

Copy the sample as rich text, then paste it below.

Press Ctrl + V or ⌘ + V. Ctrl + Z undoes the paste.

Nothing pasted yet.

One handler on the textarea

pasteAsMarkdown converts rich pastes and inserts the Markdown at the cursor. Undo still works, your state updates as if the user typed it, and plain-text pastes are left alone: they are usually Markdown already.

Pasted HTML is parsed without running or fetching anything, the output never contains raw HTML, and links keep only safe URL schemes. Images become their alt text unless you opt in.

import { htmlToMarkdown, pasteAsMarkdown } from 'react-smart-copy/markdown';

<textarea value={body} onChange={…} onPaste={pasteAsMarkdown} />

// Copy a rendered article as Markdown:
copy(() => htmlToMarkdown(articleRef.current).markdown);
import { pasteAsMarkdown } from "react-smart-copy/markdown";
<textarea value={body} onChange={(event) => setBody(event.target.value)} onPaste={pasteAsMarkdown} />

That one handler:

  • converts rich pastes (the HTML flavour) and inserts the Markdown at the cursor, replacing the selection
  • keeps the browser’s undo (Ctrl/⌘+Z undoes the paste), fires input so React state follows, respects maxlength
  • leaves plain-text pastes alone, because plain text pasted into a Markdown field is usually Markdown already, and leaves code from editors (VS Code, JetBrains) as plain code
  • in a single-line <input>, joins the result into one line

Pass options with onPaste={(event) => pasteAsMarkdown(event, { images: "keep" })}. For other flows, the pieces are separate:

import { escapeMarkdown, htmlToMarkdown, insertText, pasteToMarkdown } from "react-smart-copy/markdown";
// With usePaste or PasteField: Markdown for a paste result (plain text stays as it is).
const pasted = pasteToMarkdown(result); // { markdown, source: "html" | "text", truncated } | null
if (pasted) insertText(textarea, pasted.markdown);
// Copy a rendered article as Markdown (a DOM node is converted in place, not re-parsed).
copy(() => htmlToMarkdown(articleRef.current).markdown);
// Any text, rendered literally by any Markdown renderer.
escapeMarkdown("1. *Not* a list <b>"); // "1\\. \\*Not\\* a list \\<b>"

A “Copy page” button, so readers can paste the page into a chat, an issue or an editor. This site has one on every page, built exactly like this:

import { useCopy } from "react-smart-copy";
import { escapeMarkdown, htmlToMarkdown } from "react-smart-copy/markdown";
function CopyPage({ title }: { title: string }) {
const { copy, status } = useCopy();
return (
<button
type="button"
onClick={() =>
void copy(() => {
const article = document.querySelector("article");
if (article === null) return "";
const { markdown } = htmlToMarkdown(article, { ignore: ".demo", baseUrl: location.href });
return `# ${escapeMarkdown(title)}\n\n${markdown}\n`;
})
}
>
{status === "copied" ? "Copied" : "Copy page as Markdown"}
</button>
);
}
  • Converted during the click. The function runs inside the click, so Safari still counts the write as the user’s action.
  • Heading permalinks are left out. Starlight, Docusaurus, VitePress, MkDocs, Sphinx and GitHub add a #, or “Section titled” link to every heading. It points at the heading itself, so it is dropped (pasting a section from those sites gets the same clean headings). A heading whose whole text is a link keeps it.
  • Leave out what is not text with ignore (demos, ads, feedback widgets), or mark it data-copy-ignore. Buttons, forms, media and hidden elements are always left out.
  • baseUrl: location.href keeps links to other pages as links. Without it, relative links keep only their text.
  • On Starlight, don’t ignore .not-content: Starlight puts that class on code blocks too, and the page’s code would disappear.

This snippet is a test in the repository (test/docs-snippets.test.tsx), and this site’s button is checked in Chromium, Firefox and WebKit.

What real clipboards look like, handled. Google Docs sets bold and italic with CSS on spans, wraps everything in a non-bold <b>, nests lists as siblings and wraps links in google.com/url?q= redirects. Word writes lists as mso-list paragraphs with hidden markers. Mail clients use <div> per line and layout tables. Spreadsheets and Docs tables become GFM tables, merged cells included.

Correct by construction. Every delimiter the converter writes is checked with CommonMark’s own pairing algorithm before it is kept, so text always renders as the same text: where CommonMark can’t express a style (x**[y]**z), the style is dropped, never the text, and asterisks never leak into the output. Property tests render thousands of random documents with a CommonMark parser and compare the result character by character, with both markdown-it and micromark with GitHub’s extensions, the reference GitHub follows.

URLs and GitHub. GitHub links a bare URL in text and keeps any backslash escapes inside it, so an escaped https://example.com/my\_page is a broken link there. A plain http(s) address in pasted text is therefore written as an autolink, <https://example.com/my_page>, which every renderer shows and follows exactly; a link whose text is its own address is written the same way. URL-like text that is not a plain address (a bare https://, a non-ASCII host) is kept from being linked at all. Only what CommonMark reads as syntax is escaped: snake_case, 0.75, #hashtag and -5 °C stay as written.

Security. Pasted HTML is untrusted, so:

  • it is parsed in an inert document: scripts never run and nothing is fetched, not even images
  • the output never contains raw HTML: any < in the text is escaped
  • links keep only http:, https: and mailto: (linkProtocols); javascript:, data: and relative links keep only their text
  • images default to their alt text (images: "alt"), so rendering the Markdown loads no tracking pixels. "keep" writes ![alt](url) for https:/http: images, "drop" removes them
  • maxLength (16 MiB), maxNodes (200,000) and maxDepth (200) bound the work; content left out sets truncated: true, and merged-cell expansion is capped
Option Default Notes
images "alt" "alt", "keep" or "drop"
links "keep" "text" keeps only the link text
linkProtocols / imageProtocols http(s), mailto / https, http allowed URL schemes
baseUrl none resolves relative links and images
bullet "-" "*" or "+"
hardBreak "backslash" "spaces" for two trailing spaces
ignore none a CSS selector of elements to leave out; [data-copy-ignore] always is
codeAsText (paste) true false turns code-only pastes into fenced code blocks