Skip to content

Instantly share code, notes, and snippets.

@parttimer777
Created March 4, 2012 17:28
Show Gist options
  • Save parttimer777/1974003 to your computer and use it in GitHub Desktop.
Save parttimer777/1974003 to your computer and use it in GitHub Desktop.
markdown notes
<!--
Markdown Notes - Displays markdown templates in HTML
https://gist.github.com/1974003
About
-----
Renders a markdown template (commonly used in github READMEs) in HTML using the showdown.js lib. Automatically generates a table of contents with linkable anchors.
Usage
-----
0) Create a directory to place all the files below into
1) Download showdown.js (http://softwaremaniacs.org/playground/showdown-highlight/showdown.js)
2) Paste this gist in a file (e.g. notes.html)
3) Create a markdown template file of the same name as the gist file plus '.txt' extension (e.g. notes.txt)
4) Point browser to notes.html
For multiple templates, create a symlink to the gist html file with the same name as the template name.
Author
------
Dmitri Rodik <dmitri@dmitri.tv>
License
-------
MIT
-->
<html>
<head>
<title>Notes</title>
<style type="text/css">
html,body {
font-family: Helvetica, Arial, Verdana, sans-serif;
background-color: #fff;
font-size: 90%;
}
pre {
border: 1px solid #c0c0c0;
padding: 1px;
background: #eee
}
</style>
</head>
<body>
<div id="toc"></div>
<div id="markdown"></div>
<script src="showdown.js"></script>
<script>
//
// automatically get the filename of the notes from the URL
// e.g. mywebserver.com/notes.html -> notes.txt
// create multiple notes by creating symlinks to this file
var url = window.location.pathname;
var filename = url.substring(url.lastIndexOf('/')+1);
var markup_filename = filename;
var extension_pos = markup_filename.indexOf('.');
if (extension_pos != -1)
markup_filename = markup_filename.substring(0, extension_pos);
var request = new XMLHttpRequest();
request.open('GET', markup_filename + '.txt?cachebust='+Math.random(), false);
request.send(null);
if (request.status == 200) {
var converter = new Showdown.converter();
var html = converter.makeHtml(request.responseText);
document.getElementById("markdown").innerHTML = html;
// generate the table of contents
var toc_markup = '# Table Of Contents\n';
var markdown_elem = document.getElementById("markdown");
var elements = markdown_elem.childNodes;
var top_header = '';
for (var i = 0; i < elements.length; i++) {
var elem = elements[i];
var tag = elem.tagName;
if (!tag) continue;
// match on header tags H1, H2, H3...
var match = /^H(\d)$/.exec(tag);
if (!match || !match[1]) continue;
var header = elem.innerHTML;
var headerNum = match[1];
if (headerNum == 1)
top_header = header;
var anchor = top_header.substring(0, 30);
if (top_header != header)
anchor += '-' + header.substring(0, 60);
anchor = anchor.toLowerCase().replace(/[^a-zA-Z0-9]+/g, '_');
// indent header num times
for (var j = 1; j < headerNum; j++)
toc_markup += ' ';
// append the TOC anchor link
toc_markup += '- [' + header + '](' + filename + '#' + anchor + ') \n';
// set the ID (also the the link anchor)
elem.setAttribute("id", anchor);
}
// convert TOC markup to HTML and insert into page
document.getElementById('toc').innerHTML = converter.makeHtml(toc_markup);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment