Skip to content

Instantly share code, notes, and snippets.

@fauxsaurus
Last active March 7, 2024 15:04
Show Gist options
  • Save fauxsaurus/9bbfbae95ef638096bccdf6962e7be43 to your computer and use it in GitHub Desktop.
Save fauxsaurus/9bbfbae95ef638096bccdf6962e7be43 to your computer and use it in GitHub Desktop.
Converts text in the Grammarly editor to markdown. To use it, paste the code into the console & copy the resulting string's contents.
$('.ql-editor[contenteditable="true"]').innerHTML
/** @note escape characters */
.replace(/\#/g, '\\#')
.replace(/\*/g, '\\*')
/** @note convert headings */
.replace(/<\/h[1-6]>/g, '\n')
.replace(/<h1>/g, '# ')
.replace(/<h2>/g, '## ')
.replace(/<h3>/g, '### ')
.replace(/<h4>/g, '#### ')
.replace(/<h5>/g, '##### ')
.replace(/<h6>/g, '###### ')
/** @note replace move whitespace outside of styled text to prevent markdown problems */
.replace(/( +)(<\/(em|strong|u)+>)/g, '$2$1')
/** @note bold & italic text */
.replace(/<\/*em>/g, '*')
.replace(/<\/*strong>/g, '**')
/** @note please don't use underline, as this might not work in all markdown flavors */
.replace(/<\/*u>/g, '__')
/** @note paragraphs */
.replace(/<\/p>/g, '\n')
.replace(/<p>/g, '')
/** @note linebreaks */
.replace(/<br[^>]*>/g, '\n\n')
/** @note links */
.replace(/<a .* href="([^"]+)" [^>]+>([^<]+)<\/a>/g, '[$2]($1)')
/** @note strip remaining HTML tags */
.replace(/<\/*\w[^>]+>/g,'')
/** @note prevent paragraph indents from becoming code blocks */
.replace(/\t/g, '\n')
/** @note double dashes to em dashes */
.replace(/--/g, '—')
/** @note remove trailing whitespace */
.replace(/ +(\n)/g, '$1')
@fauxsaurus
Copy link
Author

Test string <p><strong><em><u>Bold-italic-underline</u></em></strong></p><p><em>italic</em></p><p><u>underline</u></p><h1>h1</h1><h2>h2</h2><p><a class="editor-rtfLink" href="http://startpage.com" target="_blank">link</a></p><ol data-type="ordered"><li>ordered</li><li class="ql-indent-1"><br></li><li>list</li></ol><ul data-type="bullet"><li>unordered</li><li class="ql-indent-1"><br></li><li>list</li></ul><p><br></p><p><br></p>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment