marked.esm.js 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488
  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. + ' *([-:]+ *\\|[-| :]*)' // Align
  946. + '(?:\\n((?:(?!\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)', // Cells
  947. table: '^ *\\|(.+)\\n' // Header
  948. + ' *\\|?( *[-:]+[-| :]*)' // 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: /^(`+|[^`])(?:[\s\S]*?(?:(?=[\\<!\[`*]|\b_|$)|[^ ](?= {2,}\n))|(?= {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.em.start = edit$1(inline.em.start)
  1041. .replace(/punctuation/g, inline._punctuation)
  1042. .getRegex();
  1043. inline.em.middle = edit$1(inline.em.middle)
  1044. .replace(/punctuation/g, inline._punctuation)
  1045. .replace(/overlapSkip/g, inline._overlapSkip)
  1046. .getRegex();
  1047. inline.em.endAst = edit$1(inline.em.endAst, 'g')
  1048. .replace(/punctuation/g, inline._punctuation)
  1049. .getRegex();
  1050. inline.em.endUnd = edit$1(inline.em.endUnd, 'g')
  1051. .replace(/punctuation/g, inline._punctuation)
  1052. .getRegex();
  1053. inline.strong.start = edit$1(inline.strong.start)
  1054. .replace(/punctuation/g, inline._punctuation)
  1055. .getRegex();
  1056. inline.strong.middle = edit$1(inline.strong.middle)
  1057. .replace(/punctuation/g, inline._punctuation)
  1058. .replace(/blockSkip/g, inline._blockSkip)
  1059. .getRegex();
  1060. inline.strong.endAst = edit$1(inline.strong.endAst, 'g')
  1061. .replace(/punctuation/g, inline._punctuation)
  1062. .getRegex();
  1063. inline.strong.endUnd = edit$1(inline.strong.endUnd, 'g')
  1064. .replace(/punctuation/g, inline._punctuation)
  1065. .getRegex();
  1066. inline.blockSkip = edit$1(inline._blockSkip, 'g')
  1067. .getRegex();
  1068. inline.overlapSkip = edit$1(inline._overlapSkip, 'g')
  1069. .getRegex();
  1070. inline._escapes = /\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/g;
  1071. inline._scheme = /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/;
  1072. 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])?)+(?![-_])/;
  1073. inline.autolink = edit$1(inline.autolink)
  1074. .replace('scheme', inline._scheme)
  1075. .replace('email', inline._email)
  1076. .getRegex();
  1077. inline._attribute = /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/;
  1078. inline.tag = edit$1(inline.tag)
  1079. .replace('comment', block._comment)
  1080. .replace('attribute', inline._attribute)
  1081. .getRegex();
  1082. inline._label = /(?:\[(?:\\.|[^\[\]\\])*\]|\\.|`[^`]*`|[^\[\]\\`])*?/;
  1083. inline._href = /<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*/;
  1084. inline._title = /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/;
  1085. inline.link = edit$1(inline.link)
  1086. .replace('label', inline._label)
  1087. .replace('href', inline._href)
  1088. .replace('title', inline._title)
  1089. .getRegex();
  1090. inline.reflink = edit$1(inline.reflink)
  1091. .replace('label', inline._label)
  1092. .getRegex();
  1093. inline.reflinkSearch = edit$1(inline.reflinkSearch, 'g')
  1094. .replace('reflink', inline.reflink)
  1095. .replace('nolink', inline.nolink)
  1096. .getRegex();
  1097. /**
  1098. * Normal Inline Grammar
  1099. */
  1100. inline.normal = merge$1({}, inline);
  1101. /**
  1102. * Pedantic Inline Grammar
  1103. */
  1104. inline.pedantic = merge$1({}, inline.normal, {
  1105. strong: {
  1106. start: /^__|\*\*/,
  1107. middle: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
  1108. endAst: /\*\*(?!\*)/g,
  1109. endUnd: /__(?!_)/g
  1110. },
  1111. em: {
  1112. start: /^_|\*/,
  1113. middle: /^()\*(?=\S)([\s\S]*?\S)\*(?!\*)|^_(?=\S)([\s\S]*?\S)_(?!_)/,
  1114. endAst: /\*(?!\*)/g,
  1115. endUnd: /_(?!_)/g
  1116. },
  1117. link: edit$1(/^!?\[(label)\]\((.*?)\)/)
  1118. .replace('label', inline._label)
  1119. .getRegex(),
  1120. reflink: edit$1(/^!?\[(label)\]\s*\[([^\]]*)\]/)
  1121. .replace('label', inline._label)
  1122. .getRegex()
  1123. });
  1124. /**
  1125. * GFM Inline Grammar
  1126. */
  1127. inline.gfm = merge$1({}, inline.normal, {
  1128. escape: edit$1(inline.escape).replace('])', '~|])').getRegex(),
  1129. _extended_email: /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/,
  1130. url: /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,
  1131. _backpedal: /(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/,
  1132. del: /^~+(?=\S)([\s\S]*?\S)~+/,
  1133. text: /^(`+|[^`])(?:[\s\S]*?(?:(?=[\\<!\[`*~]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@))|(?= {2,}\n|[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@))/
  1134. });
  1135. inline.gfm.url = edit$1(inline.gfm.url, 'i')
  1136. .replace('email', inline.gfm._extended_email)
  1137. .getRegex();
  1138. /**
  1139. * GFM + Line Breaks Inline Grammar
  1140. */
  1141. inline.breaks = merge$1({}, inline.gfm, {
  1142. br: edit$1(inline.br).replace('{2,}', '*').getRegex(),
  1143. text: edit$1(inline.gfm.text)
  1144. .replace('\\b_', '\\b_| {2,}\\n')
  1145. .replace(/\{2,\}/g, '*')
  1146. .getRegex()
  1147. });
  1148. var rules = {
  1149. block,
  1150. inline
  1151. };
  1152. const { defaults: defaults$2 } = defaults;
  1153. const { block: block$1, inline: inline$1 } = rules;
  1154. /**
  1155. * smartypants text replacement
  1156. */
  1157. function smartypants(text) {
  1158. return text
  1159. // em-dashes
  1160. .replace(/---/g, '\u2014')
  1161. // en-dashes
  1162. .replace(/--/g, '\u2013')
  1163. // opening singles
  1164. .replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018')
  1165. // closing singles & apostrophes
  1166. .replace(/'/g, '\u2019')
  1167. // opening doubles
  1168. .replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c')
  1169. // closing doubles
  1170. .replace(/"/g, '\u201d')
  1171. // ellipses
  1172. .replace(/\.{3}/g, '\u2026');
  1173. }
  1174. /**
  1175. * mangle email addresses
  1176. */
  1177. function mangle(text) {
  1178. let out = '',
  1179. i,
  1180. ch;
  1181. const l = text.length;
  1182. for (i = 0; i < l; i++) {
  1183. ch = text.charCodeAt(i);
  1184. if (Math.random() > 0.5) {
  1185. ch = 'x' + ch.toString(16);
  1186. }
  1187. out += '&#' + ch + ';';
  1188. }
  1189. return out;
  1190. }
  1191. /**
  1192. * Block Lexer
  1193. */
  1194. var Lexer_1 = class Lexer {
  1195. constructor(options) {
  1196. this.tokens = [];
  1197. this.tokens.links = Object.create(null);
  1198. this.options = options || defaults$2;
  1199. this.options.tokenizer = this.options.tokenizer || new Tokenizer_1();
  1200. this.tokenizer = this.options.tokenizer;
  1201. this.tokenizer.options = this.options;
  1202. const rules = {
  1203. block: block$1.normal,
  1204. inline: inline$1.normal
  1205. };
  1206. if (this.options.pedantic) {
  1207. rules.block = block$1.pedantic;
  1208. rules.inline = inline$1.pedantic;
  1209. } else if (this.options.gfm) {
  1210. rules.block = block$1.gfm;
  1211. if (this.options.breaks) {
  1212. rules.inline = inline$1.breaks;
  1213. } else {
  1214. rules.inline = inline$1.gfm;
  1215. }
  1216. }
  1217. this.tokenizer.rules = rules;
  1218. }
  1219. /**
  1220. * Expose Rules
  1221. */
  1222. static get rules() {
  1223. return {
  1224. block: block$1,
  1225. inline: inline$1
  1226. };
  1227. }
  1228. /**
  1229. * Static Lex Method
  1230. */
  1231. static lex(src, options) {
  1232. const lexer = new Lexer(options);
  1233. return lexer.lex(src);
  1234. }
  1235. /**
  1236. * Preprocessing
  1237. */
  1238. lex(src) {
  1239. src = src
  1240. .replace(/\r\n|\r/g, '\n')
  1241. .replace(/\t/g, ' ');
  1242. this.blockTokens(src, this.tokens, true);
  1243. this.inline(this.tokens);
  1244. return this.tokens;
  1245. }
  1246. /**
  1247. * Lexing
  1248. */
  1249. blockTokens(src, tokens = [], top = true) {
  1250. src = src.replace(/^ +$/gm, '');
  1251. let token, i, l, lastToken;
  1252. while (src) {
  1253. // newline
  1254. if (token = this.tokenizer.space(src)) {
  1255. src = src.substring(token.raw.length);
  1256. if (token.type) {
  1257. tokens.push(token);
  1258. }
  1259. continue;
  1260. }
  1261. // code
  1262. if (token = this.tokenizer.code(src, tokens)) {
  1263. src = src.substring(token.raw.length);
  1264. if (token.type) {
  1265. tokens.push(token);
  1266. } else {
  1267. lastToken = tokens[tokens.length - 1];
  1268. lastToken.raw += '\n' + token.raw;
  1269. lastToken.text += '\n' + token.text;
  1270. }
  1271. continue;
  1272. }
  1273. // fences
  1274. if (token = this.tokenizer.fences(src)) {
  1275. src = src.substring(token.raw.length);
  1276. tokens.push(token);
  1277. continue;
  1278. }
  1279. // heading
  1280. if (token = this.tokenizer.heading(src)) {
  1281. src = src.substring(token.raw.length);
  1282. tokens.push(token);
  1283. continue;
  1284. }
  1285. // table no leading pipe (gfm)
  1286. if (token = this.tokenizer.nptable(src)) {
  1287. src = src.substring(token.raw.length);
  1288. tokens.push(token);
  1289. continue;
  1290. }
  1291. // hr
  1292. if (token = this.tokenizer.hr(src)) {
  1293. src = src.substring(token.raw.length);
  1294. tokens.push(token);
  1295. continue;
  1296. }
  1297. // blockquote
  1298. if (token = this.tokenizer.blockquote(src)) {
  1299. src = src.substring(token.raw.length);
  1300. token.tokens = this.blockTokens(token.text, [], top);
  1301. tokens.push(token);
  1302. continue;
  1303. }
  1304. // list
  1305. if (token = this.tokenizer.list(src)) {
  1306. src = src.substring(token.raw.length);
  1307. l = token.items.length;
  1308. for (i = 0; i < l; i++) {
  1309. token.items[i].tokens = this.blockTokens(token.items[i].text, [], false);
  1310. }
  1311. tokens.push(token);
  1312. continue;
  1313. }
  1314. // html
  1315. if (token = this.tokenizer.html(src)) {
  1316. src = src.substring(token.raw.length);
  1317. tokens.push(token);
  1318. continue;
  1319. }
  1320. // def
  1321. if (top && (token = this.tokenizer.def(src))) {
  1322. src = src.substring(token.raw.length);
  1323. if (!this.tokens.links[token.tag]) {
  1324. this.tokens.links[token.tag] = {
  1325. href: token.href,
  1326. title: token.title
  1327. };
  1328. }
  1329. continue;
  1330. }
  1331. // table (gfm)
  1332. if (token = this.tokenizer.table(src)) {
  1333. src = src.substring(token.raw.length);
  1334. tokens.push(token);
  1335. continue;
  1336. }
  1337. // lheading
  1338. if (token = this.tokenizer.lheading(src)) {
  1339. src = src.substring(token.raw.length);
  1340. tokens.push(token);
  1341. continue;
  1342. }
  1343. // top-level paragraph
  1344. if (top && (token = this.tokenizer.paragraph(src))) {
  1345. src = src.substring(token.raw.length);
  1346. tokens.push(token);
  1347. continue;
  1348. }
  1349. // text
  1350. if (token = this.tokenizer.text(src, tokens)) {
  1351. src = src.substring(token.raw.length);
  1352. if (token.type) {
  1353. tokens.push(token);
  1354. } else {
  1355. lastToken = tokens[tokens.length - 1];
  1356. lastToken.raw += '\n' + token.raw;
  1357. lastToken.text += '\n' + token.text;
  1358. }
  1359. continue;
  1360. }
  1361. if (src) {
  1362. const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
  1363. if (this.options.silent) {
  1364. console.error(errMsg);
  1365. break;
  1366. } else {
  1367. throw new Error(errMsg);
  1368. }
  1369. }
  1370. }
  1371. return tokens;
  1372. }
  1373. inline(tokens) {
  1374. let i,
  1375. j,
  1376. k,
  1377. l2,
  1378. row,
  1379. token;
  1380. const l = tokens.length;
  1381. for (i = 0; i < l; i++) {
  1382. token = tokens[i];
  1383. switch (token.type) {
  1384. case 'paragraph':
  1385. case 'text':
  1386. case 'heading': {
  1387. token.tokens = [];
  1388. this.inlineTokens(token.text, token.tokens);
  1389. break;
  1390. }
  1391. case 'table': {
  1392. token.tokens = {
  1393. header: [],
  1394. cells: []
  1395. };
  1396. // header
  1397. l2 = token.header.length;
  1398. for (j = 0; j < l2; j++) {
  1399. token.tokens.header[j] = [];
  1400. this.inlineTokens(token.header[j], token.tokens.header[j]);
  1401. }
  1402. // cells
  1403. l2 = token.cells.length;
  1404. for (j = 0; j < l2; j++) {
  1405. row = token.cells[j];
  1406. token.tokens.cells[j] = [];
  1407. for (k = 0; k < row.length; k++) {
  1408. token.tokens.cells[j][k] = [];
  1409. this.inlineTokens(row[k], token.tokens.cells[j][k]);
  1410. }
  1411. }
  1412. break;
  1413. }
  1414. case 'blockquote': {
  1415. this.inline(token.tokens);
  1416. break;
  1417. }
  1418. case 'list': {
  1419. l2 = token.items.length;
  1420. for (j = 0; j < l2; j++) {
  1421. this.inline(token.items[j].tokens);
  1422. }
  1423. break;
  1424. }
  1425. }
  1426. }
  1427. return tokens;
  1428. }
  1429. /**
  1430. * Lexing/Compiling
  1431. */
  1432. inlineTokens(src, tokens = [], inLink = false, inRawBlock = false, prevChar = '') {
  1433. let token;
  1434. // String with links masked to avoid interference with em and strong
  1435. let maskedSrc = src;
  1436. let match;
  1437. // Mask out reflinks
  1438. if (this.tokens.links) {
  1439. const links = Object.keys(this.tokens.links);
  1440. if (links.length > 0) {
  1441. while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
  1442. if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
  1443. maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
  1444. }
  1445. }
  1446. }
  1447. }
  1448. // Mask out other blocks
  1449. while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
  1450. maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
  1451. }
  1452. while (src) {
  1453. // escape
  1454. if (token = this.tokenizer.escape(src)) {
  1455. src = src.substring(token.raw.length);
  1456. tokens.push(token);
  1457. continue;
  1458. }
  1459. // tag
  1460. if (token = this.tokenizer.tag(src, inLink, inRawBlock)) {
  1461. src = src.substring(token.raw.length);
  1462. inLink = token.inLink;
  1463. inRawBlock = token.inRawBlock;
  1464. tokens.push(token);
  1465. continue;
  1466. }
  1467. // link
  1468. if (token = this.tokenizer.link(src)) {
  1469. src = src.substring(token.raw.length);
  1470. if (token.type === 'link') {
  1471. token.tokens = this.inlineTokens(token.text, [], true, inRawBlock);
  1472. }
  1473. tokens.push(token);
  1474. continue;
  1475. }
  1476. // reflink, nolink
  1477. if (token = this.tokenizer.reflink(src, this.tokens.links)) {
  1478. src = src.substring(token.raw.length);
  1479. if (token.type === 'link') {
  1480. token.tokens = this.inlineTokens(token.text, [], true, inRawBlock);
  1481. }
  1482. tokens.push(token);
  1483. continue;
  1484. }
  1485. // strong
  1486. if (token = this.tokenizer.strong(src, maskedSrc, prevChar)) {
  1487. src = src.substring(token.raw.length);
  1488. token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
  1489. tokens.push(token);
  1490. continue;
  1491. }
  1492. // em
  1493. if (token = this.tokenizer.em(src, maskedSrc, prevChar)) {
  1494. src = src.substring(token.raw.length);
  1495. token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
  1496. tokens.push(token);
  1497. continue;
  1498. }
  1499. // code
  1500. if (token = this.tokenizer.codespan(src)) {
  1501. src = src.substring(token.raw.length);
  1502. tokens.push(token);
  1503. continue;
  1504. }
  1505. // br
  1506. if (token = this.tokenizer.br(src)) {
  1507. src = src.substring(token.raw.length);
  1508. tokens.push(token);
  1509. continue;
  1510. }
  1511. // del (gfm)
  1512. if (token = this.tokenizer.del(src)) {
  1513. src = src.substring(token.raw.length);
  1514. token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
  1515. tokens.push(token);
  1516. continue;
  1517. }
  1518. // autolink
  1519. if (token = this.tokenizer.autolink(src, mangle)) {
  1520. src = src.substring(token.raw.length);
  1521. tokens.push(token);
  1522. continue;
  1523. }
  1524. // url (gfm)
  1525. if (!inLink && (token = this.tokenizer.url(src, mangle))) {
  1526. src = src.substring(token.raw.length);
  1527. tokens.push(token);
  1528. continue;
  1529. }
  1530. // text
  1531. if (token = this.tokenizer.inlineText(src, inRawBlock, smartypants)) {
  1532. src = src.substring(token.raw.length);
  1533. prevChar = token.raw.slice(-1);
  1534. tokens.push(token);
  1535. continue;
  1536. }
  1537. if (src) {
  1538. const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
  1539. if (this.options.silent) {
  1540. console.error(errMsg);
  1541. break;
  1542. } else {
  1543. throw new Error(errMsg);
  1544. }
  1545. }
  1546. }
  1547. return tokens;
  1548. }
  1549. };
  1550. const { defaults: defaults$3 } = defaults;
  1551. const {
  1552. cleanUrl: cleanUrl$1,
  1553. escape: escape$2
  1554. } = helpers;
  1555. /**
  1556. * Renderer
  1557. */
  1558. var Renderer_1 = class Renderer {
  1559. constructor(options) {
  1560. this.options = options || defaults$3;
  1561. }
  1562. code(code, infostring, escaped) {
  1563. const lang = (infostring || '').match(/\S*/)[0];
  1564. if (this.options.highlight) {
  1565. const out = this.options.highlight(code, lang);
  1566. if (out != null && out !== code) {
  1567. escaped = true;
  1568. code = out;
  1569. }
  1570. }
  1571. if (!lang) {
  1572. return '<pre><code>'
  1573. + (escaped ? code : escape$2(code, true))
  1574. + '</code></pre>\n';
  1575. }
  1576. return '<pre><code class="'
  1577. + this.options.langPrefix
  1578. + escape$2(lang, true)
  1579. + '">'
  1580. + (escaped ? code : escape$2(code, true))
  1581. + '</code></pre>\n';
  1582. }
  1583. blockquote(quote) {
  1584. return '<blockquote>\n' + quote + '</blockquote>\n';
  1585. }
  1586. html(html) {
  1587. return html;
  1588. }
  1589. heading(text, level, raw, slugger) {
  1590. if (this.options.headerIds) {
  1591. return '<h'
  1592. + level
  1593. + ' id="'
  1594. + this.options.headerPrefix
  1595. + slugger.slug(raw)
  1596. + '">'
  1597. + text
  1598. + '</h'
  1599. + level
  1600. + '>\n';
  1601. }
  1602. // ignore IDs
  1603. return '<h' + level + '>' + text + '</h' + level + '>\n';
  1604. }
  1605. hr() {
  1606. return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
  1607. }
  1608. list(body, ordered, start) {
  1609. const type = ordered ? 'ol' : 'ul',
  1610. startatt = (ordered && start !== 1) ? (' start="' + start + '"') : '';
  1611. return '<' + type + startatt + '>\n' + body + '</' + type + '>\n';
  1612. }
  1613. listitem(text) {
  1614. return '<li>' + text + '</li>\n';
  1615. }
  1616. checkbox(checked) {
  1617. return '<input '
  1618. + (checked ? 'checked="" ' : '')
  1619. + 'disabled="" type="checkbox"'
  1620. + (this.options.xhtml ? ' /' : '')
  1621. + '> ';
  1622. }
  1623. paragraph(text) {
  1624. return '<p>' + text + '</p>\n';
  1625. }
  1626. table(header, body) {
  1627. if (body) body = '<tbody>' + body + '</tbody>';
  1628. return '<table>\n'
  1629. + '<thead>\n'
  1630. + header
  1631. + '</thead>\n'
  1632. + body
  1633. + '</table>\n';
  1634. }
  1635. tablerow(content) {
  1636. return '<tr>\n' + content + '</tr>\n';
  1637. }
  1638. tablecell(content, flags) {
  1639. const type = flags.header ? 'th' : 'td';
  1640. const tag = flags.align
  1641. ? '<' + type + ' align="' + flags.align + '">'
  1642. : '<' + type + '>';
  1643. return tag + content + '</' + type + '>\n';
  1644. }
  1645. // span level renderer
  1646. strong(text) {
  1647. return '<strong>' + text + '</strong>';
  1648. }
  1649. em(text) {
  1650. return '<em>' + text + '</em>';
  1651. }
  1652. codespan(text) {
  1653. return '<code>' + text + '</code>';
  1654. }
  1655. br() {
  1656. return this.options.xhtml ? '<br/>' : '<br>';
  1657. }
  1658. del(text) {
  1659. return '<del>' + text + '</del>';
  1660. }
  1661. link(href, title, text) {
  1662. href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href);
  1663. if (href === null) {
  1664. return text;
  1665. }
  1666. let out = '<a href="' + escape$2(href) + '"';
  1667. if (title) {
  1668. out += ' title="' + title + '"';
  1669. }
  1670. out += '>' + text + '</a>';
  1671. return out;
  1672. }
  1673. image(href, title, text) {
  1674. href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href);
  1675. if (href === null) {
  1676. return text;
  1677. }
  1678. let out = '<img src="' + href + '" alt="' + text + '"';
  1679. if (title) {
  1680. out += ' title="' + title + '"';
  1681. }
  1682. out += this.options.xhtml ? '/>' : '>';
  1683. return out;
  1684. }
  1685. text(text) {
  1686. return text;
  1687. }
  1688. };
  1689. /**
  1690. * TextRenderer
  1691. * returns only the textual part of the token
  1692. */
  1693. var TextRenderer_1 = class TextRenderer {
  1694. // no need for block level renderers
  1695. strong(text) {
  1696. return text;
  1697. }
  1698. em(text) {
  1699. return text;
  1700. }
  1701. codespan(text) {
  1702. return text;
  1703. }
  1704. del(text) {
  1705. return text;
  1706. }
  1707. html(text) {
  1708. return text;
  1709. }
  1710. text(text) {
  1711. return text;
  1712. }
  1713. link(href, title, text) {
  1714. return '' + text;
  1715. }
  1716. image(href, title, text) {
  1717. return '' + text;
  1718. }
  1719. br() {
  1720. return '';
  1721. }
  1722. };
  1723. /**
  1724. * Slugger generates header id
  1725. */
  1726. var Slugger_1 = class Slugger {
  1727. constructor() {
  1728. this.seen = {};
  1729. }
  1730. serialize(value) {
  1731. return value
  1732. .toLowerCase()
  1733. .trim()
  1734. // remove html tags
  1735. .replace(/<[!\/a-z].*?>/ig, '')
  1736. // remove unwanted chars
  1737. .replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '')
  1738. .replace(/\s/g, '-');
  1739. }
  1740. /**
  1741. * Finds the next safe (unique) slug to use
  1742. */
  1743. getNextSafeSlug(originalSlug, isDryRun) {
  1744. let slug = originalSlug;
  1745. let occurenceAccumulator = 0;
  1746. if (this.seen.hasOwnProperty(slug)) {
  1747. occurenceAccumulator = this.seen[originalSlug];
  1748. do {
  1749. occurenceAccumulator++;
  1750. slug = originalSlug + '-' + occurenceAccumulator;
  1751. } while (this.seen.hasOwnProperty(slug));
  1752. }
  1753. if (!isDryRun) {
  1754. this.seen[originalSlug] = occurenceAccumulator;
  1755. this.seen[slug] = 0;
  1756. }
  1757. return slug;
  1758. }
  1759. /**
  1760. * Convert string to unique id
  1761. * @param {object} options
  1762. * @param {boolean} options.dryrun Generates the next unique slug without updating the internal accumulator.
  1763. */
  1764. slug(value, options = {}) {
  1765. const slug = this.serialize(value);
  1766. return this.getNextSafeSlug(slug, options.dryrun);
  1767. }
  1768. };
  1769. const { defaults: defaults$4 } = defaults;
  1770. const {
  1771. unescape: unescape$1
  1772. } = helpers;
  1773. /**
  1774. * Parsing & Compiling
  1775. */
  1776. var Parser_1 = class Parser {
  1777. constructor(options) {
  1778. this.options = options || defaults$4;
  1779. this.options.renderer = this.options.renderer || new Renderer_1();
  1780. this.renderer = this.options.renderer;
  1781. this.renderer.options = this.options;
  1782. this.textRenderer = new TextRenderer_1();
  1783. this.slugger = new Slugger_1();
  1784. }
  1785. /**
  1786. * Static Parse Method
  1787. */
  1788. static parse(tokens, options) {
  1789. const parser = new Parser(options);
  1790. return parser.parse(tokens);
  1791. }
  1792. /**
  1793. * Parse Loop
  1794. */
  1795. parse(tokens, top = true) {
  1796. let out = '',
  1797. i,
  1798. j,
  1799. k,
  1800. l2,
  1801. l3,
  1802. row,
  1803. cell,
  1804. header,
  1805. body,
  1806. token,
  1807. ordered,
  1808. start,
  1809. loose,
  1810. itemBody,
  1811. item,
  1812. checked,
  1813. task,
  1814. checkbox;
  1815. const l = tokens.length;
  1816. for (i = 0; i < l; i++) {
  1817. token = tokens[i];
  1818. switch (token.type) {
  1819. case 'space': {
  1820. continue;
  1821. }
  1822. case 'hr': {
  1823. out += this.renderer.hr();
  1824. continue;
  1825. }
  1826. case 'heading': {
  1827. out += this.renderer.heading(
  1828. this.parseInline(token.tokens),
  1829. token.depth,
  1830. unescape$1(this.parseInline(token.tokens, this.textRenderer)),
  1831. this.slugger);
  1832. continue;
  1833. }
  1834. case 'code': {
  1835. out += this.renderer.code(token.text,
  1836. token.lang,
  1837. token.escaped);
  1838. continue;
  1839. }
  1840. case 'table': {
  1841. header = '';
  1842. // header
  1843. cell = '';
  1844. l2 = token.header.length;
  1845. for (j = 0; j < l2; j++) {
  1846. cell += this.renderer.tablecell(
  1847. this.parseInline(token.tokens.header[j]),
  1848. { header: true, align: token.align[j] }
  1849. );
  1850. }
  1851. header += this.renderer.tablerow(cell);
  1852. body = '';
  1853. l2 = token.cells.length;
  1854. for (j = 0; j < l2; j++) {
  1855. row = token.tokens.cells[j];
  1856. cell = '';
  1857. l3 = row.length;
  1858. for (k = 0; k < l3; k++) {
  1859. cell += this.renderer.tablecell(
  1860. this.parseInline(row[k]),
  1861. { header: false, align: token.align[k] }
  1862. );
  1863. }
  1864. body += this.renderer.tablerow(cell);
  1865. }
  1866. out += this.renderer.table(header, body);
  1867. continue;
  1868. }
  1869. case 'blockquote': {
  1870. body = this.parse(token.tokens);
  1871. out += this.renderer.blockquote(body);
  1872. continue;
  1873. }
  1874. case 'list': {
  1875. ordered = token.ordered;
  1876. start = token.start;
  1877. loose = token.loose;
  1878. l2 = token.items.length;
  1879. body = '';
  1880. for (j = 0; j < l2; j++) {
  1881. item = token.items[j];
  1882. checked = item.checked;
  1883. task = item.task;
  1884. itemBody = '';
  1885. if (item.task) {
  1886. checkbox = this.renderer.checkbox(checked);
  1887. if (loose) {
  1888. if (item.tokens.length > 0 && item.tokens[0].type === 'text') {
  1889. item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
  1890. if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
  1891. item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
  1892. }
  1893. } else {
  1894. item.tokens.unshift({
  1895. type: 'text',
  1896. text: checkbox
  1897. });
  1898. }
  1899. } else {
  1900. itemBody += checkbox;
  1901. }
  1902. }
  1903. itemBody += this.parse(item.tokens, loose);
  1904. body += this.renderer.listitem(itemBody, task, checked);
  1905. }
  1906. out += this.renderer.list(body, ordered, start);
  1907. continue;
  1908. }
  1909. case 'html': {
  1910. // TODO parse inline content if parameter markdown=1
  1911. out += this.renderer.html(token.text);
  1912. continue;
  1913. }
  1914. case 'paragraph': {
  1915. out += this.renderer.paragraph(this.parseInline(token.tokens));
  1916. continue;
  1917. }
  1918. case 'text': {
  1919. body = token.tokens ? this.parseInline(token.tokens) : token.text;
  1920. while (i + 1 < l && tokens[i + 1].type === 'text') {
  1921. token = tokens[++i];
  1922. body += '\n' + (token.tokens ? this.parseInline(token.tokens) : token.text);
  1923. }
  1924. out += top ? this.renderer.paragraph(body) : body;
  1925. continue;
  1926. }
  1927. default: {
  1928. const errMsg = 'Token with "' + token.type + '" type was not found.';
  1929. if (this.options.silent) {
  1930. console.error(errMsg);
  1931. return;
  1932. } else {
  1933. throw new Error(errMsg);
  1934. }
  1935. }
  1936. }
  1937. }
  1938. return out;
  1939. }
  1940. /**
  1941. * Parse Inline Tokens
  1942. */
  1943. parseInline(tokens, renderer) {
  1944. renderer = renderer || this.renderer;
  1945. let out = '',
  1946. i,
  1947. token;
  1948. const l = tokens.length;
  1949. for (i = 0; i < l; i++) {
  1950. token = tokens[i];
  1951. switch (token.type) {
  1952. case 'escape': {
  1953. out += renderer.text(token.text);
  1954. break;
  1955. }
  1956. case 'html': {
  1957. out += renderer.html(token.text);
  1958. break;
  1959. }
  1960. case 'link': {
  1961. out += renderer.link(token.href, token.title, this.parseInline(token.tokens, renderer));
  1962. break;
  1963. }
  1964. case 'image': {
  1965. out += renderer.image(token.href, token.title, token.text);
  1966. break;
  1967. }
  1968. case 'strong': {
  1969. out += renderer.strong(this.parseInline(token.tokens, renderer));
  1970. break;
  1971. }
  1972. case 'em': {
  1973. out += renderer.em(this.parseInline(token.tokens, renderer));
  1974. break;
  1975. }
  1976. case 'codespan': {
  1977. out += renderer.codespan(token.text);
  1978. break;
  1979. }
  1980. case 'br': {
  1981. out += renderer.br();
  1982. break;
  1983. }
  1984. case 'del': {
  1985. out += renderer.del(this.parseInline(token.tokens, renderer));
  1986. break;
  1987. }
  1988. case 'text': {
  1989. out += renderer.text(token.text);
  1990. break;
  1991. }
  1992. default: {
  1993. const errMsg = 'Token with "' + token.type + '" type was not found.';
  1994. if (this.options.silent) {
  1995. console.error(errMsg);
  1996. return;
  1997. } else {
  1998. throw new Error(errMsg);
  1999. }
  2000. }
  2001. }
  2002. }
  2003. return out;
  2004. }
  2005. };
  2006. const {
  2007. merge: merge$2,
  2008. checkSanitizeDeprecation: checkSanitizeDeprecation$1,
  2009. escape: escape$3
  2010. } = helpers;
  2011. const {
  2012. getDefaults,
  2013. changeDefaults,
  2014. defaults: defaults$5
  2015. } = defaults;
  2016. /**
  2017. * Marked
  2018. */
  2019. function marked(src, opt, callback) {
  2020. // throw error in case of non string input
  2021. if (typeof src === 'undefined' || src === null) {
  2022. throw new Error('marked(): input parameter is undefined or null');
  2023. }
  2024. if (typeof src !== 'string') {
  2025. throw new Error('marked(): input parameter is of type '
  2026. + Object.prototype.toString.call(src) + ', string expected');
  2027. }
  2028. if (typeof opt === 'function') {
  2029. callback = opt;
  2030. opt = null;
  2031. }
  2032. opt = merge$2({}, marked.defaults, opt || {});
  2033. checkSanitizeDeprecation$1(opt);
  2034. if (callback) {
  2035. const highlight = opt.highlight;
  2036. let tokens;
  2037. try {
  2038. tokens = Lexer_1.lex(src, opt);
  2039. } catch (e) {
  2040. return callback(e);
  2041. }
  2042. const done = function(err) {
  2043. let out;
  2044. if (!err) {
  2045. try {
  2046. out = Parser_1.parse(tokens, opt);
  2047. } catch (e) {
  2048. err = e;
  2049. }
  2050. }
  2051. opt.highlight = highlight;
  2052. return err
  2053. ? callback(err)
  2054. : callback(null, out);
  2055. };
  2056. if (!highlight || highlight.length < 3) {
  2057. return done();
  2058. }
  2059. delete opt.highlight;
  2060. if (!tokens.length) return done();
  2061. let pending = 0;
  2062. marked.walkTokens(tokens, function(token) {
  2063. if (token.type === 'code') {
  2064. pending++;
  2065. setTimeout(() => {
  2066. highlight(token.text, token.lang, function(err, code) {
  2067. if (err) {
  2068. return done(err);
  2069. }
  2070. if (code != null && code !== token.text) {
  2071. token.text = code;
  2072. token.escaped = true;
  2073. }
  2074. pending--;
  2075. if (pending === 0) {
  2076. done();
  2077. }
  2078. });
  2079. }, 0);
  2080. }
  2081. });
  2082. if (pending === 0) {
  2083. done();
  2084. }
  2085. return;
  2086. }
  2087. try {
  2088. const tokens = Lexer_1.lex(src, opt);
  2089. if (opt.walkTokens) {
  2090. marked.walkTokens(tokens, opt.walkTokens);
  2091. }
  2092. return Parser_1.parse(tokens, opt);
  2093. } catch (e) {
  2094. e.message += '\nPlease report this to https://github.com/markedjs/marked.';
  2095. if (opt.silent) {
  2096. return '<p>An error occurred:</p><pre>'
  2097. + escape$3(e.message + '', true)
  2098. + '</pre>';
  2099. }
  2100. throw e;
  2101. }
  2102. }
  2103. /**
  2104. * Options
  2105. */
  2106. marked.options =
  2107. marked.setOptions = function(opt) {
  2108. merge$2(marked.defaults, opt);
  2109. changeDefaults(marked.defaults);
  2110. return marked;
  2111. };
  2112. marked.getDefaults = getDefaults;
  2113. marked.defaults = defaults$5;
  2114. /**
  2115. * Use Extension
  2116. */
  2117. marked.use = function(extension) {
  2118. const opts = merge$2({}, extension);
  2119. if (extension.renderer) {
  2120. const renderer = marked.defaults.renderer || new Renderer_1();
  2121. for (const prop in extension.renderer) {
  2122. const prevRenderer = renderer[prop];
  2123. renderer[prop] = (...args) => {
  2124. let ret = extension.renderer[prop].apply(renderer, args);
  2125. if (ret === false) {
  2126. ret = prevRenderer.apply(renderer, args);
  2127. }
  2128. return ret;
  2129. };
  2130. }
  2131. opts.renderer = renderer;
  2132. }
  2133. if (extension.tokenizer) {
  2134. const tokenizer = marked.defaults.tokenizer || new Tokenizer_1();
  2135. for (const prop in extension.tokenizer) {
  2136. const prevTokenizer = tokenizer[prop];
  2137. tokenizer[prop] = (...args) => {
  2138. let ret = extension.tokenizer[prop].apply(tokenizer, args);
  2139. if (ret === false) {
  2140. ret = prevTokenizer.apply(tokenizer, args);
  2141. }
  2142. return ret;
  2143. };
  2144. }
  2145. opts.tokenizer = tokenizer;
  2146. }
  2147. if (extension.walkTokens) {
  2148. const walkTokens = marked.defaults.walkTokens;
  2149. opts.walkTokens = (token) => {
  2150. extension.walkTokens(token);
  2151. if (walkTokens) {
  2152. walkTokens(token);
  2153. }
  2154. };
  2155. }
  2156. marked.setOptions(opts);
  2157. };
  2158. /**
  2159. * Run callback for every token
  2160. */
  2161. marked.walkTokens = function(tokens, callback) {
  2162. for (const token of tokens) {
  2163. callback(token);
  2164. switch (token.type) {
  2165. case 'table': {
  2166. for (const cell of token.tokens.header) {
  2167. marked.walkTokens(cell, callback);
  2168. }
  2169. for (const row of token.tokens.cells) {
  2170. for (const cell of row) {
  2171. marked.walkTokens(cell, callback);
  2172. }
  2173. }
  2174. break;
  2175. }
  2176. case 'list': {
  2177. marked.walkTokens(token.items, callback);
  2178. break;
  2179. }
  2180. default: {
  2181. if (token.tokens) {
  2182. marked.walkTokens(token.tokens, callback);
  2183. }
  2184. }
  2185. }
  2186. }
  2187. };
  2188. /**
  2189. * Expose
  2190. */
  2191. marked.Parser = Parser_1;
  2192. marked.parser = Parser_1.parse;
  2193. marked.Renderer = Renderer_1;
  2194. marked.TextRenderer = TextRenderer_1;
  2195. marked.Lexer = Lexer_1;
  2196. marked.lexer = Lexer_1.lex;
  2197. marked.Tokenizer = Tokenizer_1;
  2198. marked.Slugger = Slugger_1;
  2199. marked.parse = marked;
  2200. var marked_1 = marked;
  2201. export default marked_1;