Skip to content

Instantly share code, notes, and snippets.

@afeish
Forked from max-lt/md-renderer.conf
Created September 12, 2018 10:49
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save afeish/c4cbe9b736c085fd24549469675460db to your computer and use it in GitHub Desktop.
Save afeish/c4cbe9b736c085fd24549469675460db to your computer and use it in GitHub Desktop.
Nginx config to render markdown files (client side)
location /__special {
internal;
allow all;
root /usr/share/nginx/html/__special;
}
location = /__md_file {
internal;
allow all;
add_header 'Vary' 'Accept';
default_type text/html;
alias /usr/share/nginx/html/__special/md-renderer.html;
}
location ~* \.md {
error_page 418 = /__md_file;
add_header 'Vary' 'Accept';
if (!-f $request_filename) {
break;
}
# if no "text/markdown" in "accept" header:
# redirect to /__md_file to serve html renderer
if ($http_accept !~* "text/markdown") {
return 418;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.3.0/milligram.min.css">
<script type="application/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.19/marked.min.js"></script>
<style rel="stylesheet">code.error {line-height:2rem;font-size:2rem;color:#F43;padding:2rem;display: block;}</style>
<script>
const FILENAME = location.pathname;
const set = (t) => document.getElementById('content').innerHTML = t;
const pull = fetch(FILENAME, {headers: {'Accept': 'text/markdown'}})
.then((res) => {
if (!res.ok)
throw new Error(`${res.statusText} (${res.status})`);
return res.text()
})
</script>
<script defer>
pull.then((text) => set(marked(text)))
.catch((err) => set(`<code class="error">Failed to load ${FILENAME}: ${err.message}</code>`))
</script>
</head>
<body>
<div class="container box" style="margin:5rem auto;padding:4rem">
<div id="content"></div>
</div>
</body>
</html>
@waszil
Copy link

waszil commented Oct 6, 2020

Great stuff! Thanks a lot! Is it possible to embed images somehow? I tried to place the image next to the md file and tried with several urls for the image, but haven't succeeded.
-- EDIT: it works with absoulte URLs.

@ElnuDev
Copy link

ElnuDev commented Jan 23, 2021

Is there any way to modify this to work with PHP files?

@flocsy
Copy link

flocsy commented Feb 13, 2023

thanks!

@Stephen-Harold
Copy link

A Big Thank You for Posting.
Great work!

@fabiang
Copy link

fabiang commented Jun 6, 2023

Wow just awesome. Thanks.

@jw8957
Copy link

jw8957 commented Aug 24, 2023

Worked in my machine. Thanks for the solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment