Skip to content

Instantly share code, notes, and snippets.

@rummik
Created March 22, 2014 21:50
Show Gist options
  • Save rummik/9714949 to your computer and use it in GitHub Desktop.
Save rummik/9714949 to your computer and use it in GitHub Desktop.
// JS hooks for toggling style (think a wordy `$('.toggle-source a').click(function() { ... });`)
[].forEach.call(document.querySelectorAll('.toggle-source a'), function(element) {
element.onclick = function() {
document.body.className = this.getAttribute('id').replace('view-', '');
return false;
};
});
// a slightly Markdown parser
var xhr = new XMLHttpRequest();
xhr.open('GET', '../resume.md', true);
xhr.onreadystatechange = function() {
if (xhr.readyState != 4 || xhr.status != 200)
return;
var line = 0;
var lines = xhr.responseText.match(/\n/g).length;
var w = lines.toString().length;
var content = document.createElement('div');
content.className = 'content';
// Markdown-ish parser -- includes some extra things, slightly clobbers other things
content.innerHTML = xhr.responseText
// links
.replace(/<([^>\s]+)>/g, function(g, match) {
var link = /^.+@.+$/.test(match) ? 'mailto:' + match : match;
return '<a href="' + link + '" class="quicklink">' + match + '</a>';
})
.replace(/\[([^\]]+)\]\(([^\)]+)\)/g, '<a href="$2" class="link">$1</a><a href="$2" class="link-href">$2</a>')
// headers
.replace(/^(.+)\n(=+)$/mg, '<h1>$1</h1>\n<span class="header-line">$2</span>')
.replace(/^(.+)\n(-+)$/mg, '<h2>$1</h2>\n<span class="header-line">$2</span>')
.replace(/^(#+)(.+)$/mg, function(r, hash, text) {
var n = hash.length;
return '<span class="header-hash">' + hash + '</span><h' + n + '>' + text + '</h' + n + '>';
})
// lists...don't ask. it's lazy
.replace(/^(\s*)(?:[-*])((?:[^|\n]|\n )+)/mg, '$1<li>$2</li>')
.replace(/((?:<li>(?:.|\n)+?<\/li>\n?)+)/g, '<ul>$1</ul>')
.replace(/^(.+?)(\s*)\|((?:[^|\n]|\n )+)$/mg, '<dt>$1</dt><dd>$3</dd>')
.replace(/((?:<dt>(?:.|\n)+?<\/dt><dd>(?:.|\n)+?<\/dd>\n?)+)/g, '<dl>$1</dl>')
// HTML comments
.replace(/<(!--.*--)>/g, '<span class="comment">&lt;$1&gt;</span>')
// paragraphs
.replace(/^(\s?\w(?:.|\n *\w)+)/mg, '<p>$1</p>')
// line numbers...it's better not to ask how much of a mess this makes
.replace(/^/mg, function() {
var l = (++line).toString();
return '<span class="line">' + (new Array(w - l.length + 1).join(' ')) + l + '</span> ';
});
document.body.appendChild(content);
};
xhr.send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment