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);
});
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,
});
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();
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:
- Free browser tool: HTML & Rich Text → Markdown Converter
- Open-source MV3 extension (MIT, GitHub v1.0.1 ZIP + CWS/Firefox pending): github.com/larkandlever/markdown-clipper · Extension Landing Page
- Scrubbing secrets before pasting into ChatGPT/Claude? Try our AI Prompt Secret & PII Redactor
- Obsidian starter kit & indie tracker: Free MIT Templates · Lark Dev Vault ($9)
Top comments (0)