使用浏览器预览本地Markdown文件的JavaScript脚本
## 使用浏览器预览本地Markdown文件的JavaScript脚本
```JavaScript
// ==UserScript==
// @id 763221847@qq.com.com
// @namespace https://gitee.com/isteng/
// @name markdown_preview
// @name:zh-CN Markdown本地预览
// @version 0.12
// @description 预览本地Markdown文件
// @AuThor Teng
// @match file:///*/*.md*
// @match file:///*/*.markdown*
// @grant None
// ==/UserScript==
var i=0;var len=0
// markdown转HTML代码
!function(e){"use strict";var t={newline:/^\n+/,code:/^( {4}[^\n]+\n*)+/,fences:f,hr:/^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/,heading:/^ *(#{1,6}) *([^\n]+?) *(?:#+ *)?(?:\n+|$)/,nptable:f,blockquote:/^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,list:/^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,html:"^ {0,3}(?:<(script|pre|style)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)|comment[^\\n]*(\\n+|$)|<\\?[\\s\\S]*?\\?>\\n*|<![\\s\\S]*?>\\n*|<!\\*?\\]\\]>\\n*|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:\\n{2,}|$)|<(?!script|pre|style)([\\w-]*)(?:attribute)*? */?>(?=\\h*\\n)[\\s\\S]*?(?:\\n{2,}|$)|</(?!script|pre|style)[\\w-]*\\s*>(?=\\h*\\n)[\\s\\S]*?(?:\\n{2,}|$))",def:/^ {0,3}\[(label)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)(title))? *(?:\n+|$)/,table:f,lheading:/^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,paragraph:/^([^\n]+(?:\n(?!hr|heading|lheading| {0,3}>|<\/?(?:tag)(?: +|\n|\/?>)|<(?:script|pre|style|!--))[^\n]+)*)/,text:/^[^\n]+/};function n(e){this.tokens=[],this.tokens.links=Object.create(null),this.options=e||k.defaults,this.rules=t.normal,this.options.pedantic?this.rules=t.pedantic:this.options.gfm&&(this.options.tables?this.rules=t.tables:this.rules=t.gfm)}t._label=/(?!\s*\])(?:\\[\[\]]|[^\[\]])+/,t._title=/(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/,t.def=p(t.def).replace("label",t._label).replace("title",t._title).getRegex(),t.bullet=/(?:[*+-]|\d+\.)/,t.item=/^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/,t.item=p(t.item,"gm").replace(/bull/g,t.bullet).getRegex(),t.list=p(t.list).replace(/bull/g,t.bullet).replace("hr","\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))").replace("def","\\n+(?="+t.def.source+")").getRegex(),t._tag="address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul",t._comment=/<!--(?!-?>)[\s\S]*?-->/,t.html=p(t.html,"i").replace("comment",t._comment).replace("tag",t._tag).replace("attribute",/ +[\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/).getRegex(),t.paragraph=p(t.paragraph).replace("hr",t.hr).replace("heading",t.heading).replace("lheading",t.lheading).replace("tag",t._tag).getRegex(),t.blockquote=p(t.blockquote).replace("paragraph",t.paragraph).getRegex(),t.normal=d({},t),t.gfm=d({},t.normal,{fences:/^ *(`{3,}|~{3,})[ \.]*(\S+)? *\n([\s\S]*?)\n? *\1 *(?:\n+|$)/,paragraph:/^/,heading:/^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)/}),t.gfm.paragraph=p(t.paragraph).replace("(?!","(?!"+t.gfm.fences.source.replace("\\1","\\2")+"|"+t.list.source.replace("\\1","\\3")+"|").getRegex(),t.tables=d({},t.gfm,{nptable:/^ *([^|\n ].*\|.*)\n *([-:]+ *\|[-| :]*)(?:\n((?:.*[^>\n ].*(?:\n|$))*)\n*|$)/,table:/^ *\|(.+)\n *\|?( *[-:]+[-| :]*)(?:\n((?: *[^>\n ].*(?:\n|$))*)\n*|$)/}),t.pedantic=d({},t.normal,{html:p("^ *(?:comment *(?:\\n|\\s*$)|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)|<tag(?:\"[^\"]*\"|'[^']*'|\\s[^'\"/>\\s]*)*?/?> *(?:\\n{2,}|\\s*$))").replace("comment",t._comment).replace(/tag/g,"(?!(?:a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)\\b)\\w+(?!:|[^\\w\\s@]*@)\\b").getRegex(),def:/^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/}),n.rules=t,n.lex=function(e,t){return new n(t).lex(e)},n.prototype.lex=function(e){return e=e.replace(/\r\n|\r/g,"\n").replace(/\t/g," ").replace(/\u00a0/g," ").replace(/\u2424/g,"\n"),this.token(e,!0)},n.prototype.token=function(e,n){var r,s,i,l,o,a,h,p,u,c,g,f,d,k,x,y;for(e=e.replace(/^ +$/gm,"");e;)if((i=this.rules.newline.exec(e))&&(e=e.substring(i.length),i.length>1&&this.tokens.push({type:"space"})),i=this.rules.code.exec(e))e=e.substring(i.length),i=i.replace(/^ {4}/gm,""),this.tokens.push({type:"code",text:this.options.pedantic?i:m(i,"\n")});else if(i=this.rules.fences.exec(e))e=e.substring(i.length),this.tokens.push({type:"code",lang:i,text:i||""});else if(i=this.rules.heading.exec(e))e=e.substring(i.length),this.tokens.push({type:"heading",depth:i.length,text:i});else if(n&&(i=this.rules.nptable.exec(e))&&(a={type:"table",header:b(i.replace(/^ *| *\| *$/g,"")),align:i.replace(/^ *|\| *$/g,"").split(/ *\| */),cells:i?i.replace(/\n$/,"").split("\n"):[]}).header.length===a.align.length){for(e=e.substring(i.length),g=0;g<a.align.length;g++)/^ *-+: *$/.test(a.align)?a.align="right":/^ *:-+: *$/.test(a.align)?a.align="center":/^ *:-+ *$/.test(a.align)?a.align="left":a.align=null;for(g=0;g<a.cells.length;g++)a.cells=b(a.cells,a.header.length);this.tokens.push(a)}else if(i=this.rules.hr.exec(e))e=e.substring(i.length),this.tokens.push({type:"hr"});else if(i=this.rules.blockquote.exec(e))e=e.substring(i.length),this.tokens.push({type:"blockquote_start"}),i=i.replace(/^ *> ?/gm,""),this.token(i,n),this.tokens.push({type:"blockquote_end"});else if(i=this.rules.list.exec(e)){for(e=e.substring(i.length),h={type:"list_start",ordered:k=(l=i).length>1,start:k?+l:"",loose:!1},this.tokens.push(h),p=[],r=!1,d=(i=i.match(this.rules.item)).length,g=0;g<d;g++)c=(a=i).length,~(a=a.replace(/^ *([*+-]|\d+\.) +/,"")).indexOf("\n ")&&(c-=a.length,a=this.options.pedantic?a.replace(/^ {1,4}/gm,""):a.replace(new RegExp("^ {1,"+c+"}","gm"),"")),this.options.smartLists&&g!==d-1&&(l===(o=t.bullet.exec(i))||l.length>1&&o.length>1||(e=i.slice(g+1).join("\n")+e,g=d-1)),s=r||/\n\n(?!\s*$)/.test(a),g!==d-1&&(r="\n"===a.charAt(a.length-1),s||(s=r)),s&&(h.loose=!0),y=void 0,(x=/^\[[ xX]\] /.test(a))&&(y=" "!==a,a=a.replace(/^\[[ xX]\] +/,"")),u={type:"list_item_start",task:x,checked:y,loose:s},p.push(u),this.tokens.push(u),this.token(a,!1),this.tokens.push({type:"list_item_end"});if(h.loose)for(d=p.length,g=0;g<d;g++)p.loose=!0;this.tokens.push({type:"list_end"})}else if(i=this.rules.html.exec(e))e=e.substring(i.length),this.tokens.push({type:this.options.sanitize?"paragraph":"html",pre:!this.options.sanitizer&&("pre"===i||"script"===i||"style"===i),text:i});else if(n&&(i=this.rules.def.exec(e)))e=e.substring(i.length),i&&(i=i.substring(1,i.length-1)),f=i.toLowerCase().replace(/\s+/g," "),this.tokens.links||(this.tokens.links={href:i,title:i});else if(n&&(i=this.rules.table.exec(e))&&(a={type:"table",header:b(i.replace(/^ *| *\| *$/g,"")),align:i.replace(/^ *|\| *$/g,"").split(/ *\| */),cells:i?i.replace(/(?: *\| *)?\n$/,"").split("\n"):[]}).header.length===a.align.length){for(e=e.substring(i.length),g=0;g<a.align.length;g++)/^ *-+: *$/.test(a.align)?a.align="right":/^ *:-+: *$/.test(a.align)?a.align="center":/^ *:-+ *$/.test(a.align)?a.align="left":a.align=null;for(g=0;g<a.cells.length;g++)a.cells=b(a.cells.replace(/^ *\| *| *\| *$/g,""),a.header.length);this.tokens.push(a)}else if(i=this.rules.lheading.exec(e))e=e.substring(i.length),this.tokens.push({type:"heading",depth:"="===i?1:2,text:i});else if(n&&(i=this.rules.paragraph.exec(e)))e=e.substring(i.length),this.tokens.push({type:"paragraph",text:"\n"===i.charAt(i.length-1)?i.slice(0,-1):i});else if(i=this.rules.text.exec(e))e=e.substring(i.length),this.tokens.push({type:"text",text:i});else if(e)throw new Error("Infinite loop on byte: "+e.charCodeAt(0));return this.tokens};var r={escape:/^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/,autolink:/^<(scheme:[^\s\x00-\x1f<>]*|email)>/,url:f,tag:"^comment|^</[\\w:-]*\\s*>|^<[\\w-]*(?:attribute)*?\\s*/?>|^<\\?[\\s\\S]*?\\?>|^<!+\\s[\\s\\S]*?>|^<!\\*?\\]\\]>",link:/^!?\[(label)\]\(href(?:\s+(title))?\s*\)/,reflink:/^!?\[(label)\]\[(?!\s*\])((?:\\[\[\]]?|[^\[\]\\])+)\]/,nolink:/^!?\[(?!\s*\])((?:\[[^\[\]]*\]|\\[\[\]]|[^\[\]])*)\](?:\[\])?/,strong:/^__([^\s][\s\S]*?[^\s])__(?!_)|^\*\*([^\s][\s\S]*?[^\s])\*\*(?!\*)|^__([^\s])__(?!_)|^\*\*([^\s])\*\*(?!\*)/,em:/^_([^\s][\s\S]*?[^\s_])_(?!_)|^_([^\s_][\s\S]*?[^\s])_(?!_)|^\*([^\s][\s\S]*?[^\s*])\*(?!\*)|^\*([^\s*][\s\S]*?[^\s])\*(?!\*)|^_([^\s_])_(?!_)|^\*([^\s*])\*(?!\*)/,code:/^(`+)\s*([\s\S]*?[^`]?)\s*\1(?!`)/,br:/^( {2,}|\\)\n(?!\s*$)/,del:f,text:/^[\s\S]+?(?=[\\<!\[`*]|\b_| {2,}\n|$)/};function s(e,t){if(this.options=t||k.defaults,this.links=e,this.rules=r.normal,this.renderer=this.options.renderer||new i,this.renderer.options=this.options,!this.links)throw new Error("Tokens array requires a `links` property.");this.options.pedantic?this.rules=r.pedantic:this.options.gfm&&(this.options.breaks?this.rules=r.breaks:this.rules=r.gfm)}function i(e){this.options=e||k.defaults}function l(){}function o(e){this.tokens=[],this.token=null,this.options=e||k.defaults,this.options.renderer=this.options.renderer||new i,this.renderer=this.options.renderer,this.renderer.options=this.options}function a(e,t){return e.replace(t?/&/g:/&(?!#?\w+;)/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")}function h(e){return e.replace(/&(#(?:\d+)|(?:#x+)|(?:\w+));?/gi,function(e,t){return"colon"===(t=t.toLowerCase())?":":"#"===t.charAt(0)?"x"===t.charAt(1)?String.fromCharCode(parseInt(t.substring(2),16)):String.fromCharCode(+t.substring(1)):""})}function p(e,t){return e=e.source||e,t=t||"",{replace:function(t,n){return n=(n=n.source||n).replace(/(^|[^\[])\^/g,"$1"),e=e.replace(t,n),this},getRegex:function(){return new RegExp(e,t)}}}function u(e,t){return c[" "+e]||(/^[^:]+:\/*[^/]*$/.test(e)?c[" "+e]=e+"/":c[" "+e]=m(e,"/",!0)),e=c[" "+e],"//"===t.slice(0,2)?e.replace(/:[\s\S]*/,":")+t:"/"===t.charAt(0)?e.replace(/(:\/*[^/]*)[\s\S]*/,"$1")+t:e+t}r._escapes=/\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/g,r._scheme=/{1,31}/,r._email=/+(@)(?:{0,61})?(?:\.(?:{0,61})?)+(?![-_])/,r.autolink=p(r.autolink).replace("scheme",r._scheme).replace("email",r._email).getRegex(),r._attribute=/\s+[\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/,r.tag=p(r.tag).replace("comment",t._comment).replace("attribute",r._attribute).getRegex(),r._label=/(?:\[[^\[\]]*\]|\\[\[\]]?|`[^`]*`|[^\[\]\\])*?/,r._href=/\s*(<(?:\\[<>]?|[^\s<>\\])*>|(?:\\[()]?|\([^\s\x00-\x1f\\]*\)|[^\s\x00-\x1f()\\])*?)/,r._title=/"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/,r.link=p(r.link).replace("label",r._label).replace("href",r._href).replace("title",r._title).getRegex(),r.reflink=p(r.reflink).replace("label",r._label).getRegex(),r.normal=d({},r),r.pedantic=d({},r.normal,{strong:/^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,em:/^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/,link:p(/^!?\[(label)\]\((.*?)\)/).replace("label",r._label).getRegex(),reflink:p(/^!?\[(label)\]\s*\[([^\]]*)\]/).replace("label",r._label).getRegex()}),r.gfm=d({},r.normal,{escape:p(r.escape).replace("])","~|])").getRegex(),url:p(/^((?:ftp|https?):\/\/|www\.)(?:+\.?)+[^\s<]*|^email/).replace("email",r._email).getRegex(),_backpedal:/(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?!+;$)|[?!.,:;*_~)]+(?!$))+/,del:/^~+(?=\S)([\s\S]*?\S)~+/,text:p(r.text).replace("]|","~]|").replace("|","|https?://|ftp://|www\\.|+@|").getRegex()}),r.breaks=d({},r.gfm,{br:p(r.br).replace("{2,}","*").getRegex(),text:p(r.gfm.text).replace("{2,}","*").getRegex()}),s.rules=r,s.output=function(e,t,n){return new s(t,n).output(e)},s.prototype.output=function(e){for(var t,n,r,i,l,o,h="";e;)if(l=this.rules.escape.exec(e))e=e.substring(l.length),h+=l;else if(l=this.rules.autolink.exec(e))e=e.substring(l.length),r="@"===l?"mailto:"+(n=a(this.mangle(l))):n=a(l),h+=this.renderer.link(r,null,n);else if(this.inLink||!(l=this.rules.url.exec(e))){if(l=this.rules.tag.exec(e))!this.inLink&&/^<a /i.test(l)?this.inLink=!0:this.inLink&&/^<\/a>/i.test(l)&&(this.inLink=!1),e=e.substring(l.length),h+=this.options.sanitize?this.options.sanitizer?this.options.sanitizer(l):a(l):l;else if(l=this.rules.link.exec(e))e=e.substring(l.length),this.inLink=!0,r=l,this.options.pedantic?(t=/^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(r))?(r=t,i=t):i="":i=l?l.slice(1,-1):"",r=r.trim().replace(/^<([\s\S]*)>$/,"$1"),h+=this.outputLink(l,{href:s.escapes(r),title:s.escapes(i)}),this.inLink=!1;else if((l=this.rules.reflink.exec(e))||(l=this.rules.nolink.exec(e))){if(e=e.substring(l.length),t=(l||l).replace(/\s+/g," "),!(t=this.links)||!t.href){h+=l.charAt(0),e=l.substring(1)+e;continue}this.inLink=!0,h+=this.outputLink(l,t),this.inLink=!1}else if(l=this.rules.strong.exec(e))e=e.substring(l.length),h+=this.renderer.strong(this.output(l||l||l||l));else if(l=this.rules.em.exec(e))e=e.substring(l.length),h+=this.renderer.em(this.output(l||l||l||l||l||l));else if(l=this.rules.code.exec(e))e=e.substring(l.length),h+=this.renderer.codespan(a(l.trim(),!0));else if(l=this.rules.br.exec(e))e=e.substring(l.length),h+=this.renderer.br();else if(l=this.rules.del.exec(e))e=e.substring(l.length),h+=this.renderer.del(this.output(l));else if(l=this.rules.text.exec(e))e=e.substring(l.length),h+=this.renderer.text(a(this.smartypants(l)));else if(e)throw new Error("Infinite loop on byte: "+e.charCodeAt(0))}else{do{o=l,l=this.rules._backpedal.exec(l)}while(o!==l);e=e.substring(l.length),"@"===l?r="mailto:"+(n=a(l)):(n=a(l),r="www."===l?"http://"+n:n),h+=this.renderer.link(r,null,n)}return h},s.escapes=function(e){return e?e.replace(s.rules._escapes,"$1"):e},s.prototype.outputLink=function(e,t){var n=t.href,r=t.title?a(t.title):null;return"!"!==e.charAt(0)?this.renderer.link(n,r,this.output(e)):this.renderer.image(n,r,a(e))},s.prototype.smartypants=function(e){return this.options.smartypants?e.replace(/---/g,"—").replace(/--/g,"–").replace(/(^|[-\u2014/(\[{"\s])'/g,"$1‘").replace(/'/g,"’").replace(/(^|[-\u2014/(\[{\u2018\s])"/g,"$1“").replace(/"/g,"”").replace(/\.{3}/g,"…"):e},s.prototype.mangle=function(e){if(!this.options.mangle)return e;for(var t,n="",r=e.length,s=0;s<r;s++)t=e.charCodeAt(s),Math.random()>.5&&(t="x"+t.toString(16)),n+="&#"+t+";";return n},i.prototype.code=function(e,t,n){if(this.options.highlight){var r=this.options.highlight(e,t);null!=r&&r!==e&&(n=!0,e=r)}return t?'<pre><code class="'+this.options.langPrefix+a(t,!0)+'">'+(n?e:a(e,!0))+"</code></pre>\n":"<pre><code>"+(n?e:a(e,!0))+"</code></pre>"},i.prototype.blockquote=function(e){return"<blockquote>\n"+e+"</blockquote>\n"},i.prototype.html=function(e){return e},i.prototype.heading=function(e,t,n){return this.options.headerIds?"<h"+t+' id="'+this.options.headerPrefix+n.toLowerCase().replace(/[^\w]+/g,"-")+'">'+e+"</h"+t+">\n":"<h"+t+">"+e+"</h"+t+">\n"},i.prototype.hr=function(){return this.options.xhtml?"<hr/>\n":"<hr>\n"},i.prototype.list=function(e,t,n){var r=t?"ol":"ul";return"<"+r+(t&&1!==n?' start="'+n+'"':"")+">\n"+e+"</"+r+">\n"},i.prototype.listitem=function(e){return"<li>"+e+"</li>\n"},i.prototype.checkbox=function(e){return"<input "+(e?'checked="" ':"")+'disabled="" type="checkbox"'+(this.options.xhtml?" /":"")+"> "},i.prototype.paragraph=function(e){return"<p>"+e+"</p>\n"},i.prototype.table=function(e,t){return t&&(t="<tbody>"+t+"</tbody>"),"<table>\n<thead>\n"+e+"</thead>\n"+t+"</table>\n"},i.prototype.tablerow=function(e){return"<tr>\n"+e+"</tr>\n"},i.prototype.tablecell=function(e,t){var n=t.header?"th":"td";return(t.align?"<"+n+' align="'+t.align+'">':"<"+n+">")+e+"</"+n+">\n"},i.prototype.strong=function(e){return"<strong>"+e+"</strong>"},i.prototype.em=function(e){return"<em>"+e+"</em>"},i.prototype.codespan=function(e){return"<code>"+e+"</code>"},i.prototype.br=function(){return this.options.xhtml?"<br/>":"<br>"},i.prototype.del=function(e){return"<del>"+e+"</del>"},i.prototype.link=function(e,t,n){if(this.options.sanitize){try{var r=decodeURIComponent(h(e)).replace(/[^\w:]/g,"").toLowerCase()}catch(e){return n}if(0===r.indexOf("javascript:")||0===r.indexOf("vbscript:")||0===r.indexOf("data:"))return n}this.options.baseUrl&&!g.test(e)&&(e=u(this.options.baseUrl,e));try{e=encodeURI(e).replace(/%25/g,"%")}catch(e){return n}var s='<a href="'+a(e)+'"';return t&&(s+=' title="'+t+'"'),s+=">"+n+"</a>"},i.prototype.image=function(e,t,n){this.options.baseUrl&&!g.test(e)&&(e=u(this.options.baseUrl,e));var r='<img src="'+e+'" alt="'+n+'"';return t&&(r+=' title="'+t+'"'),r+=this.options.xhtml?"/>":">"},i.prototype.text=function(e){return e},l.prototype.strong=l.prototype.em=l.prototype.codespan=l.prototype.del=l.prototype.text=function(e){return e},l.prototype.link=l.prototype.image=function(e,t,n){return""+n},l.prototype.br=function(){return""},o.parse=function(e,t){return new o(t).parse(e)},o.prototype.parse=function(e){this.inline=new s(e.links,this.options),this.inlineText=new s(e.links,d({},this.options,{renderer:new l})),this.tokens=e.reverse();for(var t="";this.next();)t+=this.tok();return t},o.prototype.next=function(){return this.token=this.tokens.pop()},o.prototype.peek=function(){return this.tokens||0},o.prototype.parseText=function(){for(var e=this.token.text;"text"===this.peek().type;)e+="\n"+this.next().text;return this.inline.output(e)},o.prototype.tok=function(){switch(this.token.type){case"space":return"";case"hr":return this.renderer.hr();case"heading":return this.renderer.heading(this.inline.output(this.token.text),this.token.depth,h(this.inlineText.output(this.token.text)));case"code":return this.renderer.code(this.token.text,this.token.lang,this.token.escaped);case"table":var e,t,n,r,s="",i="";for(n="",e=0;e<this.token.header.length;e++)n+=this.renderer.tablecell(this.inline.output(this.token.header),{header:!0,align:this.token.align});for(s+=this.renderer.tablerow(n),e=0;e<this.token.cells.length;e++){for(t=this.token.cells,n="",r=0;r<t.length;r++)n+=this.renderer.tablecell(this.inline.output(t),{header:!1,align:this.token.align});i+=this.renderer.tablerow(n)}return this.renderer.table(s,i);case"blockquote_start":for(i="";"blockquote_end"!==this.next().type;)i+=this.tok();return this.renderer.blockquote(i);case"list_start":i="";for(var l=this.token.ordered,o=this.token.start;"list_end"!==this.next().type;)i+=this.tok();return this.renderer.list(i,l,o);case"list_item_start":i="";var a=this.token.loose;for(this.token.task&&(i+=this.renderer.checkbox(this.token.checked));"list_item_end"!==this.next().type;)i+=a||"text"!==this.token.type?this.tok():this.parseText();return this.renderer.listitem(i);case"html":return this.renderer.html(this.token.text);case"paragraph":return this.renderer.paragraph(this.inline.output(this.token.text));case"text":return this.renderer.paragraph(this.parseText())}};var c={},g=/^$|^*:|^[?#]/i;function f(){}function d(e){for(var t,n,r=1;r<arguments.length;r++)for(n in t=arguments)Object.prototype.hasOwnProperty.call(t,n)&&(e=t);return e}function b(e,t){var n=e.replace(/\|/g,function(e,t,n){for(var r=!1,s=t;--s>=0&&"\\"===n;)r=!r;return r?"|":" |"}).split(/ \|/),r=0;if(n.length>t)n.splice(t);else for(;n.length<t;)n.push("");for(;r<n.length;r++)n=n.trim().replace(/\\\|/g,"|");return n}function m(e,t,n){if(0===e.length)return"";for(var r=0;r<e.length;){var s=e.charAt(e.length-r-1);if(s!==t||n){if(s===t||!n)break;r++}else r++}return e.substr(0,e.length-r)}function k(e,t,r){if(null==e)throw new Error("marked(): input parameter is undefined or null");if("string"!=typeof e)throw new Error("marked(): input parameter is of type "+Object.prototype.toString.call(e)+", string expected");if(r||"function"==typeof t){r||(r=t,t=null);var s,i,l=(t=d({},k.defaults,t||{})).highlight,h=0;try{s=n.lex(e,t)}catch(e){return r(e)}i=s.length;var p=function(e){if(e)return t.highlight=l,r(e);var n;try{n=o.parse(s,t)}catch(t){e=t}return t.highlight=l,e?r(e):r(null,n)};if(!l||l.length<3)return p();if(delete t.highlight,!i)return p();for(;h<s.length;h++)!function(e){"code"!==e.type?--i||p():l(e.text,e.lang,function(t,n){return t?p(t):null==n||n===e.text?--i||p():(e.text=n,e.escaped=!0,void(--i||p()))})}(s)}else try{return t&&(t=d({},k.defaults,t)),o.parse(n.lex(e,t),t)}catch(e){if(e.message+="\nPlease report this to https://github.com/markedjs/marked.",(t||k.defaults).silent)return"<p>An error occurred:</p><pre>"+a(e.message+"",!0)+"</pre>";throw e}}f.exec=f,k.options=k.setOptions=function(e){return d(k.defaults,e),k},k.getDefaults=function(){return{baseUrl:null,breaks:!1,gfm:!0,headerIds:!0,headerPrefix:"",highlight:null,langPrefix:"language-",mangle:!0,pedantic:!1,renderer:new i,sanitize:!1,sanitizer:null,silent:!1,smartLists:!1,smartypants:!1,tables:!0,xhtml:!1}},k.defaults=k.getDefaults(),k.Parser=o,k.parser=o.parse,k.Renderer=i,k.TextRenderer=l,k.Lexer=n,k.lexer=n.lex,k.InlineLexer=s,k.inlineLexer=s.output,k.parse=k,"undefined"!=typeof module&&"object"==typeof exports?module.exports=k:"function"==typeof define&&define.amd?define(function(){return k}):e.marked=k}(this||("undefined"!=typeof window?window:global));
// markdownCss样式
var markcss = 'a{text-decoration:none}.markdown_views{font-family:-apple-system,SF UI Text,Arial,PingFang SC,Hiragino Sans GB,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif,SimHei,SimSun}.markdown_views P{font-size:16px;color:#4f4f4f;font-weight:400;line-height:26px;margin:0 0 16px;text-align:justify}.markdown_views strong{font-weight:700}.markdown_views i,address,cite,dfn,em,var{font-style:italic}.markdown_views *{box-sizing:border-box}.markdown_views h1,.markdown_views h2,.markdown_views h3,.markdown_views h4,.markdown_views h5,.markdown_views h6{color:#4f4f4f;margin:8px 0 16px;font-weight:700}.markdown_views ol,.markdown_views ul{margin:0 0 24px;padding:0}.markdown_views ul ol{margin:0 0 24px 32px}.markdown_views ul li{list-style-type:disc;margin:8px 0 0 32px}.markdown_views ol li{list-style-type:decimal;margin-left:40px;margin-top:8px}.markdown_views img{margin:24px 0;max-width:100%}.markdown_views h1{font-size:28px;line-height:36px}.markdown_views h2{font-size:24px;line-height:32px}.markdown_views h3{font-size:22px;line-height:30px}.markdown_views h4{font-size:20px;line-height:28px}.markdown_views h5{font-size:18px;line-height:26px}.markdown_views h6{font-size:16px;line-height:24px}.markdown_views h1 code{font-size:28px}.markdown_views h2 code{font-size:24px}.markdown_views h3 code{font-size:22px}.markdown_views h4 code{font-size:20px}.markdown_views h5 code{font-size:18px}.markdown_views h6 code{font-size:16px}.markdown_views blockquote{display:block;padding:16px;margin:0 0 24px;border-left:8px solid #DDDFE4;background:#EEF0F4;overflow:auto;overflow-scrolling:touch;word-wrap:normal;word-break:normal}.markdown_views blockquote ol,.markdown_views blockquote ul{margin-bottom:0;padding:0;font-size:14px;line-height:22px}.markdown_views blockquote ol li,.markdown_views blockquote ul li{margin-bottom:0}.markdown_views blockquote p{font-size:14px;line-height:22px;color:#999;font-weight:400;margin-bottom:0}.markdown_views hr{margin:24px 0;border:none;border-bottom:solid #ddd 1px}.markdown_views table tr{border:0;border-top:1px solid #DDD;background-color:#fff}.markdown_views table{border-collapse:collapse;display:table;width:100%;text-align:center;margin-bottom:24px}.markdown_views tbody{border:0}.markdown_views table tr:nth-child(2n){background-color:#F7F7F7}.markdown_views table tr td,.markdown_views table tr th{font-size:14px;color:#4f4f4f;line-height:22px;border:1px solid #DDD;padding:8px;text-align:left;word-wrap:break-word;word-break:normal;vertical-align:middle}.markdown_views table tr td code,.markdown_views table tr th code{white-space:normal;word-break:break-all}.markdown_views table tr th{font-weight:700;background-color:#eff3f5}.markdown_views dl{margin:24px}.markdown_views dl dt{margin:8px;font-weight:700}.markdown_views dl dt dd{margin:8px}.markdown_views abbr,.markdown_views abbr{cursor:help;border-bottom:1px dotted #999}.markdown_views .initialism{font-size:90%;text-transform:uppercase}.markdown_views pre{margin-bottom:24px}.hljs{display:block;padding:8px}.hljs-comment,.hljs-comment *,.hljs-javadoc,.hljs-template_comment{color:#800}.clojure .hljs-built_in,.hljs-keyword,.hljs-list .hljs-title,.hljs-request,.hljs-status,.hljs-tag .hljs-title,.hljs-winutils,.http .hljs-title,.method,.nginx .hljs-title,.setting .hljs-value,.tex .hljs-command{color:#008}.hljs-envvar{color:#660}.hljs-attr_selector,.hljs-cdata,.hljs-date,.hljs-filter .hljs-argument,.hljs-regexp{color:#080}.clojure .hljs-attribute,.css .hljs-function,.css .hljs-value .hljs-number,.hljs-decorator,.hljs-hexcolor,.hljs-literal,.hljs-number,.hljs-pi,.hljs-prompt,.hljs-shebang,.hljs-sub .hljs-identifier,.hljs-symbol,.hljs-tag,.hljs-tag .hljs-keyword,.ini .hljs-title{color:#066}.clojure .hljs-title,.haskell .hljs-type,.hljs-built_in,.hljs-class .hljs-id,.hljs-class .hljs-title,.hljs-doctype,.hljs-javadoctag,.hljs-params,.hljs-phpdoc,.hljs-tag .hljs-attribute,.hljs-typename,.hljs-variable,.hljs-yardoctag,.setting{color:#4f4f4f}.css .hljs-tag,.hljs-pseudo,.hljs-rules .hljs-property{color:#000}.css .hljs-class,.css .hljs-id{color:#9b703f}.hljs-value .hljs-important{color:#f70;font-weight:700}.hljs-rules .hljs-keyword{color:#c5af75}.apache .hljs-sqbracket,.hljs-annotation,.nginx .hljs-built_in{color:#9b859d}.tex .hljs-formula{background-color:#eee;font-style:italic}.diff .hljs-header,.hljs-chunk{color:grey;font-weight:700}.diff .hljs-change{background-color:#bccff9}.hljs-addition{background-color:#baeeba}.hljs-deletion{background-color:#ffc8bd}.hljs-comment .hljs-yardoctag{font-weight:700}.apache .hljs-cbracket,.apache .hljs-tag,.asciidoc .hljs-header,.bash .hljs-variable,.coffeescript .hljs-attribute,.django .hljs-variable,.erlang_repl .hljs-function_or_atom,.haml .hljs-symbol,.hljs-addition,.hljs-aggregate,.hljs-constant,.hljs-flow,.hljs-parent,.hljs-pragma,.hljs-preprocessor,.hljs-preprocessor *,.hljs-rules .hljs-value,.hljs-rules .hljs-value .hljs-number,.hljs-stream,.hljs-string,.hljs-tag .hljs-value,.hljs-template_tag,.hljs-title,.markdown .hljs-header,.ruby .hljs-symbol,.ruby .hljs-symbol .hljs-string,.smalltalk .hljs-class,.tex .hljs-command,.tex .hljs-special{color:#090}.clojure .hljs-built_in,.hljs,.hljs-subst,.hljs-tag .hljs-title,.lisp .hljs-title,.nginx .hljs-title{color:#4f4f4f}.apache .hljs-cbracket,.apache .hljs-tag,.asciidoc .hljs-header,.bash .hljs-variable,.coffeescript .hljs-attribute,.django .hljs-variable,.erlang_repl .hljs-function_or_atom,.haml .hljs-symbol,.hljs-addition,.hljs-aggregate,.hljs-constant,.hljs-flow,.hljs-parent,.hljs-pragma,.hljs-preprocessor,.hljs-rules .hljs-value,.hljs-rules .hljs-value .hljs-number,.hljs-stream,.hljs-string,.hljs-tag .hljs-value,.hljs-template_tag,.hljs-title,.markdown .hljs-header,.ruby .hljs-symbol,.ruby .hljs-symbol .hljs-string,.smalltalk .hljs-class,.tex .hljs-command,.tex .hljs-special{color:#090}.markdown_views code,.markdown_views kbd,.markdown_views pre,.markdown_views samp{font-family:Consolas,Inconsolata,Courier,monospace;font-size:14px;line-height:22px;color:#000}.markdown_views code{color:#c7254e;background-color:#f9f2f4;border-radius:2px;padding:4px 2px 0}.markdown_views a{color:#4EA1DB;text-decoration:none}.markdown_views a:focus,.markdown_views a:hover{color:#ca0c16}.markdown_views a:visited{color:#6795B5}.markdown_views .footnote{vertical-align:top;position:relative;top:-4px;font-size:12px}.markdown_views .footnotes ol li{font-size:14px;line-height:22px;margin:0 0 8px 24px}.markdown_views .flow-chart,.markdown_views .sequence-diagram{text-align:center;margin-bottom:24px;font-size:14px!important}.markdown_views .flow-chart ,.markdown_views .flow-chart ,.markdown_views .flow-chart ,.markdown_views .sequence-diagram ,.markdown_views .sequence-diagram ,.markdown_views .sequence-diagram {fill:#4f4f4f}.markdown_views .flow-chart ,.markdown_views .sequence-diagram {stroke:#4f4f4f}.markdown_views .MathJax_SVG_Display{text-align:center;margin:24px 0;font-size:18px;font-weight:400;color:#4f4f4f;position:relative;text-indent:0;max-width:none;max-height:none;min-width:0;min-height:0;width:100%}.markdown_views .toc{font-size:16px;line-height:24px;margin:0 0 24px;padding:0}.markdown_views .toc ul{margin:0 0 8px;padding:0}.markdown_views .toc ul li{list-style-type:none;margin:8px 0 0 24px}.markdown_views pre code{display:block;font-size:14px;line-height:22px;overflow-x:auto;padding:0;color:#000;white-space:pre;word-wrap:normal;background-color:#f6f8fa;border-radius:4px}.markdown_views .prettyprint,.markdown_views pre.prettyprint{margin:0 0 24px;padding:8px 16px 4px 56px;background-color:#f6f8fa;border:none}.prettyprint{position:relative;overflow-y:hidden;overflow-x:auto}.markdown_views .prettyprint .pre-numbering{position:absolute;width:48px;background-color:#eef0f4;top:0;left:0;margin:0;padding:8px 0;list-style:none;text-align:right}.markdown_views .pre-numbering li{padding:0 8px;list-style:none;margin:0}.markdown_views dl dd{margin:0 0 8px 40px}.markdown_views kbd{padding:2px 8px;border:1px solid rgba(63,63,63,.25);box-shadow:0 1px 0 rgba(63,63,63,.25);background-color:#fff;color:#333;border-radius:4px;display:inline-block;margin:0 2px;white-space:nowrap}'
// 读取正文迁移到新视图
var premd = document.body.firstChild
if (premd.nodeName == 'PRE') {
document.body.innerHTML =
'<style type="text/css">'+markcss+'</style>\
<div id="container" style="width:100%">\
<div id="toc" style="width:180px;height:99%;float:left;position:fixed;overflow-x:auto;overflow-y:auto;white-space:nowrap;border-right:1px solid #eeeeee;">\
</div>\
<div id="content" class="markdown_views" style="width:80%;float:right;">'+marked(premd.textContent)+'</div>\
</div > ';}
// 把markdown视图内的a标签链接打开方式转为新窗口
var aa = document.querySelectorAll(".markdown_views a")
for (i=0, len=aa.length; i<len; i++) {
aa.target = "_blank"
}
// 把所有h标签转为目录生成目录
var a = document.querySelectorAll("h1,h2,h3,h4,h5,h6")
var toc = document.querySelector("#toc");
var txt = ''
for (i=0, len=a.length; i<len; i++) {
var nunu = " ".repeat(Number(a.tagName.slice(-1)) - 1);
a.id = a.textContent;
txt = txt + "<a href='#" + a.textContent + "'>" + nunu + a.textContent + "</a><br>";
//if(){};
}
toc.innerHTML = txt
``` 本帖最后由 Xw丶小威 于 2018-7-17 12:57 编辑
## 支持楼主
* 雷锋精神
* 才思广益
想法很新颖 是不是可以用浏览器预览makedown文件, sworddancing 发表于 2018-7-23 00:48
是不是可以用浏览器预览makedown文件,
ang, 要支持偷油猴暴力猴之类的浏览器,, 比如火狐谷歌什么的 谢谢,已搞定
页:
[1]