Skip to content

Instantly share code, notes, and snippets.

@jeremy-w
Last active January 3, 2016 07:19
Show Gist options
  • Save jeremy-w/8429124 to your computer and use it in GitHub Desktop.
Save jeremy-w/8429124 to your computer and use it in GitHub Desktop.
User script to Markdownify text in ADN Alpha. Oddly, I seem to have to launch it and hit Run to have it actually run on the page just now. :\
// ==UserScript==
// @id com.jeremywsherman.adn-alpha-markdown
// @name Styled Markdown Text for ADN Alpha
// @version 1.0
// @namespace com.jeremywsherman
// @author Jeremy W. Sherman
// @description Recognizes and fontifies **bold**, *italics*, and `code` in ADN posts.
// @include https://alpha.app.net/
// @run-at document-end
// @grant GM_xpath
// ==/UserScript==
// Inspired by: https://alpha.app.net/remus/post/19801955
var post_nodes = function () {
//console.log('post nodes')
var result = document.evaluate(
"//span[contains(@class, 'post-content')]", document, null/*nsResolver*/,
XPathResult.ANY_TYPE, null/*result*/);
var nodes = [], node = null;
while ((node = result.iterateNext())) {
nodes.push(node);
}
return nodes;
}
var markdownify = function (text) {
//console.log('markdownify', text)
bolded = text.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>');
italicized = bolded.replace(/\*([^*]+)\*/g, '<em>$1</em>');
coded = italicized.replace(/`([^`]+)`/g, '<tt>$1</tt>');
return coded;
}
var nodes = post_nodes();
var i = 0, e = nodes.length;
console.log('found ', e, ' posts')
for (; i < e; ++i) {
var html = nodes[i].innerHTML;
var marked_up = markdownify(html);
nodes[i].innerHTML = coded;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment