marked.js 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527
  1. /**
  2. * marked v14.1.3 - a markdown parser
  3. * Copyright (c) 2011-2024, 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 (global, factory) {
  11. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  12. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  13. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.marked = {}));
  14. })(this, (function (exports) { 'use strict';
  15. /**
  16. * Gets the original marked default options.
  17. */
  18. function _getDefaults() {
  19. return {
  20. async: false,
  21. breaks: false,
  22. extensions: null,
  23. gfm: true,
  24. hooks: null,
  25. pedantic: false,
  26. renderer: null,
  27. silent: false,
  28. tokenizer: null,
  29. walkTokens: null,
  30. };
  31. }
  32. exports.defaults = _getDefaults();
  33. function changeDefaults(newDefaults) {
  34. exports.defaults = newDefaults;
  35. }
  36. /**
  37. * Helpers
  38. */
  39. const escapeTest = /[&<>"']/;
  40. const escapeReplace = new RegExp(escapeTest.source, 'g');
  41. const escapeTestNoEncode = /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/;
  42. const escapeReplaceNoEncode = new RegExp(escapeTestNoEncode.source, 'g');
  43. const escapeReplacements = {
  44. '&': '&amp;',
  45. '<': '&lt;',
  46. '>': '&gt;',
  47. '"': '&quot;',
  48. "'": '&#39;',
  49. };
  50. const getEscapeReplacement = (ch) => escapeReplacements[ch];
  51. function escape$1(html, encode) {
  52. if (encode) {
  53. if (escapeTest.test(html)) {
  54. return html.replace(escapeReplace, getEscapeReplacement);
  55. }
  56. }
  57. else {
  58. if (escapeTestNoEncode.test(html)) {
  59. return html.replace(escapeReplaceNoEncode, getEscapeReplacement);
  60. }
  61. }
  62. return html;
  63. }
  64. const caret = /(^|[^\[])\^/g;
  65. function edit(regex, opt) {
  66. let source = typeof regex === 'string' ? regex : regex.source;
  67. opt = opt || '';
  68. const obj = {
  69. replace: (name, val) => {
  70. let valSource = typeof val === 'string' ? val : val.source;
  71. valSource = valSource.replace(caret, '$1');
  72. source = source.replace(name, valSource);
  73. return obj;
  74. },
  75. getRegex: () => {
  76. return new RegExp(source, opt);
  77. },
  78. };
  79. return obj;
  80. }
  81. function cleanUrl(href) {
  82. try {
  83. href = encodeURI(href).replace(/%25/g, '%');
  84. }
  85. catch {
  86. return null;
  87. }
  88. return href;
  89. }
  90. const noopTest = { exec: () => null };
  91. function splitCells(tableRow, count) {
  92. // ensure that every cell-delimiting pipe has a space
  93. // before it to distinguish it from an escaped pipe
  94. const row = tableRow.replace(/\|/g, (match, offset, str) => {
  95. let escaped = false;
  96. let curr = offset;
  97. while (--curr >= 0 && str[curr] === '\\')
  98. escaped = !escaped;
  99. if (escaped) {
  100. // odd number of slashes means | is escaped
  101. // so we leave it alone
  102. return '|';
  103. }
  104. else {
  105. // add space before unescaped |
  106. return ' |';
  107. }
  108. }), cells = row.split(/ \|/);
  109. let i = 0;
  110. // First/last cell in a row cannot be empty if it has no leading/trailing pipe
  111. if (!cells[0].trim()) {
  112. cells.shift();
  113. }
  114. if (cells.length > 0 && !cells[cells.length - 1].trim()) {
  115. cells.pop();
  116. }
  117. if (count) {
  118. if (cells.length > count) {
  119. cells.splice(count);
  120. }
  121. else {
  122. while (cells.length < count)
  123. cells.push('');
  124. }
  125. }
  126. for (; i < cells.length; i++) {
  127. // leading or trailing whitespace is ignored per the gfm spec
  128. cells[i] = cells[i].trim().replace(/\\\|/g, '|');
  129. }
  130. return cells;
  131. }
  132. /**
  133. * Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
  134. * /c*$/ is vulnerable to REDOS.
  135. *
  136. * @param str
  137. * @param c
  138. * @param invert Remove suffix of non-c chars instead. Default falsey.
  139. */
  140. function rtrim(str, c, invert) {
  141. const l = str.length;
  142. if (l === 0) {
  143. return '';
  144. }
  145. // Length of suffix matching the invert condition.
  146. let suffLen = 0;
  147. // Step left until we fail to match the invert condition.
  148. while (suffLen < l) {
  149. const currChar = str.charAt(l - suffLen - 1);
  150. if (currChar === c && !invert) {
  151. suffLen++;
  152. }
  153. else if (currChar !== c && invert) {
  154. suffLen++;
  155. }
  156. else {
  157. break;
  158. }
  159. }
  160. return str.slice(0, l - suffLen);
  161. }
  162. function findClosingBracket(str, b) {
  163. if (str.indexOf(b[1]) === -1) {
  164. return -1;
  165. }
  166. let level = 0;
  167. for (let i = 0; i < str.length; i++) {
  168. if (str[i] === '\\') {
  169. i++;
  170. }
  171. else if (str[i] === b[0]) {
  172. level++;
  173. }
  174. else if (str[i] === b[1]) {
  175. level--;
  176. if (level < 0) {
  177. return i;
  178. }
  179. }
  180. }
  181. return -1;
  182. }
  183. function outputLink(cap, link, raw, lexer) {
  184. const href = link.href;
  185. const title = link.title ? escape$1(link.title) : null;
  186. const text = cap[1].replace(/\\([\[\]])/g, '$1');
  187. if (cap[0].charAt(0) !== '!') {
  188. lexer.state.inLink = true;
  189. const token = {
  190. type: 'link',
  191. raw,
  192. href,
  193. title,
  194. text,
  195. tokens: lexer.inlineTokens(text),
  196. };
  197. lexer.state.inLink = false;
  198. return token;
  199. }
  200. return {
  201. type: 'image',
  202. raw,
  203. href,
  204. title,
  205. text: escape$1(text),
  206. };
  207. }
  208. function indentCodeCompensation(raw, text) {
  209. const matchIndentToCode = raw.match(/^(\s+)(?:```)/);
  210. if (matchIndentToCode === null) {
  211. return text;
  212. }
  213. const indentToCode = matchIndentToCode[1];
  214. return text
  215. .split('\n')
  216. .map(node => {
  217. const matchIndentInNode = node.match(/^\s+/);
  218. if (matchIndentInNode === null) {
  219. return node;
  220. }
  221. const [indentInNode] = matchIndentInNode;
  222. if (indentInNode.length >= indentToCode.length) {
  223. return node.slice(indentToCode.length);
  224. }
  225. return node;
  226. })
  227. .join('\n');
  228. }
  229. /**
  230. * Tokenizer
  231. */
  232. class _Tokenizer {
  233. options;
  234. rules; // set by the lexer
  235. lexer; // set by the lexer
  236. constructor(options) {
  237. this.options = options || exports.defaults;
  238. }
  239. space(src) {
  240. const cap = this.rules.block.newline.exec(src);
  241. if (cap && cap[0].length > 0) {
  242. return {
  243. type: 'space',
  244. raw: cap[0],
  245. };
  246. }
  247. }
  248. code(src) {
  249. const cap = this.rules.block.code.exec(src);
  250. if (cap) {
  251. const text = cap[0].replace(/^(?: {1,4}| {0,3}\t)/gm, '');
  252. return {
  253. type: 'code',
  254. raw: cap[0],
  255. codeBlockStyle: 'indented',
  256. text: !this.options.pedantic
  257. ? rtrim(text, '\n')
  258. : text,
  259. };
  260. }
  261. }
  262. fences(src) {
  263. const cap = this.rules.block.fences.exec(src);
  264. if (cap) {
  265. const raw = cap[0];
  266. const text = indentCodeCompensation(raw, cap[3] || '');
  267. return {
  268. type: 'code',
  269. raw,
  270. lang: cap[2] ? cap[2].trim().replace(this.rules.inline.anyPunctuation, '$1') : cap[2],
  271. text,
  272. };
  273. }
  274. }
  275. heading(src) {
  276. const cap = this.rules.block.heading.exec(src);
  277. if (cap) {
  278. let text = cap[2].trim();
  279. // remove trailing #s
  280. if (/#$/.test(text)) {
  281. const trimmed = rtrim(text, '#');
  282. if (this.options.pedantic) {
  283. text = trimmed.trim();
  284. }
  285. else if (!trimmed || / $/.test(trimmed)) {
  286. // CommonMark requires space before trailing #s
  287. text = trimmed.trim();
  288. }
  289. }
  290. return {
  291. type: 'heading',
  292. raw: cap[0],
  293. depth: cap[1].length,
  294. text,
  295. tokens: this.lexer.inline(text),
  296. };
  297. }
  298. }
  299. hr(src) {
  300. const cap = this.rules.block.hr.exec(src);
  301. if (cap) {
  302. return {
  303. type: 'hr',
  304. raw: rtrim(cap[0], '\n'),
  305. };
  306. }
  307. }
  308. blockquote(src) {
  309. const cap = this.rules.block.blockquote.exec(src);
  310. if (cap) {
  311. let lines = rtrim(cap[0], '\n').split('\n');
  312. let raw = '';
  313. let text = '';
  314. const tokens = [];
  315. while (lines.length > 0) {
  316. let inBlockquote = false;
  317. const currentLines = [];
  318. let i;
  319. for (i = 0; i < lines.length; i++) {
  320. // get lines up to a continuation
  321. if (/^ {0,3}>/.test(lines[i])) {
  322. currentLines.push(lines[i]);
  323. inBlockquote = true;
  324. }
  325. else if (!inBlockquote) {
  326. currentLines.push(lines[i]);
  327. }
  328. else {
  329. break;
  330. }
  331. }
  332. lines = lines.slice(i);
  333. const currentRaw = currentLines.join('\n');
  334. const currentText = currentRaw
  335. // precede setext continuation with 4 spaces so it isn't a setext
  336. .replace(/\n {0,3}((?:=+|-+) *)(?=\n|$)/g, '\n $1')
  337. .replace(/^ {0,3}>[ \t]?/gm, '');
  338. raw = raw ? `${raw}\n${currentRaw}` : currentRaw;
  339. text = text ? `${text}\n${currentText}` : currentText;
  340. // parse blockquote lines as top level tokens
  341. // merge paragraphs if this is a continuation
  342. const top = this.lexer.state.top;
  343. this.lexer.state.top = true;
  344. this.lexer.blockTokens(currentText, tokens, true);
  345. this.lexer.state.top = top;
  346. // if there is no continuation then we are done
  347. if (lines.length === 0) {
  348. break;
  349. }
  350. const lastToken = tokens[tokens.length - 1];
  351. if (lastToken?.type === 'code') {
  352. // blockquote continuation cannot be preceded by a code block
  353. break;
  354. }
  355. else if (lastToken?.type === 'blockquote') {
  356. // include continuation in nested blockquote
  357. const oldToken = lastToken;
  358. const newText = oldToken.raw + '\n' + lines.join('\n');
  359. const newToken = this.blockquote(newText);
  360. tokens[tokens.length - 1] = newToken;
  361. raw = raw.substring(0, raw.length - oldToken.raw.length) + newToken.raw;
  362. text = text.substring(0, text.length - oldToken.text.length) + newToken.text;
  363. break;
  364. }
  365. else if (lastToken?.type === 'list') {
  366. // include continuation in nested list
  367. const oldToken = lastToken;
  368. const newText = oldToken.raw + '\n' + lines.join('\n');
  369. const newToken = this.list(newText);
  370. tokens[tokens.length - 1] = newToken;
  371. raw = raw.substring(0, raw.length - lastToken.raw.length) + newToken.raw;
  372. text = text.substring(0, text.length - oldToken.raw.length) + newToken.raw;
  373. lines = newText.substring(tokens[tokens.length - 1].raw.length).split('\n');
  374. continue;
  375. }
  376. }
  377. return {
  378. type: 'blockquote',
  379. raw,
  380. tokens,
  381. text,
  382. };
  383. }
  384. }
  385. list(src) {
  386. let cap = this.rules.block.list.exec(src);
  387. if (cap) {
  388. let bull = cap[1].trim();
  389. const isordered = bull.length > 1;
  390. const list = {
  391. type: 'list',
  392. raw: '',
  393. ordered: isordered,
  394. start: isordered ? +bull.slice(0, -1) : '',
  395. loose: false,
  396. items: [],
  397. };
  398. bull = isordered ? `\\d{1,9}\\${bull.slice(-1)}` : `\\${bull}`;
  399. if (this.options.pedantic) {
  400. bull = isordered ? bull : '[*+-]';
  401. }
  402. // Get next list item
  403. const itemRegex = new RegExp(`^( {0,3}${bull})((?:[\t ][^\\n]*)?(?:\\n|$))`);
  404. let endsWithBlankLine = false;
  405. // Check if current bullet point can start a new List Item
  406. while (src) {
  407. let endEarly = false;
  408. let raw = '';
  409. let itemContents = '';
  410. if (!(cap = itemRegex.exec(src))) {
  411. break;
  412. }
  413. if (this.rules.block.hr.test(src)) { // End list if bullet was actually HR (possibly move into itemRegex?)
  414. break;
  415. }
  416. raw = cap[0];
  417. src = src.substring(raw.length);
  418. let line = cap[2].split('\n', 1)[0].replace(/^\t+/, (t) => ' '.repeat(3 * t.length));
  419. let nextLine = src.split('\n', 1)[0];
  420. let blankLine = !line.trim();
  421. let indent = 0;
  422. if (this.options.pedantic) {
  423. indent = 2;
  424. itemContents = line.trimStart();
  425. }
  426. else if (blankLine) {
  427. indent = cap[1].length + 1;
  428. }
  429. else {
  430. indent = cap[2].search(/[^ ]/); // Find first non-space char
  431. indent = indent > 4 ? 1 : indent; // Treat indented code blocks (> 4 spaces) as having only 1 indent
  432. itemContents = line.slice(indent);
  433. indent += cap[1].length;
  434. }
  435. if (blankLine && /^[ \t]*$/.test(nextLine)) { // Items begin with at most one blank line
  436. raw += nextLine + '\n';
  437. src = src.substring(nextLine.length + 1);
  438. endEarly = true;
  439. }
  440. if (!endEarly) {
  441. const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])((?:[ \t][^\\n]*)?(?:\\n|$))`);
  442. const hrRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`);
  443. const fencesBeginRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:\`\`\`|~~~)`);
  444. const headingBeginRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}#`);
  445. const htmlBeginRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}<[a-z].*>`, 'i');
  446. // Check if following lines should be included in List Item
  447. while (src) {
  448. const rawLine = src.split('\n', 1)[0];
  449. let nextLineWithoutTabs;
  450. nextLine = rawLine;
  451. // Re-align to follow commonmark nesting rules
  452. if (this.options.pedantic) {
  453. nextLine = nextLine.replace(/^ {1,4}(?=( {4})*[^ ])/g, ' ');
  454. nextLineWithoutTabs = nextLine;
  455. }
  456. else {
  457. nextLineWithoutTabs = nextLine.replace(/\t/g, ' ');
  458. }
  459. // End list item if found code fences
  460. if (fencesBeginRegex.test(nextLine)) {
  461. break;
  462. }
  463. // End list item if found start of new heading
  464. if (headingBeginRegex.test(nextLine)) {
  465. break;
  466. }
  467. // End list item if found start of html block
  468. if (htmlBeginRegex.test(nextLine)) {
  469. break;
  470. }
  471. // End list item if found start of new bullet
  472. if (nextBulletRegex.test(nextLine)) {
  473. break;
  474. }
  475. // Horizontal rule found
  476. if (hrRegex.test(nextLine)) {
  477. break;
  478. }
  479. if (nextLineWithoutTabs.search(/[^ ]/) >= indent || !nextLine.trim()) { // Dedent if possible
  480. itemContents += '\n' + nextLineWithoutTabs.slice(indent);
  481. }
  482. else {
  483. // not enough indentation
  484. if (blankLine) {
  485. break;
  486. }
  487. // paragraph continuation unless last line was a different block level element
  488. if (line.replace(/\t/g, ' ').search(/[^ ]/) >= 4) { // indented code block
  489. break;
  490. }
  491. if (fencesBeginRegex.test(line)) {
  492. break;
  493. }
  494. if (headingBeginRegex.test(line)) {
  495. break;
  496. }
  497. if (hrRegex.test(line)) {
  498. break;
  499. }
  500. itemContents += '\n' + nextLine;
  501. }
  502. if (!blankLine && !nextLine.trim()) { // Check if current line is blank
  503. blankLine = true;
  504. }
  505. raw += rawLine + '\n';
  506. src = src.substring(rawLine.length + 1);
  507. line = nextLineWithoutTabs.slice(indent);
  508. }
  509. }
  510. if (!list.loose) {
  511. // If the previous item ended with a blank line, the list is loose
  512. if (endsWithBlankLine) {
  513. list.loose = true;
  514. }
  515. else if (/\n[ \t]*\n[ \t]*$/.test(raw)) {
  516. endsWithBlankLine = true;
  517. }
  518. }
  519. let istask = null;
  520. let ischecked;
  521. // Check for task list items
  522. if (this.options.gfm) {
  523. istask = /^\[[ xX]\] /.exec(itemContents);
  524. if (istask) {
  525. ischecked = istask[0] !== '[ ] ';
  526. itemContents = itemContents.replace(/^\[[ xX]\] +/, '');
  527. }
  528. }
  529. list.items.push({
  530. type: 'list_item',
  531. raw,
  532. task: !!istask,
  533. checked: ischecked,
  534. loose: false,
  535. text: itemContents,
  536. tokens: [],
  537. });
  538. list.raw += raw;
  539. }
  540. // Do not consume newlines at end of final item. Alternatively, make itemRegex *start* with any newlines to simplify/speed up endsWithBlankLine logic
  541. list.items[list.items.length - 1].raw = list.items[list.items.length - 1].raw.trimEnd();
  542. list.items[list.items.length - 1].text = list.items[list.items.length - 1].text.trimEnd();
  543. list.raw = list.raw.trimEnd();
  544. // Item child tokens handled here at end because we needed to have the final item to trim it first
  545. for (let i = 0; i < list.items.length; i++) {
  546. this.lexer.state.top = false;
  547. list.items[i].tokens = this.lexer.blockTokens(list.items[i].text, []);
  548. if (!list.loose) {
  549. // Check if list should be loose
  550. const spacers = list.items[i].tokens.filter(t => t.type === 'space');
  551. const hasMultipleLineBreaks = spacers.length > 0 && spacers.some(t => /\n.*\n/.test(t.raw));
  552. list.loose = hasMultipleLineBreaks;
  553. }
  554. }
  555. // Set all items to loose if list is loose
  556. if (list.loose) {
  557. for (let i = 0; i < list.items.length; i++) {
  558. list.items[i].loose = true;
  559. }
  560. }
  561. return list;
  562. }
  563. }
  564. html(src) {
  565. const cap = this.rules.block.html.exec(src);
  566. if (cap) {
  567. const token = {
  568. type: 'html',
  569. block: true,
  570. raw: cap[0],
  571. pre: cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style',
  572. text: cap[0],
  573. };
  574. return token;
  575. }
  576. }
  577. def(src) {
  578. const cap = this.rules.block.def.exec(src);
  579. if (cap) {
  580. const tag = cap[1].toLowerCase().replace(/\s+/g, ' ');
  581. const href = cap[2] ? cap[2].replace(/^<(.*)>$/, '$1').replace(this.rules.inline.anyPunctuation, '$1') : '';
  582. const title = cap[3] ? cap[3].substring(1, cap[3].length - 1).replace(this.rules.inline.anyPunctuation, '$1') : cap[3];
  583. return {
  584. type: 'def',
  585. tag,
  586. raw: cap[0],
  587. href,
  588. title,
  589. };
  590. }
  591. }
  592. table(src) {
  593. const cap = this.rules.block.table.exec(src);
  594. if (!cap) {
  595. return;
  596. }
  597. if (!/[:|]/.test(cap[2])) {
  598. // delimiter row must have a pipe (|) or colon (:) otherwise it is a setext heading
  599. return;
  600. }
  601. const headers = splitCells(cap[1]);
  602. const aligns = cap[2].replace(/^\||\| *$/g, '').split('|');
  603. const rows = cap[3] && cap[3].trim() ? cap[3].replace(/\n[ \t]*$/, '').split('\n') : [];
  604. const item = {
  605. type: 'table',
  606. raw: cap[0],
  607. header: [],
  608. align: [],
  609. rows: [],
  610. };
  611. if (headers.length !== aligns.length) {
  612. // header and align columns must be equal, rows can be different.
  613. return;
  614. }
  615. for (const align of aligns) {
  616. if (/^ *-+: *$/.test(align)) {
  617. item.align.push('right');
  618. }
  619. else if (/^ *:-+: *$/.test(align)) {
  620. item.align.push('center');
  621. }
  622. else if (/^ *:-+ *$/.test(align)) {
  623. item.align.push('left');
  624. }
  625. else {
  626. item.align.push(null);
  627. }
  628. }
  629. for (let i = 0; i < headers.length; i++) {
  630. item.header.push({
  631. text: headers[i],
  632. tokens: this.lexer.inline(headers[i]),
  633. header: true,
  634. align: item.align[i],
  635. });
  636. }
  637. for (const row of rows) {
  638. item.rows.push(splitCells(row, item.header.length).map((cell, i) => {
  639. return {
  640. text: cell,
  641. tokens: this.lexer.inline(cell),
  642. header: false,
  643. align: item.align[i],
  644. };
  645. }));
  646. }
  647. return item;
  648. }
  649. lheading(src) {
  650. const cap = this.rules.block.lheading.exec(src);
  651. if (cap) {
  652. return {
  653. type: 'heading',
  654. raw: cap[0],
  655. depth: cap[2].charAt(0) === '=' ? 1 : 2,
  656. text: cap[1],
  657. tokens: this.lexer.inline(cap[1]),
  658. };
  659. }
  660. }
  661. paragraph(src) {
  662. const cap = this.rules.block.paragraph.exec(src);
  663. if (cap) {
  664. const text = cap[1].charAt(cap[1].length - 1) === '\n'
  665. ? cap[1].slice(0, -1)
  666. : cap[1];
  667. return {
  668. type: 'paragraph',
  669. raw: cap[0],
  670. text,
  671. tokens: this.lexer.inline(text),
  672. };
  673. }
  674. }
  675. text(src) {
  676. const cap = this.rules.block.text.exec(src);
  677. if (cap) {
  678. return {
  679. type: 'text',
  680. raw: cap[0],
  681. text: cap[0],
  682. tokens: this.lexer.inline(cap[0]),
  683. };
  684. }
  685. }
  686. escape(src) {
  687. const cap = this.rules.inline.escape.exec(src);
  688. if (cap) {
  689. return {
  690. type: 'escape',
  691. raw: cap[0],
  692. text: escape$1(cap[1]),
  693. };
  694. }
  695. }
  696. tag(src) {
  697. const cap = this.rules.inline.tag.exec(src);
  698. if (cap) {
  699. if (!this.lexer.state.inLink && /^<a /i.test(cap[0])) {
  700. this.lexer.state.inLink = true;
  701. }
  702. else if (this.lexer.state.inLink && /^<\/a>/i.test(cap[0])) {
  703. this.lexer.state.inLink = false;
  704. }
  705. if (!this.lexer.state.inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
  706. this.lexer.state.inRawBlock = true;
  707. }
  708. else if (this.lexer.state.inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
  709. this.lexer.state.inRawBlock = false;
  710. }
  711. return {
  712. type: 'html',
  713. raw: cap[0],
  714. inLink: this.lexer.state.inLink,
  715. inRawBlock: this.lexer.state.inRawBlock,
  716. block: false,
  717. text: cap[0],
  718. };
  719. }
  720. }
  721. link(src) {
  722. const cap = this.rules.inline.link.exec(src);
  723. if (cap) {
  724. const trimmedUrl = cap[2].trim();
  725. if (!this.options.pedantic && /^</.test(trimmedUrl)) {
  726. // commonmark requires matching angle brackets
  727. if (!(/>$/.test(trimmedUrl))) {
  728. return;
  729. }
  730. // ending angle bracket cannot be escaped
  731. const rtrimSlash = rtrim(trimmedUrl.slice(0, -1), '\\');
  732. if ((trimmedUrl.length - rtrimSlash.length) % 2 === 0) {
  733. return;
  734. }
  735. }
  736. else {
  737. // find closing parenthesis
  738. const lastParenIndex = findClosingBracket(cap[2], '()');
  739. if (lastParenIndex > -1) {
  740. const start = cap[0].indexOf('!') === 0 ? 5 : 4;
  741. const linkLen = start + cap[1].length + lastParenIndex;
  742. cap[2] = cap[2].substring(0, lastParenIndex);
  743. cap[0] = cap[0].substring(0, linkLen).trim();
  744. cap[3] = '';
  745. }
  746. }
  747. let href = cap[2];
  748. let title = '';
  749. if (this.options.pedantic) {
  750. // split pedantic href and title
  751. const link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href);
  752. if (link) {
  753. href = link[1];
  754. title = link[3];
  755. }
  756. }
  757. else {
  758. title = cap[3] ? cap[3].slice(1, -1) : '';
  759. }
  760. href = href.trim();
  761. if (/^</.test(href)) {
  762. if (this.options.pedantic && !(/>$/.test(trimmedUrl))) {
  763. // pedantic allows starting angle bracket without ending angle bracket
  764. href = href.slice(1);
  765. }
  766. else {
  767. href = href.slice(1, -1);
  768. }
  769. }
  770. return outputLink(cap, {
  771. href: href ? href.replace(this.rules.inline.anyPunctuation, '$1') : href,
  772. title: title ? title.replace(this.rules.inline.anyPunctuation, '$1') : title,
  773. }, cap[0], this.lexer);
  774. }
  775. }
  776. reflink(src, links) {
  777. let cap;
  778. if ((cap = this.rules.inline.reflink.exec(src))
  779. || (cap = this.rules.inline.nolink.exec(src))) {
  780. const linkString = (cap[2] || cap[1]).replace(/\s+/g, ' ');
  781. const link = links[linkString.toLowerCase()];
  782. if (!link) {
  783. const text = cap[0].charAt(0);
  784. return {
  785. type: 'text',
  786. raw: text,
  787. text,
  788. };
  789. }
  790. return outputLink(cap, link, cap[0], this.lexer);
  791. }
  792. }
  793. emStrong(src, maskedSrc, prevChar = '') {
  794. let match = this.rules.inline.emStrongLDelim.exec(src);
  795. if (!match)
  796. return;
  797. // _ can't be between two alphanumerics. \p{L}\p{N} includes non-english alphabet/numbers as well
  798. if (match[3] && prevChar.match(/[\p{L}\p{N}]/u))
  799. return;
  800. const nextChar = match[1] || match[2] || '';
  801. if (!nextChar || !prevChar || this.rules.inline.punctuation.exec(prevChar)) {
  802. // unicode Regex counts emoji as 1 char; spread into array for proper count (used multiple times below)
  803. const lLength = [...match[0]].length - 1;
  804. let rDelim, rLength, delimTotal = lLength, midDelimTotal = 0;
  805. const endReg = match[0][0] === '*' ? this.rules.inline.emStrongRDelimAst : this.rules.inline.emStrongRDelimUnd;
  806. endReg.lastIndex = 0;
  807. // Clip maskedSrc to same section of string as src (move to lexer?)
  808. maskedSrc = maskedSrc.slice(-1 * src.length + lLength);
  809. while ((match = endReg.exec(maskedSrc)) != null) {
  810. rDelim = match[1] || match[2] || match[3] || match[4] || match[5] || match[6];
  811. if (!rDelim)
  812. continue; // skip single * in __abc*abc__
  813. rLength = [...rDelim].length;
  814. if (match[3] || match[4]) { // found another Left Delim
  815. delimTotal += rLength;
  816. continue;
  817. }
  818. else if (match[5] || match[6]) { // either Left or Right Delim
  819. if (lLength % 3 && !((lLength + rLength) % 3)) {
  820. midDelimTotal += rLength;
  821. continue; // CommonMark Emphasis Rules 9-10
  822. }
  823. }
  824. delimTotal -= rLength;
  825. if (delimTotal > 0)
  826. continue; // Haven't found enough closing delimiters
  827. // Remove extra characters. *a*** -> *a*
  828. rLength = Math.min(rLength, rLength + delimTotal + midDelimTotal);
  829. // char length can be >1 for unicode characters;
  830. const lastCharLength = [...match[0]][0].length;
  831. const raw = src.slice(0, lLength + match.index + lastCharLength + rLength);
  832. // Create `em` if smallest delimiter has odd char count. *a***
  833. if (Math.min(lLength, rLength) % 2) {
  834. const text = raw.slice(1, -1);
  835. return {
  836. type: 'em',
  837. raw,
  838. text,
  839. tokens: this.lexer.inlineTokens(text),
  840. };
  841. }
  842. // Create 'strong' if smallest delimiter has even char count. **a***
  843. const text = raw.slice(2, -2);
  844. return {
  845. type: 'strong',
  846. raw,
  847. text,
  848. tokens: this.lexer.inlineTokens(text),
  849. };
  850. }
  851. }
  852. }
  853. codespan(src) {
  854. const cap = this.rules.inline.code.exec(src);
  855. if (cap) {
  856. let text = cap[2].replace(/\n/g, ' ');
  857. const hasNonSpaceChars = /[^ ]/.test(text);
  858. const hasSpaceCharsOnBothEnds = /^ /.test(text) && / $/.test(text);
  859. if (hasNonSpaceChars && hasSpaceCharsOnBothEnds) {
  860. text = text.substring(1, text.length - 1);
  861. }
  862. text = escape$1(text, true);
  863. return {
  864. type: 'codespan',
  865. raw: cap[0],
  866. text,
  867. };
  868. }
  869. }
  870. br(src) {
  871. const cap = this.rules.inline.br.exec(src);
  872. if (cap) {
  873. return {
  874. type: 'br',
  875. raw: cap[0],
  876. };
  877. }
  878. }
  879. del(src) {
  880. const cap = this.rules.inline.del.exec(src);
  881. if (cap) {
  882. return {
  883. type: 'del',
  884. raw: cap[0],
  885. text: cap[2],
  886. tokens: this.lexer.inlineTokens(cap[2]),
  887. };
  888. }
  889. }
  890. autolink(src) {
  891. const cap = this.rules.inline.autolink.exec(src);
  892. if (cap) {
  893. let text, href;
  894. if (cap[2] === '@') {
  895. text = escape$1(cap[1]);
  896. href = 'mailto:' + text;
  897. }
  898. else {
  899. text = escape$1(cap[1]);
  900. href = text;
  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. url(src) {
  918. let cap;
  919. if (cap = this.rules.inline.url.exec(src)) {
  920. let text, href;
  921. if (cap[2] === '@') {
  922. text = escape$1(cap[0]);
  923. href = 'mailto:' + text;
  924. }
  925. else {
  926. // do extended autolink path validation
  927. let prevCapZero;
  928. do {
  929. prevCapZero = cap[0];
  930. cap[0] = this.rules.inline._backpedal.exec(cap[0])?.[0] ?? '';
  931. } while (prevCapZero !== cap[0]);
  932. text = escape$1(cap[0]);
  933. if (cap[1] === 'www.') {
  934. href = 'http://' + cap[0];
  935. }
  936. else {
  937. href = cap[0];
  938. }
  939. }
  940. return {
  941. type: 'link',
  942. raw: cap[0],
  943. text,
  944. href,
  945. tokens: [
  946. {
  947. type: 'text',
  948. raw: text,
  949. text,
  950. },
  951. ],
  952. };
  953. }
  954. }
  955. inlineText(src) {
  956. const cap = this.rules.inline.text.exec(src);
  957. if (cap) {
  958. let text;
  959. if (this.lexer.state.inRawBlock) {
  960. text = cap[0];
  961. }
  962. else {
  963. text = escape$1(cap[0]);
  964. }
  965. return {
  966. type: 'text',
  967. raw: cap[0],
  968. text,
  969. };
  970. }
  971. }
  972. }
  973. /**
  974. * Block-Level Grammar
  975. */
  976. const newline = /^(?:[ \t]*(?:\n|$))+/;
  977. const blockCode = /^((?: {4}| {0,3}\t)[^\n]+(?:\n(?:[ \t]*(?:\n|$))*)?)+/;
  978. const fences = /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/;
  979. const hr = /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/;
  980. const heading = /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/;
  981. const bullet = /(?:[*+-]|\d{1,9}[.)])/;
  982. const lheading = edit(/^(?!bull |blockCode|fences|blockquote|heading|html)((?:.|\n(?!\s*?\n|bull |blockCode|fences|blockquote|heading|html))+?)\n {0,3}(=+|-+) *(?:\n+|$)/)
  983. .replace(/bull/g, bullet) // lists can interrupt
  984. .replace(/blockCode/g, /(?: {4}| {0,3}\t)/) // indented code blocks can interrupt
  985. .replace(/fences/g, / {0,3}(?:`{3,}|~{3,})/) // fenced code blocks can interrupt
  986. .replace(/blockquote/g, / {0,3}>/) // blockquote can interrupt
  987. .replace(/heading/g, / {0,3}#{1,6}/) // ATX heading can interrupt
  988. .replace(/html/g, / {0,3}<[^\n>]+>\n/) // block html can interrupt
  989. .getRegex();
  990. const _paragraph = /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/;
  991. const blockText = /^[^\n]+/;
  992. const _blockLabel = /(?!\s*\])(?:\\.|[^\[\]\\])+/;
  993. const def = edit(/^ {0,3}\[(label)\]: *(?:\n[ \t]*)?([^<\s][^\s]*|<.*?>)(?:(?: +(?:\n[ \t]*)?| *\n[ \t]*)(title))? *(?:\n+|$)/)
  994. .replace('label', _blockLabel)
  995. .replace('title', /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/)
  996. .getRegex();
  997. const list = edit(/^( {0,3}bull)([ \t][^\n]+?)?(?:\n|$)/)
  998. .replace(/bull/g, bullet)
  999. .getRegex();
  1000. const _tag = 'address|article|aside|base|basefont|blockquote|body|caption'
  1001. + '|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption'
  1002. + '|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe'
  1003. + '|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option'
  1004. + '|p|param|search|section|summary|table|tbody|td|tfoot|th|thead|title'
  1005. + '|tr|track|ul';
  1006. const _comment = /<!--(?:-?>|[\s\S]*?(?:-->|$))/;
  1007. const html = edit('^ {0,3}(?:' // optional indentation
  1008. + '<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)' // (1)
  1009. + '|comment[^\\n]*(\\n+|$)' // (2)
  1010. + '|<\\?[\\s\\S]*?(?:\\?>\\n*|$)' // (3)
  1011. + '|<![A-Z][\\s\\S]*?(?:>\\n*|$)' // (4)
  1012. + '|<!\\[CDATA\\[[\\s\\S]*?(?:\\]\\]>\\n*|$)' // (5)
  1013. + '|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (6)
  1014. + '|<(?!script|pre|style|textarea)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (7) open tag
  1015. + '|</(?!script|pre|style|textarea)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (7) closing tag
  1016. + ')', 'i')
  1017. .replace('comment', _comment)
  1018. .replace('tag', _tag)
  1019. .replace('attribute', / +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/)
  1020. .getRegex();
  1021. const paragraph = edit(_paragraph)
  1022. .replace('hr', hr)
  1023. .replace('heading', ' {0,3}#{1,6}(?:\\s|$)')
  1024. .replace('|lheading', '') // setext headings don't interrupt commonmark paragraphs
  1025. .replace('|table', '')
  1026. .replace('blockquote', ' {0,3}>')
  1027. .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
  1028. .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
  1029. .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
  1030. .replace('tag', _tag) // pars can be interrupted by type (6) html blocks
  1031. .getRegex();
  1032. const blockquote = edit(/^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/)
  1033. .replace('paragraph', paragraph)
  1034. .getRegex();
  1035. /**
  1036. * Normal Block Grammar
  1037. */
  1038. const blockNormal = {
  1039. blockquote,
  1040. code: blockCode,
  1041. def,
  1042. fences,
  1043. heading,
  1044. hr,
  1045. html,
  1046. lheading,
  1047. list,
  1048. newline,
  1049. paragraph,
  1050. table: noopTest,
  1051. text: blockText,
  1052. };
  1053. /**
  1054. * GFM Block Grammar
  1055. */
  1056. const gfmTable = edit('^ *([^\\n ].*)\\n' // Header
  1057. + ' {0,3}((?:\\| *)?:?-+:? *(?:\\| *:?-+:? *)*(?:\\| *)?)' // Align
  1058. + '(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)') // Cells
  1059. .replace('hr', hr)
  1060. .replace('heading', ' {0,3}#{1,6}(?:\\s|$)')
  1061. .replace('blockquote', ' {0,3}>')
  1062. .replace('code', '(?: {4}| {0,3}\t)[^\\n]')
  1063. .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
  1064. .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
  1065. .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
  1066. .replace('tag', _tag) // tables can be interrupted by type (6) html blocks
  1067. .getRegex();
  1068. const blockGfm = {
  1069. ...blockNormal,
  1070. table: gfmTable,
  1071. paragraph: edit(_paragraph)
  1072. .replace('hr', hr)
  1073. .replace('heading', ' {0,3}#{1,6}(?:\\s|$)')
  1074. .replace('|lheading', '') // setext headings don't interrupt commonmark paragraphs
  1075. .replace('table', gfmTable) // interrupt paragraphs with table
  1076. .replace('blockquote', ' {0,3}>')
  1077. .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
  1078. .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
  1079. .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
  1080. .replace('tag', _tag) // pars can be interrupted by type (6) html blocks
  1081. .getRegex(),
  1082. };
  1083. /**
  1084. * Pedantic grammar (original John Gruber's loose markdown specification)
  1085. */
  1086. const blockPedantic = {
  1087. ...blockNormal,
  1088. html: edit('^ *(?:comment *(?:\\n|\\s*$)'
  1089. + '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
  1090. + '|<tag(?:"[^"]*"|\'[^\']*\'|\\s[^\'"/>\\s]*)*?/?> *(?:\\n{2,}|\\s*$))')
  1091. .replace('comment', _comment)
  1092. .replace(/tag/g, '(?!(?:'
  1093. + 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub'
  1094. + '|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)'
  1095. + '\\b)\\w+(?!:|[^\\w\\s@]*@)\\b')
  1096. .getRegex(),
  1097. def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
  1098. heading: /^(#{1,6})(.*)(?:\n+|$)/,
  1099. fences: noopTest, // fences not supported
  1100. lheading: /^(.+?)\n {0,3}(=+|-+) *(?:\n+|$)/,
  1101. paragraph: edit(_paragraph)
  1102. .replace('hr', hr)
  1103. .replace('heading', ' *#{1,6} *[^\n]')
  1104. .replace('lheading', lheading)
  1105. .replace('|table', '')
  1106. .replace('blockquote', ' {0,3}>')
  1107. .replace('|fences', '')
  1108. .replace('|list', '')
  1109. .replace('|html', '')
  1110. .replace('|tag', '')
  1111. .getRegex(),
  1112. };
  1113. /**
  1114. * Inline-Level Grammar
  1115. */
  1116. const escape = /^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/;
  1117. const inlineCode = /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/;
  1118. const br = /^( {2,}|\\)\n(?!\s*$)/;
  1119. const inlineText = /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<!\[`*_]|\b_|$)|[^ ](?= {2,}\n)))/;
  1120. // list of unicode punctuation marks, plus any missing characters from CommonMark spec
  1121. const _punctuation = '\\p{P}\\p{S}';
  1122. const punctuation = edit(/^((?![*_])[\spunctuation])/, 'u')
  1123. .replace(/punctuation/g, _punctuation).getRegex();
  1124. // sequences em should skip over [title](link), `code`, <html>
  1125. const blockSkip = /\[[^[\]]*?\]\((?:\\.|[^\\\(\)]|\((?:\\.|[^\\\(\)])*\))*\)|`[^`]*?`|<[^<>]*?>/g;
  1126. const emStrongLDelim = edit(/^(?:\*+(?:((?!\*)[punct])|[^\s*]))|^_+(?:((?!_)[punct])|([^\s_]))/, 'u')
  1127. .replace(/punct/g, _punctuation)
  1128. .getRegex();
  1129. const emStrongRDelimAst = edit('^[^_*]*?__[^_*]*?\\*[^_*]*?(?=__)' // Skip orphan inside strong
  1130. + '|[^*]+(?=[^*])' // Consume to delim
  1131. + '|(?!\\*)[punct](\\*+)(?=[\\s]|$)' // (1) #*** can only be a Right Delimiter
  1132. + '|[^punct\\s](\\*+)(?!\\*)(?=[punct\\s]|$)' // (2) a***#, a*** can only be a Right Delimiter
  1133. + '|(?!\\*)[punct\\s](\\*+)(?=[^punct\\s])' // (3) #***a, ***a can only be Left Delimiter
  1134. + '|[\\s](\\*+)(?!\\*)(?=[punct])' // (4) ***# can only be Left Delimiter
  1135. + '|(?!\\*)[punct](\\*+)(?!\\*)(?=[punct])' // (5) #***# can be either Left or Right Delimiter
  1136. + '|[^punct\\s](\\*+)(?=[^punct\\s])', 'gu') // (6) a***a can be either Left or Right Delimiter
  1137. .replace(/punct/g, _punctuation)
  1138. .getRegex();
  1139. // (6) Not allowed for _
  1140. const emStrongRDelimUnd = edit('^[^_*]*?\\*\\*[^_*]*?_[^_*]*?(?=\\*\\*)' // Skip orphan inside strong
  1141. + '|[^_]+(?=[^_])' // Consume to delim
  1142. + '|(?!_)[punct](_+)(?=[\\s]|$)' // (1) #___ can only be a Right Delimiter
  1143. + '|[^punct\\s](_+)(?!_)(?=[punct\\s]|$)' // (2) a___#, a___ can only be a Right Delimiter
  1144. + '|(?!_)[punct\\s](_+)(?=[^punct\\s])' // (3) #___a, ___a can only be Left Delimiter
  1145. + '|[\\s](_+)(?!_)(?=[punct])' // (4) ___# can only be Left Delimiter
  1146. + '|(?!_)[punct](_+)(?!_)(?=[punct])', 'gu') // (5) #___# can be either Left or Right Delimiter
  1147. .replace(/punct/g, _punctuation)
  1148. .getRegex();
  1149. const anyPunctuation = edit(/\\([punct])/, 'gu')
  1150. .replace(/punct/g, _punctuation)
  1151. .getRegex();
  1152. const autolink = edit(/^<(scheme:[^\s\x00-\x1f<>]*|email)>/)
  1153. .replace('scheme', /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/)
  1154. .replace('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])?)+(?![-_])/)
  1155. .getRegex();
  1156. const _inlineComment = edit(_comment).replace('(?:-->|$)', '-->').getRegex();
  1157. const tag = edit('^comment'
  1158. + '|^</[a-zA-Z][\\w:-]*\\s*>' // self-closing tag
  1159. + '|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>' // open tag
  1160. + '|^<\\?[\\s\\S]*?\\?>' // processing instruction, e.g. <?php ?>
  1161. + '|^<![a-zA-Z]+\\s[\\s\\S]*?>' // declaration, e.g. <!DOCTYPE html>
  1162. + '|^<!\\[CDATA\\[[\\s\\S]*?\\]\\]>') // CDATA section
  1163. .replace('comment', _inlineComment)
  1164. .replace('attribute', /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/)
  1165. .getRegex();
  1166. const _inlineLabel = /(?:\[(?:\\.|[^\[\]\\])*\]|\\.|`[^`]*`|[^\[\]\\`])*?/;
  1167. const link = edit(/^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/)
  1168. .replace('label', _inlineLabel)
  1169. .replace('href', /<(?:\\.|[^\n<>\\])+>|[^\s\x00-\x1f]*/)
  1170. .replace('title', /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/)
  1171. .getRegex();
  1172. const reflink = edit(/^!?\[(label)\]\[(ref)\]/)
  1173. .replace('label', _inlineLabel)
  1174. .replace('ref', _blockLabel)
  1175. .getRegex();
  1176. const nolink = edit(/^!?\[(ref)\](?:\[\])?/)
  1177. .replace('ref', _blockLabel)
  1178. .getRegex();
  1179. const reflinkSearch = edit('reflink|nolink(?!\\()', 'g')
  1180. .replace('reflink', reflink)
  1181. .replace('nolink', nolink)
  1182. .getRegex();
  1183. /**
  1184. * Normal Inline Grammar
  1185. */
  1186. const inlineNormal = {
  1187. _backpedal: noopTest, // only used for GFM url
  1188. anyPunctuation,
  1189. autolink,
  1190. blockSkip,
  1191. br,
  1192. code: inlineCode,
  1193. del: noopTest,
  1194. emStrongLDelim,
  1195. emStrongRDelimAst,
  1196. emStrongRDelimUnd,
  1197. escape,
  1198. link,
  1199. nolink,
  1200. punctuation,
  1201. reflink,
  1202. reflinkSearch,
  1203. tag,
  1204. text: inlineText,
  1205. url: noopTest,
  1206. };
  1207. /**
  1208. * Pedantic Inline Grammar
  1209. */
  1210. const inlinePedantic = {
  1211. ...inlineNormal,
  1212. link: edit(/^!?\[(label)\]\((.*?)\)/)
  1213. .replace('label', _inlineLabel)
  1214. .getRegex(),
  1215. reflink: edit(/^!?\[(label)\]\s*\[([^\]]*)\]/)
  1216. .replace('label', _inlineLabel)
  1217. .getRegex(),
  1218. };
  1219. /**
  1220. * GFM Inline Grammar
  1221. */
  1222. const inlineGfm = {
  1223. ...inlineNormal,
  1224. escape: edit(escape).replace('])', '~|])').getRegex(),
  1225. url: edit(/^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/, 'i')
  1226. .replace('email', /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/)
  1227. .getRegex(),
  1228. _backpedal: /(?:[^?!.,:;*_'"~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_'"~)]+(?!$))+/,
  1229. del: /^(~~?)(?=[^\s~])([\s\S]*?[^\s~])\1(?=[^~]|$)/,
  1230. text: /^([`~]+|[^`~])(?:(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\<!\[`*~_]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)))/,
  1231. };
  1232. /**
  1233. * GFM + Line Breaks Inline Grammar
  1234. */
  1235. const inlineBreaks = {
  1236. ...inlineGfm,
  1237. br: edit(br).replace('{2,}', '*').getRegex(),
  1238. text: edit(inlineGfm.text)
  1239. .replace('\\b_', '\\b_| {2,}\\n')
  1240. .replace(/\{2,\}/g, '*')
  1241. .getRegex(),
  1242. };
  1243. /**
  1244. * exports
  1245. */
  1246. const block = {
  1247. normal: blockNormal,
  1248. gfm: blockGfm,
  1249. pedantic: blockPedantic,
  1250. };
  1251. const inline = {
  1252. normal: inlineNormal,
  1253. gfm: inlineGfm,
  1254. breaks: inlineBreaks,
  1255. pedantic: inlinePedantic,
  1256. };
  1257. /**
  1258. * Block Lexer
  1259. */
  1260. class _Lexer {
  1261. tokens;
  1262. options;
  1263. state;
  1264. tokenizer;
  1265. inlineQueue;
  1266. constructor(options) {
  1267. // TokenList cannot be created in one go
  1268. this.tokens = [];
  1269. this.tokens.links = Object.create(null);
  1270. this.options = options || exports.defaults;
  1271. this.options.tokenizer = this.options.tokenizer || new _Tokenizer();
  1272. this.tokenizer = this.options.tokenizer;
  1273. this.tokenizer.options = this.options;
  1274. this.tokenizer.lexer = this;
  1275. this.inlineQueue = [];
  1276. this.state = {
  1277. inLink: false,
  1278. inRawBlock: false,
  1279. top: true,
  1280. };
  1281. const rules = {
  1282. block: block.normal,
  1283. inline: inline.normal,
  1284. };
  1285. if (this.options.pedantic) {
  1286. rules.block = block.pedantic;
  1287. rules.inline = inline.pedantic;
  1288. }
  1289. else if (this.options.gfm) {
  1290. rules.block = block.gfm;
  1291. if (this.options.breaks) {
  1292. rules.inline = inline.breaks;
  1293. }
  1294. else {
  1295. rules.inline = inline.gfm;
  1296. }
  1297. }
  1298. this.tokenizer.rules = rules;
  1299. }
  1300. /**
  1301. * Expose Rules
  1302. */
  1303. static get rules() {
  1304. return {
  1305. block,
  1306. inline,
  1307. };
  1308. }
  1309. /**
  1310. * Static Lex Method
  1311. */
  1312. static lex(src, options) {
  1313. const lexer = new _Lexer(options);
  1314. return lexer.lex(src);
  1315. }
  1316. /**
  1317. * Static Lex Inline Method
  1318. */
  1319. static lexInline(src, options) {
  1320. const lexer = new _Lexer(options);
  1321. return lexer.inlineTokens(src);
  1322. }
  1323. /**
  1324. * Preprocessing
  1325. */
  1326. lex(src) {
  1327. src = src
  1328. .replace(/\r\n|\r/g, '\n');
  1329. this.blockTokens(src, this.tokens);
  1330. for (let i = 0; i < this.inlineQueue.length; i++) {
  1331. const next = this.inlineQueue[i];
  1332. this.inlineTokens(next.src, next.tokens);
  1333. }
  1334. this.inlineQueue = [];
  1335. return this.tokens;
  1336. }
  1337. blockTokens(src, tokens = [], lastParagraphClipped = false) {
  1338. if (this.options.pedantic) {
  1339. src = src.replace(/\t/g, ' ').replace(/^ +$/gm, '');
  1340. }
  1341. let token;
  1342. let lastToken;
  1343. let cutSrc;
  1344. while (src) {
  1345. if (this.options.extensions
  1346. && this.options.extensions.block
  1347. && this.options.extensions.block.some((extTokenizer) => {
  1348. if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
  1349. src = src.substring(token.raw.length);
  1350. tokens.push(token);
  1351. return true;
  1352. }
  1353. return false;
  1354. })) {
  1355. continue;
  1356. }
  1357. // newline
  1358. if (token = this.tokenizer.space(src)) {
  1359. src = src.substring(token.raw.length);
  1360. if (token.raw.length === 1 && tokens.length > 0) {
  1361. // if there's a single \n as a spacer, it's terminating the last line,
  1362. // so move it there so that we don't get unnecessary paragraph tags
  1363. tokens[tokens.length - 1].raw += '\n';
  1364. }
  1365. else {
  1366. tokens.push(token);
  1367. }
  1368. continue;
  1369. }
  1370. // code
  1371. if (token = this.tokenizer.code(src)) {
  1372. src = src.substring(token.raw.length);
  1373. lastToken = tokens[tokens.length - 1];
  1374. // An indented code block cannot interrupt a paragraph.
  1375. if (lastToken && (lastToken.type === 'paragraph' || lastToken.type === 'text')) {
  1376. lastToken.raw += '\n' + token.raw;
  1377. lastToken.text += '\n' + token.text;
  1378. this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
  1379. }
  1380. else {
  1381. tokens.push(token);
  1382. }
  1383. continue;
  1384. }
  1385. // fences
  1386. if (token = this.tokenizer.fences(src)) {
  1387. src = src.substring(token.raw.length);
  1388. tokens.push(token);
  1389. continue;
  1390. }
  1391. // heading
  1392. if (token = this.tokenizer.heading(src)) {
  1393. src = src.substring(token.raw.length);
  1394. tokens.push(token);
  1395. continue;
  1396. }
  1397. // hr
  1398. if (token = this.tokenizer.hr(src)) {
  1399. src = src.substring(token.raw.length);
  1400. tokens.push(token);
  1401. continue;
  1402. }
  1403. // blockquote
  1404. if (token = this.tokenizer.blockquote(src)) {
  1405. src = src.substring(token.raw.length);
  1406. tokens.push(token);
  1407. continue;
  1408. }
  1409. // list
  1410. if (token = this.tokenizer.list(src)) {
  1411. src = src.substring(token.raw.length);
  1412. tokens.push(token);
  1413. continue;
  1414. }
  1415. // html
  1416. if (token = this.tokenizer.html(src)) {
  1417. src = src.substring(token.raw.length);
  1418. tokens.push(token);
  1419. continue;
  1420. }
  1421. // def
  1422. if (token = this.tokenizer.def(src)) {
  1423. src = src.substring(token.raw.length);
  1424. lastToken = tokens[tokens.length - 1];
  1425. if (lastToken && (lastToken.type === 'paragraph' || lastToken.type === 'text')) {
  1426. lastToken.raw += '\n' + token.raw;
  1427. lastToken.text += '\n' + token.raw;
  1428. this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
  1429. }
  1430. else if (!this.tokens.links[token.tag]) {
  1431. this.tokens.links[token.tag] = {
  1432. href: token.href,
  1433. title: token.title,
  1434. };
  1435. }
  1436. continue;
  1437. }
  1438. // table (gfm)
  1439. if (token = this.tokenizer.table(src)) {
  1440. src = src.substring(token.raw.length);
  1441. tokens.push(token);
  1442. continue;
  1443. }
  1444. // lheading
  1445. if (token = this.tokenizer.lheading(src)) {
  1446. src = src.substring(token.raw.length);
  1447. tokens.push(token);
  1448. continue;
  1449. }
  1450. // top-level paragraph
  1451. // prevent paragraph consuming extensions by clipping 'src' to extension start
  1452. cutSrc = src;
  1453. if (this.options.extensions && this.options.extensions.startBlock) {
  1454. let startIndex = Infinity;
  1455. const tempSrc = src.slice(1);
  1456. let tempStart;
  1457. this.options.extensions.startBlock.forEach((getStartIndex) => {
  1458. tempStart = getStartIndex.call({ lexer: this }, tempSrc);
  1459. if (typeof tempStart === 'number' && tempStart >= 0) {
  1460. startIndex = Math.min(startIndex, tempStart);
  1461. }
  1462. });
  1463. if (startIndex < Infinity && startIndex >= 0) {
  1464. cutSrc = src.substring(0, startIndex + 1);
  1465. }
  1466. }
  1467. if (this.state.top && (token = this.tokenizer.paragraph(cutSrc))) {
  1468. lastToken = tokens[tokens.length - 1];
  1469. if (lastParagraphClipped && lastToken?.type === 'paragraph') {
  1470. lastToken.raw += '\n' + token.raw;
  1471. lastToken.text += '\n' + token.text;
  1472. this.inlineQueue.pop();
  1473. this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
  1474. }
  1475. else {
  1476. tokens.push(token);
  1477. }
  1478. lastParagraphClipped = (cutSrc.length !== src.length);
  1479. src = src.substring(token.raw.length);
  1480. continue;
  1481. }
  1482. // text
  1483. if (token = this.tokenizer.text(src)) {
  1484. src = src.substring(token.raw.length);
  1485. lastToken = tokens[tokens.length - 1];
  1486. if (lastToken && lastToken.type === 'text') {
  1487. lastToken.raw += '\n' + token.raw;
  1488. lastToken.text += '\n' + token.text;
  1489. this.inlineQueue.pop();
  1490. this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
  1491. }
  1492. else {
  1493. tokens.push(token);
  1494. }
  1495. continue;
  1496. }
  1497. if (src) {
  1498. const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
  1499. if (this.options.silent) {
  1500. console.error(errMsg);
  1501. break;
  1502. }
  1503. else {
  1504. throw new Error(errMsg);
  1505. }
  1506. }
  1507. }
  1508. this.state.top = true;
  1509. return tokens;
  1510. }
  1511. inline(src, tokens = []) {
  1512. this.inlineQueue.push({ src, tokens });
  1513. return tokens;
  1514. }
  1515. /**
  1516. * Lexing/Compiling
  1517. */
  1518. inlineTokens(src, tokens = []) {
  1519. let token, lastToken, cutSrc;
  1520. // String with links masked to avoid interference with em and strong
  1521. let maskedSrc = src;
  1522. let match;
  1523. let keepPrevChar, prevChar;
  1524. // Mask out reflinks
  1525. if (this.tokens.links) {
  1526. const links = Object.keys(this.tokens.links);
  1527. if (links.length > 0) {
  1528. while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
  1529. if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
  1530. maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
  1531. }
  1532. }
  1533. }
  1534. }
  1535. // Mask out other blocks
  1536. while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
  1537. maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
  1538. }
  1539. // Mask out escaped characters
  1540. while ((match = this.tokenizer.rules.inline.anyPunctuation.exec(maskedSrc)) != null) {
  1541. maskedSrc = maskedSrc.slice(0, match.index) + '++' + maskedSrc.slice(this.tokenizer.rules.inline.anyPunctuation.lastIndex);
  1542. }
  1543. while (src) {
  1544. if (!keepPrevChar) {
  1545. prevChar = '';
  1546. }
  1547. keepPrevChar = false;
  1548. // extensions
  1549. if (this.options.extensions
  1550. && this.options.extensions.inline
  1551. && this.options.extensions.inline.some((extTokenizer) => {
  1552. if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
  1553. src = src.substring(token.raw.length);
  1554. tokens.push(token);
  1555. return true;
  1556. }
  1557. return false;
  1558. })) {
  1559. continue;
  1560. }
  1561. // escape
  1562. if (token = this.tokenizer.escape(src)) {
  1563. src = src.substring(token.raw.length);
  1564. tokens.push(token);
  1565. continue;
  1566. }
  1567. // tag
  1568. if (token = this.tokenizer.tag(src)) {
  1569. src = src.substring(token.raw.length);
  1570. lastToken = tokens[tokens.length - 1];
  1571. if (lastToken && token.type === 'text' && lastToken.type === 'text') {
  1572. lastToken.raw += token.raw;
  1573. lastToken.text += token.text;
  1574. }
  1575. else {
  1576. tokens.push(token);
  1577. }
  1578. continue;
  1579. }
  1580. // link
  1581. if (token = this.tokenizer.link(src)) {
  1582. src = src.substring(token.raw.length);
  1583. tokens.push(token);
  1584. continue;
  1585. }
  1586. // reflink, nolink
  1587. if (token = this.tokenizer.reflink(src, this.tokens.links)) {
  1588. src = src.substring(token.raw.length);
  1589. lastToken = tokens[tokens.length - 1];
  1590. if (lastToken && token.type === 'text' && lastToken.type === 'text') {
  1591. lastToken.raw += token.raw;
  1592. lastToken.text += token.text;
  1593. }
  1594. else {
  1595. tokens.push(token);
  1596. }
  1597. continue;
  1598. }
  1599. // em & strong
  1600. if (token = this.tokenizer.emStrong(src, maskedSrc, prevChar)) {
  1601. src = src.substring(token.raw.length);
  1602. tokens.push(token);
  1603. continue;
  1604. }
  1605. // code
  1606. if (token = this.tokenizer.codespan(src)) {
  1607. src = src.substring(token.raw.length);
  1608. tokens.push(token);
  1609. continue;
  1610. }
  1611. // br
  1612. if (token = this.tokenizer.br(src)) {
  1613. src = src.substring(token.raw.length);
  1614. tokens.push(token);
  1615. continue;
  1616. }
  1617. // del (gfm)
  1618. if (token = this.tokenizer.del(src)) {
  1619. src = src.substring(token.raw.length);
  1620. tokens.push(token);
  1621. continue;
  1622. }
  1623. // autolink
  1624. if (token = this.tokenizer.autolink(src)) {
  1625. src = src.substring(token.raw.length);
  1626. tokens.push(token);
  1627. continue;
  1628. }
  1629. // url (gfm)
  1630. if (!this.state.inLink && (token = this.tokenizer.url(src))) {
  1631. src = src.substring(token.raw.length);
  1632. tokens.push(token);
  1633. continue;
  1634. }
  1635. // text
  1636. // prevent inlineText consuming extensions by clipping 'src' to extension start
  1637. cutSrc = src;
  1638. if (this.options.extensions && this.options.extensions.startInline) {
  1639. let startIndex = Infinity;
  1640. const tempSrc = src.slice(1);
  1641. let tempStart;
  1642. this.options.extensions.startInline.forEach((getStartIndex) => {
  1643. tempStart = getStartIndex.call({ lexer: this }, tempSrc);
  1644. if (typeof tempStart === 'number' && tempStart >= 0) {
  1645. startIndex = Math.min(startIndex, tempStart);
  1646. }
  1647. });
  1648. if (startIndex < Infinity && startIndex >= 0) {
  1649. cutSrc = src.substring(0, startIndex + 1);
  1650. }
  1651. }
  1652. if (token = this.tokenizer.inlineText(cutSrc)) {
  1653. src = src.substring(token.raw.length);
  1654. if (token.raw.slice(-1) !== '_') { // Track prevChar before string of ____ started
  1655. prevChar = token.raw.slice(-1);
  1656. }
  1657. keepPrevChar = true;
  1658. lastToken = tokens[tokens.length - 1];
  1659. if (lastToken && lastToken.type === 'text') {
  1660. lastToken.raw += token.raw;
  1661. lastToken.text += token.text;
  1662. }
  1663. else {
  1664. tokens.push(token);
  1665. }
  1666. continue;
  1667. }
  1668. if (src) {
  1669. const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
  1670. if (this.options.silent) {
  1671. console.error(errMsg);
  1672. break;
  1673. }
  1674. else {
  1675. throw new Error(errMsg);
  1676. }
  1677. }
  1678. }
  1679. return tokens;
  1680. }
  1681. }
  1682. /**
  1683. * Renderer
  1684. */
  1685. class _Renderer {
  1686. options;
  1687. parser; // set by the parser
  1688. constructor(options) {
  1689. this.options = options || exports.defaults;
  1690. }
  1691. space(token) {
  1692. return '';
  1693. }
  1694. code({ text, lang, escaped }) {
  1695. const langString = (lang || '').match(/^\S*/)?.[0];
  1696. const code = text.replace(/\n$/, '') + '\n';
  1697. if (!langString) {
  1698. return '<pre><code>'
  1699. + (escaped ? code : escape$1(code, true))
  1700. + '</code></pre>\n';
  1701. }
  1702. return '<pre><code class="language-'
  1703. + escape$1(langString)
  1704. + '">'
  1705. + (escaped ? code : escape$1(code, true))
  1706. + '</code></pre>\n';
  1707. }
  1708. blockquote({ tokens }) {
  1709. const body = this.parser.parse(tokens);
  1710. return `<blockquote>\n${body}</blockquote>\n`;
  1711. }
  1712. html({ text }) {
  1713. return text;
  1714. }
  1715. heading({ tokens, depth }) {
  1716. return `<h${depth}>${this.parser.parseInline(tokens)}</h${depth}>\n`;
  1717. }
  1718. hr(token) {
  1719. return '<hr>\n';
  1720. }
  1721. list(token) {
  1722. const ordered = token.ordered;
  1723. const start = token.start;
  1724. let body = '';
  1725. for (let j = 0; j < token.items.length; j++) {
  1726. const item = token.items[j];
  1727. body += this.listitem(item);
  1728. }
  1729. const type = ordered ? 'ol' : 'ul';
  1730. const startAttr = (ordered && start !== 1) ? (' start="' + start + '"') : '';
  1731. return '<' + type + startAttr + '>\n' + body + '</' + type + '>\n';
  1732. }
  1733. listitem(item) {
  1734. let itemBody = '';
  1735. if (item.task) {
  1736. const checkbox = this.checkbox({ checked: !!item.checked });
  1737. if (item.loose) {
  1738. if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') {
  1739. item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
  1740. if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
  1741. item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
  1742. }
  1743. }
  1744. else {
  1745. item.tokens.unshift({
  1746. type: 'text',
  1747. raw: checkbox + ' ',
  1748. text: checkbox + ' ',
  1749. });
  1750. }
  1751. }
  1752. else {
  1753. itemBody += checkbox + ' ';
  1754. }
  1755. }
  1756. itemBody += this.parser.parse(item.tokens, !!item.loose);
  1757. return `<li>${itemBody}</li>\n`;
  1758. }
  1759. checkbox({ checked }) {
  1760. return '<input '
  1761. + (checked ? 'checked="" ' : '')
  1762. + 'disabled="" type="checkbox">';
  1763. }
  1764. paragraph({ tokens }) {
  1765. return `<p>${this.parser.parseInline(tokens)}</p>\n`;
  1766. }
  1767. table(token) {
  1768. let header = '';
  1769. // header
  1770. let cell = '';
  1771. for (let j = 0; j < token.header.length; j++) {
  1772. cell += this.tablecell(token.header[j]);
  1773. }
  1774. header += this.tablerow({ text: cell });
  1775. let body = '';
  1776. for (let j = 0; j < token.rows.length; j++) {
  1777. const row = token.rows[j];
  1778. cell = '';
  1779. for (let k = 0; k < row.length; k++) {
  1780. cell += this.tablecell(row[k]);
  1781. }
  1782. body += this.tablerow({ text: cell });
  1783. }
  1784. if (body)
  1785. body = `<tbody>${body}</tbody>`;
  1786. return '<table>\n'
  1787. + '<thead>\n'
  1788. + header
  1789. + '</thead>\n'
  1790. + body
  1791. + '</table>\n';
  1792. }
  1793. tablerow({ text }) {
  1794. return `<tr>\n${text}</tr>\n`;
  1795. }
  1796. tablecell(token) {
  1797. const content = this.parser.parseInline(token.tokens);
  1798. const type = token.header ? 'th' : 'td';
  1799. const tag = token.align
  1800. ? `<${type} align="${token.align}">`
  1801. : `<${type}>`;
  1802. return tag + content + `</${type}>\n`;
  1803. }
  1804. /**
  1805. * span level renderer
  1806. */
  1807. strong({ tokens }) {
  1808. return `<strong>${this.parser.parseInline(tokens)}</strong>`;
  1809. }
  1810. em({ tokens }) {
  1811. return `<em>${this.parser.parseInline(tokens)}</em>`;
  1812. }
  1813. codespan({ text }) {
  1814. return `<code>${text}</code>`;
  1815. }
  1816. br(token) {
  1817. return '<br>';
  1818. }
  1819. del({ tokens }) {
  1820. return `<del>${this.parser.parseInline(tokens)}</del>`;
  1821. }
  1822. link({ href, title, tokens }) {
  1823. const text = this.parser.parseInline(tokens);
  1824. const cleanHref = cleanUrl(href);
  1825. if (cleanHref === null) {
  1826. return text;
  1827. }
  1828. href = cleanHref;
  1829. let out = '<a href="' + href + '"';
  1830. if (title) {
  1831. out += ' title="' + title + '"';
  1832. }
  1833. out += '>' + text + '</a>';
  1834. return out;
  1835. }
  1836. image({ href, title, text }) {
  1837. const cleanHref = cleanUrl(href);
  1838. if (cleanHref === null) {
  1839. return text;
  1840. }
  1841. href = cleanHref;
  1842. let out = `<img src="${href}" alt="${text}"`;
  1843. if (title) {
  1844. out += ` title="${title}"`;
  1845. }
  1846. out += '>';
  1847. return out;
  1848. }
  1849. text(token) {
  1850. return 'tokens' in token && token.tokens ? this.parser.parseInline(token.tokens) : token.text;
  1851. }
  1852. }
  1853. /**
  1854. * TextRenderer
  1855. * returns only the textual part of the token
  1856. */
  1857. class _TextRenderer {
  1858. // no need for block level renderers
  1859. strong({ text }) {
  1860. return text;
  1861. }
  1862. em({ text }) {
  1863. return text;
  1864. }
  1865. codespan({ text }) {
  1866. return text;
  1867. }
  1868. del({ text }) {
  1869. return text;
  1870. }
  1871. html({ text }) {
  1872. return text;
  1873. }
  1874. text({ text }) {
  1875. return text;
  1876. }
  1877. link({ text }) {
  1878. return '' + text;
  1879. }
  1880. image({ text }) {
  1881. return '' + text;
  1882. }
  1883. br() {
  1884. return '';
  1885. }
  1886. }
  1887. /**
  1888. * Parsing & Compiling
  1889. */
  1890. class _Parser {
  1891. options;
  1892. renderer;
  1893. textRenderer;
  1894. constructor(options) {
  1895. this.options = options || exports.defaults;
  1896. this.options.renderer = this.options.renderer || new _Renderer();
  1897. this.renderer = this.options.renderer;
  1898. this.renderer.options = this.options;
  1899. this.renderer.parser = this;
  1900. this.textRenderer = new _TextRenderer();
  1901. }
  1902. /**
  1903. * Static Parse Method
  1904. */
  1905. static parse(tokens, options) {
  1906. const parser = new _Parser(options);
  1907. return parser.parse(tokens);
  1908. }
  1909. /**
  1910. * Static Parse Inline Method
  1911. */
  1912. static parseInline(tokens, options) {
  1913. const parser = new _Parser(options);
  1914. return parser.parseInline(tokens);
  1915. }
  1916. /**
  1917. * Parse Loop
  1918. */
  1919. parse(tokens, top = true) {
  1920. let out = '';
  1921. for (let i = 0; i < tokens.length; i++) {
  1922. const anyToken = tokens[i];
  1923. // Run any renderer extensions
  1924. if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[anyToken.type]) {
  1925. const genericToken = anyToken;
  1926. const ret = this.options.extensions.renderers[genericToken.type].call({ parser: this }, genericToken);
  1927. if (ret !== false || !['space', 'hr', 'heading', 'code', 'table', 'blockquote', 'list', 'html', 'paragraph', 'text'].includes(genericToken.type)) {
  1928. out += ret || '';
  1929. continue;
  1930. }
  1931. }
  1932. const token = anyToken;
  1933. switch (token.type) {
  1934. case 'space': {
  1935. out += this.renderer.space(token);
  1936. continue;
  1937. }
  1938. case 'hr': {
  1939. out += this.renderer.hr(token);
  1940. continue;
  1941. }
  1942. case 'heading': {
  1943. out += this.renderer.heading(token);
  1944. continue;
  1945. }
  1946. case 'code': {
  1947. out += this.renderer.code(token);
  1948. continue;
  1949. }
  1950. case 'table': {
  1951. out += this.renderer.table(token);
  1952. continue;
  1953. }
  1954. case 'blockquote': {
  1955. out += this.renderer.blockquote(token);
  1956. continue;
  1957. }
  1958. case 'list': {
  1959. out += this.renderer.list(token);
  1960. continue;
  1961. }
  1962. case 'html': {
  1963. out += this.renderer.html(token);
  1964. continue;
  1965. }
  1966. case 'paragraph': {
  1967. out += this.renderer.paragraph(token);
  1968. continue;
  1969. }
  1970. case 'text': {
  1971. let textToken = token;
  1972. let body = this.renderer.text(textToken);
  1973. while (i + 1 < tokens.length && tokens[i + 1].type === 'text') {
  1974. textToken = tokens[++i];
  1975. body += '\n' + this.renderer.text(textToken);
  1976. }
  1977. if (top) {
  1978. out += this.renderer.paragraph({
  1979. type: 'paragraph',
  1980. raw: body,
  1981. text: body,
  1982. tokens: [{ type: 'text', raw: body, text: body }],
  1983. });
  1984. }
  1985. else {
  1986. out += body;
  1987. }
  1988. continue;
  1989. }
  1990. default: {
  1991. const errMsg = 'Token with "' + token.type + '" type was not found.';
  1992. if (this.options.silent) {
  1993. console.error(errMsg);
  1994. return '';
  1995. }
  1996. else {
  1997. throw new Error(errMsg);
  1998. }
  1999. }
  2000. }
  2001. }
  2002. return out;
  2003. }
  2004. /**
  2005. * Parse Inline Tokens
  2006. */
  2007. parseInline(tokens, renderer) {
  2008. renderer = renderer || this.renderer;
  2009. let out = '';
  2010. for (let i = 0; i < tokens.length; i++) {
  2011. const anyToken = tokens[i];
  2012. // Run any renderer extensions
  2013. if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[anyToken.type]) {
  2014. const ret = this.options.extensions.renderers[anyToken.type].call({ parser: this }, anyToken);
  2015. if (ret !== false || !['escape', 'html', 'link', 'image', 'strong', 'em', 'codespan', 'br', 'del', 'text'].includes(anyToken.type)) {
  2016. out += ret || '';
  2017. continue;
  2018. }
  2019. }
  2020. const token = anyToken;
  2021. switch (token.type) {
  2022. case 'escape': {
  2023. out += renderer.text(token);
  2024. break;
  2025. }
  2026. case 'html': {
  2027. out += renderer.html(token);
  2028. break;
  2029. }
  2030. case 'link': {
  2031. out += renderer.link(token);
  2032. break;
  2033. }
  2034. case 'image': {
  2035. out += renderer.image(token);
  2036. break;
  2037. }
  2038. case 'strong': {
  2039. out += renderer.strong(token);
  2040. break;
  2041. }
  2042. case 'em': {
  2043. out += renderer.em(token);
  2044. break;
  2045. }
  2046. case 'codespan': {
  2047. out += renderer.codespan(token);
  2048. break;
  2049. }
  2050. case 'br': {
  2051. out += renderer.br(token);
  2052. break;
  2053. }
  2054. case 'del': {
  2055. out += renderer.del(token);
  2056. break;
  2057. }
  2058. case 'text': {
  2059. out += renderer.text(token);
  2060. break;
  2061. }
  2062. default: {
  2063. const errMsg = 'Token with "' + token.type + '" type was not found.';
  2064. if (this.options.silent) {
  2065. console.error(errMsg);
  2066. return '';
  2067. }
  2068. else {
  2069. throw new Error(errMsg);
  2070. }
  2071. }
  2072. }
  2073. }
  2074. return out;
  2075. }
  2076. }
  2077. class _Hooks {
  2078. options;
  2079. block;
  2080. constructor(options) {
  2081. this.options = options || exports.defaults;
  2082. }
  2083. static passThroughHooks = new Set([
  2084. 'preprocess',
  2085. 'postprocess',
  2086. 'processAllTokens',
  2087. ]);
  2088. /**
  2089. * Process markdown before marked
  2090. */
  2091. preprocess(markdown) {
  2092. return markdown;
  2093. }
  2094. /**
  2095. * Process HTML after marked is finished
  2096. */
  2097. postprocess(html) {
  2098. return html;
  2099. }
  2100. /**
  2101. * Process all tokens before walk tokens
  2102. */
  2103. processAllTokens(tokens) {
  2104. return tokens;
  2105. }
  2106. /**
  2107. * Provide function to tokenize markdown
  2108. */
  2109. provideLexer() {
  2110. return this.block ? _Lexer.lex : _Lexer.lexInline;
  2111. }
  2112. /**
  2113. * Provide function to parse tokens
  2114. */
  2115. provideParser() {
  2116. return this.block ? _Parser.parse : _Parser.parseInline;
  2117. }
  2118. }
  2119. class Marked {
  2120. defaults = _getDefaults();
  2121. options = this.setOptions;
  2122. parse = this.parseMarkdown(true);
  2123. parseInline = this.parseMarkdown(false);
  2124. Parser = _Parser;
  2125. Renderer = _Renderer;
  2126. TextRenderer = _TextRenderer;
  2127. Lexer = _Lexer;
  2128. Tokenizer = _Tokenizer;
  2129. Hooks = _Hooks;
  2130. constructor(...args) {
  2131. this.use(...args);
  2132. }
  2133. /**
  2134. * Run callback for every token
  2135. */
  2136. walkTokens(tokens, callback) {
  2137. let values = [];
  2138. for (const token of tokens) {
  2139. values = values.concat(callback.call(this, token));
  2140. switch (token.type) {
  2141. case 'table': {
  2142. const tableToken = token;
  2143. for (const cell of tableToken.header) {
  2144. values = values.concat(this.walkTokens(cell.tokens, callback));
  2145. }
  2146. for (const row of tableToken.rows) {
  2147. for (const cell of row) {
  2148. values = values.concat(this.walkTokens(cell.tokens, callback));
  2149. }
  2150. }
  2151. break;
  2152. }
  2153. case 'list': {
  2154. const listToken = token;
  2155. values = values.concat(this.walkTokens(listToken.items, callback));
  2156. break;
  2157. }
  2158. default: {
  2159. const genericToken = token;
  2160. if (this.defaults.extensions?.childTokens?.[genericToken.type]) {
  2161. this.defaults.extensions.childTokens[genericToken.type].forEach((childTokens) => {
  2162. const tokens = genericToken[childTokens].flat(Infinity);
  2163. values = values.concat(this.walkTokens(tokens, callback));
  2164. });
  2165. }
  2166. else if (genericToken.tokens) {
  2167. values = values.concat(this.walkTokens(genericToken.tokens, callback));
  2168. }
  2169. }
  2170. }
  2171. }
  2172. return values;
  2173. }
  2174. use(...args) {
  2175. const extensions = this.defaults.extensions || { renderers: {}, childTokens: {} };
  2176. args.forEach((pack) => {
  2177. // copy options to new object
  2178. const opts = { ...pack };
  2179. // set async to true if it was set to true before
  2180. opts.async = this.defaults.async || opts.async || false;
  2181. // ==-- Parse "addon" extensions --== //
  2182. if (pack.extensions) {
  2183. pack.extensions.forEach((ext) => {
  2184. if (!ext.name) {
  2185. throw new Error('extension name required');
  2186. }
  2187. if ('renderer' in ext) { // Renderer extensions
  2188. const prevRenderer = extensions.renderers[ext.name];
  2189. if (prevRenderer) {
  2190. // Replace extension with func to run new extension but fall back if false
  2191. extensions.renderers[ext.name] = function (...args) {
  2192. let ret = ext.renderer.apply(this, args);
  2193. if (ret === false) {
  2194. ret = prevRenderer.apply(this, args);
  2195. }
  2196. return ret;
  2197. };
  2198. }
  2199. else {
  2200. extensions.renderers[ext.name] = ext.renderer;
  2201. }
  2202. }
  2203. if ('tokenizer' in ext) { // Tokenizer Extensions
  2204. if (!ext.level || (ext.level !== 'block' && ext.level !== 'inline')) {
  2205. throw new Error("extension level must be 'block' or 'inline'");
  2206. }
  2207. const extLevel = extensions[ext.level];
  2208. if (extLevel) {
  2209. extLevel.unshift(ext.tokenizer);
  2210. }
  2211. else {
  2212. extensions[ext.level] = [ext.tokenizer];
  2213. }
  2214. if (ext.start) { // Function to check for start of token
  2215. if (ext.level === 'block') {
  2216. if (extensions.startBlock) {
  2217. extensions.startBlock.push(ext.start);
  2218. }
  2219. else {
  2220. extensions.startBlock = [ext.start];
  2221. }
  2222. }
  2223. else if (ext.level === 'inline') {
  2224. if (extensions.startInline) {
  2225. extensions.startInline.push(ext.start);
  2226. }
  2227. else {
  2228. extensions.startInline = [ext.start];
  2229. }
  2230. }
  2231. }
  2232. }
  2233. if ('childTokens' in ext && ext.childTokens) { // Child tokens to be visited by walkTokens
  2234. extensions.childTokens[ext.name] = ext.childTokens;
  2235. }
  2236. });
  2237. opts.extensions = extensions;
  2238. }
  2239. // ==-- Parse "overwrite" extensions --== //
  2240. if (pack.renderer) {
  2241. const renderer = this.defaults.renderer || new _Renderer(this.defaults);
  2242. for (const prop in pack.renderer) {
  2243. if (!(prop in renderer)) {
  2244. throw new Error(`renderer '${prop}' does not exist`);
  2245. }
  2246. if (['options', 'parser'].includes(prop)) {
  2247. // ignore options property
  2248. continue;
  2249. }
  2250. const rendererProp = prop;
  2251. const rendererFunc = pack.renderer[rendererProp];
  2252. const prevRenderer = renderer[rendererProp];
  2253. // Replace renderer with func to run extension, but fall back if false
  2254. renderer[rendererProp] = (...args) => {
  2255. let ret = rendererFunc.apply(renderer, args);
  2256. if (ret === false) {
  2257. ret = prevRenderer.apply(renderer, args);
  2258. }
  2259. return ret || '';
  2260. };
  2261. }
  2262. opts.renderer = renderer;
  2263. }
  2264. if (pack.tokenizer) {
  2265. const tokenizer = this.defaults.tokenizer || new _Tokenizer(this.defaults);
  2266. for (const prop in pack.tokenizer) {
  2267. if (!(prop in tokenizer)) {
  2268. throw new Error(`tokenizer '${prop}' does not exist`);
  2269. }
  2270. if (['options', 'rules', 'lexer'].includes(prop)) {
  2271. // ignore options, rules, and lexer properties
  2272. continue;
  2273. }
  2274. const tokenizerProp = prop;
  2275. const tokenizerFunc = pack.tokenizer[tokenizerProp];
  2276. const prevTokenizer = tokenizer[tokenizerProp];
  2277. // Replace tokenizer with func to run extension, but fall back if false
  2278. // @ts-expect-error cannot type tokenizer function dynamically
  2279. tokenizer[tokenizerProp] = (...args) => {
  2280. let ret = tokenizerFunc.apply(tokenizer, args);
  2281. if (ret === false) {
  2282. ret = prevTokenizer.apply(tokenizer, args);
  2283. }
  2284. return ret;
  2285. };
  2286. }
  2287. opts.tokenizer = tokenizer;
  2288. }
  2289. // ==-- Parse Hooks extensions --== //
  2290. if (pack.hooks) {
  2291. const hooks = this.defaults.hooks || new _Hooks();
  2292. for (const prop in pack.hooks) {
  2293. if (!(prop in hooks)) {
  2294. throw new Error(`hook '${prop}' does not exist`);
  2295. }
  2296. if (['options', 'block'].includes(prop)) {
  2297. // ignore options and block properties
  2298. continue;
  2299. }
  2300. const hooksProp = prop;
  2301. const hooksFunc = pack.hooks[hooksProp];
  2302. const prevHook = hooks[hooksProp];
  2303. if (_Hooks.passThroughHooks.has(prop)) {
  2304. // @ts-expect-error cannot type hook function dynamically
  2305. hooks[hooksProp] = (arg) => {
  2306. if (this.defaults.async) {
  2307. return Promise.resolve(hooksFunc.call(hooks, arg)).then(ret => {
  2308. return prevHook.call(hooks, ret);
  2309. });
  2310. }
  2311. const ret = hooksFunc.call(hooks, arg);
  2312. return prevHook.call(hooks, ret);
  2313. };
  2314. }
  2315. else {
  2316. // @ts-expect-error cannot type hook function dynamically
  2317. hooks[hooksProp] = (...args) => {
  2318. let ret = hooksFunc.apply(hooks, args);
  2319. if (ret === false) {
  2320. ret = prevHook.apply(hooks, args);
  2321. }
  2322. return ret;
  2323. };
  2324. }
  2325. }
  2326. opts.hooks = hooks;
  2327. }
  2328. // ==-- Parse WalkTokens extensions --== //
  2329. if (pack.walkTokens) {
  2330. const walkTokens = this.defaults.walkTokens;
  2331. const packWalktokens = pack.walkTokens;
  2332. opts.walkTokens = function (token) {
  2333. let values = [];
  2334. values.push(packWalktokens.call(this, token));
  2335. if (walkTokens) {
  2336. values = values.concat(walkTokens.call(this, token));
  2337. }
  2338. return values;
  2339. };
  2340. }
  2341. this.defaults = { ...this.defaults, ...opts };
  2342. });
  2343. return this;
  2344. }
  2345. setOptions(opt) {
  2346. this.defaults = { ...this.defaults, ...opt };
  2347. return this;
  2348. }
  2349. lexer(src, options) {
  2350. return _Lexer.lex(src, options ?? this.defaults);
  2351. }
  2352. parser(tokens, options) {
  2353. return _Parser.parse(tokens, options ?? this.defaults);
  2354. }
  2355. parseMarkdown(blockType) {
  2356. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2357. const parse = (src, options) => {
  2358. const origOpt = { ...options };
  2359. const opt = { ...this.defaults, ...origOpt };
  2360. const throwError = this.onError(!!opt.silent, !!opt.async);
  2361. // throw error if an extension set async to true but parse was called with async: false
  2362. if (this.defaults.async === true && origOpt.async === false) {
  2363. return throwError(new Error('marked(): The async option was set to true by an extension. Remove async: false from the parse options object to return a Promise.'));
  2364. }
  2365. // throw error in case of non string input
  2366. if (typeof src === 'undefined' || src === null) {
  2367. return throwError(new Error('marked(): input parameter is undefined or null'));
  2368. }
  2369. if (typeof src !== 'string') {
  2370. return throwError(new Error('marked(): input parameter is of type '
  2371. + Object.prototype.toString.call(src) + ', string expected'));
  2372. }
  2373. if (opt.hooks) {
  2374. opt.hooks.options = opt;
  2375. opt.hooks.block = blockType;
  2376. }
  2377. const lexer = opt.hooks ? opt.hooks.provideLexer() : (blockType ? _Lexer.lex : _Lexer.lexInline);
  2378. const parser = opt.hooks ? opt.hooks.provideParser() : (blockType ? _Parser.parse : _Parser.parseInline);
  2379. if (opt.async) {
  2380. return Promise.resolve(opt.hooks ? opt.hooks.preprocess(src) : src)
  2381. .then(src => lexer(src, opt))
  2382. .then(tokens => opt.hooks ? opt.hooks.processAllTokens(tokens) : tokens)
  2383. .then(tokens => opt.walkTokens ? Promise.all(this.walkTokens(tokens, opt.walkTokens)).then(() => tokens) : tokens)
  2384. .then(tokens => parser(tokens, opt))
  2385. .then(html => opt.hooks ? opt.hooks.postprocess(html) : html)
  2386. .catch(throwError);
  2387. }
  2388. try {
  2389. if (opt.hooks) {
  2390. src = opt.hooks.preprocess(src);
  2391. }
  2392. let tokens = lexer(src, opt);
  2393. if (opt.hooks) {
  2394. tokens = opt.hooks.processAllTokens(tokens);
  2395. }
  2396. if (opt.walkTokens) {
  2397. this.walkTokens(tokens, opt.walkTokens);
  2398. }
  2399. let html = parser(tokens, opt);
  2400. if (opt.hooks) {
  2401. html = opt.hooks.postprocess(html);
  2402. }
  2403. return html;
  2404. }
  2405. catch (e) {
  2406. return throwError(e);
  2407. }
  2408. };
  2409. return parse;
  2410. }
  2411. onError(silent, async) {
  2412. return (e) => {
  2413. e.message += '\nPlease report this to https://github.com/markedjs/marked.';
  2414. if (silent) {
  2415. const msg = '<p>An error occurred:</p><pre>'
  2416. + escape$1(e.message + '', true)
  2417. + '</pre>';
  2418. if (async) {
  2419. return Promise.resolve(msg);
  2420. }
  2421. return msg;
  2422. }
  2423. if (async) {
  2424. return Promise.reject(e);
  2425. }
  2426. throw e;
  2427. };
  2428. }
  2429. }
  2430. const markedInstance = new Marked();
  2431. function marked(src, opt) {
  2432. return markedInstance.parse(src, opt);
  2433. }
  2434. /**
  2435. * Sets the default options.
  2436. *
  2437. * @param options Hash of options
  2438. */
  2439. marked.options =
  2440. marked.setOptions = function (options) {
  2441. markedInstance.setOptions(options);
  2442. marked.defaults = markedInstance.defaults;
  2443. changeDefaults(marked.defaults);
  2444. return marked;
  2445. };
  2446. /**
  2447. * Gets the original marked default options.
  2448. */
  2449. marked.getDefaults = _getDefaults;
  2450. marked.defaults = exports.defaults;
  2451. /**
  2452. * Use Extension
  2453. */
  2454. marked.use = function (...args) {
  2455. markedInstance.use(...args);
  2456. marked.defaults = markedInstance.defaults;
  2457. changeDefaults(marked.defaults);
  2458. return marked;
  2459. };
  2460. /**
  2461. * Run callback for every token
  2462. */
  2463. marked.walkTokens = function (tokens, callback) {
  2464. return markedInstance.walkTokens(tokens, callback);
  2465. };
  2466. /**
  2467. * Compiles markdown to HTML without enclosing `p` tag.
  2468. *
  2469. * @param src String of markdown source to be compiled
  2470. * @param options Hash of options
  2471. * @return String of compiled HTML
  2472. */
  2473. marked.parseInline = markedInstance.parseInline;
  2474. /**
  2475. * Expose
  2476. */
  2477. marked.Parser = _Parser;
  2478. marked.parser = _Parser.parse;
  2479. marked.Renderer = _Renderer;
  2480. marked.TextRenderer = _TextRenderer;
  2481. marked.Lexer = _Lexer;
  2482. marked.lexer = _Lexer.lex;
  2483. marked.Tokenizer = _Tokenizer;
  2484. marked.Hooks = _Hooks;
  2485. marked.parse = marked;
  2486. const options = marked.options;
  2487. const setOptions = marked.setOptions;
  2488. const use = marked.use;
  2489. const walkTokens = marked.walkTokens;
  2490. const parseInline = marked.parseInline;
  2491. const parse = marked;
  2492. const parser = _Parser.parse;
  2493. const lexer = _Lexer.lex;
  2494. exports.Hooks = _Hooks;
  2495. exports.Lexer = _Lexer;
  2496. exports.Marked = Marked;
  2497. exports.Parser = _Parser;
  2498. exports.Renderer = _Renderer;
  2499. exports.TextRenderer = _TextRenderer;
  2500. exports.Tokenizer = _Tokenizer;
  2501. exports.getDefaults = _getDefaults;
  2502. exports.lexer = lexer;
  2503. exports.marked = marked;
  2504. exports.options = options;
  2505. exports.parse = parse;
  2506. exports.parseInline = parseInline;
  2507. exports.parser = parser;
  2508. exports.setOptions = setOptions;
  2509. exports.use = use;
  2510. exports.walkTokens = walkTokens;
  2511. }));
  2512. //# sourceMappingURL=marked.umd.js.map