Skip to content

Instantly share code, notes, and snippets.

@sffc
Last active September 5, 2018 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sffc/4789c562f636260a49f9e6bd26f39557 to your computer and use it in GitHub Desktop.
Save sffc/4789c562f636260a49f9e6bd26f39557 to your computer and use it in GitHub Desktop.
Apache config to render *.md files as HTML (client-side). Prefer server-side rendering? See https://github.com/sffc/apache-php-markdown/blob/master/README.md
# File: /etc/apache2/conf-available/markdown.conf
#
# Copyright (c) 2018 Shane F. Carr
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
RewriteEngine on
RewriteCond %{HTTP_ACCEPT} !text\/markdown
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -f
RewriteRule \.md$ /.markdown/render.html [PT]
Alias /.markdown /var/www/markdown
<!DOCTYPE html>
<!--
File: /var/www/markdown/render.html
Copyright (c) 2018 Shane F. Carr
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<html>
<head>
<meta charset="utf-8"/>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/showdown/1.8.6/showdown.min.js"></script>
<link rel="stylesheet" type="text/css" href="//markdowncss.github.io/splendor/css/splendor.css"/>
</head>
<body>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
// Perform the request in the main thread to prevent "flashes" on the page
xhr.open("GET", document.location.href, false);
xhr.setRequestHeader("Accept", "text/markdown");
xhr.send(null);
if (xhr.status !== 200) {
alert("Unknown error; please report: " + xhr.statusText);
}
var converter = new showdown.Converter();
var html = converter.makeHtml(xhr.responseText);
document.body.innerHTML = html;
var h1 = document.getElementsByTagName("h1");
if (h1.length > 0) {
document.title = h1[0].textContent;
}
</script>
</body>
</html>
# Example edit to: /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
Include /etc/apache2/conf-enabled/markdown.conf
</VirtualHost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment