DEV Community

Cover image for Converting Pasted Google Docs and Rich Text to Clean Markdown in Vanilla JS
Lark & Lever
Lark & Lever

Posted on Edited on

Converting Pasted Google Docs and Rich Text to Clean Markdown in Vanilla JS

If you have ever copied formatted text from Google Docs and pasted it into a Markdown converter, you have probably seen the entire document come out wrapped in stray **...** bold markers.

Google Docs wraps copied selections inside a <b style="font-weight:normal" id="docs-internal-guid-..."> element. Technically it is a <b> tag, so HTML-to-Markdown converters treat the whole document as bold, even though the inline style explicitly sets font-weight:normal.

Here is how we handle rich clipboard pastes cleanly in vanilla JavaScript without sending a single byte to a server.

1. Intercept paste and read text/html safely

Instead of letting a contenteditable div render raw pasted styles and then scraping its live DOM, read clipboardData directly in the paste event and parse it with DOMParser:

richBox.addEventListener("paste", (e) => {
  const cd = e.clipboardData;
  if (!cd) return;
  e.preventDefault();

  const html = cd.getData("text/html");
  const plain = cd.getData("text/plain");
  const doc = new DOMParser().parseFromString(html || plain, "text/html");

  const markdown = convertBodyToMarkdown(doc.body);
});
Enter fullscreen mode Exit fullscreen mode

Using DOMParser().parseFromString(..., "text/html") creates an inert document tree — <script> elements and inline event handlers are never executed, and we only read the tree rather than injecting it into the page.

2. Unwrap Google Docs' fake <b> and strip noise

With Turndown and turndown-plugin-gfm, two small additions clean up 99% of real-world pastes from docs, wikis, and web pages:

const td = new TurndownService({
  headingStyle: "atx",
  bulletListMarker: "-",
  codeBlockStyle: "fenced",
  hr: "---",
});

td.use(window.turndownPluginGfm.gfm);
td.remove(["script", "style", "noscript", "iframe", "meta", "link"]);

// Google Docs wraps copied blocks in <b style="font-weight:normal">
td.addRule("gdocsWrapper", {
  filter: (node) =>
    node.nodeName === "B" &&
    /font-weight:\s*normal/i.test(node.getAttribute("style") || ""),
  replacement: (content) => content,
});
Enter fullscreen mode Exit fullscreen mode

3. Keep fenced code languages intact

When clipping full web articles (for instance, with Mozilla Readability before running Turndown), Readability strips CSS classes by default. That removes language-js or lang-python from <pre><code> blocks unless you preserve them:

const langClasses = new Set();
clone.querySelectorAll("pre[class], code[class]").forEach((el) => {
  el.classList.forEach((c) => {
    if (/^(language|lang)-/.test(c)) langClasses.add(c);
  });
});

const article = new Readability(clone, {
  classesToPreserve: [...langClasses],
}).parse();
Enter fullscreen mode Exit fullscreen mode

Pairing that with a pre > code rule that picks a fence longer than any inner backticks guarantees nested Markdown examples don't break your output.

Try it in your browser (or inspect the MIT source)

We put this exact client-side pipeline into a free, zero-tracking web utility so you can paste straight from Google Docs, Word, or any web page and copy clean GFM Markdown:

Top comments (0)