PHP and Javascript implementations of a simple markdown parser
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

markdown.js 46KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  1. // TODO: Linked image not parsed correctly. [![](image.jpg)](link.html)
  2. // TODO: Referenced URL definitions
  3. // TODO: HTML tags probably need better handling. Consider whether interior of matched tags should be interpreted as markdown.
  4. // TODO: {.class #cssid lang=fr}
  5. // # Header {.class}
  6. // Header {.class}
  7. // ---
  8. // [link](url){.class}
  9. // ``` {.class}
  10. // FIXME: Nested lists not working right
  11. // FIXME: Nested blockquotes require blank line
  12. // FIXME: Ordered list should start with first number
  13. class _MDHAlign {
  14. static Left = new _MDHAlign('Left');
  15. static Center = new _MDHAlign('Center');
  16. static Right = new _MDHAlign('Right');
  17. /** @var {String} */
  18. name;
  19. constructor(name) {
  20. this.name = name;
  21. }
  22. toString() {
  23. return `${this.constructor.name}.${this.name}`;
  24. }
  25. static toHTMLAttribute(align) {
  26. switch (align) {
  27. case _MDHAlign.Left: return ' align="left"';
  28. case _MDHAlign.Center: return ' align="center"';
  29. case _MDHAlign.Right: return ' align="right"';
  30. }
  31. return '';
  32. }
  33. }
  34. class _MDTokenType {
  35. static Text = new _MDTokenType('Text');
  36. static Whitespace = new _MDTokenType('Whitespace');
  37. static Underscore = new _MDTokenType('Underscore');
  38. static Asterisk = new _MDTokenType('Asterisk');
  39. static Slash = new _MDTokenType('Slash');
  40. static Tilde = new _MDTokenType('Tilde');
  41. static Bang = new _MDTokenType('Bang');
  42. static Backtick = new _MDTokenType('Backtick');
  43. static Label = new _MDTokenType('Label'); // content=label
  44. static URL = new _MDTokenType('URL'); // content=URL, extra=title
  45. static Email = new _MDTokenType('Email'); // content=email address, extra=title
  46. static SimpleLink = new _MDTokenType('SimpleLink'); // content=URL
  47. static SimpleEmail = new _MDTokenType('SimpleEmail'); // content=email address
  48. static Footnote = new _MDTokenType('Footnote'); // content=symbol
  49. static HTMLTag = new _MDTokenType('HTMLTag'); // content=tag string, tag=_MDHTMLTag
  50. static META_AnyNonWhitespace = new _MDTokenType('METAAnyNonWhitespace');
  51. /** @var {String} */
  52. name;
  53. constructor(name) {
  54. this.name = name;
  55. }
  56. toString() {
  57. return `${this.constructor.name}.${this.name}`;
  58. }
  59. }
  60. class _MDToken {
  61. /**
  62. * The original token string.
  63. * @var {String}
  64. */
  65. original;
  66. /** @var {_MDTokenType} */
  67. type;
  68. /** @var {String|null} */
  69. content;
  70. /** @var {String|null} */
  71. extra;
  72. /** @var {_MDHTMLTag|null} */
  73. tag;
  74. /**
  75. * @param {String} original
  76. * @param {_MDTokenType} type
  77. * @param {String|null} content
  78. * @param {String|null} extra
  79. * @param {_MDHTMLTag|null} tag
  80. */
  81. constructor(original, type, content=null, extra=null, tag=null) {
  82. this.original = original;
  83. this.type = type;
  84. this.content = content;
  85. this.extra = extra;
  86. this.tag = tag;
  87. }
  88. }
  89. // -- Spans -----------------------------------------------------------------
  90. class _MDSpan {
  91. /** @var {String[]} */
  92. cssClasses = [];
  93. /** @var {String|null} */
  94. id = null;
  95. /** @var {Object} */
  96. attributes = {};
  97. /**
  98. * @param {_MDState} state
  99. * @returns {String} HTML
  100. */
  101. toHTML(state) {
  102. throw new Error(self.constructor.name + ".toHTML not implemented");
  103. }
  104. htmlAttributes() {
  105. var html = '';
  106. if (this.cssClasses.length > 0) {
  107. html += ` class="${this.cssClasses.join(' ')}"`;
  108. }
  109. if (this.id !== null) {
  110. html += ` id="${this.id}"`;
  111. }
  112. for (const name in this.attributes) {
  113. let value = this.attributes[name];
  114. html += ` ${name}="${value.replace('"', '"')}"`;
  115. }
  116. return html;
  117. }
  118. /**
  119. * @param {_MDSpan[]} spans
  120. * @param {_MDState} state
  121. */
  122. static toHTML(spans, state) {
  123. return spans.map((span) => span.toHTML(state)).join("");
  124. }
  125. }
  126. class _MDMultiSpan extends _MDSpan {
  127. /** @var {_MDSpan[]} */
  128. content;
  129. /**
  130. * @param {_MDSpan[]} content
  131. */
  132. constructor(content) {
  133. super();
  134. this.content = content;
  135. }
  136. toHTML(state) {
  137. return _MDSpan.toHTML(this.content, state);
  138. }
  139. }
  140. class _MDTextSpan extends _MDSpan {
  141. /** @param {String} text */
  142. text;
  143. /**
  144. * @param {String} text
  145. */
  146. constructor(text) {
  147. super();
  148. this.text = text;
  149. }
  150. toHTML(state) {
  151. let html = this.text.replace('<', '&lt;');
  152. let abbrevs = state.abbreviations;
  153. let regexes = state.abbreviationRegexes;
  154. for (const abbrev in abbrevs) {
  155. let def = abbrevs[abbrev];
  156. let regex = regexes[abbrev];
  157. let escapedDef = def.replace('"', '&quot;');
  158. html = html.replace(regex, `<abbr title="${escapedDef}">$1</abbr>`);
  159. }
  160. return html;
  161. }
  162. }
  163. class _MDHTMLSpan extends _MDSpan {
  164. /** @param {String} html */
  165. html;
  166. /**
  167. * @param {String} html
  168. */
  169. constructor(html) {
  170. super();
  171. this.html = html;
  172. }
  173. toHTML(state) {
  174. return this.html;
  175. }
  176. }
  177. class _MDLinkSpan extends _MDSpan {
  178. /** @var {String} */
  179. link;
  180. /** @var {String|null} */
  181. target = null;
  182. /** @var {_MDSpan} */
  183. content;
  184. /**
  185. * @param {String} link
  186. * @param {_MDSpan} content
  187. */
  188. constructor(link, content) {
  189. super();
  190. this.link = link;
  191. this.content = content;
  192. }
  193. toHTML(state) {
  194. let escapedLink = this.link.replace('"', '&quot;');
  195. var html = `<a href="${escapedLink}"`;
  196. if (target) {
  197. let escapedTarget = this.target.replace('"', '&quot;');
  198. html += ` target="${escapedTarget}"`;
  199. }
  200. html += this.htmlAttributes();
  201. html += '>' + this.content.toHTML(state) + '</a>';
  202. return html;
  203. }
  204. }
  205. class _MDReferencedLinkSpan extends _MDLinkSpan {
  206. /** @var {String} id */
  207. id;
  208. constructor(id, content) {
  209. super(null, content);
  210. this.id = id;
  211. }
  212. toHTML(state) {
  213. if (this.link) {
  214. return super.toHTML(state);
  215. } else {
  216. let contentHTML = this.content.toHTML(state);
  217. return `[${contentHTML}][${this.id}]`;
  218. }
  219. }
  220. }
  221. class _MDEmphasisSpan extends _MDSpan {
  222. /** @var {_MDSpan} */
  223. #content;
  224. /**
  225. * @param {_MDSpan} content
  226. */
  227. constructor(content) {
  228. super();
  229. this.#content = content;
  230. }
  231. toHTML(state) {
  232. let contentHTML = this.#content.toHTML(state);
  233. return `<em${this.htmlAttributes()}>${contentHTML}</em>`;
  234. }
  235. }
  236. class _MDStrongSpan extends _MDSpan {
  237. /** @var {_MDSpan} content */
  238. #content;
  239. /**
  240. * @param {_MDSpan} content
  241. */
  242. constructor(content) {
  243. super();
  244. this.#content = content;
  245. }
  246. toHTML(state) {
  247. let contentHTML = this.#content.toHTML(state);
  248. return `<strong${this.htmlAttributes()}>${contentHTML}</strong>`;
  249. }
  250. }
  251. class _MDStrikethroughSpan extends _MDSpan {
  252. /** @var {_MDSpan} content */
  253. #content;
  254. /**
  255. * @param {_MDSpan} content
  256. */
  257. constructor(content) {
  258. super();
  259. this.#content = content;
  260. }
  261. toHTML(state) {
  262. let contentHTML = this.#content.toHTML(state);
  263. return `<strike${this.htmlAttributes()}>${contentHTML}</strike>`;
  264. }
  265. }
  266. class _MDCodeSpan extends _MDSpan {
  267. /** @var {_MDSpan} content */
  268. #content;
  269. /**
  270. * @param {_MDSpan} content
  271. */
  272. constructor(content) {
  273. super();
  274. this.#content = content;
  275. }
  276. toHTML(state) {
  277. let contentHTML = this.#content.toHTML(state);
  278. return `<code${this.htmlAttributes()}>${contentHTML}</code>`;
  279. }
  280. }
  281. class _MDImageSpan extends _MDSpan {
  282. /** @var {String} */
  283. source;
  284. /** @var {String|null} */
  285. alt;
  286. /** @var {String|null} */
  287. title;
  288. /**
  289. * @param {String} source
  290. */
  291. constructor(source, alt, title=null) {
  292. super();
  293. this.source = source;
  294. this.alt = alt;
  295. this.title = title;
  296. }
  297. toHTML(state) {
  298. let escapedSource = this.source.replace('"', '&quot;');
  299. let html = `<img src="${escapedSource}"`;
  300. if (this.alt) {
  301. let altEscaped = this.alt.replace('"', '&quot');
  302. html += ` alt="${altEscaped}"`;
  303. }
  304. if (this.title) {
  305. let titleEscaped = this.title.replace('"', '&quot;');
  306. html += ` title="${titleEscaped}"`;
  307. }
  308. html += this.htmlAttributes();
  309. html += '>';
  310. return html;
  311. }
  312. }
  313. class _MDReferencedImageSpan extends _MDImageSpan {
  314. /** @var {String} */
  315. id;
  316. /**
  317. * @param {String} id
  318. */
  319. constructor(id, alt) {
  320. super(null, alt);
  321. this.id = id;
  322. }
  323. toHTML(state) {
  324. if (this.source) {
  325. return super.toHTML(state);
  326. } else {
  327. let altEscaped = this.alt.replace('"', '&quot;');
  328. let idEscaped = this.id.replace('"', '&quot;');
  329. return `![${altEscaped}][${idEscaped}]`;
  330. }
  331. }
  332. }
  333. class _MDFootnoteReferenceSpan extends _MDSpan {
  334. /** @var {String} */
  335. symbol;
  336. /**
  337. * @param {String} symbol
  338. */
  339. constructor(symbol) {
  340. super();
  341. this.symbol = symbol;
  342. }
  343. toHTML(state) {
  344. return `<!--FNREF:{${this.symbol}}-->`;
  345. }
  346. }
  347. // -- Blocks ----------------------------------------------------------------
  348. class _MDBlock {
  349. /** @var {String[]} */
  350. cssClasses = [];
  351. /** @var {String|null} */
  352. id = null;
  353. /** @var {Object} */
  354. attributes = {};
  355. /**
  356. * @param {_MDState} state
  357. */
  358. toHTML(state) {
  359. throw new Error(self.constructor.name + ".toHTML not implemented");
  360. }
  361. htmlAttributes() {
  362. var html = '';
  363. if (this.cssClasses.length > 0) {
  364. html += ` class="${this.cssClasses.join(' ')}"`;
  365. }
  366. if (this.id !== null) {
  367. html += ` id="${this.id}"`;
  368. }
  369. for (const name in this.attributes) {
  370. let value = this.attributes[name];
  371. html += ` ${name}="${value.replace('"', '&quot;')}"`;
  372. }
  373. return html;
  374. }
  375. /**
  376. * @param {_MDBlock[]} blocks
  377. * @param {_MDState} state
  378. * @returns {String}
  379. */
  380. static toHTML(blocks, state) {
  381. return blocks.map((block) => block.toHTML(state)).join("\n");
  382. }
  383. }
  384. class _MDMultiBlock extends _MDBlock {
  385. /** @var {_MDBlock[]} */
  386. #blocks;
  387. /**
  388. * @param {_MDBlock[]} blocks
  389. */
  390. constructor(blocks) {
  391. super();
  392. this.#blocks = blocks;
  393. }
  394. toHTML(state) {
  395. return _MDBlock.toHTML(this.#blocks, state);
  396. }
  397. }
  398. class _MDParagraphBlock extends _MDBlock {
  399. /** @var {_MDBlock} */
  400. content;
  401. /**
  402. * @param {_MDBlock} content
  403. */
  404. constructor(content) {
  405. super();
  406. this.content = content;
  407. }
  408. toHTML(state) {
  409. let contentHTML = this.content.toHTML(state);
  410. return `<p${this.htmlAttributes()}>${contentHTML}</p>\n`;
  411. }
  412. }
  413. class _MDHeaderBlock extends _MDBlock {
  414. /** @var {number} */
  415. level;
  416. /** @var {_MDBlock} */
  417. content;
  418. /**
  419. * @param {number} level
  420. * @param {_MDBlock} content
  421. */
  422. constructor(level, content) {
  423. super();
  424. this.level = level;
  425. this.content = content;
  426. }
  427. toHTML(state) {
  428. let contentHTML = this.content.toHTML(state);
  429. return `<h${this.level}${this.htmlAttributes()}>${contentHTML}</h${this.level}>\n`;
  430. }
  431. }
  432. class _MDBlockquoteBlock extends _MDBlock {
  433. /** @var {_MDBlock[]} */
  434. content;
  435. /**
  436. * @param {_MDBlock[]} content
  437. */
  438. constructor(content) {
  439. super();
  440. this.content = content;
  441. }
  442. toHTML(state) {
  443. let contentHTML = _MDBlock.toHTML(this.content, state);
  444. return `<blockquote${this.htmlAttributes()}>\n${contentHTML}\n</blockquote>`;
  445. }
  446. }
  447. class _MDUnorderedListBlock extends _MDBlock {
  448. /** @var {_MDListItemBlock[]} */
  449. items;
  450. /**
  451. * @param {_MDListItemBlock[]} items
  452. */
  453. constructor(items) {
  454. super();
  455. this.items = items;
  456. }
  457. toHTML(state) {
  458. let contentHTML = _MDBlock.toHTML(this.items, state);
  459. return `<ul${this.htmlAttributes()}>\n${contentHTML}\n</ul>`;
  460. }
  461. }
  462. class _MDOrderedListBlock extends _MDBlock {
  463. /** @var {_MDListItemBlock[]} */
  464. items;
  465. /**
  466. * @param {_MDListItemBlock[]} items
  467. */
  468. constructor(items) {
  469. super();
  470. this.items = items;
  471. }
  472. toHTML(state) {
  473. let contentHTML = _MDBlock.toHTML(this.items, state);
  474. return `<ol${this.htmlAttributes()}>\n${contentHTML}\n</ol>`;
  475. }
  476. }
  477. class _MDListItemBlock extends _MDBlock {
  478. /** @var {_MDBlock} */
  479. content;
  480. /**
  481. * @param {_MDBlock} content
  482. */
  483. constructor(content) {
  484. super();
  485. this.content = content;
  486. }
  487. toHTML(state) {
  488. let contentHTML = this.content.toHTML(state);
  489. return `<li${this.htmlAttributes()}>${contentHTML}</li>`;
  490. }
  491. }
  492. class _MDCodeBlock extends _MDBlock {
  493. /** @var {String} */
  494. #code;
  495. /**
  496. * @param {String} code
  497. */
  498. constructor(code) {
  499. super();
  500. this.#code = code;
  501. }
  502. toHTML(state) {
  503. return `<pre${this.htmlAttributes()}><code>${this.#code}</code></pre>`;
  504. }
  505. }
  506. class _MDHorizontalRuleBlock extends _MDBlock {
  507. toHTML(state) {
  508. return `<hr${this.htmlAttributes()}>\n`;
  509. }
  510. }
  511. class _MDTableCellBlock extends _MDBlock {
  512. /** @var {_MDBlock} */
  513. #content;
  514. /** @var {_MDHAlign|null} */
  515. align = null;
  516. /**
  517. * @param {_MDBlock} content
  518. */
  519. constructor(content) {
  520. super();
  521. this.#content = content;
  522. }
  523. htmlAttributes() {
  524. var html = super.htmlAttributes();
  525. html += _MDHAlign.toHTMLAttribute(this.align);
  526. return html;
  527. }
  528. toHTML(state) {
  529. let contentHTML = this.#content.toHTML(state);
  530. return `<td${this.htmlAttributes()}>${contentHTML}</td>`;
  531. }
  532. }
  533. class _MDTableHeaderCellBlock extends _MDTableCellBlock {
  534. toHTML(state) {
  535. let html = super.toHTML(state);
  536. let groups = /^<td(.*)td>$/.exec(html);
  537. return `<th${groups[1]}th>`;
  538. }
  539. }
  540. class _MDTableRowBlock extends _MDBlock {
  541. /** @var {_MDTableCellBlock[]|_MDTableHeaderCellBlock[]} */
  542. #cells;
  543. /**
  544. * @param {_MDTableCellBlock[]|_MDTableHeaderCellBlock[]} cells
  545. */
  546. constructor(cells) {
  547. super();
  548. this.#cells = cells;
  549. }
  550. /**
  551. * @param {_MDHAlign[]} alignments
  552. */
  553. applyAlignments(alignments) {
  554. for (var i = 0; i < this.#cells.length; i++) {
  555. let cell = this.#cells[i];
  556. let align = i < alignments.length ? alignments[i] : null;
  557. cell.align = align;
  558. }
  559. }
  560. toHTML(state) {
  561. let cellsHTML = _MDBlock.toHTML(this.#cells, state);
  562. return `<tr${this.htmlAttributes()}>\n${cellsHTML}\n</tr>`;
  563. }
  564. }
  565. class _MDTableBlock extends _MDBlock {
  566. /** @var {_MDTableRowBlock} */
  567. #headerRow;
  568. /** @var {_MDTableRowBlock[]} */
  569. #bodyRows;
  570. /**
  571. * @param {_MDTableRowBlock} headerRow
  572. * @param {_MDTableRowBlock[]} bodyRows
  573. */
  574. constructor(headerRow, bodyRows) {
  575. super();
  576. this.#headerRow = headerRow;
  577. this.#bodyRows = bodyRows;
  578. }
  579. toHTML(state) {
  580. let headerRowHTML = this.#headerRow.toHTML(state);
  581. let bodyRowsHTML = _MDBlock.toHTML(this.#bodyRows, state);
  582. return `<table${this.htmlAttributes()}>\n<thead>\n${headerRowHTML}\n</thead>\n<tbody>\n${bodyRowsHTML}\n</tbody>\n</table>`;
  583. }
  584. }
  585. class _MDDefinitionListBlock extends _MDBlock {
  586. /** @var {_MDBlock[]} */
  587. #content;
  588. /**
  589. * @param {_MDBlock[]} content
  590. */
  591. constructor(content) {
  592. super();
  593. this.#content = content;
  594. }
  595. toHTML(state) {
  596. let contentHTML = _MDBlock.toHTML(this.#content, state);
  597. return `<dl${this.htmlAttributes()}>\n${contentHTML}\n</dl>`;
  598. }
  599. }
  600. class _MDDefinitionTermBlock extends _MDBlock {
  601. /** @var {_MDBlock} */
  602. #content;
  603. /**
  604. * @param {_MDBlock} content
  605. */
  606. constructor(content) {
  607. super();
  608. this.#content = content;
  609. }
  610. toHTML(state) {
  611. let contentHTML = this.#content.toHTML(state);
  612. return `<dt${this.htmlAttributes()}>${contentHTML}</dt>`;
  613. }
  614. }
  615. class _MDDefinitionDefinitionBlock extends _MDBlock {
  616. /** @var {_MDBlock} */
  617. #content;
  618. /**
  619. * @param {_MDBlock} content
  620. */
  621. constructor(content) {
  622. super();
  623. this.#content = content;
  624. }
  625. toHTML(state) {
  626. let contentHTML = this.#content.toHTML(state);
  627. return `<dd${this.htmlAttributes()}>${contentHTML}</dd>`;
  628. }
  629. }
  630. class _MDInlineBlock extends _MDBlock {
  631. /** @var {_MDSpan[]} */
  632. #content;
  633. /**
  634. * @param {_MDSpan[]} content
  635. */
  636. constructor(content) {
  637. super();
  638. this.#content = content;
  639. }
  640. toHTML(state) {
  641. return _MDSpan.toHTML(this.#content, state);
  642. }
  643. }
  644. class _MDHTMLTag {
  645. /** @var {String} */
  646. fullTag;
  647. /** @var {String} */
  648. tagName;
  649. /** @var {Boolean} */
  650. isCloser;
  651. /** @var {Object} */
  652. attributes;
  653. /**
  654. * @param {String} fullTag
  655. * @param {String} tagName
  656. * @param {Boolean} isCloser
  657. * @param {Object} attributes
  658. */
  659. constructor(fullTag, tagName, isCloser, attributes) {
  660. this.fullTag = fullTag;
  661. this.tagName = tagName;
  662. this.isCloser = isCloser;
  663. this.attributes = attributes;
  664. }
  665. }
  666. class _MDState {
  667. /** @var {String[]} */
  668. lines = [];
  669. /** @var {Object} */
  670. #abbreviations = {};
  671. /** @var {Object} */
  672. #abbreviationRegexes = {};
  673. /** @var {Object} */
  674. #footnotes = {};
  675. /** @var {number} */
  676. p = 0;
  677. /** @var {_MDState|null} */
  678. #parent = null;
  679. /** @var {Object} */
  680. get abbreviations() {
  681. return (this.#parent) ? this.#parent.abbreviations : this.#abbreviations;
  682. }
  683. /** @var {Object} */
  684. get abbreviationRegexes() {
  685. return (this.#parent) ? this.#parent.abbreviationRegexes : this.#abbreviationRegexes;
  686. }
  687. /** @var {Object} */
  688. get footnotes() {
  689. return (this.#parent) ? this.#parent.footnotes : this.#footnotes;
  690. }
  691. /**
  692. * @param {String[]} lines
  693. */
  694. copy(lines) {
  695. let cp = new _MDState();
  696. cp.#parent = this;
  697. cp.lines = lines;
  698. cp.p = 0;
  699. return cp;
  700. }
  701. /**
  702. * @param {String} abbreviation
  703. * @param {String} definition
  704. */
  705. defineAbbreviation(abbreviation, definition) {
  706. if (this.#parent) {
  707. this.#parent.defineAbbreviation(abbreviation, definition);
  708. return;
  709. }
  710. this.#abbreviations[abbreviation] = definition;
  711. let regex = new RegExp("\\b(" + abbreviation + ")\\b", "ig");
  712. this.#abbreviationRegexes[abbreviation] = regex;
  713. }
  714. /**
  715. * @param {String} symbol
  716. * @param {_MDBlock} footnote
  717. */
  718. defineFootnote(symbol, footnote) {
  719. if (this.#parent) {
  720. this.#parent.defineFootnote(symbol, footnote);
  721. } else {
  722. this.#footnotes[symbol] = footnote;
  723. }
  724. }
  725. hasLines(minCount, p=-1) {
  726. let relativeTo = (p < 0) ? this.p : p;
  727. return relativeTo + minCount <= this.lines.length;
  728. }
  729. }
  730. class Markdown {
  731. /**
  732. * @param {String} line
  733. */
  734. static #stripIndent(line, count=1) {
  735. let regex = new RegExp(`^(: {1,4}|\\t){${count}}`);
  736. return line.replace(regex, '');
  737. }
  738. /**
  739. * @param {String} line
  740. * @param {Boolean} fullIndentsOnly
  741. * @returns {Number} indent count
  742. */
  743. static #countIndents(line, fullIndentsOnly=false) {
  744. var count = 0;
  745. var lastLine = line;
  746. while (line.length > 0) {
  747. line = (fullIndentsOnly)
  748. ? line.replace(/^(?: {4}|\t)/, '')
  749. : line.replace(/^(?: {1,4}|\t)/, '');
  750. if (line != lastLine) {
  751. count++;
  752. } else {
  753. break;
  754. }
  755. lastLine = line;
  756. }
  757. return count;
  758. }
  759. /**
  760. * @param {_MDState} state
  761. * @returns {_MDBlock[]}
  762. */
  763. static #readBlocks(state) {
  764. var blocks = [];
  765. while (state.hasLines(1)) {
  766. let block = this.#readNextBlock(state);
  767. if (block) {
  768. blocks.push(block);
  769. } else {
  770. break;
  771. }
  772. }
  773. return blocks;
  774. }
  775. /**
  776. * @param {_MDState} state
  777. * @returns {_MDBlock}
  778. */
  779. static #readNextBlock(state) {
  780. while (state.hasLines(1) && state.lines[state.p].trim().length == 0) {
  781. state.p++;
  782. }
  783. var block;
  784. block = this.#readUnderlineHeader(state); if (block) return block;
  785. block = this.#readHashHeader(state); if (block) return block;
  786. block = this.#readBlockQuote(state); if (block) return block;
  787. block = this.#readUnorderedList(state); if (block) return block;
  788. block = this.#readOrderedList(state); if (block) return block;
  789. block = this.#readFencedCodeBlock(state); if (block) return block;
  790. block = this.#readIndentedCodeBlock(state); if (block) return block;
  791. block = this.#readHorizontalRule(state); if (block) return block;
  792. block = this.#readTable(state); if (block) return block;
  793. block = this.#readFootnoteDef(state); if (block) return block;
  794. block = this.#readAbbreviationDef(state); if (block) return block;
  795. block = this.#readDefinitionList(state); if (block) return block;
  796. block = this.#readParagraph(state); if (block) return block;
  797. return null;
  798. }
  799. static #htmlTagNameFirstRegex = /[a-z]/i;
  800. static #htmlTagNameMedialRegex = /[a-z0-9]/i;
  801. static #htmlAttributeNameFirstRegex = /[a-z]/i;
  802. static #htmlAttributeNameMedialRegex = /[a-z0-9-]/i;
  803. static #whitespaceCharRegex = /\s/;
  804. /**
  805. * @param {String} line
  806. * @returns {_MDHTMLTag|null} HTML tag if possible
  807. */
  808. static #htmlTag(line) {
  809. let expectOpenBracket = 0;
  810. let expectCloserOrName = 1;
  811. let expectName = 2;
  812. let expectAttributeNameOrEnd = 3;
  813. let expectEqualsOrAttributeOrEnd = 4;
  814. let expectAttributeValue = 5;
  815. let expectCloseBracket = 6;
  816. var isCloser = false;
  817. var tagName = '';
  818. var attributeName = '';
  819. var attributeValue = '';
  820. var attributeQuote = null;
  821. var attributes = {};
  822. var fullTag = null;
  823. let endAttribute = function() {
  824. if (attributeName.length > 0) {
  825. if (attributeValue.length > 0 || attributeQuote) {
  826. attributes[attributeName] = attributeValue;
  827. } else {
  828. attributes[attributeName] = true;
  829. }
  830. }
  831. attributeName = '';
  832. attributeValue = '';
  833. attributeQuote = null;
  834. };
  835. var expect = expectOpenBracket;
  836. for (var p = 0; p < line.length && fullTag === null; p++) {
  837. let ch = line.substring(p, p + 1);
  838. let isWhitespace = this.#whitespaceCharRegex.exec(ch) !== null;
  839. switch (expect) {
  840. case expectOpenBracket:
  841. if (ch != '<') return null;
  842. expect = expectCloserOrName;
  843. break;
  844. case expectCloserOrName:
  845. if (ch == '/') {
  846. isCloser = true;
  847. } else {
  848. p--;
  849. }
  850. expect = expectName;
  851. break;
  852. case expectName:
  853. if (tagName.length == 0) {
  854. if (this.#htmlTagNameFirstRegex.exec(ch) === null) return null;
  855. tagName += ch;
  856. } else {
  857. if (this.#htmlTagNameMedialRegex.exec(ch)) {
  858. tagName += ch;
  859. } else {
  860. p--;
  861. expect = (isCloser) ? expectCloseBracket : expectAttributeNameOrEnd;
  862. }
  863. }
  864. break;
  865. case expectAttributeNameOrEnd:
  866. if (attributeName.length == 0) {
  867. if (isWhitespace) {
  868. // skip whitespace
  869. } else if (ch == '/') {
  870. expect = expectCloseBracket;
  871. } else if (ch == '>') {
  872. fullTag = line.substring(0, p + 1);
  873. break;
  874. } else if (this.#htmlAttributeNameFirstRegex.exec(ch)) {
  875. attributeName += ch;
  876. } else {
  877. return null;
  878. }
  879. } else if (isWhitespace) {
  880. expect = expectEqualsOrAttributeOrEnd;
  881. } else if (ch == '/') {
  882. endAttribute();
  883. expect = expectCloseBracket;
  884. } else if (ch == '>') {
  885. endAttribute();
  886. fullTag = line.substring(0, p + 1);
  887. break;
  888. } else if (ch == '=') {
  889. expect = expectAttributeValue;
  890. } else if (this.#htmlAttributeNameMedialRegex.exec(ch)) {
  891. attributeName += ch;
  892. } else {
  893. return null;
  894. }
  895. break;
  896. case expectEqualsOrAttributeOrEnd:
  897. if (ch == '=') {
  898. expect = expectAttributeValue;
  899. } else if (isWhitespace) {
  900. // skip whitespace
  901. } else if (ch == '/') {
  902. expect = expectCloseBracket;
  903. } else if (ch == '>') {
  904. fullTag = line.substring(0, p + 1);
  905. break;
  906. } else if (this.#htmlAttributeNameFirstRegex.exec(ch)) {
  907. endAttribute();
  908. expect = expectAttributeNameOrEnd;
  909. p--;
  910. }
  911. break;
  912. case expectAttributeValue:
  913. if (attributeValue.length == 0) {
  914. if (attributeQuote === null) {
  915. if (isWhitespace) {
  916. // skip whitespace
  917. } else if (ch == '"' || ch == "'") {
  918. attributeQuote = ch;
  919. } else {
  920. attributeQuote = ''; // explicitly unquoted
  921. p--;
  922. }
  923. } else {
  924. if (ch === attributeQuote) {
  925. // Empty string
  926. endAttribute();
  927. expect = expectAttributeNameOrEnd;
  928. } else if (attributeQuote === '' && (ch == '/' || ch == '>')) {
  929. return null;
  930. } else {
  931. attributeValue += ch;
  932. }
  933. }
  934. } else {
  935. if (ch === attributeQuote) {
  936. endAttribute();
  937. expect = expectAttributeNameOrEnd;
  938. } else if (attributeQuote === '' && isWhitespace) {
  939. endAttribute();
  940. expect = expectAttributeNameOrEnd;
  941. } else {
  942. attributeValue += ch;
  943. }
  944. }
  945. break;
  946. case expectCloseBracket:
  947. if (isWhitespace) {
  948. // ignore whitespace
  949. } else if (ch == '>') {
  950. fullTag = line.substring(0, p + 1);
  951. break;
  952. }
  953. break;
  954. }
  955. }
  956. if (fullTag === null) return null;
  957. endAttribute();
  958. return new _MDHTMLTag(fullTag, tagName, isCloser, attributes);
  959. }
  960. static #textWhitespaceRegex = /^(\s*)(?:(\S|\S.*\S)(\s*?))?$/; // 1=leading WS, 2=text, 3=trailing WS
  961. // Modified from https://urlregex.com/ to remove capture groups. Matches fully qualified URLs only.
  962. static #baseURLRegex = /(?:(?:(?:[a-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[a-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[a-z0-9\.\-]+)(?:(?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/i;
  963. // Modified from https://emailregex.com/ to remove capture groups.
  964. static #baseEmailRegex = /(?:(?:[^<>()\[\]\\.,;:\s@"]+(?:\.[^<>()\[\]\\.,;:\s@"]+)*)|(?:".+"))@(?:(?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(?:(?:[a-z\-0-9]+\.)+[a-z]{2,}))/i;
  965. static #footnoteWithTitleRegex = /^\[\^(\d+?)\s+"(.*?)"\]/; // 1=symbol, 2=title
  966. static #footnoteRegex = /^\[\^(\d+?)\]/; // 1=symbol
  967. static #labelRegex = /^\[(.*?)\]/; // 1=content
  968. static #urlWithTitleRegex = /^\((\S+?)\s+"(.*?)"\)/i; // 1=URL, 2=title
  969. static #urlRegex = /^\((\S+?)\)/i; // 1=URL
  970. static #emailWithTitleRegex = new RegExp("^\\(\\s*(" + this.#baseEmailRegex.source + ")\\s+\"(.*?)\"\\s*\\)", "i"); // 1=email, 2=title
  971. static #emailRegex = new RegExp("^\\(\\s*(" + this.#baseEmailRegex.source + ")\\s*\\)", "i"); // 1=email
  972. static #simpleURLRegex = new RegExp("^<" + this.#baseURLRegex.source + ">", "i"); // 1=URL
  973. static #simpleEmailRegex = new RegExp("^<" + this.#baseEmailRegex.source + ">", "i"); // 1=email
  974. /**
  975. * @param {String} line
  976. * @returns {_MDToken[]} tokens
  977. */
  978. static #tokenize(line) {
  979. var tokens = [];
  980. var text = '';
  981. var expectLiteral = false;
  982. var groups = null;
  983. var tag = null;
  984. const endText = function() {
  985. if (text.length == 0) return;
  986. let textGroups = Markdown.#textWhitespaceRegex.exec(text);
  987. if (textGroups !== null) {
  988. if (textGroups[1].length > 0) {
  989. tokens.push(new _MDToken(textGroups[1], _MDTokenType.Whitespace, textGroups[1]));
  990. }
  991. if (textGroups[2] !== undefined && textGroups[2].length > 0) {
  992. tokens.push(new _MDToken(textGroups[2], _MDTokenType.Text, textGroups[2]));
  993. }
  994. if (textGroups[3] !== undefined && textGroups[3].length > 0) {
  995. tokens.push(new _MDToken(textGroups[3], _MDTokenType.Whitespace, textGroups[3]));
  996. }
  997. } else {
  998. tokens.push(new _MDToken(text, _MDTokenType.Text, text));
  999. }
  1000. text = '';
  1001. }
  1002. for (var p = 0; p < line.length; p++) {
  1003. let ch = line.substring(p, p + 1);
  1004. let remainder = line.substring(p);
  1005. if (expectLiteral) {
  1006. text += ch;
  1007. expectLiteral = false;
  1008. continue;
  1009. }
  1010. if (ch == '\\') {
  1011. expectLiteral = true;
  1012. } else if (ch == '*') {
  1013. endText();
  1014. tokens.push(new _MDToken(ch, _MDTokenType.Asterisk));
  1015. } else if (ch == '_') {
  1016. endText();
  1017. tokens.push(new _MDToken(ch, _MDTokenType.Underscore));
  1018. } else if (ch == '`') {
  1019. endText();
  1020. tokens.push(new _MDToken(ch, _MDTokenType.Backtick));
  1021. } else if (ch == '~') {
  1022. endText();
  1023. tokens.push(new _MDToken(ch, _MDTokenType.Tilde));
  1024. } else if (ch == '!') {
  1025. endText();
  1026. tokens.push(new _MDToken(ch, _MDTokenType.Bang));
  1027. } else if (groups = this.#footnoteWithTitleRegex.exec(remainder)) {
  1028. // Footnote with title [^1 "Foo"]
  1029. endText();
  1030. tokens.push(new _MDToken(groups[0], _MDTokenType.Footnote, groups[1], groups[2]));
  1031. p += groups[0].length - 1;
  1032. } else if (groups = this.#footnoteRegex.exec(remainder)) {
  1033. // Footnote without title [^1]
  1034. endText();
  1035. tokens.push(new _MDToken(groups[0], _MDTokenType.Footnote, groups[1]));
  1036. p += groups[0].length - 1;
  1037. } else if (groups = this.#labelRegex.exec(remainder)) {
  1038. // Label/ref for link/image [Foo]
  1039. endText();
  1040. tokens.push(new _MDToken(groups[0], _MDTokenType.Label, groups[1]));
  1041. p += groups[0].length - 1;
  1042. } else if (groups = this.#urlWithTitleRegex.exec(remainder)) {
  1043. // URL with title (https://foo "Bar")
  1044. endText();
  1045. tokens.push(new _MDToken(groups[0], _MDTokenType.URL, groups[1], groups[2]));
  1046. p += groups[0].length - 1;
  1047. } else if (groups = this.#emailWithTitleRegex.exec(remainder)) {
  1048. // Email address with title (user@example.com "Foo")
  1049. endText();
  1050. tokens.push(new _MDToken(groups[0], _MDTokenType.Email, groups[1], groups[2]));
  1051. p += groups[0].length - 1;
  1052. } else if (groups = this.#urlRegex.exec(remainder)) {
  1053. // URL (https://example.com)
  1054. endText();
  1055. tokens.push(new _MDToken(groups[0], _MDTokenType.URL, groups[1]));
  1056. p += groups[0].length - 1;
  1057. } else if (groups = this.#emailRegex.exec(remainder)) {
  1058. // Email (user@example.com)
  1059. endText();
  1060. tokens.push(new _MDToken(groups[0], _MDTokenType.Email, groups[1]));
  1061. p += groups[0].length - 1;
  1062. } else if (groups = this.#simpleURLRegex.exec(remainder)) {
  1063. // Simple URL <https://example.com>
  1064. endText();
  1065. tokens.push(new _MDToken(groups[0], _MDTokenType.SimpleLink, groups[1]));
  1066. p += groups[0].length - 1;
  1067. } else if (groups = this.#simpleEmailRegex.exec(remainder)) {
  1068. // Simple email <user@example.com>
  1069. endText();
  1070. tokens.push(new _MDToken(groups[0], _MDTokenType.SimpleEmail, groups[1]));
  1071. p += groups[0].length - 1;
  1072. } else if (tag = this.#htmlTag(remainder)) {
  1073. endText();
  1074. tokens.push(new _MDToken(tag.fullTag, _MDTokenType.HTMLTag, tag.fullTag, null, tag));
  1075. p += tag.fullTag.length - 1;
  1076. } else {
  1077. text += ch;
  1078. }
  1079. }
  1080. endText();
  1081. return tokens;
  1082. }
  1083. static #firstTokenIndex(tokens, pattern, startIndex=0) {
  1084. for (var t = startIndex; t < tokens.length; t++) {
  1085. var matchedAll = true;
  1086. for (var p = 0; p < pattern.length; p++) {
  1087. var t0 = t + p;
  1088. if (t0 >= tokens.length) return null;
  1089. let token = tokens[t0];
  1090. let elem = pattern[p];
  1091. if (elem == _MDTokenType.META_AnyNonWhitespace) {
  1092. if (token instanceof _MDToken && token.type == _MDTokenType.Whitespace) {
  1093. matchedAll = false;
  1094. break;
  1095. }
  1096. } else {
  1097. if (!(token instanceof _MDToken) || token.type != elem) {
  1098. matchedAll = false;
  1099. break;
  1100. }
  1101. }
  1102. }
  1103. if (matchedAll) {
  1104. return t;
  1105. }
  1106. }
  1107. return null;
  1108. }
  1109. /**
  1110. * @param {_MDState} state
  1111. * @param {String} line
  1112. * @returns {_MDBlock|null}
  1113. */
  1114. static #readInline(state, line) {
  1115. var tokens = this.#tokenize(line);
  1116. return new _MDInlineBlock(this.#tokensToSpans(tokens));
  1117. }
  1118. /**
  1119. * @param {Array} tokens
  1120. * @returns {_MDSpan[]} spans
  1121. */
  1122. static #tokensToSpans(tokens) {
  1123. var spans = tokens.slice(0, tokens.length);
  1124. var anyChanges = false;
  1125. var index, index0;
  1126. // First pass - contiguous constructs
  1127. do {
  1128. anyChanges = false;
  1129. // ![alt](image.jpg)
  1130. if ((index = this.#firstTokenIndex(spans, [
  1131. _MDTokenType.Bang,
  1132. _MDTokenType.Label,
  1133. _MDTokenType.URL,
  1134. ])) !== null) {
  1135. let alt = spans[index + 1];
  1136. let url = spans[index + 2];
  1137. spans.splice(index, 3, new _MDImageSpan(url.content, alt.content, url.extra));
  1138. anyChanges = true;
  1139. }
  1140. // ![alt][ref]
  1141. else if ((index = this.#firstTokenIndex(spans, [
  1142. _MDTokenType.Bang,
  1143. _MDTokenType.Label,
  1144. _MDTokenType.Label,
  1145. ])) !== null) {
  1146. let alt = spans[index + 1];
  1147. let ref = spans[index + 2];
  1148. spans.splice(index, 3, new _MDReferencedImageSpan(ref.content, alt.content));
  1149. anyChanges = true;
  1150. }
  1151. // [text](link.html)
  1152. else if ((index = this.#firstTokenIndex(spans, [
  1153. _MDTokenType.Label,
  1154. _MDTokenType.URL,
  1155. ])) !== null) {
  1156. let text = spans[index + 0];
  1157. let url = spans[index + 1];
  1158. spans.splice(index, 2, new _MDLinkSpan(url.content, this.#readInline(state, text.content)));
  1159. anyChanges = true;
  1160. }
  1161. // [text][ref]
  1162. else if ((index = this.#firstTokenIndex(spans, [
  1163. _MDTokenType.Label,
  1164. _MDTokenType.Label,
  1165. ])) !== null) {
  1166. let text = spans[index + 0];
  1167. let ref = spans[index + 1];
  1168. spans.splice(index, 2, new _MDReferencedLinkSpan(ref, this.#readInline(state, text)));
  1169. anyChanges = true;
  1170. }
  1171. // [^1]
  1172. else if ((index = this.#firstTokenIndex(spans, [
  1173. _MDTokenType.Footnote,
  1174. ])) !== null) {
  1175. let symbol = spans[index];
  1176. spans.splice(index, 1, new _MDFootnoteReferenceSpan(symbol.content));
  1177. anyChanges = true;
  1178. }
  1179. } while (anyChanges);
  1180. /**
  1181. * @param {_MDTokenType[]} delimiter
  1182. * @param {Set<_MDTokenType>} disallowedInnerTokens
  1183. */
  1184. const matchPair = function(delimiter, disallowedInnerTokens=new Set()) {
  1185. var searchStart = 0;
  1186. var hasNewStart = false;
  1187. do {
  1188. hasNewStart = false;
  1189. let startIndex = Markdown.#firstTokenIndex(spans, delimiter.concat(_MDTokenType.META_AnyNonWhitespace), searchStart);
  1190. if (startIndex === null) return null;
  1191. let endIndex = Markdown.#firstTokenIndex(spans, [_MDTokenType.META_AnyNonWhitespace].concat(delimiter), startIndex + delimiter.length);
  1192. if (endIndex === null) return null;
  1193. let contentTokens = spans.slice(startIndex + delimiter.length, endIndex + 1);
  1194. if (disallowedInnerTokens.size > 0) {
  1195. for (const token of contentTokens) {
  1196. if (token instanceof _MDToken && disallowedInnerTokens.has(token.type)) {
  1197. searchStart = startIndex + 1;
  1198. hasNewStart = true;
  1199. break;
  1200. }
  1201. }
  1202. if (hasNewStart) continue;
  1203. }
  1204. let contentSpans = Markdown.#tokensToSpans(contentTokens);
  1205. return {
  1206. startIndex: startIndex,
  1207. toDelete: endIndex - startIndex + delimiter.length + 1,
  1208. content: new _MDMultiSpan(contentSpans),
  1209. };
  1210. } while (hasNewStart);
  1211. return null;
  1212. };
  1213. var spanMatch = null;
  1214. // Second pass - paired constructs. Prioritize pairs with no other paired tokens inside.
  1215. const delimiterTokens = new Set([
  1216. _MDTokenType.Backtick,
  1217. _MDTokenType.Tilde,
  1218. _MDTokenType.Asterisk,
  1219. _MDTokenType.Underscore
  1220. ]);
  1221. for (let disallowed of [ delimiterTokens, new Set() ]) {
  1222. do {
  1223. anyChanges = false;
  1224. // ``code``
  1225. if (spanMatch = matchPair([ _MDTokenType.Backtick, _MDTokenType.Backtick ], disallowed)) {
  1226. spans.splice(spanMatch.startIndex, spanMatch.toDelete, new _MDCodeSpan(spanMatch.content));
  1227. anyChanges = true;
  1228. }
  1229. // ~~strike~~
  1230. else if (spanMatch = matchPair([ _MDTokenType.Tilde, _MDTokenType.Tilde ], disallowed)) {
  1231. spans.splice(spanMatch.startIndex, spanMatch.toDelete, new _MDStrikethroughSpan(spanMatch.content));
  1232. anyChanges = true;
  1233. }
  1234. // **strong** __strong__
  1235. else if (spanMatch = (matchPair([ _MDTokenType.Asterisk, _MDTokenType.Asterisk ], disallowed) ||
  1236. matchPair([ _MDTokenType.Underscore, _MDTokenType.Underscore ], disallowed))) {
  1237. spans.splice(spanMatch.startIndex, spanMatch.toDelete, new _MDStrongSpan(spanMatch.content));
  1238. anyChanges = true;
  1239. }
  1240. // `code`
  1241. if (spanMatch = matchPair([ _MDTokenType.Backtick ], disallowed)) {
  1242. spans.splice(spanMatch.startIndex, spanMatch.toDelete, new _MDCodeSpan(spanMatch.content));
  1243. anyChanges = true;
  1244. }
  1245. // ~strike~
  1246. else if (spanMatch = matchPair([ _MDTokenType.Tilde ], disallowed)) {
  1247. spans.splice(spanMatch.startIndex, spanMatch.toDelete, new _MDStrikethroughSpan(spanMatch.content));
  1248. anyChanges = true;
  1249. }
  1250. // *strong* _strong_
  1251. else if (spanMatch = (matchPair([ _MDTokenType.Asterisk ], disallowed) ||
  1252. matchPair([ _MDTokenType.Underscore ], disallowed))) {
  1253. spans.splice(spanMatch.startIndex, spanMatch.toDelete, new _MDEmphasisSpan(spanMatch.content));
  1254. anyChanges = true;
  1255. }
  1256. } while (anyChanges);
  1257. }
  1258. spans = spans.map(function(span) {
  1259. if (span instanceof _MDToken) {
  1260. return new _MDTextSpan(span.original);
  1261. } else if (span instanceof _MDSpan) {
  1262. return span;
  1263. } else {
  1264. throw new Error(`Unexpected span type ${span.constructor.name}`);
  1265. }
  1266. });
  1267. return spans;
  1268. }
  1269. /**
  1270. * Reads the contents of something like a list item
  1271. * @param {_MDState} state
  1272. * @param {number} firstLineStartPos
  1273. * @param {RegExp} stopRegex
  1274. * @param {Boolean} inList
  1275. * @returns {_MDBlock}
  1276. */
  1277. static #readInteriorContent(state, firstLineStartPos, stopRegex, inList=false) {
  1278. var p = state.p;
  1279. var seenBlankLine = false;
  1280. var needsBlocks = false;
  1281. var lines = [];
  1282. while (p < state.lines.length) {
  1283. let line = state.lines[p++];
  1284. if (p == state.p + 1) {
  1285. line = line.substring(firstLineStartPos);
  1286. }
  1287. let isBlank = line.trim().length == 0;
  1288. let isIndented = /^\s+/.exec(line) !== null;
  1289. if (isBlank) {
  1290. seenBlankLine = true;
  1291. lines.push(line.trim());
  1292. } else if (stopRegex && stopRegex.exec(line)) {
  1293. p--;
  1294. break;
  1295. } else if (isIndented) {
  1296. if (seenBlankLine) {
  1297. needsBlocks = true;
  1298. }
  1299. lines.push(this.#stripIndent(line));
  1300. } else {
  1301. if (seenBlankLine) {
  1302. p--;
  1303. break;
  1304. }
  1305. lines.push(this.#stripIndent(line));
  1306. }
  1307. }
  1308. while (lines.length > 0 && lines[lines.length - 1].trim().length == 0) {
  1309. lines.pop();
  1310. }
  1311. if (needsBlocks) {
  1312. let substate = state.copy(lines);
  1313. let blocks = this.#readBlocks(substate);
  1314. state.p = p;
  1315. return new _MDMultiBlock(blocks);
  1316. } else {
  1317. state.p = p;
  1318. return this.#readInline(state, lines.join("\n"));
  1319. }
  1320. }
  1321. /**
  1322. * @param {_MDState} state
  1323. * @returns {_MDBlock|null}
  1324. */
  1325. static #readUnderlineHeader(state) {
  1326. var p = state.p;
  1327. if (!state.hasLines(2)) return null;
  1328. let contentLine = state.lines[p++].trim();
  1329. let underLine = state.lines[p++].trim();
  1330. if (contentLine == '') return null;
  1331. if (/^=+$/.exec(underLine)) {
  1332. state.p = p;
  1333. return new _MDHeaderBlock(1, this.#readInline(state, contentLine));
  1334. }
  1335. if (/^\-+$/.exec(underLine)) {
  1336. state.p = p;
  1337. return new _MDHeaderBlock(2, this.#readInline(state, contentLine));
  1338. }
  1339. return null;
  1340. }
  1341. static #hashHeaderRegex = /^(#{1,6})\s*([^#].*?)\s*\#*\s*$/; // 1=hashes, 2=content
  1342. /**
  1343. * @param {_MDState} state
  1344. * @returns {_MDBlock|null}
  1345. */
  1346. static #readHashHeader(state) {
  1347. var p = state.p;
  1348. var groups = this.#hashHeaderRegex.exec(state.lines[p++]);
  1349. if (groups === null) return null;
  1350. state.p = p;
  1351. return new _MDHeaderBlock(groups[1].length, this.#readInline(state, groups[2]));
  1352. }
  1353. /**
  1354. * @param {_MDState} state
  1355. * @returns {_MDBlock|null}
  1356. */
  1357. static #readBlockQuote(state) {
  1358. var blockquoteLines = [];
  1359. var p = state.p;
  1360. while (p < state.lines.length) {
  1361. let line = state.lines[p++];
  1362. if (line.startsWith(">")) {
  1363. blockquoteLines.push(line);
  1364. } else {
  1365. break;
  1366. }
  1367. }
  1368. if (blockquoteLines.length > 0) {
  1369. let contentLines = blockquoteLines.map(function(line) {
  1370. return line.substring(1).replace(/^ {0,3}\t?/, '');
  1371. });
  1372. let substate = state.copy(contentLines);
  1373. let quotedBlocks = this.#readBlocks(substate);
  1374. state.p = p;
  1375. return new _MDBlockquoteBlock(quotedBlocks);
  1376. }
  1377. return null;
  1378. }
  1379. static #unorderedListRegex = /^([\*\+\-]\s+)(.*)$/; // 1=bullet, 2=content
  1380. static #unorderedListItemRegex = /^[\*\+\-]\s+/;
  1381. /**
  1382. * @param {_MDState} state
  1383. * @returns {_MDListItemBlock|null}
  1384. */
  1385. static #readUnorderedListItem(state) {
  1386. var p = state.p;
  1387. let line = state.lines[p];
  1388. let groups = this.#unorderedListRegex.exec(line);
  1389. if (groups === null) return null;
  1390. return new _MDListItemBlock(this.#readInteriorContent(state, groups[1].length, this.#unorderedListItemRegex, true));
  1391. }
  1392. /**
  1393. * @param {_MDState} state
  1394. * @returns {_MDBlock|null}
  1395. */
  1396. static #readUnorderedList(state) {
  1397. var items = [];
  1398. var item = null;
  1399. do {
  1400. item = this.#readUnorderedListItem(state);
  1401. if (item) items.push(item);
  1402. } while (item);
  1403. if (items.length == 0) return null;
  1404. return new _MDUnorderedListBlock(items);
  1405. }
  1406. static #orderedListRegex = /^(\d+)(\.\s+)(.*)$/; // 1=number, 2=dot, 3=content
  1407. static #orderedListItemRegex = /^\d+\.\s+/;
  1408. /**
  1409. * @param {_MDState} state
  1410. * @returns {_MDListItemBlock|null}
  1411. */
  1412. static #readOrderedListItem(state) {
  1413. var p = state.p;
  1414. let line = state.lines[p];
  1415. let groups = this.#orderedListRegex.exec(line);
  1416. if (groups === null) return null;
  1417. return new _MDListItemBlock(this.#readInteriorContent(state, groups[1].length + groups[2].length, this.#orderedListItemRegex, true));
  1418. }
  1419. /**
  1420. * @param {_MDState} state
  1421. * @returns {_MDBlock|null}
  1422. */
  1423. static #readOrderedList(state) {
  1424. var items = [];
  1425. var item = null;
  1426. do {
  1427. item = this.#readOrderedListItem(state);
  1428. if (item) items.push(item);
  1429. } while (item);
  1430. if (items.length == 0) return null;
  1431. return new _MDOrderedListBlock(items);
  1432. }
  1433. /**
  1434. * @param {_MDState} state
  1435. * @returns {_MDBlock|null}
  1436. */
  1437. static #readFencedCodeBlock(state) {
  1438. if (!state.hasLines(2)) return null;
  1439. var p = state.p;
  1440. if (state.lines[p++].trim() != '```') return null;
  1441. var codeLines = [];
  1442. while (state.hasLines(1, p)) {
  1443. let line = state.lines[p++];
  1444. if (line.trim() == '```') {
  1445. state.p = p;
  1446. return new _MDCodeBlock(codeLines.join("\n"));
  1447. }
  1448. codeLines.push(line);
  1449. }
  1450. return null;
  1451. }
  1452. /**
  1453. * @param {_MDState} state
  1454. * @returns {_MDBlock|null}
  1455. */
  1456. static #readIndentedCodeBlock(state) {
  1457. var p = state.p;
  1458. var codeLines = [];
  1459. while (state.hasLines(1, p)) {
  1460. let line = state.lines[p++];
  1461. if (this.#countIndents(line, true) < 1) {
  1462. p--;
  1463. break;
  1464. }
  1465. codeLines.push(this.#stripIndent(line));
  1466. }
  1467. if (codeLines.length == 0) return null;
  1468. state.p = p;
  1469. return new _MDCodeBlock(codeLines.join("\n"));
  1470. }
  1471. static #horizontalRuleRegex = /^\s*(?:\-(?:\s*\-){2,}|\*(?:\s*\*){2,})\s*$/;
  1472. /**
  1473. * @param {_MDState} state
  1474. * @returns {_MDBlock|null}
  1475. */
  1476. static #readHorizontalRule(state) {
  1477. var p = state.p;
  1478. let line = state.lines[p++];
  1479. if (this.#horizontalRuleRegex.exec(line)) {
  1480. state.p = p;
  1481. return new _MDHorizontalRuleBlock();
  1482. }
  1483. return null;
  1484. }
  1485. /**
  1486. * @param {_MDState} state
  1487. * @param {Boolean} isHeader
  1488. * @return {_MDTableRowBlock|null}
  1489. */
  1490. static #readTableRow(state, isHeader) {
  1491. if (!state.hasLines(1)) return null;
  1492. var p = state.p;
  1493. let line = state.lines[p++].trim();
  1494. if (/.*\|.*/.exec(line) === null) return null;
  1495. if (line.startsWith('|')) line = line.substring(1);
  1496. if (line.endsWith('|')) line = line.substring(0, line.length - 1);
  1497. let cellTokens = line.split('|');
  1498. let cells = cellTokens.map(function(token) {
  1499. let content = Markdown.#readInline(state, token);
  1500. return isHeader ? new _MDTableHeaderCellBlock(content) : new _MDTableCellBlock(content);
  1501. });
  1502. state.p = p;
  1503. return new _MDTableRowBlock(cells);
  1504. }
  1505. /**
  1506. * @param {String} line
  1507. * @returns {_MDHAlign[]}
  1508. */
  1509. static #parseColumnAlignments(line) {
  1510. line = line.trim();
  1511. if (line.startsWith('|')) line = line.substring(1);
  1512. if (line.endsWith('|')) line = line.substring(0, line.length - 1);
  1513. return line.split('|').map(function(token) {
  1514. token = token.trim();
  1515. if (token.startsWith(':')) {
  1516. if (token.endsWith(':')) {
  1517. return _MDHAlign.Center;
  1518. }
  1519. return _MDHAlign.Left;
  1520. } else if (token.endsWith(':')) {
  1521. return _MDHAlign.Right;
  1522. }
  1523. return null;
  1524. });
  1525. }
  1526. static #tableDividerRegex = /^\s*[|]?(?:\s*[:]?-+[:]?\s*\|)(?:\s*[:]?-+[:]?\s*)[|]?\s*$/;
  1527. /**
  1528. * @param {_MDState} state
  1529. * @returns {_MDBlock|null}
  1530. */
  1531. static #readTable(state) {
  1532. if (!state.hasLines(2)) return null;
  1533. let startP = state.p;
  1534. let headerRow = this.#readTableRow(state, true);
  1535. if (headerRow === null) {
  1536. state.p = startP;
  1537. return null;
  1538. }
  1539. let dividerLine = state.lines[state.p++];
  1540. let dividerGroups = this.#tableDividerRegex.exec(dividerLine);
  1541. if (dividerGroups === null) {
  1542. state.p = startP;
  1543. return null;
  1544. }
  1545. let columnAlignments = this.#parseColumnAlignments(dividerLine);
  1546. headerRow.applyAlignments(columnAlignments);
  1547. var bodyRows = [];
  1548. while (state.hasLines(1)) {
  1549. let row = this.#readTableRow(state, false);
  1550. if (row === null) break;
  1551. row.applyAlignments(columnAlignments);
  1552. bodyRows.push(row);
  1553. }
  1554. return new _MDTableBlock(headerRow, bodyRows);
  1555. }
  1556. /**
  1557. * @param {_MDState} state
  1558. * @returns {_MDBlock|null}
  1559. */
  1560. static #readDefinitionList(state) {
  1561. var p = state.p;
  1562. var groups;
  1563. var termCount = 0;
  1564. var definitionCount = 0;
  1565. var defLines = [];
  1566. while (state.hasLines(1, p)) {
  1567. let line = state.lines[p++];
  1568. if (line.trim().length == 0) {
  1569. p--;
  1570. break;
  1571. }
  1572. if (/^\s+/.exec(line)) {
  1573. if (defLines.length == 0) return null;
  1574. defLines[defLines.length - 1] += "\n" + line;
  1575. } else if (/^:\s+/.exec(line)) {
  1576. defLines.push(line);
  1577. definitionCount++;
  1578. } else {
  1579. defLines.push(line);
  1580. termCount++;
  1581. }
  1582. }
  1583. if (termCount == 0 || definitionCount == 0) return null;
  1584. let blocks = defLines.map(function(line) {
  1585. if (groups = /^:\s+(.*)$/.exec(line)) {
  1586. return new _MDDefinitionDefinitionBlock(Markdown.#readInline(state, groups[1]));
  1587. } else {
  1588. return new _MDDefinitionTermBlock(Markdown.#readInline(state, line));
  1589. }
  1590. });
  1591. state.p = p;
  1592. return new _MDDefinitionListBlock(blocks);
  1593. }
  1594. /**
  1595. * @param {_MDState} state
  1596. * @returns {_MDBlock|null}
  1597. */
  1598. static #readFootnoteDef(state) {
  1599. var p = state.p;
  1600. let groups = /^\s*\[\^\s*([^\]]+)\s*\]:\s+(.*)\s*$/.exec(state.lines[p++]);
  1601. if (groups === null) return null;
  1602. let symbol = groups[1];
  1603. let def = groups[2];
  1604. while (state.hasLines(1, p)) {
  1605. let line = state.lines[p++];
  1606. if (/^\s+/.exec(line)) {
  1607. def += "\n" + line;
  1608. } else {
  1609. p--;
  1610. break;
  1611. }
  1612. }
  1613. state.p = p;
  1614. let content = this.#readInline(state, def);
  1615. state.defineFootnote(symbol, content);
  1616. state.p = p;
  1617. return new _MDMultiBlock([]);
  1618. }
  1619. /**
  1620. * @param {_MDState} state
  1621. * @returns {_MDBlock|null}
  1622. */
  1623. static #readAbbreviationDef(state) {
  1624. var p = state.p;
  1625. let line = state.lines[p++];
  1626. let groups = /^\s*\*\[([^\]]+?)\]:\s+(.*?)\s*$/.exec(line);
  1627. if (groups === null) return null;
  1628. let abbrev = groups[1];
  1629. let def = groups[2];
  1630. state.defineAbbreviation(abbrev, def);
  1631. state.p = p;
  1632. return new _MDMultiBlock([]);
  1633. }
  1634. /**
  1635. * @param {_MDState} state
  1636. * @returns {_MDBlock|null}
  1637. */
  1638. static #readParagraph(state) {
  1639. var paragraphLines = [];
  1640. var p = state.p;
  1641. while (p < state.lines.length) {
  1642. let line = state.lines[p++];
  1643. if (line.trim().length == 0) {
  1644. break;
  1645. }
  1646. paragraphLines.push(line);
  1647. }
  1648. if (paragraphLines.length > 0) {
  1649. state.p = p;
  1650. let content = paragraphLines.join("\n");
  1651. return new _MDParagraphBlock(this.#readInline(state, content));
  1652. }
  1653. return null;
  1654. }
  1655. /**
  1656. * @param {String} html
  1657. * @param {_MDState} state
  1658. * @returns {String}
  1659. */
  1660. static #postProcessFootnotes(html, state) {
  1661. let footnotes = state.footnotes;
  1662. if (Object.keys(footnotes).length == 0) return html;
  1663. var symbolOrder = [];
  1664. var footnoteOccurrences = {};
  1665. var footnoteIndex = 0;
  1666. html = html.replace(/<!--FNREF:{(\d+)}-->/g, function(match, symbol) {
  1667. footnoteIndex++;
  1668. symbol = symbol.toLowerCase();
  1669. if (!symbolOrder.includes(symbol)) {
  1670. symbolOrder.push(symbol);
  1671. }
  1672. var occurrences = footnoteOccurrences[symbol] || [];
  1673. occurrences.push(footnoteIndex);
  1674. footnoteOccurrences[symbol] = occurrences;
  1675. return `<sup id="footnoteref_${footnoteIndex}"><a href="#footnote_${symbol}">${symbol}</a></sup>`;
  1676. });
  1677. if (footnoteIndex == 0) return html;
  1678. html += '<div class="footnotes"><hr/>';
  1679. html += '<ol>';
  1680. for (const symbol of symbolOrder) {
  1681. let content = state.footnotes[symbol];
  1682. if (!content) continue;
  1683. html += `<li value="${symbol}" id="footnote_${symbol}">${content.toHTML(state)}`;
  1684. for (const ref of footnoteOccurrences[symbol]) {
  1685. html += ` <a href="#footnoteref_${ref}" class="footnote-backref" role="doc-backlink">↩︎</a>`;
  1686. }
  1687. html += `</li>\n`;
  1688. }
  1689. html += '</ol>';
  1690. html += '</div>';
  1691. // <!--FNREF:{symbol}-->
  1692. return html;
  1693. }
  1694. /**
  1695. * @param {String} markdown
  1696. * @returns {String} HTML
  1697. */
  1698. static toHTML(markdown) {
  1699. var state = new _MDState();
  1700. let lines = markdown.split(/(?:\n|\r|\r\n)/);
  1701. state.lines = lines;
  1702. let blocks = this.#readBlocks(state);
  1703. let html = _MDBlock.toHTML(blocks, state);
  1704. html = this.#postProcessFootnotes(html, state);
  1705. return html;
  1706. }
  1707. }