marked.esm.js 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  1. /**
  2. * marked - a markdown parser
  3. * Copyright (c) 2011-2020, Christopher Jeffrey. (MIT Licensed)
  4. * https://github.com/markedjs/marked
  5. */
  6. /**
  7. * DO NOT EDIT THIS FILE
  8. * The code in this file is generated from files in ./src/
  9. */
  10. function createCommonjsModule(fn, module) {
  11. return module = { exports: {} }, fn(module, module.exports), module.exports;
  12. }
  13. var defaults = createCommonjsModule(function (module) {
  14. function getDefaults() {
  15. return {
  16. baseUrl: null,
  17. breaks: false,
  18. gfm: true,
  19. headerIds: true,
  20. headerPrefix: '',
  21. highlight: null,
  22. langPrefix: 'language-',
  23. mangle: true,
  24. pedantic: false,
  25. renderer: null,
  26. sanitize: false,
  27. sanitizer: null,
  28. silent: false,
  29. smartLists: false,
  30. smartypants: false,
  31. xhtml: false
  32. };
  33. }
  34. function changeDefaults(newDefaults) {
  35. module.exports.defaults = newDefaults;
  36. }
  37. module.exports = {
  38. defaults: getDefaults(),
  39. getDefaults,
  40. changeDefaults
  41. };
  42. });
  43. var defaults_1 = defaults.defaults;
  44. var defaults_2 = defaults.getDefaults;
  45. var defaults_3 = defaults.changeDefaults;
  46. /**
  47. * Helpers
  48. */
  49. const escapeTest = /[&<>"']/;
  50. const escapeReplace = /[&<>"']/g;
  51. const escapeTestNoEncode = /[<>"']|&(?!#?\w+;)/;
  52. const escapeReplaceNoEncode = /[<>"']|&(?!#?\w+;)/g;
  53. const escapeReplacements = {
  54. '&': '&amp;',
  55. '<': '&lt;',
  56. '>': '&gt;',
  57. '"': '&quot;',
  58. "'": '&#39;'
  59. };
  60. const getEscapeReplacement = (ch) => escapeReplacements[ch];
  61. function escape(html, encode) {
  62. if (encode) {
  63. if (escapeTest.test(html)) {
  64. return html.replace(escapeReplace, getEscapeReplacement);
  65. }
  66. } else {
  67. if (escapeTestNoEncode.test(html)) {
  68. return html.replace(escapeReplaceNoEncode, getEscapeReplacement);
  69. }
  70. }
  71. return html;
  72. }
  73. const unescapeTest = /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig;
  74. function unescape(html) {
  75. // explicitly match decimal, hex, and named HTML entities
  76. return html.replace(unescapeTest, (_, n) => {
  77. n = n.toLowerCase();
  78. if (n === 'colon') return ':';
  79. if (n.charAt(0) === '#') {
  80. return n.charAt(1) === 'x'
  81. ? String.fromCharCode(parseInt(n.substring(2), 16))
  82. : String.fromCharCode(+n.substring(1));
  83. }
  84. return '';
  85. });
  86. }
  87. const caret = /(^|[^\[])\^/g;
  88. function edit(regex, opt) {
  89. regex = regex.source || regex;
  90. opt = opt || '';
  91. const obj = {
  92. replace: (name, val) => {
  93. val = val.source || val;
  94. val = val.replace(caret, '$1');
  95. regex = regex.replace(name, val);
  96. return obj;
  97. },
  98. getRegex: () => {
  99. return new RegExp(regex, opt);
  100. }
  101. };
  102. return obj;
  103. }
  104. const nonWordAndColonTest = /[^\w:]/g;
  105. const originIndependentUrl = /^$|^[a-z][a-z0-9+.-]*:|^[?#]/i;
  106. function cleanUrl(sanitize, base, href) {
  107. if (sanitize) {
  108. let prot;
  109. try {
  110. prot = decodeURIComponent(unescape(href))
  111. .replace(nonWordAndColonTest, '')
  112. .toLowerCase();
  113. } catch (e) {
  114. return null;
  115. }
  116. if (prot.indexOf('javascript:') === 0 || prot.indexOf('vbscript:') === 0 || prot.indexOf('data:') === 0) {
  117. return null;
  118. }
  119. }
  120. if (base && !originIndependentUrl.test(href)) {
  121. href = resolveUrl(base, href);
  122. }
  123. try {
  124. href = encodeURI(href).replace(/%25/g, '%');
  125. } catch (e) {
  126. return null;
  127. }
  128. return href;
  129. }
  130. const baseUrls = {};
  131. const justDomain = /^[^:]+:\/*[^/]*$/;
  132. const protocol = /^([^:]+:)[\s\S]*$/;
  133. const domain = /^([^:]+:\/*[^/]*)[\s\S]*$/;
  134. function resolveUrl(base, href) {
  135. if (!baseUrls[' ' + base]) {
  136. // we can ignore everything in base after the last slash of its path component,
  137. // but we might need to add _that_
  138. // https://tools.ietf.org/html/rfc3986#section-3
  139. if (justDomain.test(base)) {
  140. baseUrls[' ' + base] = base + '/';
  141. } else {
  142. baseUrls[' ' + base] = rtrim(base, '/', true);
  143. }
  144. }
  145. base = baseUrls[' ' + base];
  146. const relativeBase = base.indexOf(':') === -1;
  147. if (href.substring(0, 2) === '//') {
  148. if (relativeBase) {
  149. return href;
  150. }
  151. return base.replace(protocol, '$1') + href;
  152. } else if (href.charAt(0) === '/') {
  153. if (relativeBase) {
  154. return href;
  155. }
  156. return base.replace(domain, '$1') + href;
  157. } else {
  158. return base + href;
  159. }
  160. }
  161. const noopTest = { exec: function noopTest() {} };
  162. function merge(obj) {
  163. let i = 1,
  164. target,
  165. key;
  166. for (; i < arguments.length; i++) {
  167. target = arguments[i];
  168. for (key in target) {
  169. if (Object.prototype.hasOwnProperty.call(target, key)) {
  170. obj[key] = target[key];
  171. }
  172. }
  173. }
  174. return obj;
  175. }
  176. function splitCells(tableRow, count) {
  177. // ensure that every cell-delimiting pipe has a space
  178. // before it to distinguish it from an escaped pipe
  179. const row = tableRow.replace(/\|/g, (match, offset, str) => {
  180. let escaped = false,
  181. curr = offset;
  182. while (--curr >= 0 && str[curr] === '\\') escaped = !escaped;
  183. if (escaped) {
  184. // odd number of slashes means | is escaped
  185. // so we leave it alone
  186. return '|';
  187. } else {
  188. // add space before unescaped |
  189. return ' |';
  190. }
  191. }),
  192. cells = row.split(/ \|/);
  193. let i = 0;
  194. if (cells.length > count) {
  195. cells.splice(count);
  196. } else {
  197. while (cells.length < count) cells.push('');
  198. }
  199. for (; i < cells.length; i++) {
  200. // leading or trailing whitespace is ignored per the gfm spec
  201. cells[i] = cells[i].trim().replace(/\\\|/g, '|');
  202. }
  203. return cells;
  204. }
  205. // Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
  206. // /c*$/ is vulnerable to REDOS.
  207. // invert: Remove suffix of non-c chars instead. Default falsey.
  208. function rtrim(str, c, invert) {
  209. const l = str.length;
  210. if (l === 0) {
  211. return '';
  212. }
  213. // Length of suffix matching the invert condition.
  214. let suffLen = 0;
  215. // Step left until we fail to match the invert condition.
  216. while (suffLen < l) {
  217. const currChar = str.charAt(l - suffLen - 1);
  218. if (currChar === c && !invert) {
  219. suffLen++;
  220. } else if (currChar !== c && invert) {
  221. suffLen++;
  222. } else {
  223. break;
  224. }
  225. }
  226. return str.substr(0, l - suffLen);
  227. }
  228. function findClosingBracket(str, b) {
  229. if (str.indexOf(b[1]) === -1) {
  230. return -1;
  231. }
  232. const l = str.length;
  233. let level = 0,
  234. i = 0;
  235. for (; i < l; i++) {
  236. if (str[i] === '\\') {
  237. i++;
  238. } else if (str[i] === b[0]) {
  239. level++;
  240. } else if (str[i] === b[1]) {
  241. level--;
  242. if (level < 0) {
  243. return i;
  244. }
  245. }
  246. }
  247. return -1;
  248. }
  249. function checkSanitizeDeprecation(opt) {
  250. if (opt && opt.sanitize && !opt.silent) {
  251. console.warn('marked(): sanitize and sanitizer parameters are deprecated since version 0.7.0, should not be used and will be removed in the future. Read more here: https://marked.js.org/#/USING_ADVANCED.md#options');
  252. }
  253. }
  254. var helpers = {
  255. escape,
  256. unescape,
  257. edit,
  258. cleanUrl,
  259. resolveUrl,
  260. noopTest,
  261. merge,
  262. splitCells,
  263. rtrim,
  264. findClosingBracket,
  265. checkSanitizeDeprecation
  266. };
  267. const {
  268. noopTest: noopTest$1,
  269. edit: edit$1,
  270. merge: merge$1
  271. } = helpers;
  272. /**
  273. * Block-Level Grammar
  274. */
  275. const block = {
  276. newline: /^\n+/,
  277. code: /^( {4}[^\n]+\n*)+/,
  278. fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/,
  279. hr: /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/,
  280. heading: /^ {0,3}(#{1,6}) +([^\n]*?)(?: +#+)? *(?:\n+|$)/,
  281. blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
  282. list: /^( {0,3})(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
  283. html: '^ {0,3}(?:' // optional indentation
  284. + '<(script|pre|style)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)' // (1)
  285. + '|comment[^\\n]*(\\n+|$)' // (2)
  286. + '|<\\?[\\s\\S]*?\\?>\\n*' // (3)
  287. + '|<![A-Z][\\s\\S]*?>\\n*' // (4)
  288. + '|<!\\[CDATA\\[[\\s\\S]*?\\]\\]>\\n*' // (5)
  289. + '|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:\\n{2,}|$)' // (6)
  290. + '|<(?!script|pre|style)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:\\n{2,}|$)' // (7) open tag
  291. + '|</(?!script|pre|style)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:\\n{2,}|$)' // (7) closing tag
  292. + ')',
  293. def: /^ {0,3}\[(label)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)(title))? *(?:\n+|$)/,
  294. nptable: noopTest$1,
  295. table: noopTest$1,
  296. lheading: /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/,
  297. // regex template, placeholders will be replaced according to different paragraph
  298. // interruption rules of commonmark and the original markdown spec:
  299. _paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html)[^\n]+)*)/,
  300. text: /^[^\n]+/
  301. };
  302. block._label = /(?!\s*\])(?:\\[\[\]]|[^\[\]])+/;
  303. block._title = /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/;
  304. block.def = edit$1(block.def)
  305. .replace('label', block._label)
  306. .replace('title', block._title)
  307. .getRegex();
  308. block.bullet = /(?:[*+-]|\d{1,9}\.)/;
  309. block.item = /^( *)(bull) ?[^\n]*(?:\n(?!\1bull ?)[^\n]*)*/;
  310. block.item = edit$1(block.item, 'gm')
  311. .replace(/bull/g, block.bullet)
  312. .getRegex();
  313. block.list = edit$1(block.list)
  314. .replace(/bull/g, block.bullet)
  315. .replace('hr', '\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))')
  316. .replace('def', '\\n+(?=' + block.def.source + ')')
  317. .getRegex();
  318. block._tag = 'address|article|aside|base|basefont|blockquote|body|caption'
  319. + '|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption'
  320. + '|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe'
  321. + '|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option'
  322. + '|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr'
  323. + '|track|ul';
  324. block._comment = /<!--(?!-?>)[\s\S]*?-->/;
  325. block.html = edit$1(block.html, 'i')
  326. .replace('comment', block._comment)
  327. .replace('tag', block._tag)
  328. .replace('attribute', / +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/)
  329. .getRegex();
  330. block.paragraph = edit$1(block._paragraph)
  331. .replace('hr', block.hr)
  332. .replace('heading', ' {0,3}#{1,6} +')
  333. .replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
  334. .replace('blockquote', ' {0,3}>')
  335. .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
  336. .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
  337. .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|!--)')
  338. .replace('tag', block._tag) // pars can be interrupted by type (6) html blocks
  339. .getRegex();
  340. block.blockquote = edit$1(block.blockquote)
  341. .replace('paragraph', block.paragraph)
  342. .getRegex();
  343. /**
  344. * Normal Block Grammar
  345. */
  346. block.normal = merge$1({}, block);
  347. /**
  348. * GFM Block Grammar
  349. */
  350. block.gfm = merge$1({}, block.normal, {
  351. nptable: /^ *([^|\n ].*\|.*)\n *([-:]+ *\|[-| :]*)(?:\n((?:.*[^>\n ].*(?:\n|$))*)\n*|$)/,
  352. table: /^ *\|(.+)\n *\|?( *[-:]+[-| :]*)(?:\n((?: *[^>\n ].*(?:\n|$))*)\n*|$)/
  353. });
  354. /**
  355. * Pedantic grammar (original John Gruber's loose markdown specification)
  356. */
  357. block.pedantic = merge$1({}, block.normal, {
  358. html: edit$1(
  359. '^ *(?:comment *(?:\\n|\\s*$)'
  360. + '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
  361. + '|<tag(?:"[^"]*"|\'[^\']*\'|\\s[^\'"/>\\s]*)*?/?> *(?:\\n{2,}|\\s*$))')
  362. .replace('comment', block._comment)
  363. .replace(/tag/g, '(?!(?:'
  364. + 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub'
  365. + '|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)'
  366. + '\\b)\\w+(?!:|[^\\w\\s@]*@)\\b')
  367. .getRegex(),
  368. def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
  369. heading: /^ *(#{1,6}) *([^\n]+?) *(?:#+ *)?(?:\n+|$)/,
  370. fences: noopTest$1, // fences not supported
  371. paragraph: edit$1(block.normal._paragraph)
  372. .replace('hr', block.hr)
  373. .replace('heading', ' *#{1,6} *[^\n]')
  374. .replace('lheading', block.lheading)
  375. .replace('blockquote', ' {0,3}>')
  376. .replace('|fences', '')
  377. .replace('|list', '')
  378. .replace('|html', '')
  379. .getRegex()
  380. });
  381. /**
  382. * Inline-Level Grammar
  383. */
  384. const inline = {
  385. escape: /^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/,
  386. autolink: /^<(scheme:[^\s\x00-\x1f<>]*|email)>/,
  387. url: noopTest$1,
  388. tag: '^comment'
  389. + '|^</[a-zA-Z][\\w:-]*\\s*>' // self-closing tag
  390. + '|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>' // open tag
  391. + '|^<\\?[\\s\\S]*?\\?>' // processing instruction, e.g. <?php ?>
  392. + '|^<![a-zA-Z]+\\s[\\s\\S]*?>' // declaration, e.g. <!DOCTYPE html>
  393. + '|^<!\\[CDATA\\[[\\s\\S]*?\\]\\]>', // CDATA section
  394. link: /^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/,
  395. reflink: /^!?\[(label)\]\[(?!\s*\])((?:\\[\[\]]?|[^\[\]\\])+)\]/,
  396. nolink: /^!?\[(?!\s*\])((?:\[[^\[\]]*\]|\\[\[\]]|[^\[\]])*)\](?:\[\])?/,
  397. strong: /^__([^\s_])__(?!_)|^\*\*([^\s*])\*\*(?!\*)|^__([^\s][\s\S]*?[^\s])__(?!_)|^\*\*([^\s][\s\S]*?[^\s])\*\*(?!\*)/,
  398. em: /^_([^\s_])_(?!_)|^\*([^\s*<\[])\*(?!\*)|^_([^\s<][\s\S]*?[^\s_])_(?!_|[^\spunctuation])|^_([^\s_<][\s\S]*?[^\s])_(?!_|[^\spunctuation])|^\*([^\s<"][\s\S]*?[^\s\*])\*(?!\*|[^\spunctuation])|^\*([^\s*"<\[][\s\S]*?[^\s])\*(?!\*)/,
  399. code: /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,
  400. br: /^( {2,}|\\)\n(?!\s*$)/,
  401. del: noopTest$1,
  402. text: /^(`+|[^`])(?:[\s\S]*?(?:(?=[\\<!\[`*]|\b_|$)|[^ ](?= {2,}\n))|(?= {2,}\n))/
  403. };
  404. // list of punctuation marks from common mark spec
  405. // without ` and ] to workaround Rule 17 (inline code blocks/links)
  406. inline._punctuation = '!"#$%&\'()*+,\\-./:;<=>?@\\[^_{|}~';
  407. inline.em = edit$1(inline.em).replace(/punctuation/g, inline._punctuation).getRegex();
  408. inline._escapes = /\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/g;
  409. inline._scheme = /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/;
  410. inline._email = /[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_])/;
  411. inline.autolink = edit$1(inline.autolink)
  412. .replace('scheme', inline._scheme)
  413. .replace('email', inline._email)
  414. .getRegex();
  415. inline._attribute = /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/;
  416. inline.tag = edit$1(inline.tag)
  417. .replace('comment', block._comment)
  418. .replace('attribute', inline._attribute)
  419. .getRegex();
  420. inline._label = /(?:\[[^\[\]]*\]|\\.|`[^`]*`|[^\[\]\\`])*?/;
  421. inline._href = /<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*/;
  422. inline._title = /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/;
  423. inline.link = edit$1(inline.link)
  424. .replace('label', inline._label)
  425. .replace('href', inline._href)
  426. .replace('title', inline._title)
  427. .getRegex();
  428. inline.reflink = edit$1(inline.reflink)
  429. .replace('label', inline._label)
  430. .getRegex();
  431. /**
  432. * Normal Inline Grammar
  433. */
  434. inline.normal = merge$1({}, inline);
  435. /**
  436. * Pedantic Inline Grammar
  437. */
  438. inline.pedantic = merge$1({}, inline.normal, {
  439. strong: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
  440. em: /^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/,
  441. link: edit$1(/^!?\[(label)\]\((.*?)\)/)
  442. .replace('label', inline._label)
  443. .getRegex(),
  444. reflink: edit$1(/^!?\[(label)\]\s*\[([^\]]*)\]/)
  445. .replace('label', inline._label)
  446. .getRegex()
  447. });
  448. /**
  449. * GFM Inline Grammar
  450. */
  451. inline.gfm = merge$1({}, inline.normal, {
  452. escape: edit$1(inline.escape).replace('])', '~|])').getRegex(),
  453. _extended_email: /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/,
  454. url: /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,
  455. _backpedal: /(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/,
  456. del: /^~+(?=\S)([\s\S]*?\S)~+/,
  457. text: /^(`+|[^`])(?:[\s\S]*?(?:(?=[\\<!\[`*~]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@))|(?= {2,}\n|[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@))/
  458. });
  459. inline.gfm.url = edit$1(inline.gfm.url, 'i')
  460. .replace('email', inline.gfm._extended_email)
  461. .getRegex();
  462. /**
  463. * GFM + Line Breaks Inline Grammar
  464. */
  465. inline.breaks = merge$1({}, inline.gfm, {
  466. br: edit$1(inline.br).replace('{2,}', '*').getRegex(),
  467. text: edit$1(inline.gfm.text)
  468. .replace('\\b_', '\\b_| {2,}\\n')
  469. .replace(/\{2,\}/g, '*')
  470. .getRegex()
  471. });
  472. var rules = {
  473. block,
  474. inline
  475. };
  476. const { defaults: defaults$1 } = defaults;
  477. const { block: block$1 } = rules;
  478. const {
  479. rtrim: rtrim$1,
  480. splitCells: splitCells$1,
  481. escape: escape$1
  482. } = helpers;
  483. /**
  484. * Block Lexer
  485. */
  486. var Lexer_1 = class Lexer {
  487. constructor(options) {
  488. this.tokens = [];
  489. this.tokens.links = Object.create(null);
  490. this.options = options || defaults$1;
  491. this.rules = block$1.normal;
  492. if (this.options.pedantic) {
  493. this.rules = block$1.pedantic;
  494. } else if (this.options.gfm) {
  495. this.rules = block$1.gfm;
  496. }
  497. }
  498. /**
  499. * Expose Block Rules
  500. */
  501. static get rules() {
  502. return block$1;
  503. }
  504. /**
  505. * Static Lex Method
  506. */
  507. static lex(src, options) {
  508. const lexer = new Lexer(options);
  509. return lexer.lex(src);
  510. };
  511. /**
  512. * Preprocessing
  513. */
  514. lex(src) {
  515. src = src
  516. .replace(/\r\n|\r/g, '\n')
  517. .replace(/\t/g, ' ');
  518. return this.token(src, true);
  519. };
  520. /**
  521. * Lexing
  522. */
  523. token(src, top) {
  524. src = src.replace(/^ +$/gm, '');
  525. let next,
  526. loose,
  527. cap,
  528. bull,
  529. b,
  530. item,
  531. listStart,
  532. listItems,
  533. t,
  534. space,
  535. i,
  536. tag,
  537. l,
  538. isordered,
  539. istask,
  540. ischecked;
  541. while (src) {
  542. // newline
  543. if (cap = this.rules.newline.exec(src)) {
  544. src = src.substring(cap[0].length);
  545. if (cap[0].length > 1) {
  546. this.tokens.push({
  547. type: 'space'
  548. });
  549. }
  550. }
  551. // code
  552. if (cap = this.rules.code.exec(src)) {
  553. const lastToken = this.tokens[this.tokens.length - 1];
  554. src = src.substring(cap[0].length);
  555. // An indented code block cannot interrupt a paragraph.
  556. if (lastToken && lastToken.type === 'paragraph') {
  557. lastToken.text += '\n' + cap[0].trimRight();
  558. } else {
  559. cap = cap[0].replace(/^ {4}/gm, '');
  560. this.tokens.push({
  561. type: 'code',
  562. codeBlockStyle: 'indented',
  563. text: !this.options.pedantic
  564. ? rtrim$1(cap, '\n')
  565. : cap
  566. });
  567. }
  568. continue;
  569. }
  570. // fences
  571. if (cap = this.rules.fences.exec(src)) {
  572. src = src.substring(cap[0].length);
  573. this.tokens.push({
  574. type: 'code',
  575. lang: cap[2] ? cap[2].trim() : cap[2],
  576. text: cap[3] || ''
  577. });
  578. continue;
  579. }
  580. // heading
  581. if (cap = this.rules.heading.exec(src)) {
  582. src = src.substring(cap[0].length);
  583. this.tokens.push({
  584. type: 'heading',
  585. depth: cap[1].length,
  586. text: cap[2]
  587. });
  588. continue;
  589. }
  590. // table no leading pipe (gfm)
  591. if (cap = this.rules.nptable.exec(src)) {
  592. item = {
  593. type: 'table',
  594. header: splitCells$1(cap[1].replace(/^ *| *\| *$/g, '')),
  595. align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
  596. cells: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : []
  597. };
  598. if (item.header.length === item.align.length) {
  599. src = src.substring(cap[0].length);
  600. for (i = 0; i < item.align.length; i++) {
  601. if (/^ *-+: *$/.test(item.align[i])) {
  602. item.align[i] = 'right';
  603. } else if (/^ *:-+: *$/.test(item.align[i])) {
  604. item.align[i] = 'center';
  605. } else if (/^ *:-+ *$/.test(item.align[i])) {
  606. item.align[i] = 'left';
  607. } else {
  608. item.align[i] = null;
  609. }
  610. }
  611. for (i = 0; i < item.cells.length; i++) {
  612. item.cells[i] = splitCells$1(item.cells[i], item.header.length);
  613. }
  614. this.tokens.push(item);
  615. continue;
  616. }
  617. }
  618. // hr
  619. if (cap = this.rules.hr.exec(src)) {
  620. src = src.substring(cap[0].length);
  621. this.tokens.push({
  622. type: 'hr'
  623. });
  624. continue;
  625. }
  626. // blockquote
  627. if (cap = this.rules.blockquote.exec(src)) {
  628. src = src.substring(cap[0].length);
  629. this.tokens.push({
  630. type: 'blockquote_start'
  631. });
  632. cap = cap[0].replace(/^ *> ?/gm, '');
  633. // Pass `top` to keep the current
  634. // "toplevel" state. This is exactly
  635. // how markdown.pl works.
  636. this.token(cap, top);
  637. this.tokens.push({
  638. type: 'blockquote_end'
  639. });
  640. continue;
  641. }
  642. // list
  643. if (cap = this.rules.list.exec(src)) {
  644. src = src.substring(cap[0].length);
  645. bull = cap[2];
  646. isordered = bull.length > 1;
  647. listStart = {
  648. type: 'list_start',
  649. ordered: isordered,
  650. start: isordered ? +bull : '',
  651. loose: false
  652. };
  653. this.tokens.push(listStart);
  654. // Get each top-level item.
  655. cap = cap[0].match(this.rules.item);
  656. listItems = [];
  657. next = false;
  658. l = cap.length;
  659. i = 0;
  660. for (; i < l; i++) {
  661. item = cap[i];
  662. // Remove the list item's bullet
  663. // so it is seen as the next token.
  664. space = item.length;
  665. item = item.replace(/^ *([*+-]|\d+\.) */, '');
  666. // Outdent whatever the
  667. // list item contains. Hacky.
  668. if (~item.indexOf('\n ')) {
  669. space -= item.length;
  670. item = !this.options.pedantic
  671. ? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '')
  672. : item.replace(/^ {1,4}/gm, '');
  673. }
  674. // Determine whether the next list item belongs here.
  675. // Backpedal if it does not belong in this list.
  676. if (i !== l - 1) {
  677. b = block$1.bullet.exec(cap[i + 1])[0];
  678. if (bull.length > 1 ? b.length === 1
  679. : (b.length > 1 || (this.options.smartLists && b !== bull))) {
  680. src = cap.slice(i + 1).join('\n') + src;
  681. i = l - 1;
  682. }
  683. }
  684. // Determine whether item is loose or not.
  685. // Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
  686. // for discount behavior.
  687. loose = next || /\n\n(?!\s*$)/.test(item);
  688. if (i !== l - 1) {
  689. next = item.charAt(item.length - 1) === '\n';
  690. if (!loose) loose = next;
  691. }
  692. if (loose) {
  693. listStart.loose = true;
  694. }
  695. // Check for task list items
  696. istask = /^\[[ xX]\] /.test(item);
  697. ischecked = undefined;
  698. if (istask) {
  699. ischecked = item[1] !== ' ';
  700. item = item.replace(/^\[[ xX]\] +/, '');
  701. }
  702. t = {
  703. type: 'list_item_start',
  704. task: istask,
  705. checked: ischecked,
  706. loose: loose
  707. };
  708. listItems.push(t);
  709. this.tokens.push(t);
  710. // Recurse.
  711. this.token(item, false);
  712. this.tokens.push({
  713. type: 'list_item_end'
  714. });
  715. }
  716. if (listStart.loose) {
  717. l = listItems.length;
  718. i = 0;
  719. for (; i < l; i++) {
  720. listItems[i].loose = true;
  721. }
  722. }
  723. this.tokens.push({
  724. type: 'list_end'
  725. });
  726. continue;
  727. }
  728. // html
  729. if (cap = this.rules.html.exec(src)) {
  730. src = src.substring(cap[0].length);
  731. this.tokens.push({
  732. type: this.options.sanitize
  733. ? 'paragraph'
  734. : 'html',
  735. pre: !this.options.sanitizer
  736. && (cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style'),
  737. text: this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape$1(cap[0])) : cap[0]
  738. });
  739. continue;
  740. }
  741. // def
  742. if (top && (cap = this.rules.def.exec(src))) {
  743. src = src.substring(cap[0].length);
  744. if (cap[3]) cap[3] = cap[3].substring(1, cap[3].length - 1);
  745. tag = cap[1].toLowerCase().replace(/\s+/g, ' ');
  746. if (!this.tokens.links[tag]) {
  747. this.tokens.links[tag] = {
  748. href: cap[2],
  749. title: cap[3]
  750. };
  751. }
  752. continue;
  753. }
  754. // table (gfm)
  755. if (cap = this.rules.table.exec(src)) {
  756. item = {
  757. type: 'table',
  758. header: splitCells$1(cap[1].replace(/^ *| *\| *$/g, '')),
  759. align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
  760. cells: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : []
  761. };
  762. if (item.header.length === item.align.length) {
  763. src = src.substring(cap[0].length);
  764. for (i = 0; i < item.align.length; i++) {
  765. if (/^ *-+: *$/.test(item.align[i])) {
  766. item.align[i] = 'right';
  767. } else if (/^ *:-+: *$/.test(item.align[i])) {
  768. item.align[i] = 'center';
  769. } else if (/^ *:-+ *$/.test(item.align[i])) {
  770. item.align[i] = 'left';
  771. } else {
  772. item.align[i] = null;
  773. }
  774. }
  775. for (i = 0; i < item.cells.length; i++) {
  776. item.cells[i] = splitCells$1(
  777. item.cells[i].replace(/^ *\| *| *\| *$/g, ''),
  778. item.header.length);
  779. }
  780. this.tokens.push(item);
  781. continue;
  782. }
  783. }
  784. // lheading
  785. if (cap = this.rules.lheading.exec(src)) {
  786. src = src.substring(cap[0].length);
  787. this.tokens.push({
  788. type: 'heading',
  789. depth: cap[2].charAt(0) === '=' ? 1 : 2,
  790. text: cap[1]
  791. });
  792. continue;
  793. }
  794. // top-level paragraph
  795. if (top && (cap = this.rules.paragraph.exec(src))) {
  796. src = src.substring(cap[0].length);
  797. this.tokens.push({
  798. type: 'paragraph',
  799. text: cap[1].charAt(cap[1].length - 1) === '\n'
  800. ? cap[1].slice(0, -1)
  801. : cap[1]
  802. });
  803. continue;
  804. }
  805. // text
  806. if (cap = this.rules.text.exec(src)) {
  807. // Top-level should never reach here.
  808. src = src.substring(cap[0].length);
  809. this.tokens.push({
  810. type: 'text',
  811. text: cap[0]
  812. });
  813. continue;
  814. }
  815. if (src) {
  816. throw new Error('Infinite loop on byte: ' + src.charCodeAt(0));
  817. }
  818. }
  819. return this.tokens;
  820. };
  821. };
  822. const { defaults: defaults$2 } = defaults;
  823. const {
  824. cleanUrl: cleanUrl$1,
  825. escape: escape$2
  826. } = helpers;
  827. /**
  828. * Renderer
  829. */
  830. var Renderer_1 = class Renderer {
  831. constructor(options) {
  832. this.options = options || defaults$2;
  833. }
  834. code(code, infostring, escaped) {
  835. const lang = (infostring || '').match(/\S*/)[0];
  836. if (this.options.highlight) {
  837. const out = this.options.highlight(code, lang);
  838. if (out != null && out !== code) {
  839. escaped = true;
  840. code = out;
  841. }
  842. }
  843. if (!lang) {
  844. return '<pre><code>'
  845. + (escaped ? code : escape$2(code, true))
  846. + '</code></pre>';
  847. }
  848. return '<pre><code class="'
  849. + this.options.langPrefix
  850. + escape$2(lang, true)
  851. + '">'
  852. + (escaped ? code : escape$2(code, true))
  853. + '</code></pre>\n';
  854. };
  855. blockquote(quote) {
  856. return '<blockquote>\n' + quote + '</blockquote>\n';
  857. };
  858. html(html) {
  859. return html;
  860. };
  861. heading(text, level, raw, slugger) {
  862. if (this.options.headerIds) {
  863. return '<h'
  864. + level
  865. + ' id="'
  866. + this.options.headerPrefix
  867. + slugger.slug(raw)
  868. + '">'
  869. + text
  870. + '</h'
  871. + level
  872. + '>\n';
  873. }
  874. // ignore IDs
  875. return '<h' + level + '>' + text + '</h' + level + '>\n';
  876. };
  877. hr() {
  878. return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
  879. };
  880. list(body, ordered, start) {
  881. const type = ordered ? 'ol' : 'ul',
  882. startatt = (ordered && start !== 1) ? (' start="' + start + '"') : '';
  883. return '<' + type + startatt + '>\n' + body + '</' + type + '>\n';
  884. };
  885. listitem(text) {
  886. return '<li>' + text + '</li>\n';
  887. };
  888. checkbox(checked) {
  889. return '<input '
  890. + (checked ? 'checked="" ' : '')
  891. + 'disabled="" type="checkbox"'
  892. + (this.options.xhtml ? ' /' : '')
  893. + '> ';
  894. };
  895. paragraph(text) {
  896. return '<p>' + text + '</p>\n';
  897. };
  898. table(header, body) {
  899. if (body) body = '<tbody>' + body + '</tbody>';
  900. return '<table>\n'
  901. + '<thead>\n'
  902. + header
  903. + '</thead>\n'
  904. + body
  905. + '</table>\n';
  906. };
  907. tablerow(content) {
  908. return '<tr>\n' + content + '</tr>\n';
  909. };
  910. tablecell(content, flags) {
  911. const type = flags.header ? 'th' : 'td';
  912. const tag = flags.align
  913. ? '<' + type + ' align="' + flags.align + '">'
  914. : '<' + type + '>';
  915. return tag + content + '</' + type + '>\n';
  916. };
  917. // span level renderer
  918. strong(text) {
  919. return '<strong>' + text + '</strong>';
  920. };
  921. em(text) {
  922. return '<em>' + text + '</em>';
  923. };
  924. codespan(text) {
  925. return '<code>' + text + '</code>';
  926. };
  927. br() {
  928. return this.options.xhtml ? '<br/>' : '<br>';
  929. };
  930. del(text) {
  931. return '<del>' + text + '</del>';
  932. };
  933. link(href, title, text) {
  934. href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href);
  935. if (href === null) {
  936. return text;
  937. }
  938. let out = '<a href="' + escape$2(href) + '"';
  939. if (title) {
  940. out += ' title="' + title + '"';
  941. }
  942. out += '>' + text + '</a>';
  943. return out;
  944. };
  945. image(href, title, text) {
  946. href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href);
  947. if (href === null) {
  948. return text;
  949. }
  950. let out = '<img src="' + href + '" alt="' + text + '"';
  951. if (title) {
  952. out += ' title="' + title + '"';
  953. }
  954. out += this.options.xhtml ? '/>' : '>';
  955. return out;
  956. };
  957. text(text) {
  958. return text;
  959. };
  960. };
  961. /**
  962. * Slugger generates header id
  963. */
  964. var Slugger_1 = class Slugger {
  965. constructor() {
  966. this.seen = {};
  967. }
  968. /**
  969. * Convert string to unique id
  970. */
  971. slug(value) {
  972. let slug = value
  973. .toLowerCase()
  974. .trim()
  975. .replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '')
  976. .replace(/\s/g, '-');
  977. if (this.seen.hasOwnProperty(slug)) {
  978. const originalSlug = slug;
  979. do {
  980. this.seen[originalSlug]++;
  981. slug = originalSlug + '-' + this.seen[originalSlug];
  982. } while (this.seen.hasOwnProperty(slug));
  983. }
  984. this.seen[slug] = 0;
  985. return slug;
  986. };
  987. };
  988. const { defaults: defaults$3 } = defaults;
  989. const { inline: inline$1 } = rules;
  990. const {
  991. findClosingBracket: findClosingBracket$1,
  992. escape: escape$3
  993. } = helpers;
  994. /**
  995. * Inline Lexer & Compiler
  996. */
  997. var InlineLexer_1 = class InlineLexer {
  998. constructor(links, options) {
  999. this.options = options || defaults$3;
  1000. this.links = links;
  1001. this.rules = inline$1.normal;
  1002. this.options.renderer = this.options.renderer || new Renderer_1();
  1003. this.renderer = this.options.renderer;
  1004. this.renderer.options = this.options;
  1005. if (!this.links) {
  1006. throw new Error('Tokens array requires a `links` property.');
  1007. }
  1008. if (this.options.pedantic) {
  1009. this.rules = inline$1.pedantic;
  1010. } else if (this.options.gfm) {
  1011. if (this.options.breaks) {
  1012. this.rules = inline$1.breaks;
  1013. } else {
  1014. this.rules = inline$1.gfm;
  1015. }
  1016. }
  1017. }
  1018. /**
  1019. * Expose Inline Rules
  1020. */
  1021. static get rules() {
  1022. return inline$1;
  1023. }
  1024. /**
  1025. * Static Lexing/Compiling Method
  1026. */
  1027. static output(src, links, options) {
  1028. const inline = new InlineLexer(links, options);
  1029. return inline.output(src);
  1030. }
  1031. /**
  1032. * Lexing/Compiling
  1033. */
  1034. output(src) {
  1035. let out = '',
  1036. link,
  1037. text,
  1038. href,
  1039. title,
  1040. cap,
  1041. prevCapZero;
  1042. while (src) {
  1043. // escape
  1044. if (cap = this.rules.escape.exec(src)) {
  1045. src = src.substring(cap[0].length);
  1046. out += escape$3(cap[1]);
  1047. continue;
  1048. }
  1049. // tag
  1050. if (cap = this.rules.tag.exec(src)) {
  1051. if (!this.inLink && /^<a /i.test(cap[0])) {
  1052. this.inLink = true;
  1053. } else if (this.inLink && /^<\/a>/i.test(cap[0])) {
  1054. this.inLink = false;
  1055. }
  1056. if (!this.inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
  1057. this.inRawBlock = true;
  1058. } else if (this.inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
  1059. this.inRawBlock = false;
  1060. }
  1061. src = src.substring(cap[0].length);
  1062. out += this.renderer.html(this.options.sanitize
  1063. ? (this.options.sanitizer
  1064. ? this.options.sanitizer(cap[0])
  1065. : escape$3(cap[0]))
  1066. : cap[0]);
  1067. continue;
  1068. }
  1069. // link
  1070. if (cap = this.rules.link.exec(src)) {
  1071. const lastParenIndex = findClosingBracket$1(cap[2], '()');
  1072. if (lastParenIndex > -1) {
  1073. const start = cap[0].indexOf('!') === 0 ? 5 : 4;
  1074. const linkLen = start + cap[1].length + lastParenIndex;
  1075. cap[2] = cap[2].substring(0, lastParenIndex);
  1076. cap[0] = cap[0].substring(0, linkLen).trim();
  1077. cap[3] = '';
  1078. }
  1079. src = src.substring(cap[0].length);
  1080. this.inLink = true;
  1081. href = cap[2];
  1082. if (this.options.pedantic) {
  1083. link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href);
  1084. if (link) {
  1085. href = link[1];
  1086. title = link[3];
  1087. } else {
  1088. title = '';
  1089. }
  1090. } else {
  1091. title = cap[3] ? cap[3].slice(1, -1) : '';
  1092. }
  1093. href = href.trim().replace(/^<([\s\S]*)>$/, '$1');
  1094. out += this.outputLink(cap, {
  1095. href: InlineLexer.escapes(href),
  1096. title: InlineLexer.escapes(title)
  1097. });
  1098. this.inLink = false;
  1099. continue;
  1100. }
  1101. // reflink, nolink
  1102. if ((cap = this.rules.reflink.exec(src))
  1103. || (cap = this.rules.nolink.exec(src))) {
  1104. src = src.substring(cap[0].length);
  1105. link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
  1106. link = this.links[link.toLowerCase()];
  1107. if (!link || !link.href) {
  1108. out += cap[0].charAt(0);
  1109. src = cap[0].substring(1) + src;
  1110. continue;
  1111. }
  1112. this.inLink = true;
  1113. out += this.outputLink(cap, link);
  1114. this.inLink = false;
  1115. continue;
  1116. }
  1117. // strong
  1118. if (cap = this.rules.strong.exec(src)) {
  1119. src = src.substring(cap[0].length);
  1120. out += this.renderer.strong(this.output(cap[4] || cap[3] || cap[2] || cap[1]));
  1121. continue;
  1122. }
  1123. // em
  1124. if (cap = this.rules.em.exec(src)) {
  1125. src = src.substring(cap[0].length);
  1126. out += this.renderer.em(this.output(cap[6] || cap[5] || cap[4] || cap[3] || cap[2] || cap[1]));
  1127. continue;
  1128. }
  1129. // code
  1130. if (cap = this.rules.code.exec(src)) {
  1131. src = src.substring(cap[0].length);
  1132. out += this.renderer.codespan(escape$3(cap[2].trim(), true));
  1133. continue;
  1134. }
  1135. // br
  1136. if (cap = this.rules.br.exec(src)) {
  1137. src = src.substring(cap[0].length);
  1138. out += this.renderer.br();
  1139. continue;
  1140. }
  1141. // del (gfm)
  1142. if (cap = this.rules.del.exec(src)) {
  1143. src = src.substring(cap[0].length);
  1144. out += this.renderer.del(this.output(cap[1]));
  1145. continue;
  1146. }
  1147. // autolink
  1148. if (cap = this.rules.autolink.exec(src)) {
  1149. src = src.substring(cap[0].length);
  1150. if (cap[2] === '@') {
  1151. text = escape$3(this.mangle(cap[1]));
  1152. href = 'mailto:' + text;
  1153. } else {
  1154. text = escape$3(cap[1]);
  1155. href = text;
  1156. }
  1157. out += this.renderer.link(href, null, text);
  1158. continue;
  1159. }
  1160. // url (gfm)
  1161. if (!this.inLink && (cap = this.rules.url.exec(src))) {
  1162. if (cap[2] === '@') {
  1163. text = escape$3(cap[0]);
  1164. href = 'mailto:' + text;
  1165. } else {
  1166. // do extended autolink path validation
  1167. do {
  1168. prevCapZero = cap[0];
  1169. cap[0] = this.rules._backpedal.exec(cap[0])[0];
  1170. } while (prevCapZero !== cap[0]);
  1171. text = escape$3(cap[0]);
  1172. if (cap[1] === 'www.') {
  1173. href = 'http://' + text;
  1174. } else {
  1175. href = text;
  1176. }
  1177. }
  1178. src = src.substring(cap[0].length);
  1179. out += this.renderer.link(href, null, text);
  1180. continue;
  1181. }
  1182. // text
  1183. if (cap = this.rules.text.exec(src)) {
  1184. src = src.substring(cap[0].length);
  1185. if (this.inRawBlock) {
  1186. out += this.renderer.text(this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape$3(cap[0])) : cap[0]);
  1187. } else {
  1188. out += this.renderer.text(escape$3(this.smartypants(cap[0])));
  1189. }
  1190. continue;
  1191. }
  1192. if (src) {
  1193. throw new Error('Infinite loop on byte: ' + src.charCodeAt(0));
  1194. }
  1195. }
  1196. return out;
  1197. }
  1198. static escapes(text) {
  1199. return text ? text.replace(InlineLexer.rules._escapes, '$1') : text;
  1200. }
  1201. /**
  1202. * Compile Link
  1203. */
  1204. outputLink(cap, link) {
  1205. const href = link.href,
  1206. title = link.title ? escape$3(link.title) : null;
  1207. return cap[0].charAt(0) !== '!'
  1208. ? this.renderer.link(href, title, this.output(cap[1]))
  1209. : this.renderer.image(href, title, escape$3(cap[1]));
  1210. }
  1211. /**
  1212. * Smartypants Transformations
  1213. */
  1214. smartypants(text) {
  1215. if (!this.options.smartypants) return text;
  1216. return text
  1217. // em-dashes
  1218. .replace(/---/g, '\u2014')
  1219. // en-dashes
  1220. .replace(/--/g, '\u2013')
  1221. // opening singles
  1222. .replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018')
  1223. // closing singles & apostrophes
  1224. .replace(/'/g, '\u2019')
  1225. // opening doubles
  1226. .replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c')
  1227. // closing doubles
  1228. .replace(/"/g, '\u201d')
  1229. // ellipses
  1230. .replace(/\.{3}/g, '\u2026');
  1231. }
  1232. /**
  1233. * Mangle Links
  1234. */
  1235. mangle(text) {
  1236. if (!this.options.mangle) return text;
  1237. const l = text.length;
  1238. let out = '',
  1239. i = 0,
  1240. ch;
  1241. for (; i < l; i++) {
  1242. ch = text.charCodeAt(i);
  1243. if (Math.random() > 0.5) {
  1244. ch = 'x' + ch.toString(16);
  1245. }
  1246. out += '&#' + ch + ';';
  1247. }
  1248. return out;
  1249. }
  1250. };
  1251. /**
  1252. * TextRenderer
  1253. * returns only the textual part of the token
  1254. */
  1255. var TextRenderer_1 = class TextRenderer {
  1256. // no need for block level renderers
  1257. strong(text) {
  1258. return text;
  1259. }
  1260. em(text) {
  1261. return text;
  1262. }
  1263. codespan(text) {
  1264. return text;
  1265. }
  1266. del(text) {
  1267. return text;
  1268. }
  1269. text(text) {
  1270. return text;
  1271. }
  1272. link(href, title, text) {
  1273. return '' + text;
  1274. }
  1275. image(href, title, text) {
  1276. return '' + text;
  1277. }
  1278. br() {
  1279. return '';
  1280. }
  1281. };
  1282. const { defaults: defaults$4 } = defaults;
  1283. const {
  1284. merge: merge$2,
  1285. unescape: unescape$1
  1286. } = helpers;
  1287. /**
  1288. * Parsing & Compiling
  1289. */
  1290. var Parser_1 = class Parser {
  1291. constructor(options) {
  1292. this.tokens = [];
  1293. this.token = null;
  1294. this.options = options || defaults$4;
  1295. this.options.renderer = this.options.renderer || new Renderer_1();
  1296. this.renderer = this.options.renderer;
  1297. this.renderer.options = this.options;
  1298. this.slugger = new Slugger_1();
  1299. }
  1300. /**
  1301. * Static Parse Method
  1302. */
  1303. static parse(tokens, options) {
  1304. const parser = new Parser(options);
  1305. return parser.parse(tokens);
  1306. };
  1307. /**
  1308. * Parse Loop
  1309. */
  1310. parse(tokens) {
  1311. this.inline = new InlineLexer_1(tokens.links, this.options);
  1312. // use an InlineLexer with a TextRenderer to extract pure text
  1313. this.inlineText = new InlineLexer_1(
  1314. tokens.links,
  1315. merge$2({}, this.options, { renderer: new TextRenderer_1() })
  1316. );
  1317. this.tokens = tokens.reverse();
  1318. let out = '';
  1319. while (this.next()) {
  1320. out += this.tok();
  1321. }
  1322. return out;
  1323. };
  1324. /**
  1325. * Next Token
  1326. */
  1327. next() {
  1328. this.token = this.tokens.pop();
  1329. return this.token;
  1330. };
  1331. /**
  1332. * Preview Next Token
  1333. */
  1334. peek() {
  1335. return this.tokens[this.tokens.length - 1] || 0;
  1336. };
  1337. /**
  1338. * Parse Text Tokens
  1339. */
  1340. parseText() {
  1341. let body = this.token.text;
  1342. while (this.peek().type === 'text') {
  1343. body += '\n' + this.next().text;
  1344. }
  1345. return this.inline.output(body);
  1346. };
  1347. /**
  1348. * Parse Current Token
  1349. */
  1350. tok() {
  1351. let body = '';
  1352. switch (this.token.type) {
  1353. case 'space': {
  1354. return '';
  1355. }
  1356. case 'hr': {
  1357. return this.renderer.hr();
  1358. }
  1359. case 'heading': {
  1360. return this.renderer.heading(
  1361. this.inline.output(this.token.text),
  1362. this.token.depth,
  1363. unescape$1(this.inlineText.output(this.token.text)),
  1364. this.slugger);
  1365. }
  1366. case 'code': {
  1367. return this.renderer.code(this.token.text,
  1368. this.token.lang,
  1369. this.token.escaped);
  1370. }
  1371. case 'table': {
  1372. let header = '',
  1373. i,
  1374. row,
  1375. cell,
  1376. j;
  1377. // header
  1378. cell = '';
  1379. for (i = 0; i < this.token.header.length; i++) {
  1380. cell += this.renderer.tablecell(
  1381. this.inline.output(this.token.header[i]),
  1382. { header: true, align: this.token.align[i] }
  1383. );
  1384. }
  1385. header += this.renderer.tablerow(cell);
  1386. for (i = 0; i < this.token.cells.length; i++) {
  1387. row = this.token.cells[i];
  1388. cell = '';
  1389. for (j = 0; j < row.length; j++) {
  1390. cell += this.renderer.tablecell(
  1391. this.inline.output(row[j]),
  1392. { header: false, align: this.token.align[j] }
  1393. );
  1394. }
  1395. body += this.renderer.tablerow(cell);
  1396. }
  1397. return this.renderer.table(header, body);
  1398. }
  1399. case 'blockquote_start': {
  1400. body = '';
  1401. while (this.next().type !== 'blockquote_end') {
  1402. body += this.tok();
  1403. }
  1404. return this.renderer.blockquote(body);
  1405. }
  1406. case 'list_start': {
  1407. body = '';
  1408. const ordered = this.token.ordered,
  1409. start = this.token.start;
  1410. while (this.next().type !== 'list_end') {
  1411. body += this.tok();
  1412. }
  1413. return this.renderer.list(body, ordered, start);
  1414. }
  1415. case 'list_item_start': {
  1416. body = '';
  1417. const loose = this.token.loose;
  1418. const checked = this.token.checked;
  1419. const task = this.token.task;
  1420. if (this.token.task) {
  1421. if (loose) {
  1422. if (this.peek().type === 'text') {
  1423. const nextToken = this.peek();
  1424. nextToken.text = this.renderer.checkbox(checked) + ' ' + nextToken.text;
  1425. } else {
  1426. this.tokens.push({
  1427. type: 'text',
  1428. text: this.renderer.checkbox(checked)
  1429. });
  1430. }
  1431. } else {
  1432. body += this.renderer.checkbox(checked);
  1433. }
  1434. }
  1435. while (this.next().type !== 'list_item_end') {
  1436. body += !loose && this.token.type === 'text'
  1437. ? this.parseText()
  1438. : this.tok();
  1439. }
  1440. return this.renderer.listitem(body, task, checked);
  1441. }
  1442. case 'html': {
  1443. // TODO parse inline content if parameter markdown=1
  1444. return this.renderer.html(this.token.text);
  1445. }
  1446. case 'paragraph': {
  1447. return this.renderer.paragraph(this.inline.output(this.token.text));
  1448. }
  1449. case 'text': {
  1450. return this.renderer.paragraph(this.parseText());
  1451. }
  1452. default: {
  1453. const errMsg = 'Token with "' + this.token.type + '" type was not found.';
  1454. if (this.options.silent) {
  1455. console.log(errMsg);
  1456. } else {
  1457. throw new Error(errMsg);
  1458. }
  1459. }
  1460. }
  1461. };
  1462. };
  1463. const {
  1464. merge: merge$3,
  1465. checkSanitizeDeprecation: checkSanitizeDeprecation$1,
  1466. escape: escape$4
  1467. } = helpers;
  1468. const {
  1469. getDefaults,
  1470. changeDefaults,
  1471. defaults: defaults$5
  1472. } = defaults;
  1473. /**
  1474. * Marked
  1475. */
  1476. function marked(src, opt, callback) {
  1477. // throw error in case of non string input
  1478. if (typeof src === 'undefined' || src === null) {
  1479. throw new Error('marked(): input parameter is undefined or null');
  1480. }
  1481. if (typeof src !== 'string') {
  1482. throw new Error('marked(): input parameter is of type '
  1483. + Object.prototype.toString.call(src) + ', string expected');
  1484. }
  1485. if (callback || typeof opt === 'function') {
  1486. if (!callback) {
  1487. callback = opt;
  1488. opt = null;
  1489. }
  1490. opt = merge$3({}, marked.defaults, opt || {});
  1491. checkSanitizeDeprecation$1(opt);
  1492. const highlight = opt.highlight;
  1493. let tokens,
  1494. pending,
  1495. i = 0;
  1496. try {
  1497. tokens = Lexer_1.lex(src, opt);
  1498. } catch (e) {
  1499. return callback(e);
  1500. }
  1501. pending = tokens.length;
  1502. const done = function(err) {
  1503. if (err) {
  1504. opt.highlight = highlight;
  1505. return callback(err);
  1506. }
  1507. let out;
  1508. try {
  1509. out = Parser_1.parse(tokens, opt);
  1510. } catch (e) {
  1511. err = e;
  1512. }
  1513. opt.highlight = highlight;
  1514. return err
  1515. ? callback(err)
  1516. : callback(null, out);
  1517. };
  1518. if (!highlight || highlight.length < 3) {
  1519. return done();
  1520. }
  1521. delete opt.highlight;
  1522. if (!pending) return done();
  1523. for (; i < tokens.length; i++) {
  1524. (function(token) {
  1525. if (token.type !== 'code') {
  1526. return --pending || done();
  1527. }
  1528. return highlight(token.text, token.lang, function(err, code) {
  1529. if (err) return done(err);
  1530. if (code == null || code === token.text) {
  1531. return --pending || done();
  1532. }
  1533. token.text = code;
  1534. token.escaped = true;
  1535. --pending || done();
  1536. });
  1537. })(tokens[i]);
  1538. }
  1539. return;
  1540. }
  1541. try {
  1542. opt = merge$3({}, marked.defaults, opt || {});
  1543. checkSanitizeDeprecation$1(opt);
  1544. return Parser_1.parse(Lexer_1.lex(src, opt), opt);
  1545. } catch (e) {
  1546. e.message += '\nPlease report this to https://github.com/markedjs/marked.';
  1547. if ((opt || marked.defaults).silent) {
  1548. return '<p>An error occurred:</p><pre>'
  1549. + escape$4(e.message + '', true)
  1550. + '</pre>';
  1551. }
  1552. throw e;
  1553. }
  1554. }
  1555. /**
  1556. * Options
  1557. */
  1558. marked.options =
  1559. marked.setOptions = function(opt) {
  1560. merge$3(marked.defaults, opt);
  1561. changeDefaults(marked.defaults);
  1562. return marked;
  1563. };
  1564. marked.getDefaults = getDefaults;
  1565. marked.defaults = defaults$5;
  1566. /**
  1567. * Expose
  1568. */
  1569. marked.Parser = Parser_1;
  1570. marked.parser = Parser_1.parse;
  1571. marked.Renderer = Renderer_1;
  1572. marked.TextRenderer = TextRenderer_1;
  1573. marked.Lexer = Lexer_1;
  1574. marked.lexer = Lexer_1.lex;
  1575. marked.InlineLexer = InlineLexer_1;
  1576. marked.inlineLexer = InlineLexer_1.output;
  1577. marked.Slugger = Slugger_1;
  1578. marked.parse = marked;
  1579. var marked_1 = marked;
  1580. export default marked_1;