PHP and Javascript implementations of a simple markdown parser
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

markdown.js 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. class MDState {
  2. /** @var {object} */
  3. abbreviations = {};
  4. /** @var {object} */
  5. footnotes = {};
  6. }
  7. class MDConfig {
  8. }
  9. class MDBlock {
  10. /**
  11. * @returns {String} HTML
  12. */
  13. toHTML(config, state) {
  14. throw new Error("toHTML not implemented");
  15. }
  16. }
  17. class MDHTMLBlock extends MDBlock {
  18. /** @var {String} html */
  19. html;
  20. /**
  21. * @param {String} html
  22. */
  23. constructor(html) {
  24. super();
  25. this.html = html;
  26. }
  27. toHTML = (config, state) => this.html;
  28. }
  29. class MDHTMLWrappedBlock extends MDBlock {
  30. /** @var {String} */
  31. #beforeHTML;
  32. /** @var {MDBlock} */
  33. #content;
  34. /** @var {String} */
  35. #afterHTML;
  36. /**
  37. * @param {String} beforeHTML
  38. * @param {MDBlock} content
  39. * @param {String} afterHTML
  40. */
  41. constructor(beforeHTML, content, afterHTML) {
  42. super();
  43. if (!(content instanceof MDBlock)) {
  44. throw new Error("content is of type " + typeof(content) + " instead of MDBlock");
  45. }
  46. this.#beforeHTML = beforeHTML;
  47. this.#content = content;
  48. this.#afterHTML = afterHTML;
  49. }
  50. toHTML(config, state) {
  51. return this.#beforeHTML + this.#content.toHTML() + this.#afterHTML;
  52. }
  53. }
  54. class MDInlineBlock extends MDBlock {
  55. /** @var {String} */
  56. #html;
  57. /**
  58. * @param {String} line
  59. */
  60. constructor(line) {
  61. super();
  62. this.#html = line;
  63. }
  64. toHTML = (config, state) => "inline:" + this.#html;
  65. }
  66. class MDUnprocessedLinesBlock extends MDBlock {
  67. /** @var {MDBlock[]} */
  68. #blocks = [];
  69. /**
  70. * @param {String[]} lines
  71. */
  72. constructor(lines) {
  73. super();
  74. // Find markers that always start a new block
  75. let blockQuoteBlocks = MDUnprocessedLinesBlock.findBlockQuote(lines);
  76. if (blockQuoteBlocks) {
  77. this.#blocks = blockQuoteBlocks;
  78. return;
  79. }
  80. let headerBlocks = MDUnprocessedLinesBlock.findHeader(lines);
  81. if (headerBlocks) {
  82. this.#blocks = headerBlocks;
  83. return;
  84. }
  85. let codeLines = MDUnprocessedLinesBlock.findTickCodeBlock(lines);
  86. if (codeLines) {
  87. this.#blocks = codeLines;
  88. return;
  89. }
  90. let codeLines0 = MDUnprocessedLinesBlock.findIndentCodeBlock(lines);
  91. if (codeLines0) {
  92. this.#blocks = codeLines0;
  93. return;
  94. }
  95. // Find runs of contiguous non-blank lines
  96. var contiguousLines = [];
  97. let blankRegex = /^\s*$/;
  98. for (const line of lines) {
  99. if (blankRegex.exec(line)) {
  100. if (contiguousLines.length > 0) {
  101. this.#blocks.push(new MDContiguousUnprocessedLinesBlock(contiguousLines));
  102. }
  103. contiguousLines = [];
  104. } else {
  105. contiguousLines.push(line);
  106. }
  107. }
  108. if (contiguousLines.length > 0) {
  109. this.#blocks.push(new MDContiguousUnprocessedLinesBlock(contiguousLines));
  110. }
  111. }
  112. /**
  113. * @param {String[]} lines
  114. * @returns {MDBlock[]} up to 3 blocks for the unprocessed lines and the blockquoted content, or null if not found
  115. */
  116. static findBlockQuote(lines) {
  117. var portion = 0;
  118. var beforeLines = [];
  119. var blockQuoteLines = [];
  120. var afterLines = [];
  121. for (const line of lines) {
  122. switch (portion) {
  123. case 0:
  124. if (line.startsWith(">")) {
  125. blockQuoteLines.push(line.substring(1));
  126. portion = 1;
  127. } else {
  128. beforeLines.push(line);
  129. }
  130. break;
  131. case 1:
  132. if (line.startsWith(">")) {
  133. blockQuoteLines.push(line.substring(1));
  134. } else {
  135. afterLines.push(line);
  136. portion = 2;
  137. }
  138. break;
  139. case 2:
  140. afterLines.push(line);
  141. break;
  142. }
  143. }
  144. if (blockQuoteLines.length == 0) {
  145. return null;
  146. }
  147. var blocks = [];
  148. if (beforeLines.length > 0) {
  149. blocks.push(new MDUnprocessedLinesBlock(beforeLines));
  150. }
  151. blocks.push(new MDHTMLWrappedBlock(
  152. "<blockquote>\n",
  153. new MDUnprocessedLinesBlock(Markdown.trimEvenly(blockQuoteLines)),
  154. "</blockquote>\n"));
  155. if (afterLines.length > 0) {
  156. blocks.push(new MDUnprocessedLinesBlock(afterLines));
  157. }
  158. return blocks;
  159. }
  160. /**
  161. * @param {String[]} lines
  162. * @returns {MDBlock[]} up to 3 blocks for the unprocessed lines and the header content, or null if not found
  163. */
  164. static findHeader(lines) {
  165. var portion = 0;
  166. var beforeLines = [];
  167. var headerBlock = null;
  168. var afterLines = [];
  169. for (const line of lines) {
  170. let hashMatch = /^\s*(#{1,6})\s*(.*)$/.exec(line);
  171. let dashMatch = /^(-+|=+)$/.exec(line);
  172. switch (portion) {
  173. case 0:
  174. if (hashMatch) {
  175. let headerLevel = hashMatch[1].length;
  176. let contentMarkdown = hashMatch[2];
  177. headerBlock = new MDHTMLWrappedBlock('<h' + headerLevel + '>', new MDInlineBlock(contentMarkdown), '</h' + headerLevel + ">\n");
  178. portion = 1;
  179. } else if (dashMatch && beforeLines.length > 1) {
  180. let contentMarkdown = beforeLines.pop();
  181. let headerLevel = dashMatch[1].startsWith("=") ? 1 : 2;
  182. headerBlock = new MDHTMLWrappedBlock('<h' + headerLevel + '>', new MDInlineBlock(contentMarkdown), '</h' + headerLevel + ">\n");
  183. portion = 1;
  184. } else {
  185. beforeLines.push(line);
  186. }
  187. break;
  188. case 1:
  189. afterLines.push(line);
  190. break;
  191. }
  192. }
  193. if (headerBlock == null) {
  194. return null;
  195. }
  196. var blocks = [];
  197. if (beforeLines.length > 0) {
  198. blocks.push(new MDUnprocessedLinesBlock(beforeLines));
  199. }
  200. blocks.push(headerBlock);
  201. if (afterLines.length > 0) {
  202. blocks.push(new MDUnprocessedLinesBlock(afterLines));
  203. }
  204. return blocks;
  205. }
  206. /**
  207. * @param {String[]} lines
  208. * @returns {MDBlock[]} up to 3 blocks for the unprocessed lines and the code content, or null if not found
  209. */
  210. static findTickCodeBlock(lines) {
  211. var portion = 0;
  212. var beforeLines = [];
  213. var codeLines = [];
  214. var afterLines = [];
  215. for (const line of lines) {
  216. switch (portion) {
  217. case 0:
  218. if (/^\s*```\s*$/.exec(line)) {
  219. portion = 1;
  220. } else {
  221. beforeLines.push(line);
  222. }
  223. break;
  224. case 1:
  225. if (/^\s*```\s*$/.exec(line)) {
  226. portion = 2;
  227. } else {
  228. codeLines.push(line);
  229. }
  230. break;
  231. case 2:
  232. afterLines.push(line);
  233. break;
  234. }
  235. }
  236. if (codeLines.length == 0) return null;
  237. var blocks = [];
  238. if (beforeLines.length > 0) {
  239. blocks.push(new MDUnprocessedLinesBlock(beforeLines));
  240. }
  241. blocks.push(new MDHTMLWrappedBlock("<pre>", MDHTMLBlock(codeLines.join("\n")), "</pre>\n"));
  242. if (afterLines.length > 0) {
  243. blocks.push(new MDUnprocessedLinesBlock(afterLines));
  244. }
  245. return blocks;
  246. }
  247. /**
  248. * @param {String[]} lines
  249. * @returns {MDBlock[]} up to 3 blocks for the unprocessed lines and the code content, or null if not found
  250. */
  251. static findIndentCodeBlock(lines) {
  252. var portion = 0;
  253. var beforeLines = [];
  254. var codeLines = [];
  255. var afterLines = [];
  256. let regex = /^(\s{4,})(.*)$/;
  257. var minIndent = 999999;
  258. for (const line of lines) {
  259. let indentMatch = regex.exec(line);
  260. switch (portion) {
  261. case 0:
  262. if (indentMatch) {
  263. minIndent = Math.min(minIndent, indentMatch[1].length);
  264. codeLines.push(line);
  265. portion = 1;
  266. } else {
  267. beforeLines.push(line);
  268. }
  269. break;
  270. case 1:
  271. if (indentMatch) {
  272. minIndent = Math.min(minIndent, indentMatch[1].length);
  273. codeLines.push(line);
  274. } else {
  275. afterLines.push(line);
  276. portion = 2;
  277. }
  278. break;
  279. case 2:
  280. afterLines.push(line);
  281. break;
  282. }
  283. }
  284. if (codeLines.length == 0) return null;
  285. var blocks = [];
  286. if (beforeLines.length > 0) {
  287. blocks.push(new MDUnprocessedLinesBlock(beforeLines));
  288. }
  289. blocks.push(new MDHTMLWrappedBlock("<pre>", new MDHTMLBlock(codeLines.map((l) => l.substring(minIndent)).join("\n")), "</pre>\n"));
  290. if (afterLines.length > 0) {
  291. blocks.push(new MDUnprocessedLinesBlock(afterLines));
  292. }
  293. return blocks;
  294. }
  295. toHTML(config, state) {
  296. var html = "";
  297. for (const block of this.#blocks) {
  298. html += block.toHTML() + "\n";
  299. }
  300. return html;
  301. }
  302. }
  303. class MDContiguousUnprocessedLinesBlock extends MDBlock {
  304. /** @var {MDBlock[]} */
  305. #blocks;
  306. /**
  307. * @param {String[]} lines
  308. */
  309. constructor(lines) {
  310. super();
  311. this.#blocks = [ new MDHTMLWrappedBlock('<p>contiguous: ', new MDInlineBlock(lines.join(' ')), "</p>\n\n") ];
  312. }
  313. toHTML(config, state) {
  314. var html = "";
  315. for (const block of this.#blocks) {
  316. html += block.toHTML() + "\n";
  317. }
  318. return html;
  319. }
  320. }
  321. class Markdown {
  322. /**
  323. * @param {String} markdown
  324. * @returns {String} HTML
  325. */
  326. static toHTML(markdown, config=new MDConfig()) {
  327. // Blocks that immediately start a new block
  328. // - Headers
  329. // - Blockquote
  330. // - Code block ```\ncode\n```
  331. // Blocks that need blank line first
  332. // - HR --- - - - *** * * * * * *
  333. // - Lists
  334. // - Table
  335. // - Code block [4+spaces]code
  336. // - Definition list term\n: definition\n: alternate def
  337. // Unknown blocks
  338. // - Footnotes some text[^1] [^1]: first footnote content
  339. // - Abbreviations *[HTML]: Hyper Text
  340. // Inline styles
  341. // - Links
  342. // - Italic
  343. // - Bold
  344. // - `code`
  345. // - Strikethrough
  346. // - Images ![alt text](url){.cssclass}
  347. // - Literals \*
  348. let state = new MDState();
  349. let lines = markdown.trim().replace("\r", "").split("\n");
  350. return new MDUnprocessedLinesBlock(lines).toHTML(config, state);
  351. }
  352. /**
  353. * @param {String[]} lines
  354. * @returns {String[]}
  355. */
  356. static trimEvenly(lines) {
  357. var minIndent = 999999;
  358. let regex = /^(\s*)($|\S.*$)/;
  359. for (const line of lines) {
  360. let groups = regex.exec(line);
  361. let indent = groups[1].length;
  362. if (groups[2].trim().length > 0 && indent < 4) {
  363. minIndent = Math.min(minIndent, indent);
  364. }
  365. }
  366. if (minIndent == 0) return lines;
  367. var trimmed = [];
  368. let trimRegex = new RegExp(`^\s{${minIndent}}`);
  369. for (const line of lines) {
  370. trimmed.push(line.replace(trimRegex, ''));
  371. }
  372. return trimmed;
  373. }
  374. }