Created
June 13, 2026 13:29
-
-
Save npras/5cc1765cf2e3bb08911b0e502ac99b97 to your computer and use it in GitHub Desktop.
FCC - javascript certification project - markdown to html converter
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
| 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