PHP and Javascript implementations of a simple markdown parser
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

playground.html 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Markdown Test</title>
  6. <link rel="icon" href="data:;base64,iVBORw0KGgo=">
  7. <style type="text/css">
  8. /* Structural style */
  9. :root {
  10. --color-editor-background: #00a;
  11. --color-editor-text: white;
  12. --toolbar-background: #ccc;
  13. --toolbar-text: #000;
  14. }
  15. @media (prefers-color-scheme: dark) {
  16. :root {
  17. --toolbar-background: #aaa;
  18. }
  19. }
  20. :root, body {
  21. width: 100%;
  22. height: 100%;
  23. padding: 0;
  24. margin: 0;
  25. background-color: var(--color-background);
  26. color: var(--color-text);
  27. }
  28. .inputhalf {
  29. position: absolute;
  30. width: 50%;
  31. height: 100%;
  32. left: 0;
  33. top: 0;
  34. background-color: var(--color-editor-background);
  35. color: var(--color-editor-text);
  36. }
  37. .previewhalf {
  38. position: absolute;
  39. width: 50%;
  40. height: 100%;
  41. left: 50%;
  42. top: 0;
  43. overflow-y: auto;
  44. }
  45. #preview {
  46. padding: 1em;
  47. margin: 0 auto;
  48. max-width: 600px;
  49. }
  50. .toolbar {
  51. position: absolute;
  52. z-index: 1;
  53. background-color: var(--toolbar-background);
  54. color: var(--toolbar-text);
  55. min-height: 32px;
  56. /* max-height: 300px; */
  57. width: 100%;
  58. border-bottom: 1px solid black;
  59. box-sizing: border-box;
  60. padding: 4px 20px;
  61. font-family: sans-serif;
  62. }
  63. .codeswitch-cont {
  64. position: absolute;
  65. top: 0;
  66. right: 0;
  67. height: 31px;
  68. z-index: 2;
  69. background-color: var(--toolbar-background);
  70. color: var(--toolbar-text);
  71. padding: 3px 10px;
  72. box-sizing: border-box;
  73. }
  74. #readercontainer {
  75. overflow-y: scroll;
  76. }
  77. .reader-header {
  78. margin-top: 0.25em;
  79. font-weight: bold;
  80. }
  81. .reader-check {
  82. display: inline-block;
  83. padding: 4px 8px;
  84. border: 1px dotted gray;
  85. margin: 4px;
  86. }
  87. #markdowninput {
  88. position: absolute;
  89. box-sizing: border-box;
  90. top: 32px;
  91. width: 100%;
  92. height: calc(100% - 32px);
  93. margin: 0;
  94. padding: 1em 2em;
  95. border: 0;
  96. background-color: var(--color-editor-background);
  97. color: var(--color-editor-text);
  98. }
  99. .calculated {
  100. background-color: var(--color-calculated-background);
  101. font-style: italic;
  102. }
  103. .spreadsheet-type-number,
  104. .spreadsheet-type-currency,
  105. .spreadsheet-type-percent {
  106. text-align: right;
  107. }
  108. </style>
  109. <style type="text/css">
  110. /* Document styling */
  111. /* From https://github.com/edwardtufte/tufte-css/blob/gh-pages/tufte.css */
  112. :root {
  113. --color-background: #fffff8;
  114. --color-text: #111;
  115. --color-table-header: #ddd;
  116. --color-table-border: black;
  117. --color-calculated-background: #eee;
  118. --color-highlight: #ff0;
  119. --color-highlight-text: #111;
  120. }
  121. @media (prefers-color-scheme: dark) {
  122. :root {
  123. --color-background: #151515;
  124. --color-text: #ddd;
  125. --color-table-header: #333;
  126. --color-table-border: #ccc;
  127. --color-calculated-background: #444;
  128. --color-highlight: #88f;
  129. --color-highlight-text: #ddd;
  130. }
  131. }
  132. html {
  133. font-size: 15px;
  134. }
  135. body {
  136. /* width: 87.5%;
  137. margin-left: auto;
  138. margin-right: auto;
  139. padding-left: 12.5%; */
  140. font-family: et-book, Palatino, "Palatino Linotype", "Palatino LT STD", "Book Antiqua", Georgia, serif;
  141. background-color: var(--color-background);
  142. color: var(--color-text);
  143. /* max-width: 1400px; */
  144. counter-reset: sidenote-counter;
  145. }
  146. h1 {
  147. font-weight: 400;
  148. margin-top: 4rem;
  149. margin-bottom: 1.5rem;
  150. font-size: 3.2rem;
  151. line-height: 1;
  152. }
  153. h2 {
  154. font-style: italic;
  155. font-weight: 400;
  156. margin-top: 2.1rem;
  157. margin-bottom: 1.4rem;
  158. font-size: 2.2rem;
  159. line-height: 1;
  160. }
  161. h3 {
  162. font-style: italic;
  163. font-weight: 400;
  164. font-size: 1.7rem;
  165. margin-top: 2rem;
  166. margin-bottom: 1.4rem;
  167. line-height: 1;
  168. }
  169. .subtext {
  170. font-size: 1rem;
  171. color: #888;
  172. margin-top: 0.25em;
  173. margin-bottom: 0.5em;
  174. }
  175. mark {
  176. background-color: var(--color-highlight);
  177. color: var(--color-highlight-text);
  178. }
  179. table {
  180. margin-top: 1em;
  181. margin-bottom: 1em;
  182. border-collapse: collapse;
  183. }
  184. td, th {
  185. padding: 0.4em 0.8em;
  186. font-size: 1rem;
  187. }
  188. th {
  189. background-color: var(--color-table-header);
  190. border-bottom: 2px solid var(--color-text);
  191. }
  192. tr:not(:last-child) td {
  193. border-bottom: 1px solid var(--color-text);
  194. }
  195. tr td:not(:last-child) {
  196. border-right: 1px solid var(--color-table-header);
  197. }
  198. hr {
  199. display: block;
  200. height: 1px;
  201. width: 55%;
  202. border: 0;
  203. border-top: 1px solid #ccc;
  204. margin: 1em 0;
  205. padding: 0;
  206. }
  207. article {
  208. padding: 5rem 0rem;
  209. }
  210. section {
  211. padding-top: 1rem;
  212. padding-bottom: 1rem;
  213. }
  214. p,
  215. ol,
  216. ul {
  217. font-size: 1.4rem;
  218. line-height: 2rem;
  219. }
  220. p.subtitle {
  221. font-style: italic;
  222. margin-top: 1rem;
  223. margin-bottom: 1rem;
  224. font-size: 1.8rem;
  225. display: block;
  226. line-height: 1;
  227. }
  228. code, pre > code {
  229. font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
  230. font-size: 1.0rem;
  231. line-height: 1.42;
  232. -webkit-text-size-adjust: 100%; /* Prevent adjustments of font size after orientation changes in iOS. See https://github.com/edwardtufte/tufte-css/issues/81#issuecomment-261953409 */
  233. }
  234. pre > code {
  235. font-size: 0.9rem;
  236. width: 52.5%;
  237. margin-left: 2.5%;
  238. overflow-x: auto;
  239. display: block;
  240. }
  241. a:link,
  242. a:visited {
  243. color: inherit;
  244. text-underline-offset: 0.1em;
  245. text-decoration-thickness: 0.05em;
  246. }
  247. dt:not(:first-child),
  248. li:not(:first-child) {
  249. margin-top: 0.25rem;
  250. }
  251. dt, dd {
  252. font-size: 1.2rem;
  253. }
  254. dt {
  255. font-weight: bold;
  256. }
  257. .footnotes, .footnotes li {
  258. font-size: 80%;
  259. }
  260. .footnotes li:target, sup:target a {
  261. background-color: var(--color-highlight);
  262. color: var(--color-highlight-text);
  263. }
  264. </style>
  265. <script src="js/markdown.js"></script>
  266. <script src="js/spreadsheet.js"></script>
  267. <script>
  268. var blockReaderClasses = {
  269. 'Heading (underline)': MDUnderlinedHeadingReader,
  270. 'Heading (hash)': MDHashHeadingReader,
  271. 'Subtext': MDSubtextReader,
  272. 'Block quote': MDBlockQuoteReader,
  273. 'Unordered list': MDUnorderedListReader,
  274. 'Ordered list': MDOrderedListReader,
  275. 'Code block (fenced)': MDFencedCodeBlockReader,
  276. 'Code block (indented)': MDIndentedCodeBlockReader,
  277. 'Horizontal rule': MDHorizontalRuleReader,
  278. 'Table': MDTableReader,
  279. 'Definition list': MDDefinitionListReader,
  280. 'Paragraph': MDParagraphReader,
  281. 'Spreadsheets': MDSpreadsheetReader,
  282. };
  283. var inlineReaderClasses = {
  284. 'Emphasis': MDEmphasisReader,
  285. 'Strong': MDStrongReader,
  286. 'Strikethrough': MDStrikethroughReader,
  287. 'Underline': MDUnderlineReader,
  288. 'Highlight': MDHighlightReader,
  289. 'Links': MDLinkReader,
  290. 'Referenced links': MDReferencedLinkReader,
  291. 'Images': MDImageReader,
  292. 'Referenced images': MDReferencedImageReader,
  293. 'Code span': MDCodeSpanReader,
  294. 'Subscript': MDSubscriptReader,
  295. 'Superscript': MDSuperscriptReader,
  296. 'Footnotes': MDFootnoteReader,
  297. 'Abbrevations': MDAbbreviationReader,
  298. 'HTML tags': MDHTMLTagReader,
  299. 'Modifiers': MDModifierReader,
  300. 'Line breaks': MDLineBreakReader,
  301. };
  302. var activeReaders = [];
  303. var parser = null;
  304. function onDocumentLoad() {
  305. activeReaders = [ ...Markdown.allReaders, new MDSpreadsheetReader() ];
  306. parser = new Markdown(activeReaders);
  307. document.getElementById('markdowninput').addEventListener('input', onMarkdownChange);
  308. populateReaderCheckboxes();
  309. document.getElementById('sendbutton').addEventListener('click', () => {
  310. handleSendClicked();
  311. });
  312. document.getElementById('code-js').addEventListener('change', onMarkdownChange);
  313. document.getElementById('code-php').addEventListener('change', onMarkdownChange);
  314. setTimeout(onMarkdownChange, 0);
  315. }
  316. function onMarkdownChange() {
  317. if (document.getElementById('code-php').checked) {
  318. debouncedSendPreviewRequest();
  319. } else if (document.getElementById('code-js').checked) {
  320. updatePreviewLocally();
  321. }
  322. }
  323. function updatePreviewLocally() {
  324. const textarea = document.getElementById('markdowninput');
  325. let markdown = textarea.value;
  326. let html = parser.toHTML(markdown, 'foo-');
  327. document.getElementById('preview').innerHTML = html;
  328. }
  329. var debounceTimer = null;
  330. var needsUpdate = false;
  331. function debouncedSendPreviewRequest() {
  332. if (debounceTimer) {
  333. needsUpdate = true;
  334. } else {
  335. debounceTimer = setInterval(() => {
  336. if (needsUpdate) {
  337. needsUpdate = false;
  338. sendPreviewRequest();
  339. } else {
  340. clearTimeout(debounceTimer);
  341. debounceTimer = null;
  342. }
  343. }, 500);
  344. sendPreviewRequest();
  345. }
  346. }
  347. function sendPreviewRequest() {
  348. const markdown = document.getElementById('markdowninput').value;
  349. const readers = activeReaders.map((r) => r.constructor.name);
  350. const request = new XMLHttpRequest();
  351. var formData = encodeForm({
  352. 'markdown': markdown,
  353. 'readers': readers,
  354. });
  355. request.open('POST', 'playgroundapi.php', true);
  356. request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  357. request.onreadystatechange = () => {
  358. if (request.readyState == 4 && request.status == 200) {
  359. document.getElementById('preview').innerHTML = request.responseText;
  360. }
  361. };
  362. request.send(formData);
  363. }
  364. function encodeForm(values) {
  365. var pairs = [];
  366. for (const [key, value] of Object.entries(values)) {
  367. if (value instanceof Array) {
  368. for (const v of value) {
  369. pairs.push(`${encodeURIComponent(key + '[]')}=${encodeURIComponent(v)}`);
  370. }
  371. } else {
  372. pairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
  373. }
  374. }
  375. return pairs.join('&');
  376. }
  377. function populateReaderCheckboxes() {
  378. const container = document.getElementById('readercontainer');
  379. var header = document.createElement('div');
  380. header.classList.add('reader-header');
  381. header.append(document.createTextNode('Block Readers'));
  382. container.append(header);
  383. const blockContainer = document.createElement('div');
  384. container.append(blockContainer);
  385. for (const name of Object.keys(blockReaderClasses).sort()) {
  386. const readerClass = blockReaderClasses[name];
  387. const checkbox = makeReaderCheckbox(name, readerClass);
  388. blockContainer.append(checkbox);
  389. }
  390. header = document.createElement('div');
  391. header.classList.add('reader-header');
  392. header.append(document.createTextNode('Inline Readers'));
  393. container.append(header);
  394. const inlineContainer = document.createElement('div');
  395. container.append(inlineContainer);
  396. for (const name of Object.keys(inlineReaderClasses).sort()) {
  397. const readerClass = inlineReaderClasses[name];
  398. const checkbox = makeReaderCheckbox(name, readerClass);
  399. inlineContainer.append(checkbox);
  400. }
  401. }
  402. function makeReaderCheckbox(name, readerClass) {
  403. var isSelected = false;
  404. for (const elem of activeReaders) {
  405. if (elem.constructor === readerClass) {
  406. isSelected = true;
  407. break;
  408. }
  409. }
  410. const check = document.createElement('input');
  411. check.type = 'checkbox';
  412. check.checked = isSelected;
  413. check.addEventListener('change', () => {
  414. handleCheckChanged(readerClass, check);
  415. });
  416. check.id = `reader-${readerClass.name}`;
  417. const label = document.createElement('label');
  418. label.htmlFor = check.id;
  419. label.append(document.createTextNode(name));
  420. const div = document.createElement('div');
  421. div.classList.add('reader-check');
  422. div.append(check);
  423. div.append(label);
  424. return div;
  425. }
  426. function handleCheckChanged(readerClass, check) {
  427. if (check.checked) {
  428. activeReaders.push(new readerClass());
  429. } else {
  430. activeReaders = activeReaders.filter((reader) => reader.constructor.name !== readerClass.name);
  431. }
  432. parser = new Markdown(activeReaders);
  433. onMarkdownChange();
  434. }
  435. function handleSendClicked() {
  436. sendPreviewRequest();
  437. }
  438. document.addEventListener('DOMContentLoaded', onDocumentLoad);
  439. </script>
  440. </head>
  441. <body>
  442. <div class="inputhalf half">
  443. <div class="toolbar">
  444. <details>
  445. <summary>Readers</summary>
  446. <div id="readercontainer"></div>
  447. </details>
  448. </div>
  449. <div class="codeswitch-cont">
  450. <input type="radio" id="code-js" name="code" value="js" checked>
  451. <label for="code-js">Javascript</label>
  452. <input type="radio" id="code-php" name="code" value="php">
  453. <label for="code-php">PHP</label>
  454. </div>
  455. <div id="sendbuttoncont"><button id="sendbutton">Send</button></div>
  456. <textarea id="markdowninput">## Block Formats {#top}
  457. A regular paragraph.
  458. -# Sub text
  459. * Unordered
  460. * Lists
  461. * Sub list
  462. 1. Ordered
  463. 2. Lists
  464. 6. Sub list
  465. A blockquote:
  466. &gt; "The only thing we have to fear is fear itself—nameless, unreasoning,
  467. &gt; unjustified terror which paralyzes needed efforts to convert retreat into
  468. &gt; advance." - Franklin D. Roosevelt's First Inaugural Address
  469. Some definitions:
  470. word
  471. : a unit of meaning in a sentence
  472. sentence
  473. : a collection of words
  474. Code:
  475. ```javascript
  476. function foo() {
  477. return 'Hello world';
  478. }
  479. ```
  480. function bar() {
  481. return 'Indented code';
  482. }
  483. ### Heading, hash style
  484. Heading, underline style
  485. ---
  486. ### Modified Heading {style=color:red;}
  487. | Unit Price | Qty | Subtotal |
  488. | ---: | ---: | ---: |
  489. | $1.23 | 2 | =A*B FILL |
  490. | $4.99 | 6 | |
  491. | Total | | =SUM(C:C) |
  492. ---
  493. ## Inline Formats
  494. Normal, **double asterisk,** __double underscore,__ *asterisks,* _underscores,_
  495. ~~double tildes,~~ ~single tildes,~ ^carets,^ ==double equals==, ``double backticks``,
  496. `single backticks`.
  497. Lorem ipsum dolor sit amet,[^1] consectetur adipiscing elit.[^abc] Sed sodales
  498. in odio eget porta. Proin et erat sit amet erat semper mollis vitae ut
  499. turpis.[^foo] Ut ipsum dui, maximus sit amet suscipit at, dictum id nulla.[^bar]
  500. Aenean efficitur rhoncus nulla non fermentum.
  501. [^1]: Donec ut felis volutpat, gravida ipsum scelerisque, accumsan est.
  502. [^abc]: Cras dictum rutrum quam.
  503. [^foo]: Donec maximus bibendum lorem.
  504. [^bar]: Praesent consectetur tristique leo. Morbi nec nisi sit amet quam
  505. imperdiet vehicula eu feugiat tortor.
  506. The HTML on the WWW is often full of JS and CSS.
  507. *[HTML]: Hypertext Markup Language
  508. *[WWW]: World Wide Web
  509. *[JS]: JavaScript
  510. *[CSS]: Cascading Style Sheets
  511. Click [here](#top) to return to the top. Referenced link to [Google][google].
  512. ![alt text](https://picsum.photos/100/75) ![alt text](https://picsum.photos/101/75 "With a title")
  513. ![][testimage]
  514. [testimage]: https://picsum.photos/102/75
  515. [google]: https://google.com "I prefer Duck Duck Go"
  516. Some verbatim &lt;span style="color:green;"&gt;HTML&lt;/span&gt;. A script tag
  517. will be ignored. &lt;script&gt;&lt;/script&gt;</textarea>
  518. </div>
  519. <div class="previewhalf half">
  520. <div id="preview">
  521. <p>Preview here</p>
  522. </div>
  523. </div>
  524. </body>
  525. </html>