Created
May 24, 2022 11:15
-
-
Save jaylandro/cdae3ecb671dbb684d351c8d8577fe8a to your computer and use it in GitHub Desktop.
Markdown parser JavaScript
This file contains hidden or 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
function parseMarkdown(markdownText) { | |
const htmlText = markdownText | |
.replace(/^### (.*$)/gim, '<h3>$1</h3>') | |
.replace(/^## (.*$)/gim, '<h2>$1</h2>') | |
.replace(/^# (.*$)/gim, '<h1>$1</h1>') | |
.replace(/^\> (.*$)/gim, '<blockquote>$1</blockquote>') | |
.replace(/\*\*(.*)\*\*/gim, '<b>$1</b>') | |
.replace(/\*(.*)\*/gim, '<i>$1</i>') | |
.replace(/!\[(.*?)\]\((.*?)\)/gim, "<img alt='$1' src='$2' />") | |
.replace(/\[(.*?)\]\((.*?)\)/gim, "<a href='$2'>$1</a>") | |
.replace(/\n$/gim, '<br />') | |
return htmlText.trim() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment