Created
March 2, 2012 03:30
-
-
Save yanyaoer/1955374 to your computer and use it in GitHub Desktop.
markdown for v2ex content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name markdown for v2ex content | |
// @namespace http://www.v2ex.com/ | |
// @include http://www.v2ex.com/t/* | |
// ==/UserScript== | |
/* | |
* About = { | |
* source: 'http://webreflection.blogspot.com/2012/02/js1k-markdown.html', | |
* preview: 'http://www.v2ex.com/t/28444', | |
* @TODO: [ | |
* 'ignore normal content OR add toggle button', | |
* 'reconvert origin link', | |
* 'support [word](http://) syntax', | |
* more... | |
* ] | |
* } | |
* */ | |
var markdown = function(f){/*!(C) WebReflection*/for(var b="</code></pre>",c="blockquote>",e="(?:\\r\\n|\\r|\\n|$)",d="(.+?)"+e,a=[],h=["&(?!#?[a-z0-9]+;)","&","<","<",">",">","^(?:\\t| {4})"+d,function(i,j){return a.push(j+"\n")&&"\0"},"^"+d+"=+"+e,"<h1>$1</h1>\n","^"+d+"-+"+e,"<h2>$1</h2>\n","^(#+)\\s*"+d,function(i,l,k,j){return"<h"+(j=l.length)+">"+k.replace(/#+$/,"")+"</h"+j+">\n"},"(?:\\* \\* |- - |\\*\\*|--)[-*][-* ]*"+e,"<hr/>\n"," +"+e,"<br/>","^ *(\\* |\\+ |- |\\d+. )"+d,function(i,l,k,j){return"<"+(j=/^\d/.test(l)?"ol>":"ul>")+"<li>"+markdown(k)+"</li></"+j},"</(ul|ol)>\\s*<\\1>","","([_*]{1,2})([^\\2]+?)(\\1)",function(i,l,k,j){return"<"+(j=l.length==2?"strong>":"em>")+k+"</"+j},"\\[(.+?)\\]\\((.+?) (\"|')(.+?)(\\3)\\)",'<a href="$2" title="$4">$1</a>',"^> "+d,function(i,j){return"<"+c+markdown(j)+"</"+c},"</"+c+"\\s*<"+c,"","(`{1,2})([^\\r\\n]+?)\\1","<code>$2</code>","\\0",function(i){return"<pre><code>"+a.shift()+b},b+"\\s*<pre><code>",""],g=0;g<h.length;){f=f.replace(RegExp(h[g++],"gm"),h[g++])}return f}; | |
this.onload = function () { | |
var content = document.querySelectorAll('.content'); | |
for (var i=0; i<content.length; i++) { | |
var c = content[i]; | |
c.innerHTML = markdown(c.textContent); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment