marked.esm.js 74 KB

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