Skip to content

Instantly share code, notes, and snippets.

@twardoch
Last active February 23, 2022 04:17
Show Gist options
  • Save twardoch/a153043010fa87c1b18e982f42f418c7 to your computer and use it in GitHub Desktop.
Save twardoch/a153043010fa87c1b18e982f42f418c7 to your computer and use it in GitHub Desktop.
Markdown file with `.html` extension that “self-renders”

Markdown file with .html extension that “self-renders”

Create a Markdown document and put the following code at the end of the file. Save the document with the .html extension, and a double-click will open it in the browser and show the content as rendered HTML. This is super-useful for simple documents where you want to keep just one Markdown file, but you want to “self-render”.

<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/4.0.2/marked.min.js"></script><script>h=document.head,h.innerHTML+='<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.1.0/github-markdown-light.min.css"/>',b=document.body,b.innerHTML=marked.parse(b.innerHTML,{breaks:!0,gfm:!0,headerIds:!0,smartLists:!0,smartypants:!0}),hljs.highlightAll(),b.classList.add("markdown-body"),b.style.maxWidth="640",b.style.margin="auto",b.style.padding="2rem";</script>

To also use syntax highlighting, use this:

<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/4.0.2/marked.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/highlight.min.js"></script><script>h=document.head,h.innerHTML+='<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.1.0/github-markdown-light.min.css"/><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/styles/stackoverflow-light.min.css"/>',b=document.body,b.innerHTML=marked.parse(b.innerHTML,{breaks:!0,gfm:!0,headerIds:!0,smartLists:!0,smartypants:!0,highlight:function(e,s){s=hljs.getLanguage(s)?s:"plaintext";return hljs.highlight(e,{language:s}).value}}),b.classList.add("markdown-body"),b.style.maxWidth="640",b.style.margin="auto",b.style.padding="2rem";</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment