marked.esm.js 66 KB

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