forked from xen/markdown-tg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown-tg.min.js
1 lines (1 loc) · 10.9 KB
/
markdown-tg.min.js
1
(function(){var CR_NEWLINE_R=/\r\n?/g;var TAB_R=/\t/g;var FORMFEED_R=/\f/g;var preprocess=function(source){return source.replace(CR_NEWLINE_R,"\n").replace(FORMFEED_R,"").replace(TAB_R," ")};var parserFor=function(rules){var ruleList=Object.keys(rules);ruleList.forEach(function(type){var order=rules[type].order;if((typeof order!=="number"||!isFinite(order))&&typeof console!=="undefined"){console.warn("markdown-tg: Invalid order for rule `"+type+"`: "+order)}});ruleList.sort(function(typeA,typeB){var ruleA=rules[typeA];var ruleB=rules[typeB];var orderA=ruleA.order;var orderB=ruleB.order;if(orderA!==orderB){return orderA-orderB}var secondaryOrderA=ruleA.quality?0:1;var secondaryOrderB=ruleB.quality?0:1;if(secondaryOrderA!==secondaryOrderB){return secondaryOrderA-secondaryOrderB}else if(typeA<typeB){return-1}else if(typeA>typeB){return 1}else{return 0}});var nestedParse=function(source,state){var result=[];state=state||{};var prevCapture="";while(source){var ruleType=null;var rule=null;var capture=null;var quality=NaN;var i=0;var currRuleType=ruleList[0];var currRule=rules[currRuleType];do{var currOrder=currRule.order;var currCapture=currRule.match(source,state,prevCapture);if(currCapture){var currQuality=currRule.quality?currRule.quality(currCapture,state,prevCapture):0;if(!(currQuality<=quality)){ruleType=currRuleType;rule=currRule;capture=currCapture;quality=currQuality}}i++;currRuleType=ruleList[i];currRule=rules[currRuleType]}while(currRule&&(!capture||currRule.order===currOrder&&currRule.quality));if(!capture){throw new Error("could not find rule to match content: "+source)}var parsed=rule.parse(capture,nestedParse,state);if(Array.isArray(parsed)){Array.prototype.push.apply(result,parsed)}else{if(parsed.type==null){parsed.type=ruleType}result.push(parsed)}prevCapture=capture[0];source=source.substring(prevCapture.length)}return result};var outerParse=function(source,state){return nestedParse(preprocess(source),state)};return outerParse};var inlineRegex=function(regex){var match=function(source,state){if(state.inline){return regex.exec(source)}else{return null}};match.regex=regex;return match};var blockRegex=function(regex){var match=function(source,state){if(state.inline){return null}else{return regex.exec(source)}};match.regex=regex;return match};var anyScopeRegex=function(regex){var match=function(source,state){return regex.exec(source)};match.regex=regex;return match};var reactFor=function(outputFunc){var nestedOutput=function(ast,state){state=state||{};if(Array.isArray(ast)){var oldKey=state.key;var result=[];var lastWasString=false;for(var i=0;i<ast.length;i++){state.key=""+i;var nodeOut=nestedOutput(ast[i],state);var isString=typeof nodeOut==="string";if(isString&&lastWasString){result[result.length-1]+=nodeOut}else{result.push(nodeOut)}lastWasString=isString}state.key=oldKey;return result}else{return outputFunc(ast,nestedOutput,state)}};return nestedOutput};var htmlFor=function(outputFunc){var nestedOutput=function(ast,state){state=state||{};if(Array.isArray(ast)){return ast.map(function(node){return nestedOutput(node,state)}).join("")}else{return outputFunc(ast,nestedOutput,state)}};return nestedOutput};var TYPE_SYMBOL=typeof Symbol==="function"&&Symbol.for&&Symbol.for("react.element")||60103;var reactElement=function(type,key,props){return{$$typeof:TYPE_SYMBOL,type:type,key:key,ref:null,props:props,_owner:null}};var htmlTag=function(tagName,content,attributes,isClosed){attributes=attributes||{};isClosed=typeof isClosed!=="undefined"?isClosed:true;var attributeString="";for(var attr in attributes){if(Object.prototype.hasOwnProperty.call(attributes,attr)&&attributes[attr]){attributeString+=" "+attr+'="'+attributes[attr]+'"'}}var unclosedTag="<"+tagName+attributeString+">";if(isClosed){return unclosedTag+content+"</"+tagName+">"}else{return unclosedTag}};var EMPTY_PROPS={};var sanitizeUrl=function(url){if(url==null){return null}try{var prot=decodeURIComponent(url).replace(/[^A-Za-z0-9\/:]/g,"").toLowerCase();if(prot.indexOf("javascript:")===0){return null}}catch(e){return null}return url};var UNESCAPE_URL_R=/\\([^0-9A-Za-z\s])/g;var unescapeUrl=function(rawUrlString){return rawUrlString.replace(UNESCAPE_URL_R,"$1")};var parseInline=function(parse,content,state){var isCurrentlyInline=state.inline||false;state.inline=true;var result=parse(content,state);state.inline=isCurrentlyInline;return result};var parseBlock=function(parse,content,state){var isCurrentlyInline=state.inline||false;state.inline=false;var result=parse(content+"\n\n",state);state.inline=isCurrentlyInline;return result};var parseCaptureInline=function(capture,parse,state){return{content:parseInline(parse,capture[1],state)}};var ignoreCapture=function(){return{}};var BLOCK_END_R=/\n{2,}$/;var LIST_BLOCK_END_R=BLOCK_END_R;var LIST_ITEM_END_R=/ *\n+$/;var LIST_LOOKBEHIND_R=/(?:^|\n)( *)$/;var LINK_INSIDE="(?:\\[[^\\]]*\\]|[^\\[\\]]|\\](?=[^\\[]*\\]))*";var LINK_HREF_AND_TITLE="\\s*<?((?:[^\\s\\\\]|\\\\.)*?)>?(?:\\s+['\"]([\\s\\S]*?)['\"])?\\s*";var AUTOLINK_MAILTO_CHECK_R=/mailto:/i;var parseRef=function(capture,state,refNode){var ref=(capture[2]||capture[1]).replace(/\s+/g," ").toLowerCase();if(state._defs&&state._defs[ref]){var def=state._defs[ref];refNode.target=def.target;refNode.title=def.title}state._refs=state._refs||{};state._refs[ref]=state._refs[ref]||[];state._refs[ref].push(refNode);return refNode};var defaultRules={codeBlock:{match:blockRegex(/^(?: [^\n]+\n*)+(?:\n *)+\n/),parse:function(capture,parse,state){var content=capture[0].replace(/^ /gm,"").replace(/\n+$/,"");return{lang:undefined,content:content}},react:function(node,output,state){var className=node.lang?"markdown-code-"+node.lang:undefined;return reactElement("pre",state.key,{children:reactElement("code",null,{className:className,children:node.content})})},html:function(node,output,state){var className=node.lang?"markdown-code-"+node.lang:undefined;var codeBlock=htmlTag("code",node.content,{class:className});return htmlTag("pre",codeBlock)}},fence:{match:blockRegex(/^ *(`{3,}) *(\S+)? *\n([\s\S]+?)\s*\1 *(?:\n *)+\n/),parse:function(capture,parse,state){return{type:"codeBlock",lang:capture[2]||undefined,content:capture[3]}}},newline:{match:blockRegex(/^(?:\n *)*\n/),parse:ignoreCapture,react:function(node,output,state){return"\n"},html:function(node,output,state){return"\n"}},paragraph:{match:blockRegex(/^((?:[^\n]|\n(?! *\n))+)(?:\n *)+\n/),parse:parseCaptureInline,react:function(node,output,state){return reactElement("div",state.key,{className:"paragraph",children:output(node.content,state)})},html:function(node,output,state){var attributes={class:"paragraph"};return htmlTag("div",output(node.content,state),attributes)}},escape:{match:inlineRegex(/^\\([^0-9A-Za-z\s])/),parse:function(capture,parse,state){return{type:"text",content:capture[1]}}},autolink:{match:inlineRegex(/^<([^ >]+:\/[^ >]+)>/),parse:function(capture,parse,state){return{type:"link",content:[{type:"text",content:capture[1]}],target:capture[1]}}},mailto:{match:inlineRegex(/^<([^ >]+@[^ >]+)>/),parse:function(capture,parse,state){var address=capture[1];var target=capture[1];if(!AUTOLINK_MAILTO_CHECK_R.test(target)){target="mailto:"+target}return{type:"link",content:[{type:"text",content:address}],target:target}}},url:{match:inlineRegex(/^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/),parse:function(capture,parse,state){return{type:"link",content:[{type:"text",content:capture[1]}],target:capture[1],title:undefined}}},link:{match:inlineRegex(new RegExp("^\\[("+LINK_INSIDE+")\\]\\("+LINK_HREF_AND_TITLE+"\\)")),parse:function(capture,parse,state){var link={content:parse(capture[1],state),target:unescapeUrl(capture[2]),title:capture[3]};return link},react:function(node,output,state){return reactElement("a",state.key,{href:sanitizeUrl(node.target),title:node.title,children:output(node.content,state)})},html:function(node,output,state){var attributes={href:sanitizeUrl(node.target),title:node.title};return htmlTag("a",output(node.content,state),attributes)}},em:{match:inlineRegex(/^_(?=\S)([\s\S]*?\S)_/),quality:function(capture){return capture[0].length+.2},parse:function(capture,parse,state){return{content:parse(capture[2]||capture[1],state)}},react:function(node,output,state){return reactElement("em",state.key,{children:output(node.content,state)})},html:function(node,output,state){return htmlTag("em",output(node.content,state))}},strong:{match:inlineRegex(/^\*([\s\S]+?)\*(?!\*)/),quality:function(capture){return capture[0].length+.1},parse:parseCaptureInline,react:function(node,output,state){return reactElement("strong",state.key,{children:output(node.content,state)})},html:function(node,output,state){return htmlTag("strong",output(node.content,state))}},inlineCode:{match:inlineRegex(/^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/),parse:function(capture,parse,state){return{content:capture[2]}},react:function(node,output,state){return reactElement("code",state.key,{children:node.content})},html:function(node,output,state){return htmlTag("code",node.content)}},br:{match:anyScopeRegex(/^ {2,}\n/),parse:ignoreCapture,react:function(node,output,state){return reactElement("br",state.key,EMPTY_PROPS)},html:function(node,output,state){return"<br>"}},text:{match:inlineRegex(/^[\s\S]+?(?=[^0-9A-Za-z\s\u00c0-\uffff]|\n\n| {2,}\n|\w+:\S|$)/),parse:function(capture,parse,state){return{content:capture[0]}},react:function(node,output,state){return node.content},html:function(node,output,state){return node.content}}};Object.keys(defaultRules).forEach(function(type,i){defaultRules[type].order=i});var ruleOutput=function(rules,property){if(!property&&typeof console!=="undefined"){console.warn("markdown-tg ruleOutput should take 'react' or "+"'html' as the second argument.")}property=property||"react";var nestedRuleOutput=function(ast,outputFunc,state){return rules[ast.type][property](ast,outputFunc,state)};return nestedRuleOutput};var defaultRawParse=parserFor(defaultRules);var defaultBlockParse=function(source){return defaultRawParse(source+"\n\n",{inline:false})};var defaultInlineParse=function(source){return defaultRawParse(source,{inline:true})};var defaultImplicitParse=function(source){return defaultRawParse(source,{inline:!BLOCK_END_R.test(source)})};var defaultReactOutput=reactFor(ruleOutput(defaultRules,"react"));var defaultHtmlOutput=htmlFor(ruleOutput(defaultRules,"html"));var markdownTg={defaultRules:defaultRules,parserFor:parserFor,ruleOutput:ruleOutput,reactFor:reactFor,htmlFor:htmlFor,inlineRegex:inlineRegex,blockRegex:blockRegex,anyScopeRegex:anyScopeRegex,parseInline:parseInline,parseBlock:parseBlock,defaultRawParse:defaultRawParse,defaultBlockParse:defaultBlockParse,defaultInlineParse:defaultInlineParse,defaultImplicitParse:defaultImplicitParse,defaultReactOutput:defaultReactOutput,defaultHtmlOutput:defaultHtmlOutput,preprocess:preprocess,sanitizeUrl:sanitizeUrl,unescapeUrl:unescapeUrl,defaultParse:defaultImplicitParse,outputFor:reactFor,defaultOutput:defaultReactOutput};if(typeof module!=="undefined"&&module.exports){module.exports=markdownTg}else if(typeof global!=="undefined"){global.markdownTg=markdownTg}else{window.markdownTg=markdownTg}})();