@khanacademy/simple-markdown 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/es/index.js CHANGED
@@ -1,2 +1,1665 @@
1
- var t,n,r,e,u,o,i,a,l=/\r\n?/g,c=/\t/g,f=/\f/g,p=function(t){return t.replace(l,"\n").replace(f,"").replace(c," ")},s=function(t,n){var r=t||{};if(null!=n)for(var e in n)Object.prototype.hasOwnProperty.call(n,e)&&(r[e]=n[e]);return r},h=function(t,n){var r,e=Object.keys(t).filter((function(n){var r=t[n];if(null==r||null==r.match)return!1;var e=r.order;return"number"==typeof e&&isFinite(e)||"undefined"==typeof console||console.warn("simple-markdown: Invalid order for rule `"+n+"`: "+String(e)),!0}));e.sort((function(n,r){var e=t[n],u=t[r],o=e.order,i=u.order;if(o!==i)return o-i;var a=e.quality?0:1,l=u.quality?0:1;return a!==l?a-l:n<r?-1:n>r?1:0}));var u=function n(u,o){var i=[];for(r=o=o||r;u;){var a=null,l=null,c=null,f=NaN,p=0,s=e[0],h=t[s];do{var d=h.order,g=null==o.prevCapture?"":o.prevCapture[0],m=h.match(u,o,g);if(m){var y=h.quality?h.quality(m,o,g):0;y<=f||(a=s,l=h,c=m,f=y)}p++,s=e[p],h=t[s]}while(h&&(!c||h.order===d&&h.quality));if(null==l||null==c)throw new Error("Could not find a matching rule for the below content. The rule with highest `order` should always match content provided to it. Check the definition of `match` for '"+e[e.length-1]+"'. It seems to not match the following source:\n"+u);if(c.index)throw new Error("`match` must return a capture starting at index 0 (the current parse index). Did you forget a ^ at the start of the RegExp?");var v=l.parse(c,n,o);Array.isArray(v)?Array.prototype.push.apply(i,v):(null==v.type&&(v.type=a),i.push(v)),o.prevCapture=c,u=u.substring(o.prevCapture[0].length)}return i};return function(t,e){return(r=s(e,n)).inline||r.disableAutoBlockNewlines||(t+="\n\n"),r.prevCapture=null,u(p(t),r)}},d=function(t){var n=function(n,r,e){return r.inline?t.exec(n):null};return n.regex=t,n},g=function(t){var n=function(n,r){return r.inline?null:t.exec(n)};return n.regex=t,n},m=function(t){var n=function(n,r){return t.exec(n)};return n.regex=t,n},y="function"==typeof Symbol&&Symbol.for&&Symbol.for("react.element")||60103,v=function(t,n,r){return{$$typeof:y,type:t,key:null==n?void 0:n,ref:null,props:r,_owner:null}},S=function(t,n,r,e){r=r||{},e=void 0===e||e;var u="";for(var o in r){var i=r[o];Object.prototype.hasOwnProperty.call(r,o)&&i&&(u+=" "+_(o)+'="'+_(i)+'"')}var a="<"+t+u+">";return e?a+n+"</"+t+">":a},k={},w=function(t){if(null==t)return null;try{var n=new URL(t,"https://localhost").protocol;if(0===n.indexOf("javascript:")||0===n.indexOf("vbscript:")||0===n.indexOf("data:"))return null}catch(t){return null}return t},x=/[<>&"']/g,b={"<":"&lt;",">":"&gt;","&":"&amp;",'"':"&quot;","'":"&#x27;","/":"&#x2F;","`":"&#96;"},_=function(t){return String(t).replace(x,(function(t){return b[t]}))},E=/\\([^0-9A-Za-z\s])/g,R=function(t){return t.replace(E,"$1")},A=function(t,n,r){var e=r.inline||!1;r.inline=!0;var u=t(n,r);return r.inline=e,u},$=function(t,n,r){return{content:A(n,t[1],r)}},O=function(){return{}},T=new RegExp("^( *)((?:[*+-]|\\d+\\.)) +"),j=new RegExp("( *)((?:[*+-]|\\d+\\.)) +[^\\n]*(?:\\n(?!\\1(?:[*+-]|\\d+\\.) )[^\\n]*)*(\n|$)","gm"),P=/\n{2,}$/,C=/^ (?= *`)|(` *) $/g,q=P,F=/ *\n+$/,B=new RegExp("^( *)((?:[*+-]|\\d+\\.)) [\\s\\S]+?(?:\n{2,}(?! )(?!\\1(?:[*+-]|\\d+\\.) )\\n*|\\s*\n*$)"),N=/(?:^|\n)( *)$/,L=(t=/^ *\| *| *\| *$/g,n=/ *$/,r=/^ *-+: *$/,e=/^ *:-+: *$/,u=/^ *:-+ *$/,o=function(t){return r.test(t)?"right":e.test(t)?"center":u.test(t)?"left":null},i=function(t,r,e,u){var o=e.inTable;e.inTable=!0;var i=r(t.trim(),e);e.inTable=o;var a=[[]];return i.forEach((function(t,r){"tableSeparator"===t.type?(!u||0!==r&&r!==i.length-1)&&a.push([]):("text"!==t.type||null!=i[r+1]&&"tableSeparator"!==i[r+1].type||(t.content=t.content.replace(n,"")),a[a.length-1].push(t))})),a},{parseTable:(a=function(n){return function(r,e,u){u.inline=!0;var a=i(r[1],e,u,n),l=function(n,r,e,u){return u&&(n=n.replace(t,"")),n.trim().split("|").map(o)}(r[2],0,0,n),c=function(t,n,r,e){return t.trim().split("\n").map((function(t){return i(t,n,r,e)}))}(r[3],e,u,n);return u.inline=!1,{type:"table",header:a,align:l,cells:c}}})(!0),parseNpTable:a(!1),TABLE_REGEX:/^ *(\|.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/,NPTABLE_REGEX:/^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/}),I="(?:\\[[^\\]]*\\]|[^\\[\\]]|\\](?=[^\\[]*\\]))*",z="\\s*<?((?:\\([^)]*\\)|[^\\s\\\\]|\\\\.)*?)>?(?:\\s+['\"]([\\s\\S]*?)['\"])?\\s*",G=/mailto:/i,X=function(t,n,r){var e=(t[2]||t[1]).replace(/\s+/g," ").toLowerCase();if(n._defs&&n._defs[e]){var u=n._defs[e];r.target=u.target,r.title=u.title}return n._refs=n._refs||{},n._refs[e]=n._refs[e]||[],n._refs[e].push(r),r},U=0,Z={Array:{react:function(t,n,r){for(var e=r.key,u=[],o=0,i=0;o<t.length;o++,i++){r.key=""+o;var a=t[o];if("text"===a.type)for(a={type:"text",content:a.content};o+1<t.length&&"text"===t[o+1].type;o++)a.content+=t[o+1].content;u.push(n(a,r))}return r.key=e,u},html:function(t,n,r){for(var e="",u=0;u<t.length;u++){var o=t[u];if("text"===o.type)for(o={type:"text",content:o.content};u+1<t.length&&"text"===t[u+1].type;u++)o.content+=t[u+1].content;e+=n(o,r)}return e}},heading:{order:U++,match:g(/^ *(#{1,6})([^\n]+?)#* *(?:\n *)+\n/),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){return{level:t[1].length,content:A(n,t[2].trim(),r)}})),react:function(t,n,r){return v("h"+t.level,r.key,{children:n(t.content,r)})},html:function(t,n,r){return S("h"+t.level,n(t.content,r))}},nptable:{order:U++,match:g(L.NPTABLE_REGEX),parse:L.parseNpTable,react:null,html:null},lheading:{order:U++,match:g(/^([^\n]+)\n *(=|-){3,} *(?:\n *)+\n/),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){return{type:"heading",level:"="===t[2]?1:2,content:A(n,t[1],r)}})),react:null,html:null},hr:{order:U++,match:g(/^( *[-*_]){3,} *(?:\n *)+\n/),parse:O,react:function(t,n,r){return v("hr",r.key,k)},html:function(t,n,r){return"<hr>"}},codeBlock:{order:U++,match:g(/^(?: [^\n]+\n*)+(?:\n *)+\n/),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){return{lang:void 0,content:t[0].replace(/^ /gm,"").replace(/\n+$/,"")}})),react:function(t,n,r){var e=t.lang?"markdown-code-"+t.lang:void 0;return v("pre",r.key,{children:v("code",null,{className:e,children:t.content})})},html:function(t,n,r){var e=t.lang?"markdown-code-"+t.lang:void 0,u=S("code",_(t.content),{class:e});return S("pre",u)}},fence:{order:U++,match:g(/^ *(`{3,}|~{3,}) *(?:(\S+) *)?\n([\s\S]+?)\n?\1 *(?:\n *)+\n/),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){return{type:"codeBlock",lang:t[2]||void 0,content:t[3]}})),react:null,html:null},blockQuote:{order:U++,match:g(/^( *>[^\n]+(\n[^\n]+)*\n*)+\n{2,}/),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){return{content:n(t[0].replace(/^ *> ?/gm,""),r)}})),react:function(t,n,r){return v("blockquote",r.key,{children:n(t.content,r)})},html:function(t,n,r){return S("blockquote",n(t.content,r))}},list:{order:U++,match:function(t,n){var r=null==n.prevCapture?"":n.prevCapture[0],e=N.exec(r),u=n._list||!n.inline;return e&&u?(t=e[1]+t,B.exec(t)):null},parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){var e=t[2],u=e.length>1,o=u?+e:void 0,i=t[0].replace(q,"\n").match(j),a=!1;return{ordered:u,start:o,items:i.map((function(t,e){var u=T.exec(t),o=u?u[0].length:0,l=new RegExp("^ {1,"+o+"}","gm"),c=t.replace(l,"").replace(T,""),f=e===i.length-1,p=-1!==c.indexOf("\n\n")||f&&a;a=p;var s,h=r.inline,d=r._list;r._list=!0,p?(r.inline=!1,s=c.replace(F,"\n\n")):(r.inline=!0,s=c.replace(F,""));var g=n(s,r);return r.inline=h,r._list=d,g}))}})),react:function(t,n,r){var e=t.ordered?"ol":"ul";return v(e,r.key,{start:t.start,children:t.items.map((function(t,e){return v("li",""+e,{children:n(t,r)})}))})},html:function(t,n,r){var e=t.items.map((function(t){return S("li",n(t,r))})).join(""),u=t.ordered?"ol":"ul",o={start:t.start};return S(u,e,o)}},def:{order:U++,match:g(/^ *\[([^\]]+)\]: *<?([^\s>]*)>?(?: +["(]([^\n]+)[")])? *\n(?: *\n)*/),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){var e=t[1].replace(/\s+/g," ").toLowerCase(),u=t[2],o=t[3];return r._refs&&r._refs[e]&&r._refs[e].forEach((function(t){t.target=u,t.title=o})),r._defs=r._defs||{},r._defs[e]={target:u,title:o},{def:e,target:u,title:o}})),react:function(){return null},html:function(){return""}},table:{order:U++,match:g(L.TABLE_REGEX),parse:L.parseTable,react:function(t,n,r){var e=function(n){return null==t.align[n]?{}:{textAlign:t.align[n]}},u=t.header.map((function(t,u){return v("th",""+u,{style:e(u),scope:"col",children:n(t,r)})})),o=t.cells.map((function(t,u){return v("tr",""+u,{children:t.map((function(t,u){return v("td",""+u,{style:e(u),children:n(t,r)})}))})}));return v("table",r.key,{children:[v("thead","thead",{children:v("tr",null,{children:u})}),v("tbody","tbody",{children:o})]})},html:function(t,n,r){var e=function(n){return null==t.align[n]?"":"text-align:"+t.align[n]+";"},u=t.header.map((function(t,u){return S("th",n(t,r),{style:e(u),scope:"col"})})).join(""),o=t.cells.map((function(t){var u=t.map((function(t,u){return S("td",n(t,r),{style:e(u)})})).join("");return S("tr",u)})).join(""),i=S("thead",S("tr",u)),a=S("tbody",o);return S("table",i+a)}},newline:{order:U++,match:g(/^(?:\n *)*\n/),parse:O,react:function(t,n,r){return"\n"},html:function(t,n,r){return"\n"}},paragraph:{order:U++,match:g(/^((?:[^\n]|\n(?! *\n))+)(?:\n *)+\n/),parse:$,react:function(t,n,r){return v("div",r.key,{className:"paragraph",children:n(t.content,r)})},html:function(t,n,r){return S("div",n(t.content,r),{class:"paragraph"})}},escape:{order:U++,match:d(/^\\([^0-9A-Za-z\s])/),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){return{type:"text",content:t[1]}})),react:null,html:null},tableSeparator:{order:U++,match:function(t,n){return n.inTable?/^ *\| */.exec(t):null},parse:function(){return{type:"tableSeparator"}},react:function(){return" | "},html:function(){return" &vert; "}},autolink:{order:U++,match:d(/^<([^: >]+:\/[^ >]+)>/),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){return{type:"link",content:[{type:"text",content:t[1]}],target:t[1]}})),react:null,html:null},mailto:{order:U++,match:d(/^<([^ >]+@[^ >]+)>/),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){var e=t[1],u=t[1];return G.test(u)||(u="mailto:"+u),{type:"link",content:[{type:"text",content:e}],target:u}})),react:null,html:null},url:{order:U++,match:d(/^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){return{type:"link",content:[{type:"text",content:t[1]}],target:t[1],title:void 0}})),react:null,html:null},link:{order:U++,match:d(new RegExp("^\\[("+I+")\\]\\("+z+"\\)")),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){return{content:n(t[1],r),target:R(t[2]),title:t[3]}})),react:function(t,n,r){return v("a",r.key,{href:w(t.target),title:t.title,children:n(t.content,r)})},html:function(t,n,r){var e={href:w(t.target),title:t.title};return S("a",n(t.content,r),e)}},image:{order:U++,match:d(new RegExp("^!\\[("+I+")\\]\\("+z+"\\)")),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){return{alt:t[1],target:R(t[2]),title:t[3]}})),react:function(t,n,r){return v("img",r.key,{src:w(t.target),alt:t.alt,title:t.title})},html:function(t,n,r){var e={src:w(t.target),alt:t.alt,title:t.title};return S("img","",e,!1)}},reflink:{order:U++,match:d(new RegExp("^\\[("+I+")\\]\\s*\\[([^\\]]*)\\]")),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){return X(t,r,{type:"link",content:n(t[1],r)})})),react:null,html:null},refimage:{order:U++,match:d(new RegExp("^!\\[("+I+")\\]\\s*\\[([^\\]]*)\\]")),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){return X(t,r,{type:"image",alt:t[1]})})),react:null,html:null},em:{order:U,match:d(new RegExp("^\\b_((?:__|\\\\[\\s\\S]|[^\\\\_])+?)_\\b|^\\*(?=\\S)((?:\\*\\*|\\\\[\\s\\S]|\\s+(?:\\\\[\\s\\S]|[^\\s\\*\\\\]|\\*\\*)|[^\\s\\*\\\\])+?)\\*(?!\\*)")),quality:function(t){return t[0].length+.2},parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){return{content:n(t[2]||t[1],r)}})),react:function(t,n,r){return v("em",r.key,{children:n(t.content,r)})},html:function(t,n,r){return S("em",n(t.content,r))}},strong:{order:U,match:d(/^\*\*((?:\\[\s\S]|[^\\])+?)\*\*(?!\*)/),quality:function(t){return t[0].length+.1},parse:$,react:function(t,n,r){return v("strong",r.key,{children:n(t.content,r)})},html:function(t,n,r){return S("strong",n(t.content,r))}},u:{order:U++,match:d(/^__((?:\\[\s\S]|[^\\])+?)__(?!_)/),quality:function(t){return t[0].length},parse:$,react:function(t,n,r){return v("u",r.key,{children:n(t.content,r)})},html:function(t,n,r){return S("u",n(t.content,r))}},del:{order:U++,match:d(/^~~(?=\S)((?:\\[\s\S]|~(?!~)|[^\s~\\]|\s(?!~~))+?)~~/),parse:$,react:function(t,n,r){return v("del",r.key,{children:n(t.content,r)})},html:function(t,n,r){return S("del",n(t.content,r))}},inlineCode:{order:U++,match:d(/^(`+)([\s\S]*?[^`])\1(?!`)/),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){return{content:t[2].replace(C,"$1")}})),react:function(t,n,r){return v("code",r.key,{children:t.content})},html:function(t,n,r){return S("code",_(t.content))}},br:{order:U++,match:m(/^ {2,}\n/),parse:O,react:function(t,n,r){return v("br",r.key,k)},html:function(t,n,r){return"<br>"}},text:{order:U++,match:m(/^[\s\S]+?(?=[^0-9A-Za-z\s\u00c0-\uffff]|\n\n| {2,}\n|\w+:\S|$)/),parse:function(t){function n(n,r,e){return t.apply(this,arguments)}return n.toString=function(){return t.toString()},n}((function(t,n,r){return{content:t[0]}})),react:function(t,n,r){return t.content},html:function(t,n,r){return _(t.content)}}},H=function(t,n){var r,e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(!n)throw new Error("simple-markdown: outputFor: `property` must be defined. if you just upgraded, you probably need to replace `outputFor` with `reactFor`");var u=t.Array||Z.Array,o=u[n];if(!o)throw new Error("simple-markdown: outputFor: to join nodes of type `"+n+"` you must provide an `Array:` joiner rule with that type, Please see the docs for details on specifying an Array rule.");var i=o,a=function e(u,o){return r=o=o||r,Array.isArray(u)?i(u,e,o):t[u.type][n](u,e,o)},l=function(t,n){return r=s(n,e),a(t,r)};return l},D=h(Z),M=function(t,n){return(n=n||{}).inline=!1,D(t,n)},Q=function(t,n){var r=P.test(t);return(n=n||{}).inline=!r,D(t,n)},J=H(Z,"react"),K=H(Z,"html"),V=function(t,n){return J(M(t,n),n)},W={defaultRules:Z,parserFor:h,outputFor:H,inlineRegex:d,blockRegex:g,anyScopeRegex:m,parseInline:A,parseBlock:function(t,n,r){var e=r.inline||!1;r.inline=!1;var u=t(n+"\n\n",r);return r.inline=e,u},markdownToReact:V,markdownToHtml:function(t,n){return K(M(t,n),n)},ReactMarkdown:function(t){var n={};for(var r in t)"source"!==r&&Object.prototype.hasOwnProperty.call(t,r)&&(n[r]=t[r]);return n.children=V(t.source),v("div",null,n)},defaultBlockParse:M,defaultInlineParse:function(t,n){return(n=n||{}).inline=!0,D(t,n)},defaultImplicitParse:Q,defaultReactOutput:J,defaultHtmlOutput:K,preprocess:p,sanitizeText:_,sanitizeUrl:w,unescapeUrl:R,htmlTag:S,reactElement:v,defaultRawParse:D,ruleOutput:function(t,n){n||"undefined"==typeof console||console.warn("simple-markdown ruleOutput should take 'react' or 'html' as the second argument.");return function(r,e,u){return t[r.type][n](r,e,u)}},reactFor:function(t){return function n(r,e){if(e=e||{},Array.isArray(r)){for(var u=e.key,o=[],i=null,a=0;a<r.length;a++){e.key=""+a;var l=n(r[a],e);"string"==typeof l&&"string"==typeof i?(i+=l,o[o.length-1]=i):(o.push(l),i=l)}return e.key=u,o}return t(r,n,e)}},htmlFor:function(t){return function n(r,e){return e=e||{},Array.isArray(r)?r.map((function(t){return n(t,e)})).join(""):t(r,n,e)}},defaultParse:function(){return"undefined"!=typeof console&&console.warn("defaultParse is deprecated, please use `defaultImplicitParse`"),Q.apply(null,arguments)},defaultOutput:function(){return"undefined"!=typeof console&&console.warn("defaultOutput is deprecated, please use `defaultReactOutput`"),J.apply(null,arguments)}};export{W as default};
1
+ /* eslint-disable prefer-spread, no-regex-spaces, no-unused-vars, guard-for-in, no-console, no-var */
2
+
3
+ /**
4
+ * Simple-Markdown
5
+ * ===============
6
+ *
7
+ * Simple-Markdown's primary goal is to be easy to adapt. It aims
8
+ * to be compliant with John Gruber's [Markdown Syntax page][1],
9
+ * but compatiblity with other markdown implementations' edge-cases
10
+ * will be sacrificed where it conflicts with simplicity or
11
+ * extensibility.
12
+ *
13
+ * If your goal is to simply embed a standard markdown implementation
14
+ * in your website, simple-markdown is probably not the best library
15
+ * for you (although it should work). But if you have struggled to
16
+ * customize an existing library to meet your needs, simple-markdown
17
+ * might be able to help.
18
+ *
19
+ * Many of the regexes and original logic has been adapted from
20
+ * the wonderful [marked.js](https://github.com/chjj/marked)
21
+ */
22
+ // Flow Type Definitions:
23
+ // We want to clarify our defaultRules types a little bit more so clients can
24
+ // reuse defaultRules built-ins. So we make some stronger guarantess when
25
+ // we can:
26
+ // End Flow Definitions
27
+ var CR_NEWLINE_R = /\r\n?/g;
28
+ var TAB_R = /\t/g;
29
+ var FORMFEED_R = /\f/g;
30
+ /**
31
+ * Turn various whitespace into easy-to-process whitespace
32
+ */
33
+
34
+ var preprocess = function preprocess(source) {
35
+ return source.replace(CR_NEWLINE_R, "\n").replace(FORMFEED_R, "").replace(TAB_R, " ");
36
+ };
37
+
38
+ var populateInitialState = function populateInitialState(givenState, defaultState) {
39
+ var state = givenState || {};
40
+
41
+ if (defaultState != null) {
42
+ for (var prop in defaultState) {
43
+ // $FlowFixMe
44
+ if (Object.prototype.hasOwnProperty.call(defaultState, prop)) {
45
+ state[prop] = defaultState[prop];
46
+ }
47
+ }
48
+ }
49
+
50
+ return state;
51
+ };
52
+ /**
53
+ * Creates a parser for a given set of rules, with the precedence
54
+ * specified as a list of rules.
55
+ *
56
+ * @param {SimpleMarkdown.ParserRules} rules
57
+ * an object containing
58
+ * rule type -> {match, order, parse} objects
59
+ * (lower order is higher precedence)
60
+ * @param {SimpleMarkdown.OptionalState} [defaultState]
61
+ *
62
+ * @returns {SimpleMarkdown.Parser}
63
+ * The resulting parse function, with the following parameters:
64
+ * @source: the input source string to be parsed
65
+ * @state: an optional object to be threaded through parse
66
+ * calls. Allows clients to add stateful operations to
67
+ * parsing, such as keeping track of how many levels deep
68
+ * some nesting is. For an example use-case, see passage-ref
69
+ * parsing in src/widgets/passage/passage-markdown.jsx
70
+ */
71
+
72
+
73
+ var parserFor = function parserFor(rules, defaultState) {
74
+ // Sorts rules in order of increasing order, then
75
+ // ascending rule name in case of ties.
76
+ var ruleList = Object.keys(rules).filter(function (type) {
77
+ var rule = rules[type];
78
+
79
+ if (rule == null || rule.match == null) {
80
+ return false;
81
+ }
82
+
83
+ var order = rule.order;
84
+
85
+ if ((typeof order !== "number" || !isFinite(order)) && typeof console !== "undefined") {
86
+ console.warn("simple-markdown: Invalid order for rule `" + type + "`: " + String(order));
87
+ }
88
+
89
+ return true;
90
+ });
91
+ ruleList.sort(function (typeA, typeB) {
92
+ var ruleA = rules[typeA];
93
+ var ruleB = rules[typeB];
94
+ var orderA = ruleA.order;
95
+ var orderB = ruleB.order; // First sort based on increasing order
96
+
97
+ if (orderA !== orderB) {
98
+ return orderA - orderB;
99
+ }
100
+
101
+ var secondaryOrderA = ruleA.quality ? 0 : 1;
102
+ var secondaryOrderB = ruleB.quality ? 0 : 1;
103
+
104
+ if (secondaryOrderA !== secondaryOrderB) {
105
+ return secondaryOrderA - secondaryOrderB; // Then based on increasing unicode lexicographic ordering
106
+ } else if (typeA < typeB) {
107
+ return -1;
108
+ } else if (typeA > typeB) {
109
+ return 1;
110
+ } else {
111
+ // Rules should never have the same name,
112
+ // but this is provided for completeness.
113
+ return 0;
114
+ }
115
+ });
116
+ var latestState;
117
+
118
+ var nestedParse = function nestedParse(source, state) {
119
+ var result = [];
120
+ state = state || latestState;
121
+ latestState = state;
122
+
123
+ while (source) {
124
+ // store the best match, it's rule, and quality:
125
+ var ruleType = null;
126
+ var rule = null;
127
+ var capture = null;
128
+ var quality = NaN; // loop control variables:
129
+
130
+ var i = 0;
131
+ var currRuleType = ruleList[0]; // $FlowFixMe
132
+
133
+ var currRule = rules[currRuleType];
134
+
135
+ do {
136
+ var currOrder = currRule.order;
137
+ var prevCaptureStr = state.prevCapture == null ? "" : state.prevCapture[0];
138
+ var currCapture = currRule.match(source, state, prevCaptureStr);
139
+
140
+ if (currCapture) {
141
+ var currQuality = currRule.quality ? currRule.quality(currCapture, state, prevCaptureStr) : 0; // This should always be true the first time because
142
+ // the initial quality is NaN (that's why there's the
143
+ // condition negation).
144
+
145
+ if (!(currQuality <= quality)) {
146
+ ruleType = currRuleType;
147
+ rule = currRule;
148
+ capture = currCapture;
149
+ quality = currQuality;
150
+ }
151
+ } // Move on to the next item.
152
+ // Note that this makes `currRule` be the next item
153
+
154
+
155
+ i++;
156
+ currRuleType = ruleList[i]; // $FlowFixMe
157
+
158
+ currRule = rules[currRuleType];
159
+ } while ( // keep looping while we're still within the ruleList
160
+ currRule && ( // if we don't have a match yet, continue
161
+ !capture || // or if we have a match, but the next rule is
162
+ // at the same order, and has a quality measurement
163
+ // functions, then this rule must have a quality
164
+ // measurement function (since they are sorted before
165
+ // those without), and we need to check if there is
166
+ // a better quality match
167
+ currRule.order === currOrder && currRule.quality)); // TODO(aria): Write tests for these
168
+
169
+
170
+ if (rule == null || capture == null) {
171
+ throw new Error("Could not find a matching rule for the below " + "content. The rule with highest `order` should " + "always match content provided to it. Check " + "the definition of `match` for '" + ruleList[ruleList.length - 1] + "'. It seems to not match the following source:\n" + source);
172
+ }
173
+
174
+ if (capture.index) {
175
+ // If present and non-zero, i.e. a non-^ regexp result:
176
+ throw new Error("`match` must return a capture starting at index 0 " + "(the current parse index). Did you forget a ^ at the " + "start of the RegExp?");
177
+ } // $FlowFixMe
178
+
179
+
180
+ var parsed = rule.parse(capture, nestedParse, state); // We maintain the same object here so that rules can
181
+ // store references to the objects they return and
182
+ // modify them later. (oops sorry! but this adds a lot
183
+ // of power--see reflinks.)
184
+
185
+ if (Array.isArray(parsed)) {
186
+ // $FlowFixMe
187
+ Array.prototype.push.apply(result, parsed);
188
+ } else {
189
+ // We also let rules override the default type of
190
+ // their parsed node if they would like to, so that
191
+ // there can be a single output function for all links,
192
+ // even if there are several rules to parse them.
193
+ if (parsed.type == null) {
194
+ // $FlowFixMe
195
+ parsed.type = ruleType;
196
+ }
197
+
198
+ result.push(parsed);
199
+ }
200
+
201
+ state.prevCapture = capture;
202
+ source = source.substring(state.prevCapture[0].length);
203
+ } // $FlowFixMe
204
+
205
+
206
+ return result;
207
+ };
208
+
209
+ var outerParse = function outerParse(source, state) {
210
+ latestState = populateInitialState(state, defaultState);
211
+
212
+ if (!latestState.inline && !latestState.disableAutoBlockNewlines) {
213
+ source = source + "\n\n";
214
+ } // We store the previous capture so that match functions can
215
+ // use some limited amount of lookbehind. Lists use this to
216
+ // ensure they don't match arbitrary '- ' or '* ' in inline
217
+ // text (see the list rule for more information). This stores
218
+ // the full regex capture object, if there is one.
219
+
220
+
221
+ latestState.prevCapture = null;
222
+ return nestedParse(preprocess(source), latestState);
223
+ }; // $FlowFixMe
224
+
225
+
226
+ return outerParse;
227
+ }; // Creates a match function for an inline scoped element from a regex
228
+
229
+
230
+ var inlineRegex = function inlineRegex(regex) {
231
+ var match = function match(source, state, prevCapture) {
232
+ if (state.inline) {
233
+ // $FlowFixMe
234
+ return regex.exec(source);
235
+ } else {
236
+ return null;
237
+ }
238
+ };
239
+
240
+ match.regex = regex;
241
+ return match;
242
+ }; // Creates a match function for a block scoped element from a regex
243
+
244
+
245
+ var blockRegex = function blockRegex(regex) {
246
+ // $FlowFixMe
247
+ var match = function match(source, state) {
248
+ if (state.inline) {
249
+ return null;
250
+ } else {
251
+ return regex.exec(source);
252
+ }
253
+ };
254
+
255
+ match.regex = regex;
256
+ return match;
257
+ }; // Creates a match function from a regex, ignoring block/inline scope
258
+
259
+
260
+ var anyScopeRegex = function anyScopeRegex(regex) {
261
+ // $FlowFixMe
262
+ var match = function match(source, state) {
263
+ return regex.exec(source);
264
+ };
265
+
266
+ match.regex = regex;
267
+ return match;
268
+ };
269
+
270
+ var TYPE_SYMBOL = typeof Symbol === "function" && Symbol.for && Symbol.for("react.element") || 0xeac7;
271
+
272
+ var reactElement = function reactElement(type, key, props) {
273
+ var element = {
274
+ $$typeof: TYPE_SYMBOL,
275
+ type: type,
276
+ key: key == null ? undefined : key,
277
+ ref: null,
278
+ props: props,
279
+ _owner: null
280
+ };
281
+ return element;
282
+ };
283
+ /** Returns a closed HTML tag.
284
+ * @param {string} tagName - Name of HTML tag (eg. "em" or "a")
285
+ * @param {string} content - Inner content of tag
286
+ * @param {{ [attr: string]: SimpleMarkdown.Attr }} [attributes] - Optional extra attributes of tag as an object of key-value pairs
287
+ * eg. { "href": "http://google.com" }. Falsey attributes are filtered out.
288
+ * @param {boolean} [isClosed] - boolean that controls whether tag is closed or not (eg. img tags).
289
+ * defaults to true
290
+ */
291
+
292
+
293
+ var htmlTag = function htmlTag(tagName, content, attributes, isClosed) {
294
+ attributes = attributes || {};
295
+ isClosed = typeof isClosed !== "undefined" ? isClosed : true;
296
+ var attributeString = "";
297
+
298
+ for (var attr in attributes) {
299
+ var attribute = attributes[attr]; // Removes falsey attributes
300
+
301
+ if ( // $FlowFixMe
302
+ Object.prototype.hasOwnProperty.call(attributes, attr) && attribute) {
303
+ attributeString += " " + sanitizeText(attr) + '="' + sanitizeText(attribute) + '"';
304
+ }
305
+ }
306
+
307
+ var unclosedTag = "<" + tagName + attributeString + ">";
308
+
309
+ if (isClosed) {
310
+ return unclosedTag + content + "</" + tagName + ">";
311
+ } else {
312
+ return unclosedTag;
313
+ }
314
+ };
315
+
316
+ var EMPTY_PROPS = {};
317
+ /**
318
+ * @param {string | null | undefined} url - url to sanitize
319
+ * @returns {string | null} - url if safe, or null if a safe url could not be made
320
+ */
321
+
322
+ var sanitizeUrl = function sanitizeUrl(url) {
323
+ if (url == null) {
324
+ return null;
325
+ }
326
+
327
+ try {
328
+ var prot = new URL(url, "https://localhost").protocol;
329
+
330
+ if (prot.indexOf("javascript:") === 0 || prot.indexOf("vbscript:") === 0 || prot.indexOf("data:") === 0) {
331
+ return null;
332
+ }
333
+ } catch (e) {
334
+ // invalid URLs should throw a TypeError
335
+ // see for instance: `new URL("");`
336
+ return null;
337
+ }
338
+
339
+ return url;
340
+ };
341
+
342
+ var SANITIZE_TEXT_R = /[<>&"']/g;
343
+ var SANITIZE_TEXT_CODES = {
344
+ "<": "&lt;",
345
+ ">": "&gt;",
346
+ "&": "&amp;",
347
+ '"': "&quot;",
348
+ "'": "&#x27;",
349
+ "/": "&#x2F;",
350
+ "`": "&#96;"
351
+ };
352
+
353
+ var sanitizeText = function sanitizeText(text) {
354
+ return String(text).replace(SANITIZE_TEXT_R, function (chr) {
355
+ return SANITIZE_TEXT_CODES[chr];
356
+ });
357
+ };
358
+
359
+ var UNESCAPE_URL_R = /\\([^0-9A-Za-z\s])/g;
360
+
361
+ var unescapeUrl = function unescapeUrl(rawUrlString) {
362
+ return rawUrlString.replace(UNESCAPE_URL_R, "$1");
363
+ };
364
+ /**
365
+ * Parse some content with the parser `parse`, with state.inline
366
+ * set to true. Useful for block elements; not generally necessary
367
+ * to be used by inline elements (where state.inline is already true.
368
+ */
369
+
370
+
371
+ var parseInline = function parseInline(parse, content, state) {
372
+ var isCurrentlyInline = state.inline || false;
373
+ state.inline = true;
374
+ var result = parse(content, state);
375
+ state.inline = isCurrentlyInline;
376
+ return result;
377
+ };
378
+
379
+ var parseBlock = function parseBlock(parse, content, state) {
380
+ var isCurrentlyInline = state.inline || false;
381
+ state.inline = false;
382
+ var result = parse(content + "\n\n", state);
383
+ state.inline = isCurrentlyInline;
384
+ return result;
385
+ };
386
+
387
+ var parseCaptureInline = function parseCaptureInline(capture, parse, state) {
388
+ return {
389
+ content: parseInline(parse, capture[1], state)
390
+ };
391
+ };
392
+
393
+ var ignoreCapture = function ignoreCapture() {
394
+ return {};
395
+ }; // recognize a `*` `-`, `+`, `1.`, `2.`... list bullet
396
+
397
+
398
+ var LIST_BULLET = "(?:[*+-]|\\d+\\.)"; // recognize the start of a list item:
399
+ // leading space plus a bullet plus a space (` * `)
400
+
401
+ var LIST_ITEM_PREFIX = "( *)(" + LIST_BULLET + ") +";
402
+ var LIST_ITEM_PREFIX_R = new RegExp("^" + LIST_ITEM_PREFIX); // recognize an individual list item:
403
+ // * hi
404
+ // this is part of the same item
405
+ //
406
+ // as is this, which is a new paragraph in the same item
407
+ //
408
+ // * but this is not part of the same item
409
+
410
+ var LIST_ITEM_R = new RegExp(LIST_ITEM_PREFIX + "[^\\n]*(?:\\n" + "(?!\\1" + LIST_BULLET + " )[^\\n]*)*(\n|$)", "gm");
411
+ var BLOCK_END_R = /\n{2,}$/;
412
+ var INLINE_CODE_ESCAPE_BACKTICKS_R = /^ (?= *`)|(` *) $/g; // recognize the end of a paragraph block inside a list item:
413
+ // two or more newlines at end end of the item
414
+
415
+ var LIST_BLOCK_END_R = BLOCK_END_R;
416
+ var LIST_ITEM_END_R = / *\n+$/; // check whether a list item has paragraphs: if it does,
417
+ // we leave the newlines at the end
418
+
419
+ var LIST_R = new RegExp("^( *)(" + LIST_BULLET + ") " + "[\\s\\S]+?(?:\n{2,}(?! )" + "(?!\\1" + LIST_BULLET + " )\\n*" + // the \\s*$ here is so that we can parse the inside of nested
420
+ // lists, where our content might end before we receive two `\n`s
421
+ "|\\s*\n*$)");
422
+ var LIST_LOOKBEHIND_R = /(?:^|\n)( *)$/;
423
+
424
+ var TABLES = function () {
425
+ var TABLE_ROW_SEPARATOR_TRIM = /^ *\| *| *\| *$/g;
426
+ var TABLE_CELL_END_TRIM = / *$/;
427
+ var TABLE_RIGHT_ALIGN = /^ *-+: *$/;
428
+ var TABLE_CENTER_ALIGN = /^ *:-+: *$/;
429
+ var TABLE_LEFT_ALIGN = /^ *:-+ *$/; // TODO: This needs a real type
430
+
431
+ var parseTableAlignCapture = function parseTableAlignCapture(alignCapture) {
432
+ if (TABLE_RIGHT_ALIGN.test(alignCapture)) {
433
+ return "right";
434
+ } else if (TABLE_CENTER_ALIGN.test(alignCapture)) {
435
+ return "center";
436
+ } else if (TABLE_LEFT_ALIGN.test(alignCapture)) {
437
+ return "left";
438
+ } else {
439
+ return null;
440
+ }
441
+ };
442
+
443
+ var parseTableAlign = function parseTableAlign(source, parse, state, trimEndSeparators) {
444
+ if (trimEndSeparators) {
445
+ source = source.replace(TABLE_ROW_SEPARATOR_TRIM, "");
446
+ }
447
+
448
+ var alignText = source.trim().split("|");
449
+ return alignText.map(parseTableAlignCapture);
450
+ };
451
+
452
+ var parseTableRow = function parseTableRow(source, parse, state, trimEndSeparators) {
453
+ var prevInTable = state.inTable;
454
+ state.inTable = true;
455
+ var tableRow = parse(source.trim(), state);
456
+ state.inTable = prevInTable;
457
+ var cells = [[]];
458
+ tableRow.forEach(function (node, i) {
459
+ if (node.type === "tableSeparator") {
460
+ // Filter out empty table separators at the start/end:
461
+ if (!trimEndSeparators || i !== 0 && i !== tableRow.length - 1) {
462
+ // Split the current row:
463
+ cells.push([]);
464
+ }
465
+ } else {
466
+ if (node.type === "text" && (tableRow[i + 1] == null || tableRow[i + 1].type === "tableSeparator")) {
467
+ node.content = node.content.replace(TABLE_CELL_END_TRIM, "");
468
+ }
469
+
470
+ cells[cells.length - 1].push(node);
471
+ }
472
+ });
473
+ return cells;
474
+ };
475
+ /**
476
+ * @param {string} source
477
+ * @param {SimpleMarkdown.Parser} parse
478
+ * @param {SimpleMarkdown.State} state
479
+ * @param {boolean} trimEndSeparators
480
+ * @returns {SimpleMarkdown.ASTNode[][]}
481
+ */
482
+
483
+
484
+ var parseTableCells = function parseTableCells(source, parse, state, trimEndSeparators) {
485
+ var rowsText = source.trim().split("\n");
486
+ return rowsText.map(function (rowText) {
487
+ // $FlowFixMe
488
+ return parseTableRow(rowText, parse, state, trimEndSeparators);
489
+ });
490
+ };
491
+ /**
492
+ * @param {boolean} trimEndSeparators
493
+ * @returns {SimpleMarkdown.SingleNodeParseFunction}
494
+ */
495
+
496
+
497
+ var parseTable = function parseTable(trimEndSeparators) {
498
+ return function (capture, parse, state) {
499
+ state.inline = true;
500
+ var header = parseTableRow(capture[1], parse, state, trimEndSeparators);
501
+ var align = parseTableAlign(capture[2], parse, state, trimEndSeparators);
502
+ var cells = parseTableCells(capture[3], parse, state, trimEndSeparators);
503
+ state.inline = false;
504
+ return {
505
+ type: "table",
506
+ header: header,
507
+ align: align,
508
+ cells: cells
509
+ };
510
+ };
511
+ };
512
+
513
+ return {
514
+ parseTable: parseTable(true),
515
+ parseNpTable: parseTable(false),
516
+ TABLE_REGEX: /^ *(\|.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/,
517
+ NPTABLE_REGEX: /^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/
518
+ };
519
+ }();
520
+
521
+ var LINK_INSIDE = "(?:\\[[^\\]]*\\]|[^\\[\\]]|\\](?=[^\\[]*\\]))*";
522
+ var LINK_HREF_AND_TITLE = "\\s*<?((?:\\([^)]*\\)|[^\\s\\\\]|\\\\.)*?)>?(?:\\s+['\"]([\\s\\S]*?)['\"])?\\s*";
523
+ var AUTOLINK_MAILTO_CHECK_R = /mailto:/i;
524
+
525
+ var parseRef = function parseRef(capture, state, refNode) {
526
+ var ref = (capture[2] || capture[1]).replace(/\s+/g, " ").toLowerCase(); // We store information about previously seen defs on
527
+ // state._defs (_ to deconflict with client-defined
528
+ // state). If the def for this reflink/refimage has
529
+ // already been seen, we can use its target/source
530
+ // and title here:
531
+
532
+ if (state._defs && state._defs[ref]) {
533
+ var def = state._defs[ref]; // `refNode` can be a link or an image. Both use
534
+ // target and title properties.
535
+
536
+ refNode.target = def.target;
537
+ refNode.title = def.title;
538
+ } // In case we haven't seen our def yet (or if someone
539
+ // overwrites that def later on), we add this node
540
+ // to the list of ref nodes for that def. Then, when
541
+ // we find the def, we can modify this link/image AST
542
+ // node :).
543
+ // I'm sorry.
544
+
545
+
546
+ state._refs = state._refs || {};
547
+ state._refs[ref] = state._refs[ref] || [];
548
+
549
+ state._refs[ref].push(refNode);
550
+
551
+ return refNode;
552
+ };
553
+
554
+ var currOrder = 0;
555
+ var defaultRules = {
556
+ Array: {
557
+ react: function react(arr, output, state) {
558
+ var oldKey = state.key;
559
+ var result = []; // map output over the ast, except group any text
560
+ // nodes together into a single string output.
561
+
562
+ for (var i = 0, key = 0; i < arr.length; i++, key++) {
563
+ // `key` is our numerical `state.key`, which we increment for
564
+ // every output node, but don't change for joined text nodes.
565
+ // (i, however, must change for joined text nodes)
566
+ state.key = "" + i;
567
+ var node = arr[i];
568
+
569
+ if (node.type === "text") {
570
+ node = {
571
+ type: "text",
572
+ content: node.content
573
+ };
574
+
575
+ for (; i + 1 < arr.length && arr[i + 1].type === "text"; i++) {
576
+ node.content += arr[i + 1].content;
577
+ }
578
+ }
579
+
580
+ result.push(output(node, state));
581
+ }
582
+
583
+ state.key = oldKey;
584
+ return result;
585
+ },
586
+ html: function html(arr, output, state) {
587
+ var result = ""; // map output over the ast, except group any text
588
+ // nodes together into a single string output.
589
+
590
+ for (var i = 0; i < arr.length; i++) {
591
+ var node = arr[i];
592
+
593
+ if (node.type === "text") {
594
+ node = {
595
+ type: "text",
596
+ content: node.content
597
+ };
598
+
599
+ for (; i + 1 < arr.length && arr[i + 1].type === "text"; i++) {
600
+ node.content += arr[i + 1].content;
601
+ }
602
+ }
603
+
604
+ result += output(node, state);
605
+ }
606
+
607
+ return result;
608
+ }
609
+ },
610
+ heading: {
611
+ order: currOrder++,
612
+ match: blockRegex(/^ *(#{1,6})([^\n]+?)#* *(?:\n *)+\n/),
613
+ parse: function (_parse) {
614
+ function parse(_x, _x2, _x3) {
615
+ return _parse.apply(this, arguments);
616
+ }
617
+
618
+ parse.toString = function () {
619
+ return _parse.toString();
620
+ };
621
+
622
+ return parse;
623
+ }(function (capture, parse, state) {
624
+ return {
625
+ level: capture[1].length,
626
+ content: parseInline(parse, capture[2].trim(), state)
627
+ };
628
+ }),
629
+ react: function react(node, output, state) {
630
+ return reactElement("h" + node.level, state.key, {
631
+ children: output(node.content, state)
632
+ });
633
+ },
634
+ html: function html(node, output, state) {
635
+ return htmlTag("h" + node.level, output(node.content, state));
636
+ }
637
+ },
638
+ nptable: {
639
+ order: currOrder++,
640
+ match: blockRegex(TABLES.NPTABLE_REGEX),
641
+ parse: TABLES.parseNpTable,
642
+ react: null,
643
+ html: null
644
+ },
645
+ lheading: {
646
+ order: currOrder++,
647
+ match: blockRegex(/^([^\n]+)\n *(=|-){3,} *(?:\n *)+\n/),
648
+ parse: function (_parse2) {
649
+ function parse(_x4, _x5, _x6) {
650
+ return _parse2.apply(this, arguments);
651
+ }
652
+
653
+ parse.toString = function () {
654
+ return _parse2.toString();
655
+ };
656
+
657
+ return parse;
658
+ }(function (capture, parse, state) {
659
+ return {
660
+ type: "heading",
661
+ level: capture[2] === "=" ? 1 : 2,
662
+ content: parseInline(parse, capture[1], state)
663
+ };
664
+ }),
665
+ react: null,
666
+ html: null
667
+ },
668
+ hr: {
669
+ order: currOrder++,
670
+ match: blockRegex(/^( *[-*_]){3,} *(?:\n *)+\n/),
671
+ parse: ignoreCapture,
672
+ react: function react(node, output, state) {
673
+ return reactElement("hr", state.key, EMPTY_PROPS);
674
+ },
675
+ html: function html(node, output, state) {
676
+ return "<hr>";
677
+ }
678
+ },
679
+ codeBlock: {
680
+ order: currOrder++,
681
+ match: blockRegex(/^(?: [^\n]+\n*)+(?:\n *)+\n/),
682
+ parse: function (_parse3) {
683
+ function parse(_x7, _x8, _x9) {
684
+ return _parse3.apply(this, arguments);
685
+ }
686
+
687
+ parse.toString = function () {
688
+ return _parse3.toString();
689
+ };
690
+
691
+ return parse;
692
+ }(function (capture, parse, state) {
693
+ var content = capture[0].replace(/^ /gm, "").replace(/\n+$/, "");
694
+ return {
695
+ lang: undefined,
696
+ content: content
697
+ };
698
+ }),
699
+ react: function react(node, output, state) {
700
+ var className = node.lang ? "markdown-code-" + node.lang : undefined;
701
+ return reactElement("pre", state.key, {
702
+ children: reactElement("code", null, {
703
+ className: className,
704
+ children: node.content
705
+ })
706
+ });
707
+ },
708
+ html: function html(node, output, state) {
709
+ var className = node.lang ? "markdown-code-" + node.lang : undefined;
710
+ var codeBlock = htmlTag("code", sanitizeText(node.content), {
711
+ class: className
712
+ });
713
+ return htmlTag("pre", codeBlock);
714
+ }
715
+ },
716
+ fence: {
717
+ order: currOrder++,
718
+ match: blockRegex(/^ *(`{3,}|~{3,}) *(?:(\S+) *)?\n([\s\S]+?)\n?\1 *(?:\n *)+\n/),
719
+ parse: function (_parse4) {
720
+ function parse(_x10, _x11, _x12) {
721
+ return _parse4.apply(this, arguments);
722
+ }
723
+
724
+ parse.toString = function () {
725
+ return _parse4.toString();
726
+ };
727
+
728
+ return parse;
729
+ }(function (capture, parse, state) {
730
+ return {
731
+ type: "codeBlock",
732
+ lang: capture[2] || undefined,
733
+ content: capture[3]
734
+ };
735
+ }),
736
+ react: null,
737
+ html: null
738
+ },
739
+ blockQuote: {
740
+ order: currOrder++,
741
+ match: blockRegex(/^( *>[^\n]+(\n[^\n]+)*\n*)+\n{2,}/),
742
+ parse: function (_parse5) {
743
+ function parse(_x13, _x14, _x15) {
744
+ return _parse5.apply(this, arguments);
745
+ }
746
+
747
+ parse.toString = function () {
748
+ return _parse5.toString();
749
+ };
750
+
751
+ return parse;
752
+ }(function (capture, parse, state) {
753
+ var content = capture[0].replace(/^ *> ?/gm, "");
754
+ return {
755
+ content: parse(content, state)
756
+ };
757
+ }),
758
+ react: function react(node, output, state) {
759
+ return reactElement("blockquote", state.key, {
760
+ children: output(node.content, state)
761
+ });
762
+ },
763
+ html: function html(node, output, state) {
764
+ return htmlTag("blockquote", output(node.content, state));
765
+ }
766
+ },
767
+ list: {
768
+ order: currOrder++,
769
+ // $FlowFixMe
770
+ match: function match(source, state) {
771
+ // We only want to break into a list if we are at the start of a
772
+ // line. This is to avoid parsing "hi * there" with "* there"
773
+ // becoming a part of a list.
774
+ // You might wonder, "but that's inline, so of course it wouldn't
775
+ // start a list?". You would be correct! Except that some of our
776
+ // lists can be inline, because they might be inside another list,
777
+ // in which case we can parse with inline scope, but need to allow
778
+ // nested lists inside this inline scope.
779
+ var prevCaptureStr = state.prevCapture == null ? "" : state.prevCapture[0];
780
+ var isStartOfLineCapture = LIST_LOOKBEHIND_R.exec(prevCaptureStr);
781
+ var isListBlock = state._list || !state.inline;
782
+
783
+ if (isStartOfLineCapture && isListBlock) {
784
+ source = isStartOfLineCapture[1] + source;
785
+ return LIST_R.exec(source);
786
+ } else {
787
+ return null;
788
+ }
789
+ },
790
+ parse: function (_parse6) {
791
+ function parse(_x16, _x17, _x18) {
792
+ return _parse6.apply(this, arguments);
793
+ }
794
+
795
+ parse.toString = function () {
796
+ return _parse6.toString();
797
+ };
798
+
799
+ return parse;
800
+ }(function (capture, parse, state) {
801
+ var bullet = capture[2];
802
+ var ordered = bullet.length > 1;
803
+ var start = ordered ? +bullet : undefined;
804
+ var items = capture[0].replace(LIST_BLOCK_END_R, "\n").match(LIST_ITEM_R); // We know this will match here, because of how the regexes are
805
+ // defined
806
+
807
+ var lastItemWasAParagraph = false;
808
+ var itemContent = items.map(function (item, i) {
809
+ // We need to see how far indented this item is:
810
+ var prefixCapture = LIST_ITEM_PREFIX_R.exec(item);
811
+ var space = prefixCapture ? prefixCapture[0].length : 0; // And then we construct a regex to "unindent" the subsequent
812
+ // lines of the items by that amount:
813
+
814
+ var spaceRegex = new RegExp("^ {1," + space + "}", "gm"); // Before processing the item, we need a couple things
815
+
816
+ var content = item // remove indents on trailing lines:
817
+ .replace(spaceRegex, "") // remove the bullet:
818
+ .replace(LIST_ITEM_PREFIX_R, ""); // I'm not sur4 why this is necessary again?
819
+ // Handling "loose" lists, like:
820
+ //
821
+ // * this is wrapped in a paragraph
822
+ //
823
+ // * as is this
824
+ //
825
+ // * as is this
826
+
827
+ var isLastItem = i === items.length - 1;
828
+ var containsBlocks = content.indexOf("\n\n") !== -1; // Any element in a list is a block if it contains multiple
829
+ // newlines. The last element in the list can also be a block
830
+ // if the previous item in the list was a block (this is
831
+ // because non-last items in the list can end with \n\n, but
832
+ // the last item can't, so we just "inherit" this property
833
+ // from our previous element).
834
+
835
+ var thisItemIsAParagraph = containsBlocks || isLastItem && lastItemWasAParagraph;
836
+ lastItemWasAParagraph = thisItemIsAParagraph; // backup our state for restoration afterwards. We're going to
837
+ // want to set state._list to true, and state.inline depending
838
+ // on our list's looseness.
839
+
840
+ var oldStateInline = state.inline;
841
+ var oldStateList = state._list;
842
+ state._list = true; // Parse inline if we're in a tight list, or block if we're in
843
+ // a loose list.
844
+
845
+ var adjustedContent;
846
+
847
+ if (thisItemIsAParagraph) {
848
+ state.inline = false;
849
+ adjustedContent = content.replace(LIST_ITEM_END_R, "\n\n");
850
+ } else {
851
+ state.inline = true;
852
+ adjustedContent = content.replace(LIST_ITEM_END_R, "");
853
+ }
854
+
855
+ var result = parse(adjustedContent, state); // Restore our state before returning
856
+
857
+ state.inline = oldStateInline;
858
+ state._list = oldStateList;
859
+ return result;
860
+ });
861
+ return {
862
+ ordered: ordered,
863
+ start: start,
864
+ items: itemContent
865
+ };
866
+ }),
867
+ react: function react(node, output, state) {
868
+ var ListWrapper = node.ordered ? "ol" : "ul";
869
+ return reactElement(ListWrapper, state.key, {
870
+ start: node.start,
871
+ children: node.items.map(function (item, i) {
872
+ return reactElement("li", "" + i, {
873
+ children: output(item, state)
874
+ });
875
+ })
876
+ });
877
+ },
878
+ html: function html(node, output, state) {
879
+ var listItems = node.items.map(function (item) {
880
+ return htmlTag("li", output(item, state));
881
+ }).join("");
882
+ var listTag = node.ordered ? "ol" : "ul";
883
+ var attributes = {
884
+ start: node.start
885
+ };
886
+ return htmlTag(listTag, listItems, attributes);
887
+ }
888
+ },
889
+ def: {
890
+ order: currOrder++,
891
+ // TODO(aria): This will match without a blank line before the next
892
+ // block element, which is inconsistent with most of the rest of
893
+ // simple-markdown.
894
+ match: blockRegex(/^ *\[([^\]]+)\]: *<?([^\s>]*)>?(?: +["(]([^\n]+)[")])? *\n(?: *\n)*/),
895
+ parse: function (_parse7) {
896
+ function parse(_x19, _x20, _x21) {
897
+ return _parse7.apply(this, arguments);
898
+ }
899
+
900
+ parse.toString = function () {
901
+ return _parse7.toString();
902
+ };
903
+
904
+ return parse;
905
+ }(function (capture, parse, state) {
906
+ var def = capture[1].replace(/\s+/g, " ").toLowerCase();
907
+ var target = capture[2];
908
+ var title = capture[3]; // Look for previous links/images using this def
909
+ // If any links/images using this def have already been declared,
910
+ // they will have added themselves to the state._refs[def] list
911
+ // (_ to deconflict with client-defined state). We look through
912
+ // that list of reflinks for this def, and modify those AST nodes
913
+ // with our newly found information now.
914
+ // Sorry :(.
915
+
916
+ if (state._refs && state._refs[def]) {
917
+ // `refNode` can be a link or an image
918
+ state._refs[def].forEach(function (refNode) {
919
+ refNode.target = target;
920
+ refNode.title = title;
921
+ });
922
+ } // Add this def to our map of defs for any future links/images
923
+ // In case we haven't found any or all of the refs referring to
924
+ // this def yet, we add our def to the table of known defs, so
925
+ // that future reflinks can modify themselves appropriately with
926
+ // this information.
927
+
928
+
929
+ state._defs = state._defs || {};
930
+ state._defs[def] = {
931
+ target: target,
932
+ title: title
933
+ }; // return the relevant parsed information
934
+ // for debugging only.
935
+
936
+ return {
937
+ def: def,
938
+ target: target,
939
+ title: title
940
+ };
941
+ }),
942
+ react: function react() {
943
+ return null;
944
+ },
945
+ html: function html() {
946
+ return "";
947
+ }
948
+ },
949
+ table: {
950
+ order: currOrder++,
951
+ match: blockRegex(TABLES.TABLE_REGEX),
952
+ parse: TABLES.parseTable,
953
+ react: function react(node, output, state) {
954
+ var getStyle = function getStyle(colIndex) {
955
+ return node.align[colIndex] == null ? {} : {
956
+ textAlign: node.align[colIndex]
957
+ };
958
+ };
959
+
960
+ var headers = node.header.map(function (content, i) {
961
+ return reactElement("th", "" + i, {
962
+ style: getStyle(i),
963
+ scope: "col",
964
+ children: output(content, state)
965
+ });
966
+ });
967
+ var rows = node.cells.map(function (row, r) {
968
+ return reactElement("tr", "" + r, {
969
+ children: row.map(function (content, c) {
970
+ return reactElement("td", "" + c, {
971
+ style: getStyle(c),
972
+ children: output(content, state)
973
+ });
974
+ })
975
+ });
976
+ });
977
+ return reactElement("table", state.key, {
978
+ children: [reactElement("thead", "thead", {
979
+ children: reactElement("tr", null, {
980
+ children: headers
981
+ })
982
+ }), reactElement("tbody", "tbody", {
983
+ children: rows
984
+ })]
985
+ });
986
+ },
987
+ html: function html(node, output, state) {
988
+ var getStyle = function getStyle(colIndex) {
989
+ return node.align[colIndex] == null ? "" : "text-align:" + node.align[colIndex] + ";";
990
+ };
991
+
992
+ var headers = node.header.map(function (content, i) {
993
+ return htmlTag("th", output(content, state), {
994
+ style: getStyle(i),
995
+ scope: "col"
996
+ });
997
+ }).join("");
998
+ var rows = node.cells.map(function (row) {
999
+ var cols = row.map(function (content, c) {
1000
+ return htmlTag("td", output(content, state), {
1001
+ style: getStyle(c)
1002
+ });
1003
+ }).join("");
1004
+ return htmlTag("tr", cols);
1005
+ }).join("");
1006
+ var thead = htmlTag("thead", htmlTag("tr", headers));
1007
+ var tbody = htmlTag("tbody", rows);
1008
+ return htmlTag("table", thead + tbody);
1009
+ }
1010
+ },
1011
+ newline: {
1012
+ order: currOrder++,
1013
+ match: blockRegex(/^(?:\n *)*\n/),
1014
+ parse: ignoreCapture,
1015
+ react: function react(node, output, state) {
1016
+ return "\n";
1017
+ },
1018
+ html: function html(node, output, state) {
1019
+ return "\n";
1020
+ }
1021
+ },
1022
+ paragraph: {
1023
+ order: currOrder++,
1024
+ match: blockRegex(/^((?:[^\n]|\n(?! *\n))+)(?:\n *)+\n/),
1025
+ parse: parseCaptureInline,
1026
+ react: function react(node, output, state) {
1027
+ return reactElement("div", state.key, {
1028
+ className: "paragraph",
1029
+ children: output(node.content, state)
1030
+ });
1031
+ },
1032
+ html: function html(node, output, state) {
1033
+ var attributes = {
1034
+ class: "paragraph"
1035
+ };
1036
+ return htmlTag("div", output(node.content, state), attributes);
1037
+ }
1038
+ },
1039
+ escape: {
1040
+ order: currOrder++,
1041
+ // We don't allow escaping numbers, letters, or spaces here so that
1042
+ // backslashes used in plain text still get rendered. But allowing
1043
+ // escaping anything else provides a very flexible escape mechanism,
1044
+ // regardless of how this grammar is extended.
1045
+ match: inlineRegex(/^\\([^0-9A-Za-z\s])/),
1046
+ parse: function (_parse8) {
1047
+ function parse(_x22, _x23, _x24) {
1048
+ return _parse8.apply(this, arguments);
1049
+ }
1050
+
1051
+ parse.toString = function () {
1052
+ return _parse8.toString();
1053
+ };
1054
+
1055
+ return parse;
1056
+ }(function (capture, parse, state) {
1057
+ return {
1058
+ type: "text",
1059
+ content: capture[1]
1060
+ };
1061
+ }),
1062
+ react: null,
1063
+ html: null
1064
+ },
1065
+ tableSeparator: {
1066
+ order: currOrder++,
1067
+ // $FlowFixMe
1068
+ match: function match(source, state) {
1069
+ if (!state.inTable) {
1070
+ return null;
1071
+ }
1072
+
1073
+ return /^ *\| */.exec(source);
1074
+ },
1075
+ parse: function parse() {
1076
+ return {
1077
+ type: "tableSeparator"
1078
+ };
1079
+ },
1080
+ // These shouldn't be reached, but in case they are, be reasonable:
1081
+ react: function react() {
1082
+ return " | ";
1083
+ },
1084
+ html: function html() {
1085
+ return " &vert; ";
1086
+ }
1087
+ },
1088
+ autolink: {
1089
+ order: currOrder++,
1090
+ match: inlineRegex(/^<([^: >]+:\/[^ >]+)>/),
1091
+ parse: function (_parse9) {
1092
+ function parse(_x25, _x26, _x27) {
1093
+ return _parse9.apply(this, arguments);
1094
+ }
1095
+
1096
+ parse.toString = function () {
1097
+ return _parse9.toString();
1098
+ };
1099
+
1100
+ return parse;
1101
+ }(function (capture, parse, state) {
1102
+ return {
1103
+ type: "link",
1104
+ content: [{
1105
+ type: "text",
1106
+ content: capture[1]
1107
+ }],
1108
+ target: capture[1]
1109
+ };
1110
+ }),
1111
+ react: null,
1112
+ html: null
1113
+ },
1114
+ mailto: {
1115
+ order: currOrder++,
1116
+ match: inlineRegex(/^<([^ >]+@[^ >]+)>/),
1117
+ parse: function (_parse10) {
1118
+ function parse(_x28, _x29, _x30) {
1119
+ return _parse10.apply(this, arguments);
1120
+ }
1121
+
1122
+ parse.toString = function () {
1123
+ return _parse10.toString();
1124
+ };
1125
+
1126
+ return parse;
1127
+ }(function (capture, parse, state) {
1128
+ var address = capture[1];
1129
+ var target = capture[1]; // Check for a `mailto:` already existing in the link:
1130
+
1131
+ if (!AUTOLINK_MAILTO_CHECK_R.test(target)) {
1132
+ target = "mailto:" + target;
1133
+ }
1134
+
1135
+ return {
1136
+ type: "link",
1137
+ content: [{
1138
+ type: "text",
1139
+ content: address
1140
+ }],
1141
+ target: target
1142
+ };
1143
+ }),
1144
+ react: null,
1145
+ html: null
1146
+ },
1147
+ url: {
1148
+ order: currOrder++,
1149
+ match: inlineRegex(/^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/),
1150
+ parse: function (_parse11) {
1151
+ function parse(_x31, _x32, _x33) {
1152
+ return _parse11.apply(this, arguments);
1153
+ }
1154
+
1155
+ parse.toString = function () {
1156
+ return _parse11.toString();
1157
+ };
1158
+
1159
+ return parse;
1160
+ }(function (capture, parse, state) {
1161
+ return {
1162
+ type: "link",
1163
+ content: [{
1164
+ type: "text",
1165
+ content: capture[1]
1166
+ }],
1167
+ target: capture[1],
1168
+ title: undefined
1169
+ };
1170
+ }),
1171
+ react: null,
1172
+ html: null
1173
+ },
1174
+ link: {
1175
+ order: currOrder++,
1176
+ match: inlineRegex(new RegExp("^\\[(" + LINK_INSIDE + ")\\]\\(" + LINK_HREF_AND_TITLE + "\\)")),
1177
+ parse: function (_parse12) {
1178
+ function parse(_x34, _x35, _x36) {
1179
+ return _parse12.apply(this, arguments);
1180
+ }
1181
+
1182
+ parse.toString = function () {
1183
+ return _parse12.toString();
1184
+ };
1185
+
1186
+ return parse;
1187
+ }(function (capture, parse, state) {
1188
+ var link = {
1189
+ content: parse(capture[1], state),
1190
+ target: unescapeUrl(capture[2]),
1191
+ title: capture[3]
1192
+ };
1193
+ return link;
1194
+ }),
1195
+ react: function react(node, output, state) {
1196
+ return reactElement("a", state.key, {
1197
+ href: sanitizeUrl(node.target),
1198
+ title: node.title,
1199
+ children: output(node.content, state)
1200
+ });
1201
+ },
1202
+ html: function html(node, output, state) {
1203
+ var attributes = {
1204
+ href: sanitizeUrl(node.target),
1205
+ title: node.title
1206
+ };
1207
+ return htmlTag("a", output(node.content, state), attributes);
1208
+ }
1209
+ },
1210
+ image: {
1211
+ order: currOrder++,
1212
+ match: inlineRegex(new RegExp("^!\\[(" + LINK_INSIDE + ")\\]\\(" + LINK_HREF_AND_TITLE + "\\)")),
1213
+ parse: function (_parse13) {
1214
+ function parse(_x37, _x38, _x39) {
1215
+ return _parse13.apply(this, arguments);
1216
+ }
1217
+
1218
+ parse.toString = function () {
1219
+ return _parse13.toString();
1220
+ };
1221
+
1222
+ return parse;
1223
+ }(function (capture, parse, state) {
1224
+ var image = {
1225
+ alt: capture[1],
1226
+ target: unescapeUrl(capture[2]),
1227
+ title: capture[3]
1228
+ };
1229
+ return image;
1230
+ }),
1231
+ react: function react(node, output, state) {
1232
+ return reactElement("img", state.key, {
1233
+ src: sanitizeUrl(node.target),
1234
+ alt: node.alt,
1235
+ title: node.title
1236
+ });
1237
+ },
1238
+ html: function html(node, output, state) {
1239
+ var attributes = {
1240
+ src: sanitizeUrl(node.target),
1241
+ alt: node.alt,
1242
+ title: node.title
1243
+ };
1244
+ return htmlTag("img", "", attributes, false);
1245
+ }
1246
+ },
1247
+ reflink: {
1248
+ order: currOrder++,
1249
+ match: inlineRegex(new RegExp( // The first [part] of the link
1250
+ "^\\[(" + LINK_INSIDE + ")\\]" + // The [ref] target of the link
1251
+ "\\s*\\[([^\\]]*)\\]")),
1252
+ parse: function (_parse14) {
1253
+ function parse(_x40, _x41, _x42) {
1254
+ return _parse14.apply(this, arguments);
1255
+ }
1256
+
1257
+ parse.toString = function () {
1258
+ return _parse14.toString();
1259
+ };
1260
+
1261
+ return parse;
1262
+ }(function (capture, parse, state) {
1263
+ return parseRef(capture, state, {
1264
+ type: "link",
1265
+ content: parse(capture[1], state)
1266
+ });
1267
+ }),
1268
+ react: null,
1269
+ html: null
1270
+ },
1271
+ refimage: {
1272
+ order: currOrder++,
1273
+ match: inlineRegex(new RegExp( // The first [part] of the link
1274
+ "^!\\[(" + LINK_INSIDE + ")\\]" + // The [ref] target of the link
1275
+ "\\s*\\[([^\\]]*)\\]")),
1276
+ parse: function (_parse15) {
1277
+ function parse(_x43, _x44, _x45) {
1278
+ return _parse15.apply(this, arguments);
1279
+ }
1280
+
1281
+ parse.toString = function () {
1282
+ return _parse15.toString();
1283
+ };
1284
+
1285
+ return parse;
1286
+ }(function (capture, parse, state) {
1287
+ return parseRef(capture, state, {
1288
+ type: "image",
1289
+ alt: capture[1]
1290
+ });
1291
+ }),
1292
+ react: null,
1293
+ html: null
1294
+ },
1295
+ em: {
1296
+ order: currOrder
1297
+ /* same as strong/u */
1298
+ ,
1299
+ match: inlineRegex(new RegExp( // only match _s surrounding words.
1300
+ "^\\b_" + "((?:__|\\\\[\\s\\S]|[^\\\\_])+?)_" + "\\b" + // Or match *s:
1301
+ "|" + // Only match *s that are followed by a non-space:
1302
+ "^\\*(?=\\S)(" + // Match at least one of:
1303
+ "(?:" + // - `**`: so that bolds inside italics don't close the
1304
+ // italics
1305
+ "\\*\\*|" + // - escape sequence: so escaped *s don't close us
1306
+ "\\\\[\\s\\S]|" + // - whitespace: followed by a non-* (we don't
1307
+ // want ' *' to close an italics--it might
1308
+ // start a list)
1309
+ "\\s+(?:\\\\[\\s\\S]|[^\\s\\*\\\\]|\\*\\*)|" + // - non-whitespace, non-*, non-backslash characters
1310
+ "[^\\s\\*\\\\]" + ")+?" + // followed by a non-space, non-* then *
1311
+ ")\\*(?!\\*)")),
1312
+ quality: function quality(capture) {
1313
+ // precedence by length, `em` wins ties:
1314
+ return capture[0].length + 0.2;
1315
+ },
1316
+ parse: function (_parse16) {
1317
+ function parse(_x46, _x47, _x48) {
1318
+ return _parse16.apply(this, arguments);
1319
+ }
1320
+
1321
+ parse.toString = function () {
1322
+ return _parse16.toString();
1323
+ };
1324
+
1325
+ return parse;
1326
+ }(function (capture, parse, state) {
1327
+ return {
1328
+ content: parse(capture[2] || capture[1], state)
1329
+ };
1330
+ }),
1331
+ react: function react(node, output, state) {
1332
+ return reactElement("em", state.key, {
1333
+ children: output(node.content, state)
1334
+ });
1335
+ },
1336
+ html: function html(node, output, state) {
1337
+ return htmlTag("em", output(node.content, state));
1338
+ }
1339
+ },
1340
+ strong: {
1341
+ order: currOrder
1342
+ /* same as em */
1343
+ ,
1344
+ match: inlineRegex(/^\*\*((?:\\[\s\S]|[^\\])+?)\*\*(?!\*)/),
1345
+ quality: function quality(capture) {
1346
+ // precedence by length, wins ties vs `u`:
1347
+ return capture[0].length + 0.1;
1348
+ },
1349
+ parse: parseCaptureInline,
1350
+ react: function react(node, output, state) {
1351
+ return reactElement("strong", state.key, {
1352
+ children: output(node.content, state)
1353
+ });
1354
+ },
1355
+ html: function html(node, output, state) {
1356
+ return htmlTag("strong", output(node.content, state));
1357
+ }
1358
+ },
1359
+ u: {
1360
+ order: currOrder++
1361
+ /* same as em&strong; increment for next rule */
1362
+ ,
1363
+ match: inlineRegex(/^__((?:\\[\s\S]|[^\\])+?)__(?!_)/),
1364
+ quality: function quality(capture) {
1365
+ // precedence by length, loses all ties
1366
+ return capture[0].length;
1367
+ },
1368
+ parse: parseCaptureInline,
1369
+ react: function react(node, output, state) {
1370
+ return reactElement("u", state.key, {
1371
+ children: output(node.content, state)
1372
+ });
1373
+ },
1374
+ html: function html(node, output, state) {
1375
+ return htmlTag("u", output(node.content, state));
1376
+ }
1377
+ },
1378
+ del: {
1379
+ order: currOrder++,
1380
+ match: inlineRegex(/^~~(?=\S)((?:\\[\s\S]|~(?!~)|[^\s~\\]|\s(?!~~))+?)~~/),
1381
+ parse: parseCaptureInline,
1382
+ react: function react(node, output, state) {
1383
+ return reactElement("del", state.key, {
1384
+ children: output(node.content, state)
1385
+ });
1386
+ },
1387
+ html: function html(node, output, state) {
1388
+ return htmlTag("del", output(node.content, state));
1389
+ }
1390
+ },
1391
+ inlineCode: {
1392
+ order: currOrder++,
1393
+ match: inlineRegex(/^(`+)([\s\S]*?[^`])\1(?!`)/),
1394
+ parse: function (_parse17) {
1395
+ function parse(_x49, _x50, _x51) {
1396
+ return _parse17.apply(this, arguments);
1397
+ }
1398
+
1399
+ parse.toString = function () {
1400
+ return _parse17.toString();
1401
+ };
1402
+
1403
+ return parse;
1404
+ }(function (capture, parse, state) {
1405
+ return {
1406
+ content: capture[2].replace(INLINE_CODE_ESCAPE_BACKTICKS_R, "$1")
1407
+ };
1408
+ }),
1409
+ react: function react(node, output, state) {
1410
+ return reactElement("code", state.key, {
1411
+ children: node.content
1412
+ });
1413
+ },
1414
+ html: function html(node, output, state) {
1415
+ return htmlTag("code", sanitizeText(node.content));
1416
+ }
1417
+ },
1418
+ br: {
1419
+ order: currOrder++,
1420
+ match: anyScopeRegex(/^ {2,}\n/),
1421
+ parse: ignoreCapture,
1422
+ react: function react(node, output, state) {
1423
+ return reactElement("br", state.key, EMPTY_PROPS);
1424
+ },
1425
+ html: function html(node, output, state) {
1426
+ return "<br>";
1427
+ }
1428
+ },
1429
+ text: {
1430
+ order: currOrder++,
1431
+ // Here we look for anything followed by non-symbols,
1432
+ // double newlines, or double-space-newlines
1433
+ // We break on any symbol characters so that this grammar
1434
+ // is easy to extend without needing to modify this regex
1435
+ match: anyScopeRegex(/^[\s\S]+?(?=[^0-9A-Za-z\s\u00c0-\uffff]|\n\n| {2,}\n|\w+:\S|$)/),
1436
+ parse: function (_parse18) {
1437
+ function parse(_x52, _x53, _x54) {
1438
+ return _parse18.apply(this, arguments);
1439
+ }
1440
+
1441
+ parse.toString = function () {
1442
+ return _parse18.toString();
1443
+ };
1444
+
1445
+ return parse;
1446
+ }(function (capture, parse, state) {
1447
+ return {
1448
+ content: capture[0]
1449
+ };
1450
+ }),
1451
+ react: function react(node, output, state) {
1452
+ return node.content;
1453
+ },
1454
+ html: function html(node, output, state) {
1455
+ return sanitizeText(node.content);
1456
+ }
1457
+ }
1458
+ };
1459
+ /** (deprecated) */
1460
+
1461
+ var ruleOutput = function ruleOutput( // $FlowFixMe
1462
+ rules, property) {
1463
+ if (!property && typeof console !== "undefined") {
1464
+ console.warn("simple-markdown ruleOutput should take 'react' or " + "'html' as the second argument.");
1465
+ }
1466
+
1467
+ var nestedRuleOutput = function nestedRuleOutput(ast, outputFunc, state) {
1468
+ return rules[ast.type][property](ast, outputFunc, state);
1469
+ };
1470
+
1471
+ return nestedRuleOutput;
1472
+ };
1473
+ /** (deprecated)
1474
+ */
1475
+
1476
+
1477
+ var reactFor = function reactFor(outputFunc) {
1478
+ var nestedOutput = function nestedOutput(ast, state) {
1479
+ state = state || {};
1480
+
1481
+ if (Array.isArray(ast)) {
1482
+ var oldKey = state.key;
1483
+ var result = []; // map nestedOutput over the ast, except group any text
1484
+ // nodes together into a single string output.
1485
+
1486
+ var lastResult = null;
1487
+
1488
+ for (var i = 0; i < ast.length; i++) {
1489
+ state.key = "" + i;
1490
+ var nodeOut = nestedOutput(ast[i], state);
1491
+
1492
+ if (typeof nodeOut === "string" && typeof lastResult === "string") {
1493
+ lastResult = lastResult + nodeOut;
1494
+ result[result.length - 1] = lastResult;
1495
+ } else {
1496
+ result.push(nodeOut);
1497
+ lastResult = nodeOut;
1498
+ }
1499
+ }
1500
+
1501
+ state.key = oldKey;
1502
+ return result;
1503
+ } else {
1504
+ return outputFunc(ast, nestedOutput, state);
1505
+ }
1506
+ };
1507
+
1508
+ return nestedOutput;
1509
+ };
1510
+ /** (deprecated)
1511
+ */
1512
+
1513
+
1514
+ var htmlFor = function htmlFor(outputFunc) {
1515
+ var nestedOutput = function nestedOutput(ast, state) {
1516
+ state = state || {};
1517
+
1518
+ if (Array.isArray(ast)) {
1519
+ return ast.map(function (node) {
1520
+ return nestedOutput(node, state);
1521
+ }).join("");
1522
+ } else {
1523
+ return outputFunc(ast, nestedOutput, state);
1524
+ }
1525
+ };
1526
+
1527
+ return nestedOutput;
1528
+ };
1529
+
1530
+ var outputFor = function outputFor(rules, property) {
1531
+ var defaultState = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1532
+
1533
+ if (!property) {
1534
+ throw new Error("simple-markdown: outputFor: `property` must be " + "defined. " + "if you just upgraded, you probably need to replace `outputFor` " + "with `reactFor`");
1535
+ }
1536
+
1537
+ var latestState; // $FlowFixMe[incompatible-type]
1538
+
1539
+ var arrayRule = rules.Array || defaultRules.Array; // Tricks to convince tsc that this var is not null:
1540
+
1541
+ var arrayRuleCheck = arrayRule[property];
1542
+
1543
+ if (!arrayRuleCheck) {
1544
+ throw new Error("simple-markdown: outputFor: to join nodes of type `" + property + "` you must provide an `Array:` joiner rule with that type, " + "Please see the docs for details on specifying an Array rule.");
1545
+ }
1546
+
1547
+ var arrayRuleOutput = arrayRuleCheck;
1548
+
1549
+ var nestedOutput = function nestedOutput(ast, state) {
1550
+ state = state || latestState;
1551
+ latestState = state;
1552
+
1553
+ if (Array.isArray(ast)) {
1554
+ return arrayRuleOutput(ast, nestedOutput, state);
1555
+ } else {
1556
+ return rules[ast.type][property](ast, nestedOutput, state);
1557
+ }
1558
+ };
1559
+
1560
+ var outerOutput = function outerOutput(ast, state) {
1561
+ latestState = populateInitialState(state, defaultState);
1562
+ return nestedOutput(ast, latestState);
1563
+ };
1564
+
1565
+ return outerOutput;
1566
+ }; // $FlowFixMe[incompatible-call]
1567
+
1568
+
1569
+ var defaultRawParse = parserFor(defaultRules);
1570
+
1571
+ var defaultBlockParse = function defaultBlockParse(source, state) {
1572
+ state = state || {};
1573
+ state.inline = false;
1574
+ return defaultRawParse(source, state);
1575
+ };
1576
+
1577
+ var defaultInlineParse = function defaultInlineParse(source, state) {
1578
+ state = state || {};
1579
+ state.inline = true;
1580
+ return defaultRawParse(source, state);
1581
+ };
1582
+
1583
+ var defaultImplicitParse = function defaultImplicitParse(source, state) {
1584
+ var isBlock = BLOCK_END_R.test(source);
1585
+ state = state || {};
1586
+ state.inline = !isBlock;
1587
+ return defaultRawParse(source, state);
1588
+ }; // $FlowFixMe[incompatible-call]
1589
+
1590
+
1591
+ var defaultReactOutput = outputFor(defaultRules, "react"); // $FlowFixMe[incompatible-call]
1592
+
1593
+ var defaultHtmlOutput = outputFor(defaultRules, "html");
1594
+
1595
+ var markdownToReact = function markdownToReact(source, state) {
1596
+ return defaultReactOutput(defaultBlockParse(source, state), state);
1597
+ };
1598
+
1599
+ var markdownToHtml = function markdownToHtml(source, state) {
1600
+ return defaultHtmlOutput(defaultBlockParse(source, state), state);
1601
+ }; // TODO: This needs definition
1602
+
1603
+
1604
+ var ReactMarkdown = function ReactMarkdown(props) {
1605
+ var divProps = {};
1606
+
1607
+ for (var prop in props) {
1608
+ if (prop !== "source" && // $FlowFixMe
1609
+ Object.prototype.hasOwnProperty.call(props, prop)) {
1610
+ divProps[prop] = props[prop];
1611
+ }
1612
+ }
1613
+
1614
+ divProps.children = markdownToReact(props.source);
1615
+ return reactElement("div", null, divProps);
1616
+ };
1617
+
1618
+ // $FlowFixMe
1619
+ var SimpleMarkdown = {
1620
+ defaultRules: defaultRules,
1621
+ parserFor: parserFor,
1622
+ outputFor: outputFor,
1623
+ inlineRegex: inlineRegex,
1624
+ blockRegex: blockRegex,
1625
+ anyScopeRegex: anyScopeRegex,
1626
+ parseInline: parseInline,
1627
+ parseBlock: parseBlock,
1628
+ // default wrappers:
1629
+ markdownToReact: markdownToReact,
1630
+ markdownToHtml: markdownToHtml,
1631
+ ReactMarkdown: ReactMarkdown,
1632
+ defaultBlockParse: defaultBlockParse,
1633
+ defaultInlineParse: defaultInlineParse,
1634
+ defaultImplicitParse: defaultImplicitParse,
1635
+ defaultReactOutput: defaultReactOutput,
1636
+ defaultHtmlOutput: defaultHtmlOutput,
1637
+ preprocess: preprocess,
1638
+ sanitizeText: sanitizeText,
1639
+ sanitizeUrl: sanitizeUrl,
1640
+ unescapeUrl: unescapeUrl,
1641
+ htmlTag: htmlTag,
1642
+ reactElement: reactElement,
1643
+ // deprecated:
1644
+ defaultRawParse: defaultRawParse,
1645
+ ruleOutput: ruleOutput,
1646
+ reactFor: reactFor,
1647
+ htmlFor: htmlFor,
1648
+ defaultParse: function defaultParse() {
1649
+ if (typeof console !== "undefined") {
1650
+ console.warn("defaultParse is deprecated, please use `defaultImplicitParse`");
1651
+ }
1652
+
1653
+ return defaultImplicitParse.apply(null, arguments);
1654
+ },
1655
+ defaultOutput: function defaultOutput() {
1656
+ if (typeof console !== "undefined") {
1657
+ console.warn("defaultOutput is deprecated, please use `defaultReactOutput`");
1658
+ }
1659
+
1660
+ return defaultReactOutput.apply(null, arguments);
1661
+ }
1662
+ };
1663
+
1664
+ export { SimpleMarkdown as default };
2
1665
  //# sourceMappingURL=index.js.map