Skip to content

Instantly share code, notes, and snippets.

@npras
Created June 13, 2026 13:29
Show Gist options
  • Select an option

  • Save npras/5cc1765cf2e3bb08911b0e502ac99b97 to your computer and use it in GitHub Desktop.

Select an option

Save npras/5cc1765cf2e3bb08911b0e502ac99b97 to your computer and use it in GitHub Desktop.
FCC - javascript certification project - markdown to html converter
const mdInput = document.querySelector('#markdown-input');
const htmlOutput = document.querySelector('#html-output');
const htmlPreview = document.querySelector('#preview');
const Transformers = [
// block level elems
[/^# (.+)/gm, "<h1>$1</h1>"],
[/^## (.+)/gm, "<h2>$1</h2>"],
[/^### (.+)/gm, "<h3>$1</h3>"],
[/^> (.+)/gm, "<blockquote>$1</blockquote>"],
// inline elems
// bold first
[/\*\*(.+)\*\*/gm, "<strong>$1</strong>"],
[/__(.+)__/gm, "<strong>$1</strong>"],
// italics next
[/\*(.+)\*/gm, "<em>$1</em>"],
[/_(.+)_/gm, "<em>$1</em>"],
// img first, then a (link)
[/!\[(.+)\]\((.+)\)/gm, "<img alt='$1' src='$2' />"],
[/\[(.+)\]\((.+)\)/gm, "<a href='$2'>$1</a>"],
];
window.convertMarkdown = () => {
return Transformers.reduce(
(txt, [pattern, replacement]) => txt.replace(pattern, replacement),
mdInput.value
);
};
document.addEventListener('DOMContentLoaded', () => {
mdInput.value = '';
mdInput.addEventListener('input', () => {
const txt = convertMarkdown();
htmlOutput.textContent = txt;
htmlPreview.innerHTML = txt;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment