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

markdown.js 48KB

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