Markdown Parser Snippet (JS)
29. Markdown Parser Snippet (JS)
===============================
function simpleMarkdown(text) {
return text
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
.replace(/\*(.*?)\*/g, '<em>$1</em>')
.replace(/`(.*?)`/g, '<code>$1</code>')
.replace(/^(#{1,6})\s+(.*)$/gm, (match, hashes, title) =>
`<h${hashes.length}>${title}</h${hashes.length}>`);
}
// Usage
console.log(simpleMarkdown('**PasteText** is *awesome*! `code`.\n# Title'));
Featured
Advertisement
Category
Action