marked.esm.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356
  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. tokenizer: null,
  32. walkTokens: null,
  33. xhtml: false
  34. };
  35. }
  36. function changeDefaults(newDefaults) {
  37. module.exports.defaults = newDefaults;
  38. }
  39. module.exports = {
  40. defaults: getDefaults(),
  41. getDefaults,
  42. changeDefaults
  43. };
  44. });
  45. var defaults_1 = defaults.defaults;
  46. var defaults_2 = defaults.getDefaults;
  47. var defaults_3 = defaults.changeDefaults;
  48. /**
  49. * Helpers
  50. */
  51. const escapeTest = /[&<>"']/;
  52. const escapeReplace = /[&<>"']/g;
  53. const escapeTestNoEncode = /[<>"']|&(?!#?\w+;)/;
  54. const escapeReplaceNoEncode = /[<>"']|&(?!#?\w+;)/g;
  55. const escapeReplacements = {
  56. '&': '&amp;',
  57. '<': '&lt;',
  58. '>': '&gt;',
  59. '"': '&quot;',
  60. "'": '&#39;'
  61. };
  62. const getEscapeReplacement = (ch) => escapeReplacements[ch];
  63. function escape(html, encode) {
  64. if (encode) {
  65. if (escapeTest.test(html)) {
  66. return html.replace(escapeReplace, getEscapeReplacement);
  67. }
  68. } else {
  69. if (escapeTestNoEncode.test(html)) {
  70. return html.replace(escapeReplaceNoEncode, getEscapeReplacement);
  71. }
  72. }
  73. return html;
  74. }
  75. const unescapeTest = /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig;
  76. function unescape(html) {
  77. // explicitly match decimal, hex, and named HTML entities
  78. return html.replace(unescapeTest, (_, n) => {
  79. n = n.toLowerCase();
  80. if (n === 'colon') return ':';
  81. if (n.charAt(0) === '#') {
  82. return n.charAt(1) === 'x'
  83. ? String.fromCharCode(parseInt(n.substring(2), 16))
  84. : String.fromCharCode(+n.substring(1));
  85. }
  86. return '';
  87. });
  88. }
  89. const caret = /(^|[^\[])\^/g;
  90. function edit(regex, opt) {
  91. regex = regex.source || regex;
  92. opt = opt || '';
  93. const obj = {
  94. replace: (name, val) => {
  95. val = val.source || val;
  96. val = val.replace(caret, '$1');
  97. regex = regex.replace(name, val);
  98. return obj;
  99. },
  100. getRegex: () => {
  101. return new RegExp(regex, opt);
  102. }
  103. };
  104. return obj;
  105. }
  106. const nonWordAndColonTest = /[^\w:]/g;
  107. const originIndependentUrl = /^$|^[a-z][a-z0-9+.-]*:|^[?#]/i;
  108. function cleanUrl(sanitize, base, href) {
  109. if (sanitize) {
  110. let prot;
  111. try {
  112. prot = decodeURIComponent(unescape(href))
  113. .replace(nonWordAndColonTest, '')
  114. .toLowerCase();
  115. } catch (e) {
  116. return null;
  117. }
  118. if (prot.indexOf('javascript:') === 0 || prot.indexOf('vbscript:') === 0 || prot.indexOf('data:') === 0) {
  119. return null;
  120. }
  121. }
  122. if (base && !originIndependentUrl.test(href)) {
  123. href = resolveUrl(base, href);
  124. }
  125. try {
  126. href = encodeURI(href).replace(/%25/g, '%');
  127. } catch (e) {
  128. return null;
  129. }
  130. return href;
  131. }
  132. const baseUrls = {};
  133. const justDomain = /^[^:]+:\/*[^/]*$/;
  134. const protocol = /^([^:]+:)[\s\S]*$/;
  135. const domain = /^([^:]+:\/*[^/]*)[\s\S]*$/;
  136. function resolveUrl(base, href) {
  137. if (!baseUrls[' ' + base]) {
  138. // we can ignore everything in base after the last slash of its path component,
  139. // but we might need to add _that_
  140. // https://tools.ietf.org/html/rfc3986#section-3
  141. if (justDomain.test(base)) {
  142. baseUrls[' ' + base] = base + '/';
  143. } else {
  144. baseUrls[' ' + base] = rtrim(base, '/', true);
  145. }
  146. }
  147. base = baseUrls[' ' + base];
  148. const relativeBase = base.indexOf(':') === -1;
  149. if (href.substring(0, 2) === '//') {
  150. if (relativeBase) {
  151. return href;
  152. }
  153. return base.replace(protocol, '$1') + href;
  154. } else if (href.charAt(0) === '/') {
  155. if (relativeBase) {
  156. return href;
  157. }
  158. return base.replace(domain, '$1') + href;
  159. } else {
  160. return base + href;
  161. }
  162. }
  163. const noopTest = { exec: function noopTest() {} };
  164. function merge(obj) {
  165. let i = 1,
  166. target,
  167. key;
  168. for (; i < arguments.length; i++) {
  169. target = arguments[i];
  170. for (key in target) {
  171. if (Object.prototype.hasOwnProperty.call(target, key)) {
  172. obj[key] = target[key];
  173. }
  174. }
  175. }
  176. return obj;
  177. }
  178. function splitCells(tableRow, count) {
  179. // ensure that every cell-delimiting pipe has a space
  180. // before it to distinguish it from an escaped pipe
  181. const row = tableRow.replace(/\|/g, (match, offset, str) => {
  182. let escaped = false,
  183. curr = offset;
  184. while (--curr >= 0 && str[curr] === '\\') escaped = !escaped;
  185. if (escaped) {
  186. // odd number of slashes means | is escaped
  187. // so we leave it alone
  188. return '|';
  189. } else {
  190. // add space before unescaped |
  191. return ' |';
  192. }
  193. }),
  194. cells = row.split(/ \|/);
  195. let i = 0;
  196. if (cells.length > count) {
  197. cells.splice(count);
  198. } else {
  199. while (cells.length < count) cells.push('');
  200. }
  201. for (; i < cells.length; i++) {
  202. // leading or trailing whitespace is ignored per the gfm spec
  203. cells[i] = cells[i].trim().replace(/\\\|/g, '|');
  204. }
  205. return cells;
  206. }
  207. // Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
  208. // /c*$/ is vulnerable to REDOS.
  209. // invert: Remove suffix of non-c chars instead. Default falsey.
  210. function rtrim(str, c, invert) {
  211. const l = str.length;
  212. if (l === 0) {
  213. return '';
  214. }
  215. // Length of suffix matching the invert condition.
  216. let suffLen = 0;
  217. // Step left until we fail to match the invert condition.
  218. while (suffLen < l) {
  219. const currChar = str.charAt(l - suffLen - 1);
  220. if (currChar === c && !invert) {
  221. suffLen++;
  222. } else if (currChar !== c && invert) {
  223. suffLen++;
  224. } else {
  225. break;
  226. }
  227. }
  228. return str.substr(0, l - suffLen);
  229. }
  230. function findClosingBracket(str, b) {
  231. if (str.indexOf(b[1]) === -1) {
  232. return -1;
  233. }
  234. const l = str.length;
  235. let level = 0,
  236. i = 0;
  237. for (; i < l; i++) {
  238. if (str[i] === '\\') {
  239. i++;
  240. } else if (str[i] === b[0]) {
  241. level++;
  242. } else if (str[i] === b[1]) {
  243. level--;
  244. if (level < 0) {
  245. return i;
  246. }
  247. }
  248. }
  249. return -1;
  250. }
  251. function checkSanitizeDeprecation(opt) {
  252. if (opt && opt.sanitize && !opt.silent) {
  253. 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');
  254. }
  255. }
  256. var helpers = {
  257. escape,
  258. unescape,
  259. edit,
  260. cleanUrl,
  261. resolveUrl,
  262. noopTest,
  263. merge,
  264. splitCells,
  265. rtrim,
  266. findClosingBracket,
  267. checkSanitizeDeprecation
  268. };
  269. const { defaults: defaults$1 } = defaults;
  270. const {
  271. rtrim: rtrim$1,
  272. splitCells: splitCells$1,
  273. escape: escape$1,
  274. findClosingBracket: findClosingBracket$1
  275. } = helpers;
  276. function outputLink(cap, link, raw) {
  277. const href = link.href;
  278. const title = link.title ? escape$1(link.title) : null;
  279. const text = cap[1].replace(/\\([\[\]])/g, '$1');
  280. if (cap[0].charAt(0) !== '!') {
  281. return {
  282. type: 'link',
  283. raw,
  284. href,
  285. title,
  286. text
  287. };
  288. } else {
  289. return {
  290. type: 'image',
  291. raw,
  292. href,
  293. title,
  294. text: escape$1(text)
  295. };
  296. }
  297. }
  298. function indentCodeCompensation(raw, text) {
  299. const matchIndentToCode = raw.match(/^(\s+)(?:```)/);
  300. if (matchIndentToCode === null) {
  301. return text;
  302. }
  303. const indentToCode = matchIndentToCode[1];
  304. return text
  305. .split('\n')
  306. .map(node => {
  307. const matchIndentInNode = node.match(/^\s+/);
  308. if (matchIndentInNode === null) {
  309. return node;
  310. }
  311. const [indentInNode] = matchIndentInNode;
  312. if (indentInNode.length >= indentToCode.length) {
  313. return node.slice(indentToCode.length);
  314. }
  315. return node;
  316. })
  317. .join('\n');
  318. }
  319. /**
  320. * Tokenizer
  321. */
  322. var Tokenizer_1 = class Tokenizer {
  323. constructor(options) {
  324. this.options = options || defaults$1;
  325. }
  326. space(src) {
  327. const cap = this.rules.block.newline.exec(src);
  328. if (cap) {
  329. if (cap[0].length > 1) {
  330. return {
  331. type: 'space',
  332. raw: cap[0]
  333. };
  334. }
  335. return { raw: '\n' };
  336. }
  337. }
  338. code(src, tokens) {
  339. const cap = this.rules.block.code.exec(src);
  340. if (cap) {
  341. const lastToken = tokens[tokens.length - 1];
  342. // An indented code block cannot interrupt a paragraph.
  343. if (lastToken && lastToken.type === 'paragraph') {
  344. return {
  345. raw: cap[0],
  346. text: cap[0].trimRight()
  347. };
  348. }
  349. const text = cap[0].replace(/^ {4}/gm, '');
  350. return {
  351. type: 'code',
  352. raw: cap[0],
  353. codeBlockStyle: 'indented',
  354. text: !this.options.pedantic
  355. ? rtrim$1(text, '\n')
  356. : text
  357. };
  358. }
  359. }
  360. fences(src) {
  361. const cap = this.rules.block.fences.exec(src);
  362. if (cap) {
  363. const raw = cap[0];
  364. const text = indentCodeCompensation(raw, cap[3] || '');
  365. return {
  366. type: 'code',
  367. raw,
  368. lang: cap[2] ? cap[2].trim() : cap[2],
  369. text
  370. };
  371. }
  372. }
  373. heading(src) {
  374. const cap = this.rules.block.heading.exec(src);
  375. if (cap) {
  376. return {
  377. type: 'heading',
  378. raw: cap[0],
  379. depth: cap[1].length,
  380. text: cap[2]
  381. };
  382. }
  383. }
  384. nptable(src) {
  385. const cap = this.rules.block.nptable.exec(src);
  386. if (cap) {
  387. const item = {
  388. type: 'table',
  389. header: splitCells$1(cap[1].replace(/^ *| *\| *$/g, '')),
  390. align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
  391. cells: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : [],
  392. raw: cap[0]
  393. };
  394. if (item.header.length === item.align.length) {
  395. let l = item.align.length;
  396. let i;
  397. for (i = 0; i < l; i++) {
  398. if (/^ *-+: *$/.test(item.align[i])) {
  399. item.align[i] = 'right';
  400. } else if (/^ *:-+: *$/.test(item.align[i])) {
  401. item.align[i] = 'center';
  402. } else if (/^ *:-+ *$/.test(item.align[i])) {
  403. item.align[i] = 'left';
  404. } else {
  405. item.align[i] = null;
  406. }
  407. }
  408. l = item.cells.length;
  409. for (i = 0; i < l; i++) {
  410. item.cells[i] = splitCells$1(item.cells[i], item.header.length);
  411. }
  412. return item;
  413. }
  414. }
  415. }
  416. hr(src) {
  417. const cap = this.rules.block.hr.exec(src);
  418. if (cap) {
  419. return {
  420. type: 'hr',
  421. raw: cap[0]
  422. };
  423. }
  424. }
  425. blockquote(src) {
  426. const cap = this.rules.block.blockquote.exec(src);
  427. if (cap) {
  428. const text = cap[0].replace(/^ *> ?/gm, '');
  429. return {
  430. type: 'blockquote',
  431. raw: cap[0],
  432. text
  433. };
  434. }
  435. }
  436. list(src) {
  437. const cap = this.rules.block.list.exec(src);
  438. if (cap) {
  439. let raw = cap[0];
  440. const bull = cap[2];
  441. const isordered = bull.length > 1;
  442. const list = {
  443. type: 'list',
  444. raw,
  445. ordered: isordered,
  446. start: isordered ? +bull : '',
  447. loose: false,
  448. items: []
  449. };
  450. // Get each top-level item.
  451. const itemMatch = cap[0].match(this.rules.block.item);
  452. let next = false,
  453. item,
  454. space,
  455. b,
  456. addBack,
  457. loose,
  458. istask,
  459. ischecked;
  460. const l = itemMatch.length;
  461. for (let i = 0; i < l; i++) {
  462. item = itemMatch[i];
  463. raw = item;
  464. // Remove the list item's bullet
  465. // so it is seen as the next token.
  466. space = item.length;
  467. item = item.replace(/^ *([*+-]|\d+\.) */, '');
  468. // Outdent whatever the
  469. // list item contains. Hacky.
  470. if (~item.indexOf('\n ')) {
  471. space -= item.length;
  472. item = !this.options.pedantic
  473. ? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '')
  474. : item.replace(/^ {1,4}/gm, '');
  475. }
  476. // Determine whether the next list item belongs here.
  477. // Backpedal if it does not belong in this list.
  478. if (i !== l - 1) {
  479. b = this.rules.block.bullet.exec(itemMatch[i + 1])[0];
  480. if (bull.length > 1 ? b.length === 1
  481. : (b.length > 1 || (this.options.smartLists && b !== bull))) {
  482. addBack = itemMatch.slice(i + 1).join('\n');
  483. list.raw = list.raw.substring(0, list.raw.length - addBack.length);
  484. i = l - 1;
  485. }
  486. }
  487. // Determine whether item is loose or not.
  488. // Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
  489. // for discount behavior.
  490. loose = next || /\n\n(?!\s*$)/.test(item);
  491. if (i !== l - 1) {
  492. next = item.charAt(item.length - 1) === '\n';
  493. if (!loose) loose = next;
  494. }
  495. if (loose) {
  496. list.loose = true;
  497. }
  498. // Check for task list items
  499. istask = /^\[[ xX]\] /.test(item);
  500. ischecked = undefined;
  501. if (istask) {
  502. ischecked = item[1] !== ' ';
  503. item = item.replace(/^\[[ xX]\] +/, '');
  504. }
  505. list.items.push({
  506. type: 'list_item',
  507. raw,
  508. task: istask,
  509. checked: ischecked,
  510. loose: loose,
  511. text: item
  512. });
  513. }
  514. return list;
  515. }
  516. }
  517. html(src) {
  518. const cap = this.rules.block.html.exec(src);
  519. if (cap) {
  520. return {
  521. type: this.options.sanitize
  522. ? 'paragraph'
  523. : 'html',
  524. raw: cap[0],
  525. pre: !this.options.sanitizer
  526. && (cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style'),
  527. text: this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape$1(cap[0])) : cap[0]
  528. };
  529. }
  530. }
  531. def(src) {
  532. const cap = this.rules.block.def.exec(src);
  533. if (cap) {
  534. if (cap[3]) cap[3] = cap[3].substring(1, cap[3].length - 1);
  535. const tag = cap[1].toLowerCase().replace(/\s+/g, ' ');
  536. return {
  537. tag,
  538. raw: cap[0],
  539. href: cap[2],
  540. title: cap[3]
  541. };
  542. }
  543. }
  544. table(src) {
  545. const cap = this.rules.block.table.exec(src);
  546. if (cap) {
  547. const item = {
  548. type: 'table',
  549. header: splitCells$1(cap[1].replace(/^ *| *\| *$/g, '')),
  550. align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
  551. cells: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : []
  552. };
  553. if (item.header.length === item.align.length) {
  554. item.raw = cap[0];
  555. let l = item.align.length;
  556. let i;
  557. for (i = 0; i < l; i++) {
  558. if (/^ *-+: *$/.test(item.align[i])) {
  559. item.align[i] = 'right';
  560. } else if (/^ *:-+: *$/.test(item.align[i])) {
  561. item.align[i] = 'center';
  562. } else if (/^ *:-+ *$/.test(item.align[i])) {
  563. item.align[i] = 'left';
  564. } else {
  565. item.align[i] = null;
  566. }
  567. }
  568. l = item.cells.length;
  569. for (i = 0; i < l; i++) {
  570. item.cells[i] = splitCells$1(
  571. item.cells[i].replace(/^ *\| *| *\| *$/g, ''),
  572. item.header.length);
  573. }
  574. return item;
  575. }
  576. }
  577. }
  578. lheading(src) {
  579. const cap = this.rules.block.lheading.exec(src);
  580. if (cap) {
  581. return {
  582. type: 'heading',
  583. raw: cap[0],
  584. depth: cap[2].charAt(0) === '=' ? 1 : 2,
  585. text: cap[1]
  586. };
  587. }
  588. }
  589. paragraph(src) {
  590. const cap = this.rules.block.paragraph.exec(src);
  591. if (cap) {
  592. return {
  593. type: 'paragraph',
  594. raw: cap[0],
  595. text: cap[1].charAt(cap[1].length - 1) === '\n'
  596. ? cap[1].slice(0, -1)
  597. : cap[1]
  598. };
  599. }
  600. }
  601. text(src, tokens) {
  602. const cap = this.rules.block.text.exec(src);
  603. if (cap) {
  604. const lastToken = tokens[tokens.length - 1];
  605. if (lastToken && lastToken.type === 'text') {
  606. return {
  607. raw: cap[0],
  608. text: cap[0]
  609. };
  610. }
  611. return {
  612. type: 'text',
  613. raw: cap[0],
  614. text: cap[0]
  615. };
  616. }
  617. }
  618. escape(src) {
  619. const cap = this.rules.inline.escape.exec(src);
  620. if (cap) {
  621. return {
  622. type: 'escape',
  623. raw: cap[0],
  624. text: escape$1(cap[1])
  625. };
  626. }
  627. }
  628. tag(src, inLink, inRawBlock) {
  629. const cap = this.rules.inline.tag.exec(src);
  630. if (cap) {
  631. if (!inLink && /^<a /i.test(cap[0])) {
  632. inLink = true;
  633. } else if (inLink && /^<\/a>/i.test(cap[0])) {
  634. inLink = false;
  635. }
  636. if (!inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
  637. inRawBlock = true;
  638. } else if (inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
  639. inRawBlock = false;
  640. }
  641. return {
  642. type: this.options.sanitize
  643. ? 'text'
  644. : 'html',
  645. raw: cap[0],
  646. inLink,
  647. inRawBlock,
  648. text: this.options.sanitize
  649. ? (this.options.sanitizer
  650. ? this.options.sanitizer(cap[0])
  651. : escape$1(cap[0]))
  652. : cap[0]
  653. };
  654. }
  655. }
  656. link(src) {
  657. const cap = this.rules.inline.link.exec(src);
  658. if (cap) {
  659. const lastParenIndex = findClosingBracket$1(cap[2], '()');
  660. if (lastParenIndex > -1) {
  661. const start = cap[0].indexOf('!') === 0 ? 5 : 4;
  662. const linkLen = start + cap[1].length + lastParenIndex;
  663. cap[2] = cap[2].substring(0, lastParenIndex);
  664. cap[0] = cap[0].substring(0, linkLen).trim();
  665. cap[3] = '';
  666. }
  667. let href = cap[2];
  668. let title = '';
  669. if (this.options.pedantic) {
  670. const link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href);
  671. if (link) {
  672. href = link[1];
  673. title = link[3];
  674. } else {
  675. title = '';
  676. }
  677. } else {
  678. title = cap[3] ? cap[3].slice(1, -1) : '';
  679. }
  680. href = href.trim().replace(/^<([\s\S]*)>$/, '$1');
  681. const token = outputLink(cap, {
  682. href: href ? href.replace(this.rules.inline._escapes, '$1') : href,
  683. title: title ? title.replace(this.rules.inline._escapes, '$1') : title
  684. }, cap[0]);
  685. return token;
  686. }
  687. }
  688. reflink(src, links) {
  689. let cap;
  690. if ((cap = this.rules.inline.reflink.exec(src))
  691. || (cap = this.rules.inline.nolink.exec(src))) {
  692. let link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
  693. link = links[link.toLowerCase()];
  694. if (!link || !link.href) {
  695. const text = cap[0].charAt(0);
  696. return {
  697. type: 'text',
  698. raw: text,
  699. text
  700. };
  701. }
  702. const token = outputLink(cap, link, cap[0]);
  703. return token;
  704. }
  705. }
  706. strong(src) {
  707. const cap = this.rules.inline.strong.exec(src);
  708. if (cap) {
  709. return {
  710. type: 'strong',
  711. raw: cap[0],
  712. text: cap[4] || cap[3] || cap[2] || cap[1]
  713. };
  714. }
  715. }
  716. em(src) {
  717. const cap = this.rules.inline.em.exec(src);
  718. if (cap) {
  719. return {
  720. type: 'em',
  721. raw: cap[0],
  722. text: cap[6] || cap[5] || cap[4] || cap[3] || cap[2] || cap[1]
  723. };
  724. }
  725. }
  726. codespan(src) {
  727. const cap = this.rules.inline.code.exec(src);
  728. if (cap) {
  729. let text = cap[2].replace(/\n/g, ' ');
  730. const hasNonSpaceChars = /[^ ]/.test(text);
  731. const hasSpaceCharsOnBothEnds = text.startsWith(' ') && text.endsWith(' ');
  732. if (hasNonSpaceChars && hasSpaceCharsOnBothEnds) {
  733. text = text.substring(1, text.length - 1);
  734. }
  735. text = escape$1(text, true);
  736. return {
  737. type: 'codespan',
  738. raw: cap[0],
  739. text
  740. };
  741. }
  742. }
  743. br(src) {
  744. const cap = this.rules.inline.br.exec(src);
  745. if (cap) {
  746. return {
  747. type: 'br',
  748. raw: cap[0]
  749. };
  750. }
  751. }
  752. del(src) {
  753. const cap = this.rules.inline.del.exec(src);
  754. if (cap) {
  755. return {
  756. type: 'del',
  757. raw: cap[0],
  758. text: cap[1]
  759. };
  760. }
  761. }
  762. autolink(src, mangle) {
  763. const cap = this.rules.inline.autolink.exec(src);
  764. if (cap) {
  765. let text, href;
  766. if (cap[2] === '@') {
  767. text = escape$1(this.options.mangle ? mangle(cap[1]) : cap[1]);
  768. href = 'mailto:' + text;
  769. } else {
  770. text = escape$1(cap[1]);
  771. href = text;
  772. }
  773. return {
  774. type: 'link',
  775. raw: cap[0],
  776. text,
  777. href,
  778. tokens: [
  779. {
  780. type: 'text',
  781. raw: text,
  782. text
  783. }
  784. ]
  785. };
  786. }
  787. }
  788. url(src, mangle) {
  789. let cap;
  790. if (cap = this.rules.inline.url.exec(src)) {
  791. let text, href;
  792. if (cap[2] === '@') {
  793. text = escape$1(this.options.mangle ? mangle(cap[0]) : cap[0]);
  794. href = 'mailto:' + text;
  795. } else {
  796. // do extended autolink path validation
  797. let prevCapZero;
  798. do {
  799. prevCapZero = cap[0];
  800. cap[0] = this.rules.inline._backpedal.exec(cap[0])[0];
  801. } while (prevCapZero !== cap[0]);
  802. text = escape$1(cap[0]);
  803. if (cap[1] === 'www.') {
  804. href = 'http://' + text;
  805. } else {
  806. href = text;
  807. }
  808. }
  809. return {
  810. type: 'link',
  811. raw: cap[0],
  812. text,
  813. href,
  814. tokens: [
  815. {
  816. type: 'text',
  817. raw: text,
  818. text
  819. }
  820. ]
  821. };
  822. }
  823. }
  824. inlineText(src, inRawBlock, smartypants) {
  825. const cap = this.rules.inline.text.exec(src);
  826. if (cap) {
  827. let text;
  828. if (inRawBlock) {
  829. text = this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape$1(cap[0])) : cap[0];
  830. } else {
  831. text = escape$1(this.options.smartypants ? smartypants(cap[0]) : cap[0]);
  832. }
  833. return {
  834. type: 'text',
  835. raw: cap[0],
  836. text
  837. };
  838. }
  839. }
  840. };
  841. const {
  842. noopTest: noopTest$1,
  843. edit: edit$1,
  844. merge: merge$1
  845. } = helpers;
  846. /**
  847. * Block-Level Grammar
  848. */
  849. const block = {
  850. newline: /^\n+/,
  851. code: /^( {4}[^\n]+\n*)+/,
  852. fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/,
  853. hr: /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/,
  854. heading: /^ {0,3}(#{1,6}) +([^\n]*?)(?: +#+)? *(?:\n+|$)/,
  855. blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
  856. list: /^( {0,3})(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
  857. html: '^ {0,3}(?:' // optional indentation
  858. + '<(script|pre|style)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)' // (1)
  859. + '|comment[^\\n]*(\\n+|$)' // (2)
  860. + '|<\\?[\\s\\S]*?\\?>\\n*' // (3)
  861. + '|<![A-Z][\\s\\S]*?>\\n*' // (4)
  862. + '|<!\\[CDATA\\[[\\s\\S]*?\\]\\]>\\n*' // (5)
  863. + '|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:\\n{2,}|$)' // (6)
  864. + '|<(?!script|pre|style)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:\\n{2,}|$)' // (7) open tag
  865. + '|</(?!script|pre|style)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:\\n{2,}|$)' // (7) closing tag
  866. + ')',
  867. def: /^ {0,3}\[(label)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)(title))? *(?:\n+|$)/,
  868. nptable: noopTest$1,
  869. table: noopTest$1,
  870. lheading: /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/,
  871. // regex template, placeholders will be replaced according to different paragraph
  872. // interruption rules of commonmark and the original markdown spec:
  873. _paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html)[^\n]+)*)/,
  874. text: /^[^\n]+/
  875. };
  876. block._label = /(?!\s*\])(?:\\[\[\]]|[^\[\]])+/;
  877. block._title = /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/;
  878. block.def = edit$1(block.def)
  879. .replace('label', block._label)
  880. .replace('title', block._title)
  881. .getRegex();
  882. block.bullet = /(?:[*+-]|\d{1,9}\.)/;
  883. block.item = /^( *)(bull) ?[^\n]*(?:\n(?!\1bull ?)[^\n]*)*/;
  884. block.item = edit$1(block.item, 'gm')
  885. .replace(/bull/g, block.bullet)
  886. .getRegex();
  887. block.list = edit$1(block.list)
  888. .replace(/bull/g, block.bullet)
  889. .replace('hr', '\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))')
  890. .replace('def', '\\n+(?=' + block.def.source + ')')
  891. .getRegex();
  892. block._tag = 'address|article|aside|base|basefont|blockquote|body|caption'
  893. + '|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption'
  894. + '|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe'
  895. + '|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option'
  896. + '|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr'
  897. + '|track|ul';
  898. block._comment = /<!--(?!-?>)[\s\S]*?-->/;
  899. block.html = edit$1(block.html, 'i')
  900. .replace('comment', block._comment)
  901. .replace('tag', block._tag)
  902. .replace('attribute', / +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/)
  903. .getRegex();
  904. block.paragraph = edit$1(block._paragraph)
  905. .replace('hr', block.hr)
  906. .replace('heading', ' {0,3}#{1,6} ')
  907. .replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
  908. .replace('blockquote', ' {0,3}>')
  909. .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
  910. .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
  911. .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|!--)')
  912. .replace('tag', block._tag) // pars can be interrupted by type (6) html blocks
  913. .getRegex();
  914. block.blockquote = edit$1(block.blockquote)
  915. .replace('paragraph', block.paragraph)
  916. .getRegex();
  917. /**
  918. * Normal Block Grammar
  919. */
  920. block.normal = merge$1({}, block);
  921. /**
  922. * GFM Block Grammar
  923. */
  924. block.gfm = merge$1({}, block.normal, {
  925. nptable: '^ *([^|\\n ].*\\|.*)\\n' // Header
  926. + ' *([-:]+ *\\|[-| :]*)' // Align
  927. + '(?:\\n((?:(?!\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)', // Cells
  928. table: '^ *\\|(.+)\\n' // Header
  929. + ' *\\|?( *[-:]+[-| :]*)' // Align
  930. + '(?:\\n *((?:(?!\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells
  931. });
  932. block.gfm.nptable = edit$1(block.gfm.nptable)
  933. .replace('hr', block.hr)
  934. .replace('heading', ' {0,3}#{1,6} ')
  935. .replace('blockquote', ' {0,3}>')
  936. .replace('code', ' {4}[^\\n]')
  937. .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
  938. .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
  939. .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|!--)')
  940. .replace('tag', block._tag) // tables can be interrupted by type (6) html blocks
  941. .getRegex();
  942. block.gfm.table = edit$1(block.gfm.table)
  943. .replace('hr', block.hr)
  944. .replace('heading', ' {0,3}#{1,6} ')
  945. .replace('blockquote', ' {0,3}>')
  946. .replace('code', ' {4}[^\\n]')
  947. .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
  948. .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
  949. .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|!--)')
  950. .replace('tag', block._tag) // tables can be interrupted by type (6) html blocks
  951. .getRegex();
  952. /**
  953. * Pedantic grammar (original John Gruber's loose markdown specification)
  954. */
  955. block.pedantic = merge$1({}, block.normal, {
  956. html: edit$1(
  957. '^ *(?:comment *(?:\\n|\\s*$)'
  958. + '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
  959. + '|<tag(?:"[^"]*"|\'[^\']*\'|\\s[^\'"/>\\s]*)*?/?> *(?:\\n{2,}|\\s*$))')
  960. .replace('comment', block._comment)
  961. .replace(/tag/g, '(?!(?:'
  962. + 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub'
  963. + '|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)'
  964. + '\\b)\\w+(?!:|[^\\w\\s@]*@)\\b')
  965. .getRegex(),
  966. def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
  967. heading: /^ *(#{1,6}) *([^\n]+?) *(?:#+ *)?(?:\n+|$)/,
  968. fences: noopTest$1, // fences not supported
  969. paragraph: edit$1(block.normal._paragraph)
  970. .replace('hr', block.hr)
  971. .replace('heading', ' *#{1,6} *[^\n]')
  972. .replace('lheading', block.lheading)
  973. .replace('blockquote', ' {0,3}>')
  974. .replace('|fences', '')
  975. .replace('|list', '')
  976. .replace('|html', '')
  977. .getRegex()
  978. });
  979. /**
  980. * Inline-Level Grammar
  981. */
  982. const inline = {
  983. escape: /^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/,
  984. autolink: /^<(scheme:[^\s\x00-\x1f<>]*|email)>/,
  985. url: noopTest$1,
  986. tag: '^comment'
  987. + '|^</[a-zA-Z][\\w:-]*\\s*>' // self-closing tag
  988. + '|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>' // open tag
  989. + '|^<\\?[\\s\\S]*?\\?>' // processing instruction, e.g. <?php ?>
  990. + '|^<![a-zA-Z]+\\s[\\s\\S]*?>' // declaration, e.g. <!DOCTYPE html>
  991. + '|^<!\\[CDATA\\[[\\s\\S]*?\\]\\]>', // CDATA section
  992. link: /^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/,
  993. reflink: /^!?\[(label)\]\[(?!\s*\])((?:\\[\[\]]?|[^\[\]\\])+)\]/,
  994. nolink: /^!?\[(?!\s*\])((?:\[[^\[\]]*\]|\\[\[\]]|[^\[\]])*)\](?:\[\])?/,
  995. strong: /^__([^\s_])__(?!_)|^\*\*([^\s*])\*\*(?!\*)|^__([^\s][\s\S]*?[^\s])__(?!_)|^\*\*([^\s][\s\S]*?[^\s])\*\*(?!\*)/,
  996. em: /^_([^\s_])_(?!_)|^_([^\s_<][\s\S]*?[^\s_])_(?!_|[^\s,punctuation])|^_([^\s_<][\s\S]*?[^\s])_(?!_|[^\s,punctuation])|^\*([^\s*<\[])\*(?!\*)|^\*([^\s<"][\s\S]*?[^\s\[\*])\*(?![\]`punctuation])|^\*([^\s*"<\[][\s\S]*[^\s])\*(?!\*)/,
  997. code: /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,
  998. br: /^( {2,}|\\)\n(?!\s*$)/,
  999. del: noopTest$1,
  1000. text: /^(`+|[^`])(?:[\s\S]*?(?:(?=[\\<!\[`*]|\b_|$)|[^ ](?= {2,}\n))|(?= {2,}\n))/
  1001. };
  1002. // list of punctuation marks from common mark spec
  1003. // without ` and ] to workaround Rule 17 (inline code blocks/links)
  1004. // without , to work around example 393
  1005. inline._punctuation = '!"#$%&\'()*+\\-./:;<=>?@\\[^_{|}~';
  1006. inline.em = edit$1(inline.em).replace(/punctuation/g, inline._punctuation).getRegex();
  1007. inline._escapes = /\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/g;
  1008. inline._scheme = /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/;
  1009. 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])?)+(?![-_])/;
  1010. inline.autolink = edit$1(inline.autolink)
  1011. .replace('scheme', inline._scheme)
  1012. .replace('email', inline._email)
  1013. .getRegex();
  1014. inline._attribute = /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/;
  1015. inline.tag = edit$1(inline.tag)
  1016. .replace('comment', block._comment)
  1017. .replace('attribute', inline._attribute)
  1018. .getRegex();
  1019. inline._label = /(?:\[(?:\\.|[^\[\]\\])*\]|\\.|`[^`]*`|[^\[\]\\`])*?/;
  1020. inline._href = /<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*/;
  1021. inline._title = /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/;
  1022. inline.link = edit$1(inline.link)
  1023. .replace('label', inline._label)
  1024. .replace('href', inline._href)
  1025. .replace('title', inline._title)
  1026. .getRegex();
  1027. inline.reflink = edit$1(inline.reflink)
  1028. .replace('label', inline._label)
  1029. .getRegex();
  1030. /**
  1031. * Normal Inline Grammar
  1032. */
  1033. inline.normal = merge$1({}, inline);
  1034. /**
  1035. * Pedantic Inline Grammar
  1036. */
  1037. inline.pedantic = merge$1({}, inline.normal, {
  1038. strong: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
  1039. em: /^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/,
  1040. link: edit$1(/^!?\[(label)\]\((.*?)\)/)
  1041. .replace('label', inline._label)
  1042. .getRegex(),
  1043. reflink: edit$1(/^!?\[(label)\]\s*\[([^\]]*)\]/)
  1044. .replace('label', inline._label)
  1045. .getRegex()
  1046. });
  1047. /**
  1048. * GFM Inline Grammar
  1049. */
  1050. inline.gfm = merge$1({}, inline.normal, {
  1051. escape: edit$1(inline.escape).replace('])', '~|])').getRegex(),
  1052. _extended_email: /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/,
  1053. url: /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,
  1054. _backpedal: /(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/,
  1055. del: /^~+(?=\S)([\s\S]*?\S)~+/,
  1056. text: /^(`+|[^`])(?:[\s\S]*?(?:(?=[\\<!\[`*~]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@))|(?= {2,}\n|[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@))/
  1057. });
  1058. inline.gfm.url = edit$1(inline.gfm.url, 'i')
  1059. .replace('email', inline.gfm._extended_email)
  1060. .getRegex();
  1061. /**
  1062. * GFM + Line Breaks Inline Grammar
  1063. */
  1064. inline.breaks = merge$1({}, inline.gfm, {
  1065. br: edit$1(inline.br).replace('{2,}', '*').getRegex(),
  1066. text: edit$1(inline.gfm.text)
  1067. .replace('\\b_', '\\b_| {2,}\\n')
  1068. .replace(/\{2,\}/g, '*')
  1069. .getRegex()
  1070. });
  1071. var rules = {
  1072. block,
  1073. inline
  1074. };
  1075. const { defaults: defaults$2 } = defaults;
  1076. const { block: block$1, inline: inline$1 } = rules;
  1077. /**
  1078. * smartypants text replacement
  1079. */
  1080. function smartypants(text) {
  1081. return text
  1082. // em-dashes
  1083. .replace(/---/g, '\u2014')
  1084. // en-dashes
  1085. .replace(/--/g, '\u2013')
  1086. // opening singles
  1087. .replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018')
  1088. // closing singles & apostrophes
  1089. .replace(/'/g, '\u2019')
  1090. // opening doubles
  1091. .replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c')
  1092. // closing doubles
  1093. .replace(/"/g, '\u201d')
  1094. // ellipses
  1095. .replace(/\.{3}/g, '\u2026');
  1096. }
  1097. /**
  1098. * mangle email addresses
  1099. */
  1100. function mangle(text) {
  1101. let out = '',
  1102. i,
  1103. ch;
  1104. const l = text.length;
  1105. for (i = 0; i < l; i++) {
  1106. ch = text.charCodeAt(i);
  1107. if (Math.random() > 0.5) {
  1108. ch = 'x' + ch.toString(16);
  1109. }
  1110. out += '&#' + ch + ';';
  1111. }
  1112. return out;
  1113. }
  1114. /**
  1115. * Block Lexer
  1116. */
  1117. var Lexer_1 = class Lexer {
  1118. constructor(options) {
  1119. this.tokens = [];
  1120. this.tokens.links = Object.create(null);
  1121. this.options = options || defaults$2;
  1122. this.options.tokenizer = this.options.tokenizer || new Tokenizer_1();
  1123. this.tokenizer = this.options.tokenizer;
  1124. this.tokenizer.options = this.options;
  1125. const rules = {
  1126. block: block$1.normal,
  1127. inline: inline$1.normal
  1128. };
  1129. if (this.options.pedantic) {
  1130. rules.block = block$1.pedantic;
  1131. rules.inline = inline$1.pedantic;
  1132. } else if (this.options.gfm) {
  1133. rules.block = block$1.gfm;
  1134. if (this.options.breaks) {
  1135. rules.inline = inline$1.breaks;
  1136. } else {
  1137. rules.inline = inline$1.gfm;
  1138. }
  1139. }
  1140. this.tokenizer.rules = rules;
  1141. }
  1142. /**
  1143. * Expose Rules
  1144. */
  1145. static get rules() {
  1146. return {
  1147. block: block$1,
  1148. inline: inline$1
  1149. };
  1150. }
  1151. /**
  1152. * Static Lex Method
  1153. */
  1154. static lex(src, options) {
  1155. const lexer = new Lexer(options);
  1156. return lexer.lex(src);
  1157. }
  1158. /**
  1159. * Preprocessing
  1160. */
  1161. lex(src) {
  1162. src = src
  1163. .replace(/\r\n|\r/g, '\n')
  1164. .replace(/\t/g, ' ');
  1165. this.blockTokens(src, this.tokens, true);
  1166. this.inline(this.tokens);
  1167. return this.tokens;
  1168. }
  1169. /**
  1170. * Lexing
  1171. */
  1172. blockTokens(src, tokens = [], top = true) {
  1173. src = src.replace(/^ +$/gm, '');
  1174. let token, i, l, lastToken;
  1175. while (src) {
  1176. // newline
  1177. if (token = this.tokenizer.space(src)) {
  1178. src = src.substring(token.raw.length);
  1179. if (token.type) {
  1180. tokens.push(token);
  1181. }
  1182. continue;
  1183. }
  1184. // code
  1185. if (token = this.tokenizer.code(src, tokens)) {
  1186. src = src.substring(token.raw.length);
  1187. if (token.type) {
  1188. tokens.push(token);
  1189. } else {
  1190. lastToken = tokens[tokens.length - 1];
  1191. lastToken.raw += '\n' + token.raw;
  1192. lastToken.text += '\n' + token.text;
  1193. }
  1194. continue;
  1195. }
  1196. // fences
  1197. if (token = this.tokenizer.fences(src)) {
  1198. src = src.substring(token.raw.length);
  1199. tokens.push(token);
  1200. continue;
  1201. }
  1202. // heading
  1203. if (token = this.tokenizer.heading(src)) {
  1204. src = src.substring(token.raw.length);
  1205. tokens.push(token);
  1206. continue;
  1207. }
  1208. // table no leading pipe (gfm)
  1209. if (token = this.tokenizer.nptable(src)) {
  1210. src = src.substring(token.raw.length);
  1211. tokens.push(token);
  1212. continue;
  1213. }
  1214. // hr
  1215. if (token = this.tokenizer.hr(src)) {
  1216. src = src.substring(token.raw.length);
  1217. tokens.push(token);
  1218. continue;
  1219. }
  1220. // blockquote
  1221. if (token = this.tokenizer.blockquote(src)) {
  1222. src = src.substring(token.raw.length);
  1223. token.tokens = this.blockTokens(token.text, [], top);
  1224. tokens.push(token);
  1225. continue;
  1226. }
  1227. // list
  1228. if (token = this.tokenizer.list(src)) {
  1229. src = src.substring(token.raw.length);
  1230. l = token.items.length;
  1231. for (i = 0; i < l; i++) {
  1232. token.items[i].tokens = this.blockTokens(token.items[i].text, [], false);
  1233. }
  1234. tokens.push(token);
  1235. continue;
  1236. }
  1237. // html
  1238. if (token = this.tokenizer.html(src)) {
  1239. src = src.substring(token.raw.length);
  1240. tokens.push(token);
  1241. continue;
  1242. }
  1243. // def
  1244. if (top && (token = this.tokenizer.def(src))) {
  1245. src = src.substring(token.raw.length);
  1246. if (!this.tokens.links[token.tag]) {
  1247. this.tokens.links[token.tag] = {
  1248. href: token.href,
  1249. title: token.title
  1250. };
  1251. }
  1252. continue;
  1253. }
  1254. // table (gfm)
  1255. if (token = this.tokenizer.table(src)) {
  1256. src = src.substring(token.raw.length);
  1257. tokens.push(token);
  1258. continue;
  1259. }
  1260. // lheading
  1261. if (token = this.tokenizer.lheading(src)) {
  1262. src = src.substring(token.raw.length);
  1263. tokens.push(token);
  1264. continue;
  1265. }
  1266. // top-level paragraph
  1267. if (top && (token = this.tokenizer.paragraph(src))) {
  1268. src = src.substring(token.raw.length);
  1269. tokens.push(token);
  1270. continue;
  1271. }
  1272. // text
  1273. if (token = this.tokenizer.text(src, tokens)) {
  1274. src = src.substring(token.raw.length);
  1275. if (token.type) {
  1276. tokens.push(token);
  1277. } else {
  1278. lastToken = tokens[tokens.length - 1];
  1279. lastToken.raw += '\n' + token.raw;
  1280. lastToken.text += '\n' + token.text;
  1281. }
  1282. continue;
  1283. }
  1284. if (src) {
  1285. const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
  1286. if (this.options.silent) {
  1287. console.error(errMsg);
  1288. break;
  1289. } else {
  1290. throw new Error(errMsg);
  1291. }
  1292. }
  1293. }
  1294. return tokens;
  1295. }
  1296. inline(tokens) {
  1297. let i,
  1298. j,
  1299. k,
  1300. l2,
  1301. row,
  1302. token;
  1303. const l = tokens.length;
  1304. for (i = 0; i < l; i++) {
  1305. token = tokens[i];
  1306. switch (token.type) {
  1307. case 'paragraph':
  1308. case 'text':
  1309. case 'heading': {
  1310. token.tokens = [];
  1311. this.inlineTokens(token.text, token.tokens);
  1312. break;
  1313. }
  1314. case 'table': {
  1315. token.tokens = {
  1316. header: [],
  1317. cells: []
  1318. };
  1319. // header
  1320. l2 = token.header.length;
  1321. for (j = 0; j < l2; j++) {
  1322. token.tokens.header[j] = [];
  1323. this.inlineTokens(token.header[j], token.tokens.header[j]);
  1324. }
  1325. // cells
  1326. l2 = token.cells.length;
  1327. for (j = 0; j < l2; j++) {
  1328. row = token.cells[j];
  1329. token.tokens.cells[j] = [];
  1330. for (k = 0; k < row.length; k++) {
  1331. token.tokens.cells[j][k] = [];
  1332. this.inlineTokens(row[k], token.tokens.cells[j][k]);
  1333. }
  1334. }
  1335. break;
  1336. }
  1337. case 'blockquote': {
  1338. this.inline(token.tokens);
  1339. break;
  1340. }
  1341. case 'list': {
  1342. l2 = token.items.length;
  1343. for (j = 0; j < l2; j++) {
  1344. this.inline(token.items[j].tokens);
  1345. }
  1346. break;
  1347. }
  1348. }
  1349. }
  1350. return tokens;
  1351. }
  1352. /**
  1353. * Lexing/Compiling
  1354. */
  1355. inlineTokens(src, tokens = [], inLink = false, inRawBlock = false) {
  1356. let token;
  1357. while (src) {
  1358. // escape
  1359. if (token = this.tokenizer.escape(src)) {
  1360. src = src.substring(token.raw.length);
  1361. tokens.push(token);
  1362. continue;
  1363. }
  1364. // tag
  1365. if (token = this.tokenizer.tag(src, inLink, inRawBlock)) {
  1366. src = src.substring(token.raw.length);
  1367. inLink = token.inLink;
  1368. inRawBlock = token.inRawBlock;
  1369. tokens.push(token);
  1370. continue;
  1371. }
  1372. // link
  1373. if (token = this.tokenizer.link(src)) {
  1374. src = src.substring(token.raw.length);
  1375. if (token.type === 'link') {
  1376. token.tokens = this.inlineTokens(token.text, [], true, inRawBlock);
  1377. }
  1378. tokens.push(token);
  1379. continue;
  1380. }
  1381. // reflink, nolink
  1382. if (token = this.tokenizer.reflink(src, this.tokens.links)) {
  1383. src = src.substring(token.raw.length);
  1384. if (token.type === 'link') {
  1385. token.tokens = this.inlineTokens(token.text, [], true, inRawBlock);
  1386. }
  1387. tokens.push(token);
  1388. continue;
  1389. }
  1390. // strong
  1391. if (token = this.tokenizer.strong(src)) {
  1392. src = src.substring(token.raw.length);
  1393. token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
  1394. tokens.push(token);
  1395. continue;
  1396. }
  1397. // em
  1398. if (token = this.tokenizer.em(src)) {
  1399. src = src.substring(token.raw.length);
  1400. token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
  1401. tokens.push(token);
  1402. continue;
  1403. }
  1404. // code
  1405. if (token = this.tokenizer.codespan(src)) {
  1406. src = src.substring(token.raw.length);
  1407. tokens.push(token);
  1408. continue;
  1409. }
  1410. // br
  1411. if (token = this.tokenizer.br(src)) {
  1412. src = src.substring(token.raw.length);
  1413. tokens.push(token);
  1414. continue;
  1415. }
  1416. // del (gfm)
  1417. if (token = this.tokenizer.del(src)) {
  1418. src = src.substring(token.raw.length);
  1419. token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
  1420. tokens.push(token);
  1421. continue;
  1422. }
  1423. // autolink
  1424. if (token = this.tokenizer.autolink(src, mangle)) {
  1425. src = src.substring(token.raw.length);
  1426. tokens.push(token);
  1427. continue;
  1428. }
  1429. // url (gfm)
  1430. if (!inLink && (token = this.tokenizer.url(src, mangle))) {
  1431. src = src.substring(token.raw.length);
  1432. tokens.push(token);
  1433. continue;
  1434. }
  1435. // text
  1436. if (token = this.tokenizer.inlineText(src, inRawBlock, smartypants)) {
  1437. src = src.substring(token.raw.length);
  1438. tokens.push(token);
  1439. continue;
  1440. }
  1441. if (src) {
  1442. const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
  1443. if (this.options.silent) {
  1444. console.error(errMsg);
  1445. break;
  1446. } else {
  1447. throw new Error(errMsg);
  1448. }
  1449. }
  1450. }
  1451. return tokens;
  1452. }
  1453. };
  1454. const { defaults: defaults$3 } = defaults;
  1455. const {
  1456. cleanUrl: cleanUrl$1,
  1457. escape: escape$2
  1458. } = helpers;
  1459. /**
  1460. * Renderer
  1461. */
  1462. var Renderer_1 = class Renderer {
  1463. constructor(options) {
  1464. this.options = options || defaults$3;
  1465. }
  1466. code(code, infostring, escaped) {
  1467. const lang = (infostring || '').match(/\S*/)[0];
  1468. if (this.options.highlight) {
  1469. const out = this.options.highlight(code, lang);
  1470. if (out != null && out !== code) {
  1471. escaped = true;
  1472. code = out;
  1473. }
  1474. }
  1475. if (!lang) {
  1476. return '<pre><code>'
  1477. + (escaped ? code : escape$2(code, true))
  1478. + '</code></pre>\n';
  1479. }
  1480. return '<pre><code class="'
  1481. + this.options.langPrefix
  1482. + escape$2(lang, true)
  1483. + '">'
  1484. + (escaped ? code : escape$2(code, true))
  1485. + '</code></pre>\n';
  1486. }
  1487. blockquote(quote) {
  1488. return '<blockquote>\n' + quote + '</blockquote>\n';
  1489. }
  1490. html(html) {
  1491. return html;
  1492. }
  1493. heading(text, level, raw, slugger) {
  1494. if (this.options.headerIds) {
  1495. return '<h'
  1496. + level
  1497. + ' id="'
  1498. + this.options.headerPrefix
  1499. + slugger.slug(raw)
  1500. + '">'
  1501. + text
  1502. + '</h'
  1503. + level
  1504. + '>\n';
  1505. }
  1506. // ignore IDs
  1507. return '<h' + level + '>' + text + '</h' + level + '>\n';
  1508. }
  1509. hr() {
  1510. return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
  1511. }
  1512. list(body, ordered, start) {
  1513. const type = ordered ? 'ol' : 'ul',
  1514. startatt = (ordered && start !== 1) ? (' start="' + start + '"') : '';
  1515. return '<' + type + startatt + '>\n' + body + '</' + type + '>\n';
  1516. }
  1517. listitem(text) {
  1518. return '<li>' + text + '</li>\n';
  1519. }
  1520. checkbox(checked) {
  1521. return '<input '
  1522. + (checked ? 'checked="" ' : '')
  1523. + 'disabled="" type="checkbox"'
  1524. + (this.options.xhtml ? ' /' : '')
  1525. + '> ';
  1526. }
  1527. paragraph(text) {
  1528. return '<p>' + text + '</p>\n';
  1529. }
  1530. table(header, body) {
  1531. if (body) body = '<tbody>' + body + '</tbody>';
  1532. return '<table>\n'
  1533. + '<thead>\n'
  1534. + header
  1535. + '</thead>\n'
  1536. + body
  1537. + '</table>\n';
  1538. }
  1539. tablerow(content) {
  1540. return '<tr>\n' + content + '</tr>\n';
  1541. }
  1542. tablecell(content, flags) {
  1543. const type = flags.header ? 'th' : 'td';
  1544. const tag = flags.align
  1545. ? '<' + type + ' align="' + flags.align + '">'
  1546. : '<' + type + '>';
  1547. return tag + content + '</' + type + '>\n';
  1548. }
  1549. // span level renderer
  1550. strong(text) {
  1551. return '<strong>' + text + '</strong>';
  1552. }
  1553. em(text) {
  1554. return '<em>' + text + '</em>';
  1555. }
  1556. codespan(text) {
  1557. return '<code>' + text + '</code>';
  1558. }
  1559. br() {
  1560. return this.options.xhtml ? '<br/>' : '<br>';
  1561. }
  1562. del(text) {
  1563. return '<del>' + text + '</del>';
  1564. }
  1565. link(href, title, text) {
  1566. href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href);
  1567. if (href === null) {
  1568. return text;
  1569. }
  1570. let out = '<a href="' + escape$2(href) + '"';
  1571. if (title) {
  1572. out += ' title="' + title + '"';
  1573. }
  1574. out += '>' + text + '</a>';
  1575. return out;
  1576. }
  1577. image(href, title, text) {
  1578. href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href);
  1579. if (href === null) {
  1580. return text;
  1581. }
  1582. let out = '<img src="' + href + '" alt="' + text + '"';
  1583. if (title) {
  1584. out += ' title="' + title + '"';
  1585. }
  1586. out += this.options.xhtml ? '/>' : '>';
  1587. return out;
  1588. }
  1589. text(text) {
  1590. return text;
  1591. }
  1592. };
  1593. /**
  1594. * TextRenderer
  1595. * returns only the textual part of the token
  1596. */
  1597. var TextRenderer_1 = class TextRenderer {
  1598. // no need for block level renderers
  1599. strong(text) {
  1600. return text;
  1601. }
  1602. em(text) {
  1603. return text;
  1604. }
  1605. codespan(text) {
  1606. return text;
  1607. }
  1608. del(text) {
  1609. return text;
  1610. }
  1611. html(text) {
  1612. return text;
  1613. }
  1614. text(text) {
  1615. return text;
  1616. }
  1617. link(href, title, text) {
  1618. return '' + text;
  1619. }
  1620. image(href, title, text) {
  1621. return '' + text;
  1622. }
  1623. br() {
  1624. return '';
  1625. }
  1626. };
  1627. /**
  1628. * Slugger generates header id
  1629. */
  1630. var Slugger_1 = class Slugger {
  1631. constructor() {
  1632. this.seen = {};
  1633. }
  1634. /**
  1635. * Convert string to unique id
  1636. */
  1637. slug(value) {
  1638. let slug = value
  1639. .toLowerCase()
  1640. .trim()
  1641. // remove html tags
  1642. .replace(/<[!\/a-z].*?>/ig, '')
  1643. // remove unwanted chars
  1644. .replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '')
  1645. .replace(/\s/g, '-');
  1646. if (this.seen.hasOwnProperty(slug)) {
  1647. const originalSlug = slug;
  1648. do {
  1649. this.seen[originalSlug]++;
  1650. slug = originalSlug + '-' + this.seen[originalSlug];
  1651. } while (this.seen.hasOwnProperty(slug));
  1652. }
  1653. this.seen[slug] = 0;
  1654. return slug;
  1655. }
  1656. };
  1657. const { defaults: defaults$4 } = defaults;
  1658. const {
  1659. unescape: unescape$1
  1660. } = helpers;
  1661. /**
  1662. * Parsing & Compiling
  1663. */
  1664. var Parser_1 = class Parser {
  1665. constructor(options) {
  1666. this.options = options || defaults$4;
  1667. this.options.renderer = this.options.renderer || new Renderer_1();
  1668. this.renderer = this.options.renderer;
  1669. this.renderer.options = this.options;
  1670. this.textRenderer = new TextRenderer_1();
  1671. this.slugger = new Slugger_1();
  1672. }
  1673. /**
  1674. * Static Parse Method
  1675. */
  1676. static parse(tokens, options) {
  1677. const parser = new Parser(options);
  1678. return parser.parse(tokens);
  1679. }
  1680. /**
  1681. * Parse Loop
  1682. */
  1683. parse(tokens, top = true) {
  1684. let out = '',
  1685. i,
  1686. j,
  1687. k,
  1688. l2,
  1689. l3,
  1690. row,
  1691. cell,
  1692. header,
  1693. body,
  1694. token,
  1695. ordered,
  1696. start,
  1697. loose,
  1698. itemBody,
  1699. item,
  1700. checked,
  1701. task,
  1702. checkbox;
  1703. const l = tokens.length;
  1704. for (i = 0; i < l; i++) {
  1705. token = tokens[i];
  1706. switch (token.type) {
  1707. case 'space': {
  1708. continue;
  1709. }
  1710. case 'hr': {
  1711. out += this.renderer.hr();
  1712. continue;
  1713. }
  1714. case 'heading': {
  1715. out += this.renderer.heading(
  1716. this.parseInline(token.tokens),
  1717. token.depth,
  1718. unescape$1(this.parseInline(token.tokens, this.textRenderer)),
  1719. this.slugger);
  1720. continue;
  1721. }
  1722. case 'code': {
  1723. out += this.renderer.code(token.text,
  1724. token.lang,
  1725. token.escaped);
  1726. continue;
  1727. }
  1728. case 'table': {
  1729. header = '';
  1730. // header
  1731. cell = '';
  1732. l2 = token.header.length;
  1733. for (j = 0; j < l2; j++) {
  1734. cell += this.renderer.tablecell(
  1735. this.parseInline(token.tokens.header[j]),
  1736. { header: true, align: token.align[j] }
  1737. );
  1738. }
  1739. header += this.renderer.tablerow(cell);
  1740. body = '';
  1741. l2 = token.cells.length;
  1742. for (j = 0; j < l2; j++) {
  1743. row = token.tokens.cells[j];
  1744. cell = '';
  1745. l3 = row.length;
  1746. for (k = 0; k < l3; k++) {
  1747. cell += this.renderer.tablecell(
  1748. this.parseInline(row[k]),
  1749. { header: false, align: token.align[k] }
  1750. );
  1751. }
  1752. body += this.renderer.tablerow(cell);
  1753. }
  1754. out += this.renderer.table(header, body);
  1755. continue;
  1756. }
  1757. case 'blockquote': {
  1758. body = this.parse(token.tokens);
  1759. out += this.renderer.blockquote(body);
  1760. continue;
  1761. }
  1762. case 'list': {
  1763. ordered = token.ordered;
  1764. start = token.start;
  1765. loose = token.loose;
  1766. l2 = token.items.length;
  1767. body = '';
  1768. for (j = 0; j < l2; j++) {
  1769. item = token.items[j];
  1770. checked = item.checked;
  1771. task = item.task;
  1772. itemBody = '';
  1773. if (item.task) {
  1774. checkbox = this.renderer.checkbox(checked);
  1775. if (loose) {
  1776. if (item.tokens.length > 0 && item.tokens[0].type === 'text') {
  1777. item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
  1778. if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
  1779. item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
  1780. }
  1781. } else {
  1782. item.tokens.unshift({
  1783. type: 'text',
  1784. text: checkbox
  1785. });
  1786. }
  1787. } else {
  1788. itemBody += checkbox;
  1789. }
  1790. }
  1791. itemBody += this.parse(item.tokens, loose);
  1792. body += this.renderer.listitem(itemBody, task, checked);
  1793. }
  1794. out += this.renderer.list(body, ordered, start);
  1795. continue;
  1796. }
  1797. case 'html': {
  1798. // TODO parse inline content if parameter markdown=1
  1799. out += this.renderer.html(token.text);
  1800. continue;
  1801. }
  1802. case 'paragraph': {
  1803. out += this.renderer.paragraph(this.parseInline(token.tokens));
  1804. continue;
  1805. }
  1806. case 'text': {
  1807. body = token.tokens ? this.parseInline(token.tokens) : token.text;
  1808. while (i + 1 < l && tokens[i + 1].type === 'text') {
  1809. token = tokens[++i];
  1810. body += '\n' + (token.tokens ? this.parseInline(token.tokens) : token.text);
  1811. }
  1812. out += top ? this.renderer.paragraph(body) : body;
  1813. continue;
  1814. }
  1815. default: {
  1816. const errMsg = 'Token with "' + token.type + '" type was not found.';
  1817. if (this.options.silent) {
  1818. console.error(errMsg);
  1819. return;
  1820. } else {
  1821. throw new Error(errMsg);
  1822. }
  1823. }
  1824. }
  1825. }
  1826. return out;
  1827. }
  1828. /**
  1829. * Parse Inline Tokens
  1830. */
  1831. parseInline(tokens, renderer) {
  1832. renderer = renderer || this.renderer;
  1833. let out = '',
  1834. i,
  1835. token;
  1836. const l = tokens.length;
  1837. for (i = 0; i < l; i++) {
  1838. token = tokens[i];
  1839. switch (token.type) {
  1840. case 'escape': {
  1841. out += renderer.text(token.text);
  1842. break;
  1843. }
  1844. case 'html': {
  1845. out += renderer.html(token.text);
  1846. break;
  1847. }
  1848. case 'link': {
  1849. out += renderer.link(token.href, token.title, this.parseInline(token.tokens, renderer));
  1850. break;
  1851. }
  1852. case 'image': {
  1853. out += renderer.image(token.href, token.title, token.text);
  1854. break;
  1855. }
  1856. case 'strong': {
  1857. out += renderer.strong(this.parseInline(token.tokens, renderer));
  1858. break;
  1859. }
  1860. case 'em': {
  1861. out += renderer.em(this.parseInline(token.tokens, renderer));
  1862. break;
  1863. }
  1864. case 'codespan': {
  1865. out += renderer.codespan(token.text);
  1866. break;
  1867. }
  1868. case 'br': {
  1869. out += renderer.br();
  1870. break;
  1871. }
  1872. case 'del': {
  1873. out += renderer.del(this.parseInline(token.tokens, renderer));
  1874. break;
  1875. }
  1876. case 'text': {
  1877. out += renderer.text(token.text);
  1878. break;
  1879. }
  1880. default: {
  1881. const errMsg = 'Token with "' + token.type + '" type was not found.';
  1882. if (this.options.silent) {
  1883. console.error(errMsg);
  1884. return;
  1885. } else {
  1886. throw new Error(errMsg);
  1887. }
  1888. }
  1889. }
  1890. }
  1891. return out;
  1892. }
  1893. };
  1894. const {
  1895. merge: merge$2,
  1896. checkSanitizeDeprecation: checkSanitizeDeprecation$1,
  1897. escape: escape$3
  1898. } = helpers;
  1899. const {
  1900. getDefaults,
  1901. changeDefaults,
  1902. defaults: defaults$5
  1903. } = defaults;
  1904. /**
  1905. * Marked
  1906. */
  1907. function marked(src, opt, callback) {
  1908. // throw error in case of non string input
  1909. if (typeof src === 'undefined' || src === null) {
  1910. throw new Error('marked(): input parameter is undefined or null');
  1911. }
  1912. if (typeof src !== 'string') {
  1913. throw new Error('marked(): input parameter is of type '
  1914. + Object.prototype.toString.call(src) + ', string expected');
  1915. }
  1916. if (typeof opt === 'function') {
  1917. callback = opt;
  1918. opt = null;
  1919. }
  1920. opt = merge$2({}, marked.defaults, opt || {});
  1921. checkSanitizeDeprecation$1(opt);
  1922. if (callback) {
  1923. const highlight = opt.highlight;
  1924. let tokens;
  1925. try {
  1926. tokens = Lexer_1.lex(src, opt);
  1927. } catch (e) {
  1928. return callback(e);
  1929. }
  1930. const done = function(err) {
  1931. let out;
  1932. if (!err) {
  1933. try {
  1934. out = Parser_1.parse(tokens, opt);
  1935. } catch (e) {
  1936. err = e;
  1937. }
  1938. }
  1939. opt.highlight = highlight;
  1940. return err
  1941. ? callback(err)
  1942. : callback(null, out);
  1943. };
  1944. if (!highlight || highlight.length < 3) {
  1945. return done();
  1946. }
  1947. delete opt.highlight;
  1948. if (!tokens.length) return done();
  1949. let pending = 0;
  1950. marked.walkTokens(tokens, function(token) {
  1951. if (token.type === 'code') {
  1952. pending++;
  1953. setTimeout(() => {
  1954. highlight(token.text, token.lang, function(err, code) {
  1955. if (err) {
  1956. return done(err);
  1957. }
  1958. if (code != null && code !== token.text) {
  1959. token.text = code;
  1960. token.escaped = true;
  1961. }
  1962. pending--;
  1963. if (pending === 0) {
  1964. done();
  1965. }
  1966. });
  1967. }, 0);
  1968. }
  1969. });
  1970. if (pending === 0) {
  1971. done();
  1972. }
  1973. return;
  1974. }
  1975. try {
  1976. const tokens = Lexer_1.lex(src, opt);
  1977. if (opt.walkTokens) {
  1978. marked.walkTokens(tokens, opt.walkTokens);
  1979. }
  1980. return Parser_1.parse(tokens, opt);
  1981. } catch (e) {
  1982. e.message += '\nPlease report this to https://github.com/markedjs/marked.';
  1983. if (opt.silent) {
  1984. return '<p>An error occurred:</p><pre>'
  1985. + escape$3(e.message + '', true)
  1986. + '</pre>';
  1987. }
  1988. throw e;
  1989. }
  1990. }
  1991. /**
  1992. * Options
  1993. */
  1994. marked.options =
  1995. marked.setOptions = function(opt) {
  1996. merge$2(marked.defaults, opt);
  1997. changeDefaults(marked.defaults);
  1998. return marked;
  1999. };
  2000. marked.getDefaults = getDefaults;
  2001. marked.defaults = defaults$5;
  2002. /**
  2003. * Use Extension
  2004. */
  2005. marked.use = function(extension) {
  2006. const opts = merge$2({}, extension);
  2007. if (extension.renderer) {
  2008. const renderer = marked.defaults.renderer || new Renderer_1();
  2009. for (const prop in extension.renderer) {
  2010. const prevRenderer = renderer[prop];
  2011. renderer[prop] = (...args) => {
  2012. let ret = extension.renderer[prop].apply(renderer, args);
  2013. if (ret === false) {
  2014. ret = prevRenderer.apply(renderer, args);
  2015. }
  2016. return ret;
  2017. };
  2018. }
  2019. opts.renderer = renderer;
  2020. }
  2021. if (extension.tokenizer) {
  2022. const tokenizer = marked.defaults.tokenizer || new Tokenizer_1();
  2023. for (const prop in extension.tokenizer) {
  2024. const prevTokenizer = tokenizer[prop];
  2025. tokenizer[prop] = (...args) => {
  2026. let ret = extension.tokenizer[prop].apply(tokenizer, args);
  2027. if (ret === false) {
  2028. ret = prevTokenizer.apply(tokenizer, args);
  2029. }
  2030. return ret;
  2031. };
  2032. }
  2033. opts.tokenizer = tokenizer;
  2034. }
  2035. if (extension.walkTokens) {
  2036. const walkTokens = marked.defaults.walkTokens;
  2037. opts.walkTokens = (token) => {
  2038. extension.walkTokens(token);
  2039. if (walkTokens) {
  2040. walkTokens(token);
  2041. }
  2042. };
  2043. }
  2044. marked.setOptions(opts);
  2045. };
  2046. /**
  2047. * Run callback for every token
  2048. */
  2049. marked.walkTokens = function(tokens, callback) {
  2050. for (const token of tokens) {
  2051. callback(token);
  2052. switch (token.type) {
  2053. case 'table': {
  2054. for (const cell of token.tokens.header) {
  2055. marked.walkTokens(cell, callback);
  2056. }
  2057. for (const row of token.tokens.cells) {
  2058. for (const cell of row) {
  2059. marked.walkTokens(cell, callback);
  2060. }
  2061. }
  2062. break;
  2063. }
  2064. case 'list': {
  2065. marked.walkTokens(token.items, callback);
  2066. break;
  2067. }
  2068. default: {
  2069. if (token.tokens) {
  2070. marked.walkTokens(token.tokens, callback);
  2071. }
  2072. }
  2073. }
  2074. }
  2075. };
  2076. /**
  2077. * Expose
  2078. */
  2079. marked.Parser = Parser_1;
  2080. marked.parser = Parser_1.parse;
  2081. marked.Renderer = Renderer_1;
  2082. marked.TextRenderer = TextRenderer_1;
  2083. marked.Lexer = Lexer_1;
  2084. marked.lexer = Lexer_1.lex;
  2085. marked.Tokenizer = Tokenizer_1;
  2086. marked.Slugger = Slugger_1;
  2087. marked.parse = marked;
  2088. var marked_1 = marked;
  2089. export default marked_1;