Menu

Converting Pasted Google Docs and Rich Text to Clean Markdown in Vanilla JS
πŸ“°

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

DEV CommunityΒ·Lark & LeverΒ·4 days ago
#pbhfc8YN
#dev#html#const#markdown#docs
Long article (605 words) β€” no AI summary yet.
Reading 0:00
15s threshold

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 .…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.