Last active
March 7, 2024 15:04
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('.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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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>