Skip to content

Instantly share code, notes, and snippets.

@jaylandro
Created May 24, 2022 11:15
Show Gist options
  • Save jaylandro/cdae3ecb671dbb684d351c8d8577fe8a to your computer and use it in GitHub Desktop.
Save jaylandro/cdae3ecb671dbb684d351c8d8577fe8a to your computer and use it in GitHub Desktop.
Markdown parser JavaScript
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