markdown.js
2.61 KB
CodeMirror.defineMode("markdown",function(t,n){function e(t,n,e){return n.f=n.inline=e,e(t,n)}function r(t,n,e){return n.f=n.block=e,e(t,n)}function i(t){return t.em=!1,t.strong=!1,null}function o(t,n){var r;if(n.indentationDiff>=4)return n.indentation-=n.indentationDiff,t.skipToEnd(),x;if(t.eatSpace())return null;if("#"===t.peek()||t.match(T))n.header=!0;else if(t.eat(">"))n.indentation++,n.quote=!0;else{if("["===t.peek())return e(t,n,d);if(t.match($,!0))return M;if(r=t.match(D,!0)||t.match(E,!0))return n.indentation+=r[0].length,v}return e(t,n,n.inline)}function a(t,n){var e=p.token(t,n.htmlState);return"tag"!==e||"openTag"===n.htmlState.type||n.htmlState.context||(n.f=c,n.block=o),e}function u(t){var n=[];return t.strong?n.push(t.em?C:y):t.em&&n.push(w),t.header&&n.push(k),t.quote&&n.push(S),n.length?n.join(" "):null}function f(t,n){if(t.match(_,!0))return u(n)}function c(t,n){var i=n.text(t,n);if("undefined"!=typeof i)return i;var o=t.next();if("\\"===o)return t.next(),u(n);if("`"===o)return e(t,n,g(x,"`"));if("["===o)return e(t,n,l);if("<"===o&&t.match(/^\w/,!1))return t.backUp(1),r(t,n,a);var f=u(n);return"*"===o||"_"===o?t.eat(o)?(n.strong=!n.strong)?u(n):f:(n.em=!n.em)?u(n):f:u(n)}function l(t,n){for(;!t.eol();){var e=t.next();if("\\"===e&&t.next(),"]"===e)return n.inline=n.f=m,b}return b}function m(t,n){t.eatSpace();var r=t.next();return"("===r||"["===r?e(t,n,g(q,"("===r?")":"]")):"error"}function d(t,n){return t.match(/^[^\]]*\]:/,!0)?(n.f=h,b):e(t,n,c)}function h(t,n){return t.eatSpace(),t.match(/^[^\s]+/,!0),n.f=n.inline=c,q}function s(t){return s[t]||(s[t]=new RegExp("^(?:[^\\\\\\"+t+"]|\\\\.)*(?:\\"+t+"|$)")),s[t]}function g(t,n,e){return e=e||c,function(r,i){return r.match(s(n)),i.inline=i.f=e,t}}var p=CodeMirror.getMode(t,{name:"xml",htmlMode:!0}),k="header",x="comment",S="quote",v="string",M="hr",b="link",q="string",w="em",y="strong",C="emstrong",$=/^([*\-=_])(?:\s*\1){2,}\s*$/,D=/^[*\-+]\s+/,E=/^[0-9]+\.\s+/,T=/^(?:\={3,}|-{3,})$/,_=/^[^\[*_\\<>`]+/;return{startState:function(){return{f:o,block:o,htmlState:p.startState(),indentation:0,inline:c,text:f,em:!1,strong:!1,header:!1,quote:!1}},copyState:function(t){return{f:t.f,block:t.block,htmlState:CodeMirror.copyState(p,t.htmlState),indentation:t.indentation,inline:t.inline,text:t.text,em:t.em,strong:t.strong,header:t.header,quote:t.quote}},token:function(t,n){if(t.sol()){if(t.match(/^\s*$/,!0))return i(n);n.header=!1,n.quote=!1,n.f=n.block;var e=t.match(/^\s*/,!0)[0].replace(/\t/g," ").length;if(n.indentationDiff=e-n.indentation,n.indentation=e,e>0)return null}return n.f(t,n)},blankLine:i,getType:u}},"xml"),CodeMirror.defineMIME("text/x-markdown","markdown");