marked.esm.js 63 KB

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