marked.esm.js 67 KB

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