PHP and Javascript implementations of a simple markdown parser
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

markdown.js 45KB

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