PHP and Javascript implementations of a simple markdown parser
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

markdown.js 42KB

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