marked.esm.js 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626
  1. /**
  2. * marked - a markdown parser
  3. * Copyright (c) 2011-2021, 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) {
  11. var module = { exports: {} };
  12. return fn(module, module.exports), module.exports;
  13. }
  14. var defaults$5 = createCommonjsModule(function (module) {
  15. function getDefaults() {
  16. return {
  17. baseUrl: null,
  18. breaks: false,
  19. gfm: true,
  20. headerIds: true,
  21. headerPrefix: '',
  22. highlight: null,
  23. langPrefix: 'language-',
  24. mangle: true,
  25. pedantic: false,
  26. renderer: null,
  27. sanitize: false,
  28. sanitizer: null,
  29. silent: false,
  30. smartLists: false,
  31. smartypants: false,
  32. tokenizer: null,
  33. walkTokens: null,
  34. xhtml: false
  35. };
  36. }
  37. function changeDefaults(newDefaults) {
  38. module.exports.defaults = newDefaults;
  39. }
  40. module.exports = {
  41. defaults: getDefaults(),
  42. getDefaults,
  43. changeDefaults
  44. };
  45. });
  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$3(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$1(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$1(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$1(sanitize, base, href) {
  107. if (sanitize) {
  108. let prot;
  109. try {
  110. prot = decodeURIComponent(unescape$1(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$1(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$1 = { exec: function noopTest() {} };
  162. function merge$2(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$1(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$1(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$1(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$1(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. // copied from https://stackoverflow.com/a/5450113/806777
  255. function repeatString$1(pattern, count) {
  256. if (count < 1) {
  257. return '';
  258. }
  259. let result = '';
  260. while (count > 1) {
  261. if (count & 1) {
  262. result += pattern;
  263. }
  264. count >>= 1;
  265. pattern += pattern;
  266. }
  267. return result + pattern;
  268. }
  269. var helpers = {
  270. escape: escape$3,
  271. unescape: unescape$1,
  272. edit: edit$1,
  273. cleanUrl: cleanUrl$1,
  274. resolveUrl,
  275. noopTest: noopTest$1,
  276. merge: merge$2,
  277. splitCells: splitCells$1,
  278. rtrim: rtrim$1,
  279. findClosingBracket: findClosingBracket$1,
  280. checkSanitizeDeprecation: checkSanitizeDeprecation$1,
  281. repeatString: repeatString$1
  282. };
  283. const { defaults: defaults$4 } = defaults$5;
  284. const {
  285. rtrim,
  286. splitCells,
  287. escape: escape$2,
  288. findClosingBracket
  289. } = helpers;
  290. function outputLink(cap, link, raw) {
  291. const href = link.href;
  292. const title = link.title ? escape$2(link.title) : null;
  293. const text = cap[1].replace(/\\([\[\]])/g, '$1');
  294. if (cap[0].charAt(0) !== '!') {
  295. return {
  296. type: 'link',
  297. raw,
  298. href,
  299. title,
  300. text
  301. };
  302. } else {
  303. return {
  304. type: 'image',
  305. raw,
  306. href,
  307. title,
  308. text: escape$2(text)
  309. };
  310. }
  311. }
  312. function indentCodeCompensation(raw, text) {
  313. const matchIndentToCode = raw.match(/^(\s+)(?:```)/);
  314. if (matchIndentToCode === null) {
  315. return text;
  316. }
  317. const indentToCode = matchIndentToCode[1];
  318. return text
  319. .split('\n')
  320. .map(node => {
  321. const matchIndentInNode = node.match(/^\s+/);
  322. if (matchIndentInNode === null) {
  323. return node;
  324. }
  325. const [indentInNode] = matchIndentInNode;
  326. if (indentInNode.length >= indentToCode.length) {
  327. return node.slice(indentToCode.length);
  328. }
  329. return node;
  330. })
  331. .join('\n');
  332. }
  333. /**
  334. * Tokenizer
  335. */
  336. var Tokenizer_1 = class Tokenizer {
  337. constructor(options) {
  338. this.options = options || defaults$4;
  339. }
  340. space(src) {
  341. const cap = this.rules.block.newline.exec(src);
  342. if (cap) {
  343. if (cap[0].length > 1) {
  344. return {
  345. type: 'space',
  346. raw: cap[0]
  347. };
  348. }
  349. return { raw: '\n' };
  350. }
  351. }
  352. code(src) {
  353. const cap = this.rules.block.code.exec(src);
  354. if (cap) {
  355. const text = cap[0].replace(/^ {1,4}/gm, '');
  356. return {
  357. type: 'code',
  358. raw: cap[0],
  359. codeBlockStyle: 'indented',
  360. text: !this.options.pedantic
  361. ? rtrim(text, '\n')
  362. : text
  363. };
  364. }
  365. }
  366. fences(src) {
  367. const cap = this.rules.block.fences.exec(src);
  368. if (cap) {
  369. const raw = cap[0];
  370. const text = indentCodeCompensation(raw, cap[3] || '');
  371. return {
  372. type: 'code',
  373. raw,
  374. lang: cap[2] ? cap[2].trim() : cap[2],
  375. text
  376. };
  377. }
  378. }
  379. heading(src) {
  380. const cap = this.rules.block.heading.exec(src);
  381. if (cap) {
  382. let text = cap[2].trim();
  383. // remove trailing #s
  384. if (/#$/.test(text)) {
  385. const trimmed = rtrim(text, '#');
  386. if (this.options.pedantic) {
  387. text = trimmed.trim();
  388. } else if (!trimmed || / $/.test(trimmed)) {
  389. // CommonMark requires space before trailing #s
  390. text = trimmed.trim();
  391. }
  392. }
  393. return {
  394. type: 'heading',
  395. raw: cap[0],
  396. depth: cap[1].length,
  397. text: text
  398. };
  399. }
  400. }
  401. nptable(src) {
  402. const cap = this.rules.block.nptable.exec(src);
  403. if (cap) {
  404. const item = {
  405. type: 'table',
  406. header: splitCells(cap[1].replace(/^ *| *\| *$/g, '')),
  407. align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
  408. cells: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : [],
  409. raw: cap[0]
  410. };
  411. if (item.header.length === item.align.length) {
  412. let l = item.align.length;
  413. let i;
  414. for (i = 0; i < l; i++) {
  415. if (/^ *-+: *$/.test(item.align[i])) {
  416. item.align[i] = 'right';
  417. } else if (/^ *:-+: *$/.test(item.align[i])) {
  418. item.align[i] = 'center';
  419. } else if (/^ *:-+ *$/.test(item.align[i])) {
  420. item.align[i] = 'left';
  421. } else {
  422. item.align[i] = null;
  423. }
  424. }
  425. l = item.cells.length;
  426. for (i = 0; i < l; i++) {
  427. item.cells[i] = splitCells(item.cells[i], item.header.length);
  428. }
  429. return item;
  430. }
  431. }
  432. }
  433. hr(src) {
  434. const cap = this.rules.block.hr.exec(src);
  435. if (cap) {
  436. return {
  437. type: 'hr',
  438. raw: cap[0]
  439. };
  440. }
  441. }
  442. blockquote(src) {
  443. const cap = this.rules.block.blockquote.exec(src);
  444. if (cap) {
  445. const text = cap[0].replace(/^ *> ?/gm, '');
  446. return {
  447. type: 'blockquote',
  448. raw: cap[0],
  449. text
  450. };
  451. }
  452. }
  453. list(src) {
  454. const cap = this.rules.block.list.exec(src);
  455. if (cap) {
  456. let raw = cap[0];
  457. const bull = cap[2];
  458. const isordered = bull.length > 1;
  459. const list = {
  460. type: 'list',
  461. raw,
  462. ordered: isordered,
  463. start: isordered ? +bull.slice(0, -1) : '',
  464. loose: false,
  465. items: []
  466. };
  467. // Get each top-level item.
  468. const itemMatch = cap[0].match(this.rules.block.item);
  469. let next = false,
  470. item,
  471. space,
  472. bcurr,
  473. bnext,
  474. addBack,
  475. loose,
  476. istask,
  477. ischecked,
  478. endMatch;
  479. let l = itemMatch.length;
  480. bcurr = this.rules.block.listItemStart.exec(itemMatch[0]);
  481. for (let i = 0; i < l; i++) {
  482. item = itemMatch[i];
  483. raw = item;
  484. if (!this.options.pedantic) {
  485. // Determine if current item contains the end of the list
  486. endMatch = item.match(new RegExp('\\n\\s*\\n {0,' + (bcurr[0].length - 1) + '}\\S'));
  487. if (endMatch) {
  488. addBack = item.length - endMatch.index + itemMatch.slice(i + 1).join('\n').length;
  489. list.raw = list.raw.substring(0, list.raw.length - addBack);
  490. item = item.substring(0, endMatch.index);
  491. raw = item;
  492. l = i + 1;
  493. }
  494. }
  495. // Determine whether the next list item belongs here.
  496. // Backpedal if it does not belong in this list.
  497. if (i !== l - 1) {
  498. bnext = this.rules.block.listItemStart.exec(itemMatch[i + 1]);
  499. if (
  500. !this.options.pedantic
  501. ? bnext[1].length >= bcurr[0].length || bnext[1].length > 3
  502. : bnext[1].length > bcurr[1].length
  503. ) {
  504. // nested list or continuation
  505. itemMatch.splice(i, 2, itemMatch[i] + (!this.options.pedantic && bnext[1].length < bcurr[0].length && !itemMatch[i].match(/\n$/) ? '' : '\n') + itemMatch[i + 1]);
  506. i--;
  507. l--;
  508. continue;
  509. } else if (
  510. // different bullet style
  511. !this.options.pedantic || this.options.smartLists
  512. ? bnext[2][bnext[2].length - 1] !== bull[bull.length - 1]
  513. : isordered === (bnext[2].length === 1)
  514. ) {
  515. addBack = itemMatch.slice(i + 1).join('\n').length;
  516. list.raw = list.raw.substring(0, list.raw.length - addBack);
  517. i = l - 1;
  518. }
  519. bcurr = bnext;
  520. }
  521. // Remove the list item's bullet
  522. // so it is seen as the next token.
  523. space = item.length;
  524. item = item.replace(/^ *([*+-]|\d+[.)]) ?/, '');
  525. // Outdent whatever the
  526. // list item contains. Hacky.
  527. if (~item.indexOf('\n ')) {
  528. space -= item.length;
  529. item = !this.options.pedantic
  530. ? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '')
  531. : item.replace(/^ {1,4}/gm, '');
  532. }
  533. // trim item newlines at end
  534. item = rtrim(item, '\n');
  535. if (i !== l - 1) {
  536. raw = raw + '\n';
  537. }
  538. // Determine whether item is loose or not.
  539. // Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
  540. // for discount behavior.
  541. loose = next || /\n\n(?!\s*$)/.test(raw);
  542. if (i !== l - 1) {
  543. next = raw.slice(-2) === '\n\n';
  544. if (!loose) loose = next;
  545. }
  546. if (loose) {
  547. list.loose = true;
  548. }
  549. // Check for task list items
  550. if (this.options.gfm) {
  551. istask = /^\[[ xX]\] /.test(item);
  552. ischecked = undefined;
  553. if (istask) {
  554. ischecked = item[1] !== ' ';
  555. item = item.replace(/^\[[ xX]\] +/, '');
  556. }
  557. }
  558. list.items.push({
  559. type: 'list_item',
  560. raw,
  561. task: istask,
  562. checked: ischecked,
  563. loose: loose,
  564. text: item
  565. });
  566. }
  567. return list;
  568. }
  569. }
  570. html(src) {
  571. const cap = this.rules.block.html.exec(src);
  572. if (cap) {
  573. return {
  574. type: this.options.sanitize
  575. ? 'paragraph'
  576. : 'html',
  577. raw: cap[0],
  578. pre: !this.options.sanitizer
  579. && (cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style'),
  580. text: this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape$2(cap[0])) : cap[0]
  581. };
  582. }
  583. }
  584. def(src) {
  585. const cap = this.rules.block.def.exec(src);
  586. if (cap) {
  587. if (cap[3]) cap[3] = cap[3].substring(1, cap[3].length - 1);
  588. const tag = cap[1].toLowerCase().replace(/\s+/g, ' ');
  589. return {
  590. type: 'def',
  591. tag,
  592. raw: cap[0],
  593. href: cap[2],
  594. title: cap[3]
  595. };
  596. }
  597. }
  598. table(src) {
  599. const cap = this.rules.block.table.exec(src);
  600. if (cap) {
  601. const item = {
  602. type: 'table',
  603. header: splitCells(cap[1].replace(/^ *| *\| *$/g, '')),
  604. align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
  605. cells: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : []
  606. };
  607. if (item.header.length === item.align.length) {
  608. item.raw = cap[0];
  609. let l = item.align.length;
  610. let i;
  611. for (i = 0; i < l; i++) {
  612. if (/^ *-+: *$/.test(item.align[i])) {
  613. item.align[i] = 'right';
  614. } else if (/^ *:-+: *$/.test(item.align[i])) {
  615. item.align[i] = 'center';
  616. } else if (/^ *:-+ *$/.test(item.align[i])) {
  617. item.align[i] = 'left';
  618. } else {
  619. item.align[i] = null;
  620. }
  621. }
  622. l = item.cells.length;
  623. for (i = 0; i < l; i++) {
  624. item.cells[i] = splitCells(
  625. item.cells[i].replace(/^ *\| *| *\| *$/g, ''),
  626. item.header.length);
  627. }
  628. return item;
  629. }
  630. }
  631. }
  632. lheading(src) {
  633. const cap = this.rules.block.lheading.exec(src);
  634. if (cap) {
  635. return {
  636. type: 'heading',
  637. raw: cap[0],
  638. depth: cap[2].charAt(0) === '=' ? 1 : 2,
  639. text: cap[1]
  640. };
  641. }
  642. }
  643. paragraph(src) {
  644. const cap = this.rules.block.paragraph.exec(src);
  645. if (cap) {
  646. return {
  647. type: 'paragraph',
  648. raw: cap[0],
  649. text: cap[1].charAt(cap[1].length - 1) === '\n'
  650. ? cap[1].slice(0, -1)
  651. : cap[1]
  652. };
  653. }
  654. }
  655. text(src) {
  656. const cap = this.rules.block.text.exec(src);
  657. if (cap) {
  658. return {
  659. type: 'text',
  660. raw: cap[0],
  661. text: cap[0]
  662. };
  663. }
  664. }
  665. escape(src) {
  666. const cap = this.rules.inline.escape.exec(src);
  667. if (cap) {
  668. return {
  669. type: 'escape',
  670. raw: cap[0],
  671. text: escape$2(cap[1])
  672. };
  673. }
  674. }
  675. tag(src, inLink, inRawBlock) {
  676. const cap = this.rules.inline.tag.exec(src);
  677. if (cap) {
  678. if (!inLink && /^<a /i.test(cap[0])) {
  679. inLink = true;
  680. } else if (inLink && /^<\/a>/i.test(cap[0])) {
  681. inLink = false;
  682. }
  683. if (!inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
  684. inRawBlock = true;
  685. } else if (inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
  686. inRawBlock = false;
  687. }
  688. return {
  689. type: this.options.sanitize
  690. ? 'text'
  691. : 'html',
  692. raw: cap[0],
  693. inLink,
  694. inRawBlock,
  695. text: this.options.sanitize
  696. ? (this.options.sanitizer
  697. ? this.options.sanitizer(cap[0])
  698. : escape$2(cap[0]))
  699. : cap[0]
  700. };
  701. }
  702. }
  703. link(src) {
  704. const cap = this.rules.inline.link.exec(src);
  705. if (cap) {
  706. const trimmedUrl = cap[2].trim();
  707. if (!this.options.pedantic && /^</.test(trimmedUrl)) {
  708. // commonmark requires matching angle brackets
  709. if (!(/>$/.test(trimmedUrl))) {
  710. return;
  711. }
  712. // ending angle bracket cannot be escaped
  713. const rtrimSlash = rtrim(trimmedUrl.slice(0, -1), '\\');
  714. if ((trimmedUrl.length - rtrimSlash.length) % 2 === 0) {
  715. return;
  716. }
  717. } else {
  718. // find closing parenthesis
  719. const lastParenIndex = findClosingBracket(cap[2], '()');
  720. if (lastParenIndex > -1) {
  721. const start = cap[0].indexOf('!') === 0 ? 5 : 4;
  722. const linkLen = start + cap[1].length + lastParenIndex;
  723. cap[2] = cap[2].substring(0, lastParenIndex);
  724. cap[0] = cap[0].substring(0, linkLen).trim();
  725. cap[3] = '';
  726. }
  727. }
  728. let href = cap[2];
  729. let title = '';
  730. if (this.options.pedantic) {
  731. // split pedantic href and title
  732. const link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href);
  733. if (link) {
  734. href = link[1];
  735. title = link[3];
  736. }
  737. } else {
  738. title = cap[3] ? cap[3].slice(1, -1) : '';
  739. }
  740. href = href.trim();
  741. if (/^</.test(href)) {
  742. if (this.options.pedantic && !(/>$/.test(trimmedUrl))) {
  743. // pedantic allows starting angle bracket without ending angle bracket
  744. href = href.slice(1);
  745. } else {
  746. href = href.slice(1, -1);
  747. }
  748. }
  749. return outputLink(cap, {
  750. href: href ? href.replace(this.rules.inline._escapes, '$1') : href,
  751. title: title ? title.replace(this.rules.inline._escapes, '$1') : title
  752. }, cap[0]);
  753. }
  754. }
  755. reflink(src, links) {
  756. let cap;
  757. if ((cap = this.rules.inline.reflink.exec(src))
  758. || (cap = this.rules.inline.nolink.exec(src))) {
  759. let link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
  760. link = links[link.toLowerCase()];
  761. if (!link || !link.href) {
  762. const text = cap[0].charAt(0);
  763. return {
  764. type: 'text',
  765. raw: text,
  766. text
  767. };
  768. }
  769. return outputLink(cap, link, cap[0]);
  770. }
  771. }
  772. emStrong(src, maskedSrc, prevChar = '') {
  773. let match = this.rules.inline.emStrong.lDelim.exec(src);
  774. if (!match) return;
  775. if (match[3] && prevChar.match(/[\p{L}\p{N}]/u)) return; // _ can't be between two alphanumerics. \p{L}\p{N} includes non-english alphabet/numbers as well
  776. const nextChar = match[1] || match[2] || '';
  777. if (!nextChar || (nextChar && (prevChar === '' || this.rules.inline.punctuation.exec(prevChar)))) {
  778. const lLength = match[0].length - 1;
  779. let rDelim, rLength, delimTotal = lLength, midDelimTotal = 0;
  780. const endReg = match[0][0] === '*' ? this.rules.inline.emStrong.rDelimAst : this.rules.inline.emStrong.rDelimUnd;
  781. endReg.lastIndex = 0;
  782. maskedSrc = maskedSrc.slice(-1 * src.length + lLength); // Bump maskedSrc to same section of string as src (move to lexer?)
  783. while ((match = endReg.exec(maskedSrc)) != null) {
  784. rDelim = match[1] || match[2] || match[3] || match[4] || match[5] || match[6];
  785. if (!rDelim) continue; // matched the first alternative in rules.js (skip the * in __abc*abc__)
  786. rLength = rDelim.length;
  787. if (match[3] || match[4]) { // found another Left Delim
  788. delimTotal += rLength;
  789. continue;
  790. } else if (match[5] || match[6]) { // either Left or Right Delim
  791. if (lLength % 3 && !((lLength + rLength) % 3)) {
  792. midDelimTotal += rLength;
  793. continue; // CommonMark Emphasis Rules 9-10
  794. }
  795. }
  796. delimTotal -= rLength;
  797. if (delimTotal > 0) continue; // Haven't found enough closing delimiters
  798. // If this is the last rDelimiter, remove extra characters. *a*** -> *a*
  799. if (delimTotal + midDelimTotal - rLength <= 0 && !maskedSrc.slice(endReg.lastIndex).match(endReg)) {
  800. rLength = Math.min(rLength, rLength + delimTotal + midDelimTotal);
  801. }
  802. if (Math.min(lLength, rLength) % 2) {
  803. return {
  804. type: 'em',
  805. raw: src.slice(0, lLength + match.index + rLength + 1),
  806. text: src.slice(1, lLength + match.index + rLength)
  807. };
  808. }
  809. if (Math.min(lLength, rLength) % 2 === 0) {
  810. return {
  811. type: 'strong',
  812. raw: src.slice(0, lLength + match.index + rLength + 1),
  813. text: src.slice(2, lLength + match.index + rLength - 1)
  814. };
  815. }
  816. }
  817. }
  818. }
  819. codespan(src) {
  820. const cap = this.rules.inline.code.exec(src);
  821. if (cap) {
  822. let text = cap[2].replace(/\n/g, ' ');
  823. const hasNonSpaceChars = /[^ ]/.test(text);
  824. const hasSpaceCharsOnBothEnds = /^ /.test(text) && / $/.test(text);
  825. if (hasNonSpaceChars && hasSpaceCharsOnBothEnds) {
  826. text = text.substring(1, text.length - 1);
  827. }
  828. text = escape$2(text, true);
  829. return {
  830. type: 'codespan',
  831. raw: cap[0],
  832. text
  833. };
  834. }
  835. }
  836. br(src) {
  837. const cap = this.rules.inline.br.exec(src);
  838. if (cap) {
  839. return {
  840. type: 'br',
  841. raw: cap[0]
  842. };
  843. }
  844. }
  845. del(src) {
  846. const cap = this.rules.inline.del.exec(src);
  847. if (cap) {
  848. return {
  849. type: 'del',
  850. raw: cap[0],
  851. text: cap[2]
  852. };
  853. }
  854. }
  855. autolink(src, mangle) {
  856. const cap = this.rules.inline.autolink.exec(src);
  857. if (cap) {
  858. let text, href;
  859. if (cap[2] === '@') {
  860. text = escape$2(this.options.mangle ? mangle(cap[1]) : cap[1]);
  861. href = 'mailto:' + text;
  862. } else {
  863. text = escape$2(cap[1]);
  864. href = text;
  865. }
  866. return {
  867. type: 'link',
  868. raw: cap[0],
  869. text,
  870. href,
  871. tokens: [
  872. {
  873. type: 'text',
  874. raw: text,
  875. text
  876. }
  877. ]
  878. };
  879. }
  880. }
  881. url(src, mangle) {
  882. let cap;
  883. if (cap = this.rules.inline.url.exec(src)) {
  884. let text, href;
  885. if (cap[2] === '@') {
  886. text = escape$2(this.options.mangle ? mangle(cap[0]) : cap[0]);
  887. href = 'mailto:' + text;
  888. } else {
  889. // do extended autolink path validation
  890. let prevCapZero;
  891. do {
  892. prevCapZero = cap[0];
  893. cap[0] = this.rules.inline._backpedal.exec(cap[0])[0];
  894. } while (prevCapZero !== cap[0]);
  895. text = escape$2(cap[0]);
  896. if (cap[1] === 'www.') {
  897. href = 'http://' + text;
  898. } else {
  899. href = text;
  900. }
  901. }
  902. return {
  903. type: 'link',
  904. raw: cap[0],
  905. text,
  906. href,
  907. tokens: [
  908. {
  909. type: 'text',
  910. raw: text,
  911. text
  912. }
  913. ]
  914. };
  915. }
  916. }
  917. inlineText(src, inRawBlock, smartypants) {
  918. const cap = this.rules.inline.text.exec(src);
  919. if (cap) {
  920. let text;
  921. if (inRawBlock) {
  922. text = this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape$2(cap[0])) : cap[0];
  923. } else {
  924. text = escape$2(this.options.smartypants ? smartypants(cap[0]) : cap[0]);
  925. }
  926. return {
  927. type: 'text',
  928. raw: cap[0],
  929. text
  930. };
  931. }
  932. }
  933. };
  934. const {
  935. noopTest,
  936. edit,
  937. merge: merge$1
  938. } = helpers;
  939. /**
  940. * Block-Level Grammar
  941. */
  942. const block$1 = {
  943. newline: /^(?: *(?:\n|$))+/,
  944. code: /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,
  945. fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/,
  946. hr: /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/,
  947. heading: /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/,
  948. blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
  949. list: /^( {0,3})(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?! {0,3}bull )\n*|\s*$)/,
  950. html: '^ {0,3}(?:' // optional indentation
  951. + '<(script|pre|style)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)' // (1)
  952. + '|comment[^\\n]*(\\n+|$)' // (2)
  953. + '|<\\?[\\s\\S]*?(?:\\?>\\n*|$)' // (3)
  954. + '|<![A-Z][\\s\\S]*?(?:>\\n*|$)' // (4)
  955. + '|<!\\[CDATA\\[[\\s\\S]*?(?:\\]\\]>\\n*|$)' // (5)
  956. + '|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:\\n{2,}|$)' // (6)
  957. + '|<(?!script|pre|style)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:\\n{2,}|$)' // (7) open tag
  958. + '|</(?!script|pre|style)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:\\n{2,}|$)' // (7) closing tag
  959. + ')',
  960. def: /^ {0,3}\[(label)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)(title))? *(?:\n+|$)/,
  961. nptable: noopTest,
  962. table: noopTest,
  963. lheading: /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/,
  964. // regex template, placeholders will be replaced according to different paragraph
  965. // interruption rules of commonmark and the original markdown spec:
  966. _paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html| +\n)[^\n]+)*)/,
  967. text: /^[^\n]+/
  968. };
  969. block$1._label = /(?!\s*\])(?:\\[\[\]]|[^\[\]])+/;
  970. block$1._title = /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/;
  971. block$1.def = edit(block$1.def)
  972. .replace('label', block$1._label)
  973. .replace('title', block$1._title)
  974. .getRegex();
  975. block$1.bullet = /(?:[*+-]|\d{1,9}[.)])/;
  976. block$1.item = /^( *)(bull) ?[^\n]*(?:\n(?! *bull ?)[^\n]*)*/;
  977. block$1.item = edit(block$1.item, 'gm')
  978. .replace(/bull/g, block$1.bullet)
  979. .getRegex();
  980. block$1.listItemStart = edit(/^( *)(bull) */)
  981. .replace('bull', block$1.bullet)
  982. .getRegex();
  983. block$1.list = edit(block$1.list)
  984. .replace(/bull/g, block$1.bullet)
  985. .replace('hr', '\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))')
  986. .replace('def', '\\n+(?=' + block$1.def.source + ')')
  987. .getRegex();
  988. block$1._tag = 'address|article|aside|base|basefont|blockquote|body|caption'
  989. + '|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption'
  990. + '|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe'
  991. + '|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option'
  992. + '|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr'
  993. + '|track|ul';
  994. block$1._comment = /<!--(?!-?>)[\s\S]*?(?:-->|$)/;
  995. block$1.html = edit(block$1.html, 'i')
  996. .replace('comment', block$1._comment)
  997. .replace('tag', block$1._tag)
  998. .replace('attribute', / +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/)
  999. .getRegex();
  1000. block$1.paragraph = edit(block$1._paragraph)
  1001. .replace('hr', block$1.hr)
  1002. .replace('heading', ' {0,3}#{1,6} ')
  1003. .replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
  1004. .replace('blockquote', ' {0,3}>')
  1005. .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
  1006. .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
  1007. .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|!--)')
  1008. .replace('tag', block$1._tag) // pars can be interrupted by type (6) html blocks
  1009. .getRegex();
  1010. block$1.blockquote = edit(block$1.blockquote)
  1011. .replace('paragraph', block$1.paragraph)
  1012. .getRegex();
  1013. /**
  1014. * Normal Block Grammar
  1015. */
  1016. block$1.normal = merge$1({}, block$1);
  1017. /**
  1018. * GFM Block Grammar
  1019. */
  1020. block$1.gfm = merge$1({}, block$1.normal, {
  1021. nptable: '^ *([^|\\n ].*\\|.*)\\n' // Header
  1022. + ' {0,3}([-:]+ *\\|[-| :]*)' // Align
  1023. + '(?:\\n((?:(?!\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)', // Cells
  1024. table: '^ *\\|(.+)\\n' // Header
  1025. + ' {0,3}\\|?( *[-:]+[-| :]*)' // Align
  1026. + '(?:\\n *((?:(?!\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells
  1027. });
  1028. block$1.gfm.nptable = edit(block$1.gfm.nptable)
  1029. .replace('hr', block$1.hr)
  1030. .replace('heading', ' {0,3}#{1,6} ')
  1031. .replace('blockquote', ' {0,3}>')
  1032. .replace('code', ' {4}[^\\n]')
  1033. .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
  1034. .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
  1035. .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|!--)')
  1036. .replace('tag', block$1._tag) // tables can be interrupted by type (6) html blocks
  1037. .getRegex();
  1038. block$1.gfm.table = edit(block$1.gfm.table)
  1039. .replace('hr', block$1.hr)
  1040. .replace('heading', ' {0,3}#{1,6} ')
  1041. .replace('blockquote', ' {0,3}>')
  1042. .replace('code', ' {4}[^\\n]')
  1043. .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
  1044. .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
  1045. .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|!--)')
  1046. .replace('tag', block$1._tag) // tables can be interrupted by type (6) html blocks
  1047. .getRegex();
  1048. /**
  1049. * Pedantic grammar (original John Gruber's loose markdown specification)
  1050. */
  1051. block$1.pedantic = merge$1({}, block$1.normal, {
  1052. html: edit(
  1053. '^ *(?:comment *(?:\\n|\\s*$)'
  1054. + '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
  1055. + '|<tag(?:"[^"]*"|\'[^\']*\'|\\s[^\'"/>\\s]*)*?/?> *(?:\\n{2,}|\\s*$))')
  1056. .replace('comment', block$1._comment)
  1057. .replace(/tag/g, '(?!(?:'
  1058. + 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub'
  1059. + '|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)'
  1060. + '\\b)\\w+(?!:|[^\\w\\s@]*@)\\b')
  1061. .getRegex(),
  1062. def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
  1063. heading: /^(#{1,6})(.*)(?:\n+|$)/,
  1064. fences: noopTest, // fences not supported
  1065. paragraph: edit(block$1.normal._paragraph)
  1066. .replace('hr', block$1.hr)
  1067. .replace('heading', ' *#{1,6} *[^\n]')
  1068. .replace('lheading', block$1.lheading)
  1069. .replace('blockquote', ' {0,3}>')
  1070. .replace('|fences', '')
  1071. .replace('|list', '')
  1072. .replace('|html', '')
  1073. .getRegex()
  1074. });
  1075. /**
  1076. * Inline-Level Grammar
  1077. */
  1078. const inline$1 = {
  1079. escape: /^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/,
  1080. autolink: /^<(scheme:[^\s\x00-\x1f<>]*|email)>/,
  1081. url: noopTest,
  1082. tag: '^comment'
  1083. + '|^</[a-zA-Z][\\w:-]*\\s*>' // self-closing tag
  1084. + '|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>' // open tag
  1085. + '|^<\\?[\\s\\S]*?\\?>' // processing instruction, e.g. <?php ?>
  1086. + '|^<![a-zA-Z]+\\s[\\s\\S]*?>' // declaration, e.g. <!DOCTYPE html>
  1087. + '|^<!\\[CDATA\\[[\\s\\S]*?\\]\\]>', // CDATA section
  1088. link: /^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/,
  1089. reflink: /^!?\[(label)\]\[(?!\s*\])((?:\\[\[\]]?|[^\[\]\\])+)\]/,
  1090. nolink: /^!?\[(?!\s*\])((?:\[[^\[\]]*\]|\\[\[\]]|[^\[\]])*)\](?:\[\])?/,
  1091. reflinkSearch: 'reflink|nolink(?!\\()',
  1092. emStrong: {
  1093. lDelim: /^(?:\*+(?:([punct_])|[^\s*]))|^_+(?:([punct*])|([^\s_]))/,
  1094. // (1) and (2) can only be a Right Delimiter. (3) and (4) can only be Left. (5) and (6) can be either Left or Right.
  1095. // () Skip other delimiter (1) #*** (2) a***#, a*** (3) #***a, ***a (4) ***# (5) #***# (6) a***a
  1096. rDelimAst: /\_\_[^_]*?\*[^_]*?\_\_|[punct_](\*+)(?=[\s]|$)|[^punct*_\s](\*+)(?=[punct_\s]|$)|[punct_\s](\*+)(?=[^punct*_\s])|[\s](\*+)(?=[punct_])|[punct_](\*+)(?=[punct_])|[^punct*_\s](\*+)(?=[^punct*_\s])/,
  1097. rDelimUnd: /\*\*[^*]*?\_[^*]*?\*\*|[punct*](\_+)(?=[\s]|$)|[^punct*_\s](\_+)(?=[punct*\s]|$)|[punct*\s](\_+)(?=[^punct*_\s])|[\s](\_+)(?=[punct*])|[punct*](\_+)(?=[punct*])/ // ^- Not allowed for _
  1098. },
  1099. code: /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,
  1100. br: /^( {2,}|\\)\n(?!\s*$)/,
  1101. del: noopTest,
  1102. text: /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<!\[`*_]|\b_|$)|[^ ](?= {2,}\n)))/,
  1103. punctuation: /^([\spunctuation])/
  1104. };
  1105. // list of punctuation marks from CommonMark spec
  1106. // without * and _ to handle the different emphasis markers * and _
  1107. inline$1._punctuation = '!"#$%&\'()+\\-.,/:;<=>?@\\[\\]`^{|}~';
  1108. inline$1.punctuation = edit(inline$1.punctuation).replace(/punctuation/g, inline$1._punctuation).getRegex();
  1109. // sequences em should skip over [title](link), `code`, <html>
  1110. inline$1.blockSkip = /\[[^\]]*?\]\([^\)]*?\)|`[^`]*?`|<[^>]*?>/g;
  1111. inline$1.escapedEmSt = /\\\*|\\_/g;
  1112. inline$1._comment = edit(block$1._comment).replace('(?:-->|$)', '-->').getRegex();
  1113. inline$1.emStrong.lDelim = edit(inline$1.emStrong.lDelim)
  1114. .replace(/punct/g, inline$1._punctuation)
  1115. .getRegex();
  1116. inline$1.emStrong.rDelimAst = edit(inline$1.emStrong.rDelimAst, 'g')
  1117. .replace(/punct/g, inline$1._punctuation)
  1118. .getRegex();
  1119. inline$1.emStrong.rDelimUnd = edit(inline$1.emStrong.rDelimUnd, 'g')
  1120. .replace(/punct/g, inline$1._punctuation)
  1121. .getRegex();
  1122. inline$1._escapes = /\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/g;
  1123. inline$1._scheme = /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/;
  1124. inline$1._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])?)+(?![-_])/;
  1125. inline$1.autolink = edit(inline$1.autolink)
  1126. .replace('scheme', inline$1._scheme)
  1127. .replace('email', inline$1._email)
  1128. .getRegex();
  1129. inline$1._attribute = /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/;
  1130. inline$1.tag = edit(inline$1.tag)
  1131. .replace('comment', inline$1._comment)
  1132. .replace('attribute', inline$1._attribute)
  1133. .getRegex();
  1134. inline$1._label = /(?:\[(?:\\.|[^\[\]\\])*\]|\\.|`[^`]*`|[^\[\]\\`])*?/;
  1135. inline$1._href = /<(?:\\.|[^\n<>\\])+>|[^\s\x00-\x1f]*/;
  1136. inline$1._title = /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/;
  1137. inline$1.link = edit(inline$1.link)
  1138. .replace('label', inline$1._label)
  1139. .replace('href', inline$1._href)
  1140. .replace('title', inline$1._title)
  1141. .getRegex();
  1142. inline$1.reflink = edit(inline$1.reflink)
  1143. .replace('label', inline$1._label)
  1144. .getRegex();
  1145. inline$1.reflinkSearch = edit(inline$1.reflinkSearch, 'g')
  1146. .replace('reflink', inline$1.reflink)
  1147. .replace('nolink', inline$1.nolink)
  1148. .getRegex();
  1149. /**
  1150. * Normal Inline Grammar
  1151. */
  1152. inline$1.normal = merge$1({}, inline$1);
  1153. /**
  1154. * Pedantic Inline Grammar
  1155. */
  1156. inline$1.pedantic = merge$1({}, inline$1.normal, {
  1157. strong: {
  1158. start: /^__|\*\*/,
  1159. middle: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
  1160. endAst: /\*\*(?!\*)/g,
  1161. endUnd: /__(?!_)/g
  1162. },
  1163. em: {
  1164. start: /^_|\*/,
  1165. middle: /^()\*(?=\S)([\s\S]*?\S)\*(?!\*)|^_(?=\S)([\s\S]*?\S)_(?!_)/,
  1166. endAst: /\*(?!\*)/g,
  1167. endUnd: /_(?!_)/g
  1168. },
  1169. link: edit(/^!?\[(label)\]\((.*?)\)/)
  1170. .replace('label', inline$1._label)
  1171. .getRegex(),
  1172. reflink: edit(/^!?\[(label)\]\s*\[([^\]]*)\]/)
  1173. .replace('label', inline$1._label)
  1174. .getRegex()
  1175. });
  1176. /**
  1177. * GFM Inline Grammar
  1178. */
  1179. inline$1.gfm = merge$1({}, inline$1.normal, {
  1180. escape: edit(inline$1.escape).replace('])', '~|])').getRegex(),
  1181. _extended_email: /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/,
  1182. url: /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,
  1183. _backpedal: /(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/,
  1184. del: /^(~~?)(?=[^\s~])([\s\S]*?[^\s~])\1(?=[^~]|$)/,
  1185. text: /^([`~]+|[^`~])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<!\[`*~_]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@))|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@))/
  1186. });
  1187. inline$1.gfm.url = edit(inline$1.gfm.url, 'i')
  1188. .replace('email', inline$1.gfm._extended_email)
  1189. .getRegex();
  1190. /**
  1191. * GFM + Line Breaks Inline Grammar
  1192. */
  1193. inline$1.breaks = merge$1({}, inline$1.gfm, {
  1194. br: edit(inline$1.br).replace('{2,}', '*').getRegex(),
  1195. text: edit(inline$1.gfm.text)
  1196. .replace('\\b_', '\\b_| {2,}\\n')
  1197. .replace(/\{2,\}/g, '*')
  1198. .getRegex()
  1199. });
  1200. var rules = {
  1201. block: block$1,
  1202. inline: inline$1
  1203. };
  1204. const { defaults: defaults$3 } = defaults$5;
  1205. const { block, inline } = rules;
  1206. const { repeatString } = helpers;
  1207. /**
  1208. * smartypants text replacement
  1209. */
  1210. function smartypants(text) {
  1211. return text
  1212. // em-dashes
  1213. .replace(/---/g, '\u2014')
  1214. // en-dashes
  1215. .replace(/--/g, '\u2013')
  1216. // opening singles
  1217. .replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018')
  1218. // closing singles & apostrophes
  1219. .replace(/'/g, '\u2019')
  1220. // opening doubles
  1221. .replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c')
  1222. // closing doubles
  1223. .replace(/"/g, '\u201d')
  1224. // ellipses
  1225. .replace(/\.{3}/g, '\u2026');
  1226. }
  1227. /**
  1228. * mangle email addresses
  1229. */
  1230. function mangle(text) {
  1231. let out = '',
  1232. i,
  1233. ch;
  1234. const l = text.length;
  1235. for (i = 0; i < l; i++) {
  1236. ch = text.charCodeAt(i);
  1237. if (Math.random() > 0.5) {
  1238. ch = 'x' + ch.toString(16);
  1239. }
  1240. out += '&#' + ch + ';';
  1241. }
  1242. return out;
  1243. }
  1244. /**
  1245. * Block Lexer
  1246. */
  1247. var Lexer_1 = class Lexer {
  1248. constructor(options) {
  1249. this.tokens = [];
  1250. this.tokens.links = Object.create(null);
  1251. this.options = options || defaults$3;
  1252. this.options.tokenizer = this.options.tokenizer || new Tokenizer_1();
  1253. this.tokenizer = this.options.tokenizer;
  1254. this.tokenizer.options = this.options;
  1255. const rules = {
  1256. block: block.normal,
  1257. inline: inline.normal
  1258. };
  1259. if (this.options.pedantic) {
  1260. rules.block = block.pedantic;
  1261. rules.inline = inline.pedantic;
  1262. } else if (this.options.gfm) {
  1263. rules.block = block.gfm;
  1264. if (this.options.breaks) {
  1265. rules.inline = inline.breaks;
  1266. } else {
  1267. rules.inline = inline.gfm;
  1268. }
  1269. }
  1270. this.tokenizer.rules = rules;
  1271. }
  1272. /**
  1273. * Expose Rules
  1274. */
  1275. static get rules() {
  1276. return {
  1277. block,
  1278. inline
  1279. };
  1280. }
  1281. /**
  1282. * Static Lex Method
  1283. */
  1284. static lex(src, options) {
  1285. const lexer = new Lexer(options);
  1286. return lexer.lex(src);
  1287. }
  1288. /**
  1289. * Static Lex Inline Method
  1290. */
  1291. static lexInline(src, options) {
  1292. const lexer = new Lexer(options);
  1293. return lexer.inlineTokens(src);
  1294. }
  1295. /**
  1296. * Preprocessing
  1297. */
  1298. lex(src) {
  1299. src = src
  1300. .replace(/\r\n|\r/g, '\n')
  1301. .replace(/\t/g, ' ');
  1302. this.blockTokens(src, this.tokens, true);
  1303. this.inline(this.tokens);
  1304. return this.tokens;
  1305. }
  1306. /**
  1307. * Lexing
  1308. */
  1309. blockTokens(src, tokens = [], top = true) {
  1310. if (this.options.pedantic) {
  1311. src = src.replace(/^ +$/gm, '');
  1312. }
  1313. let token, i, l, lastToken;
  1314. while (src) {
  1315. // newline
  1316. if (token = this.tokenizer.space(src)) {
  1317. src = src.substring(token.raw.length);
  1318. if (token.type) {
  1319. tokens.push(token);
  1320. }
  1321. continue;
  1322. }
  1323. // code
  1324. if (token = this.tokenizer.code(src)) {
  1325. src = src.substring(token.raw.length);
  1326. lastToken = tokens[tokens.length - 1];
  1327. // An indented code block cannot interrupt a paragraph.
  1328. if (lastToken && lastToken.type === 'paragraph') {
  1329. lastToken.raw += '\n' + token.raw;
  1330. lastToken.text += '\n' + token.text;
  1331. } else {
  1332. tokens.push(token);
  1333. }
  1334. continue;
  1335. }
  1336. // fences
  1337. if (token = this.tokenizer.fences(src)) {
  1338. src = src.substring(token.raw.length);
  1339. tokens.push(token);
  1340. continue;
  1341. }
  1342. // heading
  1343. if (token = this.tokenizer.heading(src)) {
  1344. src = src.substring(token.raw.length);
  1345. tokens.push(token);
  1346. continue;
  1347. }
  1348. // table no leading pipe (gfm)
  1349. if (token = this.tokenizer.nptable(src)) {
  1350. src = src.substring(token.raw.length);
  1351. tokens.push(token);
  1352. continue;
  1353. }
  1354. // hr
  1355. if (token = this.tokenizer.hr(src)) {
  1356. src = src.substring(token.raw.length);
  1357. tokens.push(token);
  1358. continue;
  1359. }
  1360. // blockquote
  1361. if (token = this.tokenizer.blockquote(src)) {
  1362. src = src.substring(token.raw.length);
  1363. token.tokens = this.blockTokens(token.text, [], top);
  1364. tokens.push(token);
  1365. continue;
  1366. }
  1367. // list
  1368. if (token = this.tokenizer.list(src)) {
  1369. src = src.substring(token.raw.length);
  1370. l = token.items.length;
  1371. for (i = 0; i < l; i++) {
  1372. token.items[i].tokens = this.blockTokens(token.items[i].text, [], false);
  1373. }
  1374. tokens.push(token);
  1375. continue;
  1376. }
  1377. // html
  1378. if (token = this.tokenizer.html(src)) {
  1379. src = src.substring(token.raw.length);
  1380. tokens.push(token);
  1381. continue;
  1382. }
  1383. // def
  1384. if (top && (token = this.tokenizer.def(src))) {
  1385. src = src.substring(token.raw.length);
  1386. if (!this.tokens.links[token.tag]) {
  1387. this.tokens.links[token.tag] = {
  1388. href: token.href,
  1389. title: token.title
  1390. };
  1391. }
  1392. continue;
  1393. }
  1394. // table (gfm)
  1395. if (token = this.tokenizer.table(src)) {
  1396. src = src.substring(token.raw.length);
  1397. tokens.push(token);
  1398. continue;
  1399. }
  1400. // lheading
  1401. if (token = this.tokenizer.lheading(src)) {
  1402. src = src.substring(token.raw.length);
  1403. tokens.push(token);
  1404. continue;
  1405. }
  1406. // top-level paragraph
  1407. if (top && (token = this.tokenizer.paragraph(src))) {
  1408. src = src.substring(token.raw.length);
  1409. tokens.push(token);
  1410. continue;
  1411. }
  1412. // text
  1413. if (token = this.tokenizer.text(src)) {
  1414. src = src.substring(token.raw.length);
  1415. lastToken = tokens[tokens.length - 1];
  1416. if (lastToken && lastToken.type === 'text') {
  1417. lastToken.raw += '\n' + token.raw;
  1418. lastToken.text += '\n' + token.text;
  1419. } else {
  1420. tokens.push(token);
  1421. }
  1422. continue;
  1423. }
  1424. if (src) {
  1425. const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
  1426. if (this.options.silent) {
  1427. console.error(errMsg);
  1428. break;
  1429. } else {
  1430. throw new Error(errMsg);
  1431. }
  1432. }
  1433. }
  1434. return tokens;
  1435. }
  1436. inline(tokens) {
  1437. let i,
  1438. j,
  1439. k,
  1440. l2,
  1441. row,
  1442. token;
  1443. const l = tokens.length;
  1444. for (i = 0; i < l; i++) {
  1445. token = tokens[i];
  1446. switch (token.type) {
  1447. case 'paragraph':
  1448. case 'text':
  1449. case 'heading': {
  1450. token.tokens = [];
  1451. this.inlineTokens(token.text, token.tokens);
  1452. break;
  1453. }
  1454. case 'table': {
  1455. token.tokens = {
  1456. header: [],
  1457. cells: []
  1458. };
  1459. // header
  1460. l2 = token.header.length;
  1461. for (j = 0; j < l2; j++) {
  1462. token.tokens.header[j] = [];
  1463. this.inlineTokens(token.header[j], token.tokens.header[j]);
  1464. }
  1465. // cells
  1466. l2 = token.cells.length;
  1467. for (j = 0; j < l2; j++) {
  1468. row = token.cells[j];
  1469. token.tokens.cells[j] = [];
  1470. for (k = 0; k < row.length; k++) {
  1471. token.tokens.cells[j][k] = [];
  1472. this.inlineTokens(row[k], token.tokens.cells[j][k]);
  1473. }
  1474. }
  1475. break;
  1476. }
  1477. case 'blockquote': {
  1478. this.inline(token.tokens);
  1479. break;
  1480. }
  1481. case 'list': {
  1482. l2 = token.items.length;
  1483. for (j = 0; j < l2; j++) {
  1484. this.inline(token.items[j].tokens);
  1485. }
  1486. break;
  1487. }
  1488. }
  1489. }
  1490. return tokens;
  1491. }
  1492. /**
  1493. * Lexing/Compiling
  1494. */
  1495. inlineTokens(src, tokens = [], inLink = false, inRawBlock = false) {
  1496. let token, lastToken;
  1497. // String with links masked to avoid interference with em and strong
  1498. let maskedSrc = src;
  1499. let match;
  1500. let keepPrevChar, prevChar;
  1501. // Mask out reflinks
  1502. if (this.tokens.links) {
  1503. const links = Object.keys(this.tokens.links);
  1504. if (links.length > 0) {
  1505. while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
  1506. if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
  1507. maskedSrc = maskedSrc.slice(0, match.index) + '[' + repeatString('a', match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
  1508. }
  1509. }
  1510. }
  1511. }
  1512. // Mask out other blocks
  1513. while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
  1514. maskedSrc = maskedSrc.slice(0, match.index) + '[' + repeatString('a', match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
  1515. }
  1516. // Mask out escaped em & strong delimiters
  1517. while ((match = this.tokenizer.rules.inline.escapedEmSt.exec(maskedSrc)) != null) {
  1518. maskedSrc = maskedSrc.slice(0, match.index) + '++' + maskedSrc.slice(this.tokenizer.rules.inline.escapedEmSt.lastIndex);
  1519. }
  1520. while (src) {
  1521. if (!keepPrevChar) {
  1522. prevChar = '';
  1523. }
  1524. keepPrevChar = false;
  1525. // escape
  1526. if (token = this.tokenizer.escape(src)) {
  1527. src = src.substring(token.raw.length);
  1528. tokens.push(token);
  1529. continue;
  1530. }
  1531. // tag
  1532. if (token = this.tokenizer.tag(src, inLink, inRawBlock)) {
  1533. src = src.substring(token.raw.length);
  1534. inLink = token.inLink;
  1535. inRawBlock = token.inRawBlock;
  1536. const lastToken = tokens[tokens.length - 1];
  1537. if (lastToken && token.type === 'text' && lastToken.type === 'text') {
  1538. lastToken.raw += token.raw;
  1539. lastToken.text += token.text;
  1540. } else {
  1541. tokens.push(token);
  1542. }
  1543. continue;
  1544. }
  1545. // link
  1546. if (token = this.tokenizer.link(src)) {
  1547. src = src.substring(token.raw.length);
  1548. if (token.type === 'link') {
  1549. token.tokens = this.inlineTokens(token.text, [], true, inRawBlock);
  1550. }
  1551. tokens.push(token);
  1552. continue;
  1553. }
  1554. // reflink, nolink
  1555. if (token = this.tokenizer.reflink(src, this.tokens.links)) {
  1556. src = src.substring(token.raw.length);
  1557. const lastToken = tokens[tokens.length - 1];
  1558. if (token.type === 'link') {
  1559. token.tokens = this.inlineTokens(token.text, [], true, inRawBlock);
  1560. tokens.push(token);
  1561. } else if (lastToken && token.type === 'text' && lastToken.type === 'text') {
  1562. lastToken.raw += token.raw;
  1563. lastToken.text += token.text;
  1564. } else {
  1565. tokens.push(token);
  1566. }
  1567. continue;
  1568. }
  1569. // em & strong
  1570. if (token = this.tokenizer.emStrong(src, maskedSrc, prevChar)) {
  1571. src = src.substring(token.raw.length);
  1572. token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
  1573. tokens.push(token);
  1574. continue;
  1575. }
  1576. // code
  1577. if (token = this.tokenizer.codespan(src)) {
  1578. src = src.substring(token.raw.length);
  1579. tokens.push(token);
  1580. continue;
  1581. }
  1582. // br
  1583. if (token = this.tokenizer.br(src)) {
  1584. src = src.substring(token.raw.length);
  1585. tokens.push(token);
  1586. continue;
  1587. }
  1588. // del (gfm)
  1589. if (token = this.tokenizer.del(src)) {
  1590. src = src.substring(token.raw.length);
  1591. token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
  1592. tokens.push(token);
  1593. continue;
  1594. }
  1595. // autolink
  1596. if (token = this.tokenizer.autolink(src, mangle)) {
  1597. src = src.substring(token.raw.length);
  1598. tokens.push(token);
  1599. continue;
  1600. }
  1601. // url (gfm)
  1602. if (!inLink && (token = this.tokenizer.url(src, mangle))) {
  1603. src = src.substring(token.raw.length);
  1604. tokens.push(token);
  1605. continue;
  1606. }
  1607. // text
  1608. if (token = this.tokenizer.inlineText(src, inRawBlock, smartypants)) {
  1609. src = src.substring(token.raw.length);
  1610. if (token.raw.slice(-1) !== '_') { // Track prevChar before string of ____ started
  1611. prevChar = token.raw.slice(-1);
  1612. }
  1613. keepPrevChar = true;
  1614. lastToken = tokens[tokens.length - 1];
  1615. if (lastToken && lastToken.type === 'text') {
  1616. lastToken.raw += token.raw;
  1617. lastToken.text += token.text;
  1618. } else {
  1619. tokens.push(token);
  1620. }
  1621. continue;
  1622. }
  1623. if (src) {
  1624. const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
  1625. if (this.options.silent) {
  1626. console.error(errMsg);
  1627. break;
  1628. } else {
  1629. throw new Error(errMsg);
  1630. }
  1631. }
  1632. }
  1633. return tokens;
  1634. }
  1635. };
  1636. const { defaults: defaults$2 } = defaults$5;
  1637. const {
  1638. cleanUrl,
  1639. escape: escape$1
  1640. } = helpers;
  1641. /**
  1642. * Renderer
  1643. */
  1644. var Renderer_1 = class Renderer {
  1645. constructor(options) {
  1646. this.options = options || defaults$2;
  1647. }
  1648. code(code, infostring, escaped) {
  1649. const lang = (infostring || '').match(/\S*/)[0];
  1650. if (this.options.highlight) {
  1651. const out = this.options.highlight(code, lang);
  1652. if (out != null && out !== code) {
  1653. escaped = true;
  1654. code = out;
  1655. }
  1656. }
  1657. code = code.replace(/\n$/, '') + '\n';
  1658. if (!lang) {
  1659. return '<pre><code>'
  1660. + (escaped ? code : escape$1(code, true))
  1661. + '</code></pre>\n';
  1662. }
  1663. return '<pre><code class="'
  1664. + this.options.langPrefix
  1665. + escape$1(lang, true)
  1666. + '">'
  1667. + (escaped ? code : escape$1(code, true))
  1668. + '</code></pre>\n';
  1669. }
  1670. blockquote(quote) {
  1671. return '<blockquote>\n' + quote + '</blockquote>\n';
  1672. }
  1673. html(html) {
  1674. return html;
  1675. }
  1676. heading(text, level, raw, slugger) {
  1677. if (this.options.headerIds) {
  1678. return '<h'
  1679. + level
  1680. + ' id="'
  1681. + this.options.headerPrefix
  1682. + slugger.slug(raw)
  1683. + '">'
  1684. + text
  1685. + '</h'
  1686. + level
  1687. + '>\n';
  1688. }
  1689. // ignore IDs
  1690. return '<h' + level + '>' + text + '</h' + level + '>\n';
  1691. }
  1692. hr() {
  1693. return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
  1694. }
  1695. list(body, ordered, start) {
  1696. const type = ordered ? 'ol' : 'ul',
  1697. startatt = (ordered && start !== 1) ? (' start="' + start + '"') : '';
  1698. return '<' + type + startatt + '>\n' + body + '</' + type + '>\n';
  1699. }
  1700. listitem(text) {
  1701. return '<li>' + text + '</li>\n';
  1702. }
  1703. checkbox(checked) {
  1704. return '<input '
  1705. + (checked ? 'checked="" ' : '')
  1706. + 'disabled="" type="checkbox"'
  1707. + (this.options.xhtml ? ' /' : '')
  1708. + '> ';
  1709. }
  1710. paragraph(text) {
  1711. return '<p>' + text + '</p>\n';
  1712. }
  1713. table(header, body) {
  1714. if (body) body = '<tbody>' + body + '</tbody>';
  1715. return '<table>\n'
  1716. + '<thead>\n'
  1717. + header
  1718. + '</thead>\n'
  1719. + body
  1720. + '</table>\n';
  1721. }
  1722. tablerow(content) {
  1723. return '<tr>\n' + content + '</tr>\n';
  1724. }
  1725. tablecell(content, flags) {
  1726. const type = flags.header ? 'th' : 'td';
  1727. const tag = flags.align
  1728. ? '<' + type + ' align="' + flags.align + '">'
  1729. : '<' + type + '>';
  1730. return tag + content + '</' + type + '>\n';
  1731. }
  1732. // span level renderer
  1733. strong(text) {
  1734. return '<strong>' + text + '</strong>';
  1735. }
  1736. em(text) {
  1737. return '<em>' + text + '</em>';
  1738. }
  1739. codespan(text) {
  1740. return '<code>' + text + '</code>';
  1741. }
  1742. br() {
  1743. return this.options.xhtml ? '<br/>' : '<br>';
  1744. }
  1745. del(text) {
  1746. return '<del>' + text + '</del>';
  1747. }
  1748. link(href, title, text) {
  1749. href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
  1750. if (href === null) {
  1751. return text;
  1752. }
  1753. let out = '<a href="' + escape$1(href) + '"';
  1754. if (title) {
  1755. out += ' title="' + title + '"';
  1756. }
  1757. out += '>' + text + '</a>';
  1758. return out;
  1759. }
  1760. image(href, title, text) {
  1761. href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
  1762. if (href === null) {
  1763. return text;
  1764. }
  1765. let out = '<img src="' + href + '" alt="' + text + '"';
  1766. if (title) {
  1767. out += ' title="' + title + '"';
  1768. }
  1769. out += this.options.xhtml ? '/>' : '>';
  1770. return out;
  1771. }
  1772. text(text) {
  1773. return text;
  1774. }
  1775. };
  1776. /**
  1777. * TextRenderer
  1778. * returns only the textual part of the token
  1779. */
  1780. var TextRenderer_1 = class TextRenderer {
  1781. // no need for block level renderers
  1782. strong(text) {
  1783. return text;
  1784. }
  1785. em(text) {
  1786. return text;
  1787. }
  1788. codespan(text) {
  1789. return text;
  1790. }
  1791. del(text) {
  1792. return text;
  1793. }
  1794. html(text) {
  1795. return text;
  1796. }
  1797. text(text) {
  1798. return text;
  1799. }
  1800. link(href, title, text) {
  1801. return '' + text;
  1802. }
  1803. image(href, title, text) {
  1804. return '' + text;
  1805. }
  1806. br() {
  1807. return '';
  1808. }
  1809. };
  1810. /**
  1811. * Slugger generates header id
  1812. */
  1813. var Slugger_1 = class Slugger {
  1814. constructor() {
  1815. this.seen = {};
  1816. }
  1817. serialize(value) {
  1818. return value
  1819. .toLowerCase()
  1820. .trim()
  1821. // remove html tags
  1822. .replace(/<[!\/a-z].*?>/ig, '')
  1823. // remove unwanted chars
  1824. .replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '')
  1825. .replace(/\s/g, '-');
  1826. }
  1827. /**
  1828. * Finds the next safe (unique) slug to use
  1829. */
  1830. getNextSafeSlug(originalSlug, isDryRun) {
  1831. let slug = originalSlug;
  1832. let occurenceAccumulator = 0;
  1833. if (this.seen.hasOwnProperty(slug)) {
  1834. occurenceAccumulator = this.seen[originalSlug];
  1835. do {
  1836. occurenceAccumulator++;
  1837. slug = originalSlug + '-' + occurenceAccumulator;
  1838. } while (this.seen.hasOwnProperty(slug));
  1839. }
  1840. if (!isDryRun) {
  1841. this.seen[originalSlug] = occurenceAccumulator;
  1842. this.seen[slug] = 0;
  1843. }
  1844. return slug;
  1845. }
  1846. /**
  1847. * Convert string to unique id
  1848. * @param {object} options
  1849. * @param {boolean} options.dryrun Generates the next unique slug without updating the internal accumulator.
  1850. */
  1851. slug(value, options = {}) {
  1852. const slug = this.serialize(value);
  1853. return this.getNextSafeSlug(slug, options.dryrun);
  1854. }
  1855. };
  1856. const { defaults: defaults$1 } = defaults$5;
  1857. const {
  1858. unescape
  1859. } = helpers;
  1860. /**
  1861. * Parsing & Compiling
  1862. */
  1863. var Parser_1 = class Parser {
  1864. constructor(options) {
  1865. this.options = options || defaults$1;
  1866. this.options.renderer = this.options.renderer || new Renderer_1();
  1867. this.renderer = this.options.renderer;
  1868. this.renderer.options = this.options;
  1869. this.textRenderer = new TextRenderer_1();
  1870. this.slugger = new Slugger_1();
  1871. }
  1872. /**
  1873. * Static Parse Method
  1874. */
  1875. static parse(tokens, options) {
  1876. const parser = new Parser(options);
  1877. return parser.parse(tokens);
  1878. }
  1879. /**
  1880. * Static Parse Inline Method
  1881. */
  1882. static parseInline(tokens, options) {
  1883. const parser = new Parser(options);
  1884. return parser.parseInline(tokens);
  1885. }
  1886. /**
  1887. * Parse Loop
  1888. */
  1889. parse(tokens, top = true) {
  1890. let out = '',
  1891. i,
  1892. j,
  1893. k,
  1894. l2,
  1895. l3,
  1896. row,
  1897. cell,
  1898. header,
  1899. body,
  1900. token,
  1901. ordered,
  1902. start,
  1903. loose,
  1904. itemBody,
  1905. item,
  1906. checked,
  1907. task,
  1908. checkbox;
  1909. const l = tokens.length;
  1910. for (i = 0; i < l; i++) {
  1911. token = tokens[i];
  1912. switch (token.type) {
  1913. case 'space': {
  1914. continue;
  1915. }
  1916. case 'hr': {
  1917. out += this.renderer.hr();
  1918. continue;
  1919. }
  1920. case 'heading': {
  1921. out += this.renderer.heading(
  1922. this.parseInline(token.tokens),
  1923. token.depth,
  1924. unescape(this.parseInline(token.tokens, this.textRenderer)),
  1925. this.slugger);
  1926. continue;
  1927. }
  1928. case 'code': {
  1929. out += this.renderer.code(token.text,
  1930. token.lang,
  1931. token.escaped);
  1932. continue;
  1933. }
  1934. case 'table': {
  1935. header = '';
  1936. // header
  1937. cell = '';
  1938. l2 = token.header.length;
  1939. for (j = 0; j < l2; j++) {
  1940. cell += this.renderer.tablecell(
  1941. this.parseInline(token.tokens.header[j]),
  1942. { header: true, align: token.align[j] }
  1943. );
  1944. }
  1945. header += this.renderer.tablerow(cell);
  1946. body = '';
  1947. l2 = token.cells.length;
  1948. for (j = 0; j < l2; j++) {
  1949. row = token.tokens.cells[j];
  1950. cell = '';
  1951. l3 = row.length;
  1952. for (k = 0; k < l3; k++) {
  1953. cell += this.renderer.tablecell(
  1954. this.parseInline(row[k]),
  1955. { header: false, align: token.align[k] }
  1956. );
  1957. }
  1958. body += this.renderer.tablerow(cell);
  1959. }
  1960. out += this.renderer.table(header, body);
  1961. continue;
  1962. }
  1963. case 'blockquote': {
  1964. body = this.parse(token.tokens);
  1965. out += this.renderer.blockquote(body);
  1966. continue;
  1967. }
  1968. case 'list': {
  1969. ordered = token.ordered;
  1970. start = token.start;
  1971. loose = token.loose;
  1972. l2 = token.items.length;
  1973. body = '';
  1974. for (j = 0; j < l2; j++) {
  1975. item = token.items[j];
  1976. checked = item.checked;
  1977. task = item.task;
  1978. itemBody = '';
  1979. if (item.task) {
  1980. checkbox = this.renderer.checkbox(checked);
  1981. if (loose) {
  1982. if (item.tokens.length > 0 && item.tokens[0].type === 'text') {
  1983. item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
  1984. if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
  1985. item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
  1986. }
  1987. } else {
  1988. item.tokens.unshift({
  1989. type: 'text',
  1990. text: checkbox
  1991. });
  1992. }
  1993. } else {
  1994. itemBody += checkbox;
  1995. }
  1996. }
  1997. itemBody += this.parse(item.tokens, loose);
  1998. body += this.renderer.listitem(itemBody, task, checked);
  1999. }
  2000. out += this.renderer.list(body, ordered, start);
  2001. continue;
  2002. }
  2003. case 'html': {
  2004. // TODO parse inline content if parameter markdown=1
  2005. out += this.renderer.html(token.text);
  2006. continue;
  2007. }
  2008. case 'paragraph': {
  2009. out += this.renderer.paragraph(this.parseInline(token.tokens));
  2010. continue;
  2011. }
  2012. case 'text': {
  2013. body = token.tokens ? this.parseInline(token.tokens) : token.text;
  2014. while (i + 1 < l && tokens[i + 1].type === 'text') {
  2015. token = tokens[++i];
  2016. body += '\n' + (token.tokens ? this.parseInline(token.tokens) : token.text);
  2017. }
  2018. out += top ? this.renderer.paragraph(body) : body;
  2019. continue;
  2020. }
  2021. default: {
  2022. const errMsg = 'Token with "' + token.type + '" type was not found.';
  2023. if (this.options.silent) {
  2024. console.error(errMsg);
  2025. return;
  2026. } else {
  2027. throw new Error(errMsg);
  2028. }
  2029. }
  2030. }
  2031. }
  2032. return out;
  2033. }
  2034. /**
  2035. * Parse Inline Tokens
  2036. */
  2037. parseInline(tokens, renderer) {
  2038. renderer = renderer || this.renderer;
  2039. let out = '',
  2040. i,
  2041. token;
  2042. const l = tokens.length;
  2043. for (i = 0; i < l; i++) {
  2044. token = tokens[i];
  2045. switch (token.type) {
  2046. case 'escape': {
  2047. out += renderer.text(token.text);
  2048. break;
  2049. }
  2050. case 'html': {
  2051. out += renderer.html(token.text);
  2052. break;
  2053. }
  2054. case 'link': {
  2055. out += renderer.link(token.href, token.title, this.parseInline(token.tokens, renderer));
  2056. break;
  2057. }
  2058. case 'image': {
  2059. out += renderer.image(token.href, token.title, token.text);
  2060. break;
  2061. }
  2062. case 'strong': {
  2063. out += renderer.strong(this.parseInline(token.tokens, renderer));
  2064. break;
  2065. }
  2066. case 'em': {
  2067. out += renderer.em(this.parseInline(token.tokens, renderer));
  2068. break;
  2069. }
  2070. case 'codespan': {
  2071. out += renderer.codespan(token.text);
  2072. break;
  2073. }
  2074. case 'br': {
  2075. out += renderer.br();
  2076. break;
  2077. }
  2078. case 'del': {
  2079. out += renderer.del(this.parseInline(token.tokens, renderer));
  2080. break;
  2081. }
  2082. case 'text': {
  2083. out += renderer.text(token.text);
  2084. break;
  2085. }
  2086. default: {
  2087. const errMsg = 'Token with "' + token.type + '" type was not found.';
  2088. if (this.options.silent) {
  2089. console.error(errMsg);
  2090. return;
  2091. } else {
  2092. throw new Error(errMsg);
  2093. }
  2094. }
  2095. }
  2096. }
  2097. return out;
  2098. }
  2099. };
  2100. const {
  2101. merge,
  2102. checkSanitizeDeprecation,
  2103. escape
  2104. } = helpers;
  2105. const {
  2106. getDefaults,
  2107. changeDefaults,
  2108. defaults
  2109. } = defaults$5;
  2110. /**
  2111. * Marked
  2112. */
  2113. function marked(src, opt, callback) {
  2114. // throw error in case of non string input
  2115. if (typeof src === 'undefined' || src === null) {
  2116. throw new Error('marked(): input parameter is undefined or null');
  2117. }
  2118. if (typeof src !== 'string') {
  2119. throw new Error('marked(): input parameter is of type '
  2120. + Object.prototype.toString.call(src) + ', string expected');
  2121. }
  2122. if (typeof opt === 'function') {
  2123. callback = opt;
  2124. opt = null;
  2125. }
  2126. opt = merge({}, marked.defaults, opt || {});
  2127. checkSanitizeDeprecation(opt);
  2128. if (callback) {
  2129. const highlight = opt.highlight;
  2130. let tokens;
  2131. try {
  2132. tokens = Lexer_1.lex(src, opt);
  2133. } catch (e) {
  2134. return callback(e);
  2135. }
  2136. const done = function(err) {
  2137. let out;
  2138. if (!err) {
  2139. try {
  2140. out = Parser_1.parse(tokens, opt);
  2141. } catch (e) {
  2142. err = e;
  2143. }
  2144. }
  2145. opt.highlight = highlight;
  2146. return err
  2147. ? callback(err)
  2148. : callback(null, out);
  2149. };
  2150. if (!highlight || highlight.length < 3) {
  2151. return done();
  2152. }
  2153. delete opt.highlight;
  2154. if (!tokens.length) return done();
  2155. let pending = 0;
  2156. marked.walkTokens(tokens, function(token) {
  2157. if (token.type === 'code') {
  2158. pending++;
  2159. setTimeout(() => {
  2160. highlight(token.text, token.lang, function(err, code) {
  2161. if (err) {
  2162. return done(err);
  2163. }
  2164. if (code != null && code !== token.text) {
  2165. token.text = code;
  2166. token.escaped = true;
  2167. }
  2168. pending--;
  2169. if (pending === 0) {
  2170. done();
  2171. }
  2172. });
  2173. }, 0);
  2174. }
  2175. });
  2176. if (pending === 0) {
  2177. done();
  2178. }
  2179. return;
  2180. }
  2181. try {
  2182. const tokens = Lexer_1.lex(src, opt);
  2183. if (opt.walkTokens) {
  2184. marked.walkTokens(tokens, opt.walkTokens);
  2185. }
  2186. return Parser_1.parse(tokens, opt);
  2187. } catch (e) {
  2188. e.message += '\nPlease report this to https://github.com/markedjs/marked.';
  2189. if (opt.silent) {
  2190. return '<p>An error occurred:</p><pre>'
  2191. + escape(e.message + '', true)
  2192. + '</pre>';
  2193. }
  2194. throw e;
  2195. }
  2196. }
  2197. /**
  2198. * Options
  2199. */
  2200. marked.options =
  2201. marked.setOptions = function(opt) {
  2202. merge(marked.defaults, opt);
  2203. changeDefaults(marked.defaults);
  2204. return marked;
  2205. };
  2206. marked.getDefaults = getDefaults;
  2207. marked.defaults = defaults;
  2208. /**
  2209. * Use Extension
  2210. */
  2211. marked.use = function(extension) {
  2212. const opts = merge({}, extension);
  2213. if (extension.renderer) {
  2214. const renderer = marked.defaults.renderer || new Renderer_1();
  2215. for (const prop in extension.renderer) {
  2216. const prevRenderer = renderer[prop];
  2217. renderer[prop] = (...args) => {
  2218. let ret = extension.renderer[prop].apply(renderer, args);
  2219. if (ret === false) {
  2220. ret = prevRenderer.apply(renderer, args);
  2221. }
  2222. return ret;
  2223. };
  2224. }
  2225. opts.renderer = renderer;
  2226. }
  2227. if (extension.tokenizer) {
  2228. const tokenizer = marked.defaults.tokenizer || new Tokenizer_1();
  2229. for (const prop in extension.tokenizer) {
  2230. const prevTokenizer = tokenizer[prop];
  2231. tokenizer[prop] = (...args) => {
  2232. let ret = extension.tokenizer[prop].apply(tokenizer, args);
  2233. if (ret === false) {
  2234. ret = prevTokenizer.apply(tokenizer, args);
  2235. }
  2236. return ret;
  2237. };
  2238. }
  2239. opts.tokenizer = tokenizer;
  2240. }
  2241. if (extension.walkTokens) {
  2242. const walkTokens = marked.defaults.walkTokens;
  2243. opts.walkTokens = (token) => {
  2244. extension.walkTokens(token);
  2245. if (walkTokens) {
  2246. walkTokens(token);
  2247. }
  2248. };
  2249. }
  2250. marked.setOptions(opts);
  2251. };
  2252. /**
  2253. * Run callback for every token
  2254. */
  2255. marked.walkTokens = function(tokens, callback) {
  2256. for (const token of tokens) {
  2257. callback(token);
  2258. switch (token.type) {
  2259. case 'table': {
  2260. for (const cell of token.tokens.header) {
  2261. marked.walkTokens(cell, callback);
  2262. }
  2263. for (const row of token.tokens.cells) {
  2264. for (const cell of row) {
  2265. marked.walkTokens(cell, callback);
  2266. }
  2267. }
  2268. break;
  2269. }
  2270. case 'list': {
  2271. marked.walkTokens(token.items, callback);
  2272. break;
  2273. }
  2274. default: {
  2275. if (token.tokens) {
  2276. marked.walkTokens(token.tokens, callback);
  2277. }
  2278. }
  2279. }
  2280. }
  2281. };
  2282. /**
  2283. * Parse Inline
  2284. */
  2285. marked.parseInline = function(src, opt) {
  2286. // throw error in case of non string input
  2287. if (typeof src === 'undefined' || src === null) {
  2288. throw new Error('marked.parseInline(): input parameter is undefined or null');
  2289. }
  2290. if (typeof src !== 'string') {
  2291. throw new Error('marked.parseInline(): input parameter is of type '
  2292. + Object.prototype.toString.call(src) + ', string expected');
  2293. }
  2294. opt = merge({}, marked.defaults, opt || {});
  2295. checkSanitizeDeprecation(opt);
  2296. try {
  2297. const tokens = Lexer_1.lexInline(src, opt);
  2298. if (opt.walkTokens) {
  2299. marked.walkTokens(tokens, opt.walkTokens);
  2300. }
  2301. return Parser_1.parseInline(tokens, opt);
  2302. } catch (e) {
  2303. e.message += '\nPlease report this to https://github.com/markedjs/marked.';
  2304. if (opt.silent) {
  2305. return '<p>An error occurred:</p><pre>'
  2306. + escape(e.message + '', true)
  2307. + '</pre>';
  2308. }
  2309. throw e;
  2310. }
  2311. };
  2312. /**
  2313. * Expose
  2314. */
  2315. marked.Parser = Parser_1;
  2316. marked.parser = Parser_1.parse;
  2317. marked.Renderer = Renderer_1;
  2318. marked.TextRenderer = TextRenderer_1;
  2319. marked.Lexer = Lexer_1;
  2320. marked.lexer = Lexer_1.lex;
  2321. marked.Tokenizer = Tokenizer_1;
  2322. marked.Slugger = Slugger_1;
  2323. marked.parse = marked;
  2324. var marked_1 = marked;
  2325. export default marked_1;