marked.cjs 96 KB

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