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ů.

markdownjs.html 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #readercontainer {
  64. overflow-y: scroll;
  65. }
  66. .reader-header {
  67. margin-top: 0.25em;
  68. font-weight: bold;
  69. }
  70. .reader-check {
  71. display: inline-block;
  72. padding: 4px 8px;
  73. border: 1px dotted gray;
  74. margin: 4px;
  75. }
  76. #markdowninput {
  77. position: absolute;
  78. box-sizing: border-box;
  79. top: 32px;
  80. width: 100%;
  81. height: calc(100% - 32px);
  82. margin: 0;
  83. padding: 1em 2em;
  84. border: 0;
  85. background-color: var(--color-editor-background);
  86. color: var(--color-editor-text);
  87. }
  88. .calculated {
  89. background-color: var(--color-calculated-background);
  90. font-style: italic;
  91. }
  92. .spreadsheet-type-number,
  93. .spreadsheet-type-currency,
  94. .spreadsheet-type-percent {
  95. text-align: right;
  96. }
  97. </style>
  98. <style type="text/css">
  99. /* Document styling */
  100. /* From https://github.com/edwardtufte/tufte-css/blob/gh-pages/tufte.css */
  101. :root {
  102. --color-background: #fffff8;
  103. --color-text: #111;
  104. --color-table-header: #ddd;
  105. --color-table-border: black;
  106. --color-calculated-background: #eee;
  107. --color-highlight: #ff0;
  108. --color-highlight-text: #111;
  109. }
  110. @media (prefers-color-scheme: dark) {
  111. :root {
  112. --color-background: #151515;
  113. --color-text: #ddd;
  114. --color-table-header: #333;
  115. --color-table-border: #ccc;
  116. --color-calculated-background: #444;
  117. --color-highlight: #88f;
  118. --color-highlight-text: #ddd;
  119. }
  120. }
  121. html {
  122. font-size: 15px;
  123. }
  124. body {
  125. /* width: 87.5%;
  126. margin-left: auto;
  127. margin-right: auto;
  128. padding-left: 12.5%; */
  129. font-family: et-book, Palatino, "Palatino Linotype", "Palatino LT STD", "Book Antiqua", Georgia, serif;
  130. background-color: var(--color-background);
  131. color: var(--color-text);
  132. /* max-width: 1400px; */
  133. counter-reset: sidenote-counter;
  134. }
  135. h1 {
  136. font-weight: 400;
  137. margin-top: 4rem;
  138. margin-bottom: 1.5rem;
  139. font-size: 3.2rem;
  140. line-height: 1;
  141. }
  142. h2 {
  143. font-style: italic;
  144. font-weight: 400;
  145. margin-top: 2.1rem;
  146. margin-bottom: 1.4rem;
  147. font-size: 2.2rem;
  148. line-height: 1;
  149. }
  150. h3 {
  151. font-style: italic;
  152. font-weight: 400;
  153. font-size: 1.7rem;
  154. margin-top: 2rem;
  155. margin-bottom: 1.4rem;
  156. line-height: 1;
  157. }
  158. .subtext {
  159. font-size: 1rem;
  160. color: #888;
  161. margin-top: 0.25em;
  162. margin-bottom: 0.5em;
  163. }
  164. mark {
  165. background-color: var(--color-highlight);
  166. color: var(--color-highlight-text);
  167. }
  168. table {
  169. margin-top: 1em;
  170. margin-bottom: 1em;
  171. border-collapse: collapse;
  172. }
  173. td, th {
  174. padding: 0.4em 0.8em;
  175. font-size: 1rem;
  176. }
  177. th {
  178. background-color: var(--color-table-header);
  179. border-bottom: 2px solid var(--color-text);
  180. }
  181. tr:not(:last-child) td {
  182. border-bottom: 1px solid var(--color-text);
  183. }
  184. tr td:not(:last-child) {
  185. border-right: 1px solid var(--color-table-header);
  186. }
  187. hr {
  188. display: block;
  189. height: 1px;
  190. width: 55%;
  191. border: 0;
  192. border-top: 1px solid #ccc;
  193. margin: 1em 0;
  194. padding: 0;
  195. }
  196. article {
  197. padding: 5rem 0rem;
  198. }
  199. section {
  200. padding-top: 1rem;
  201. padding-bottom: 1rem;
  202. }
  203. p,
  204. ol,
  205. ul {
  206. font-size: 1.4rem;
  207. line-height: 2rem;
  208. }
  209. p.subtitle {
  210. font-style: italic;
  211. margin-top: 1rem;
  212. margin-bottom: 1rem;
  213. font-size: 1.8rem;
  214. display: block;
  215. line-height: 1;
  216. }
  217. code, pre > code {
  218. font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
  219. font-size: 1.0rem;
  220. line-height: 1.42;
  221. -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 */
  222. }
  223. pre > code {
  224. font-size: 0.9rem;
  225. width: 52.5%;
  226. margin-left: 2.5%;
  227. overflow-x: auto;
  228. display: block;
  229. }
  230. a:link,
  231. a:visited {
  232. color: inherit;
  233. text-underline-offset: 0.1em;
  234. text-decoration-thickness: 0.05em;
  235. }
  236. dt:not(:first-child),
  237. li:not(:first-child) {
  238. margin-top: 0.25rem;
  239. }
  240. dt, dd {
  241. font-size: 1.2rem;
  242. }
  243. dt {
  244. font-weight: bold;
  245. }
  246. .footnotes, .footnotes li {
  247. font-size: 80%;
  248. }
  249. .footnotes li:target, sup:target a {
  250. background-color: var(--color-highlight);
  251. color: var(--color-highlight-text);
  252. }
  253. </style>
  254. <script src="js/markdown.js"></script>
  255. <script src="js/spreadsheet.js"></script>
  256. <script>
  257. var parser = new Markdown([ ...Markdown.allReaders, new MDSpreadsheetReader() ]);
  258. function onDocumentLoad() {
  259. document.getElementById('markdowninput').addEventListener('input', onMarkdownChange);
  260. populateReaderCheckboxes();
  261. setTimeout(onMarkdownChange, 0);
  262. }
  263. function onMarkdownChange() {
  264. let markdown = document.getElementById('markdowninput').value;
  265. let html = parser.toHTML(markdown, 'foo-');
  266. document.getElementById('preview').innerHTML = html;
  267. }
  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. };
  301. var activeReaders = [];
  302. function populateReaderCheckboxes() {
  303. const container = document.getElementById('readercontainer');
  304. var header = document.createElement('div');
  305. header.classList.add('reader-header');
  306. header.append(document.createTextNode('Block Readers'));
  307. container.append(header);
  308. const blockContainer = document.createElement('div');
  309. container.append(blockContainer);
  310. for (const name of Object.keys(blockReaderClasses).sort()) {
  311. const readerClass = blockReaderClasses[name];
  312. activeReaders.push(new readerClass());
  313. const checkbox = makeReaderCheckbox(name, readerClass);
  314. blockContainer.append(checkbox);
  315. }
  316. header = document.createElement('div');
  317. header.classList.add('reader-header');
  318. header.append(document.createTextNode('Inline Readers'));
  319. container.append(header);
  320. const inlineContainer = document.createElement('div');
  321. container.append(inlineContainer);
  322. for (const name of Object.keys(inlineReaderClasses).sort()) {
  323. const readerClass = inlineReaderClasses[name];
  324. activeReaders.push(new readerClass());
  325. const checkbox = makeReaderCheckbox(name, readerClass);
  326. inlineContainer.append(checkbox);
  327. }
  328. }
  329. function makeReaderCheckbox(name, readerClass) {
  330. const check = document.createElement('input');
  331. check.type = 'checkbox';
  332. check.checked = true;
  333. check.addEventListener('change', () => {
  334. handleCheckChanged(readerClass, check);
  335. });
  336. check.id = `reader-${readerClass.name}`;
  337. const label = document.createElement('label');
  338. label.htmlFor = check.id;
  339. label.append(document.createTextNode(name));
  340. const div = document.createElement('div');
  341. div.classList.add('reader-check');
  342. div.append(check);
  343. div.append(label);
  344. return div;
  345. }
  346. function handleCheckChanged(readerClass, check) {
  347. console.info(`${readerClass.name}: ${check.checked}`);
  348. if (check.checked) {
  349. activeReaders.push(new readerClass());
  350. } else {
  351. activeReaders = activeReaders.filter((reader) => reader.constructor.name !== readerClass.name);
  352. }
  353. parser = new Markdown(activeReaders);
  354. onMarkdownChange();
  355. }
  356. document.addEventListener('DOMContentLoaded', onDocumentLoad);
  357. </script>
  358. </head>
  359. <body>
  360. <div class="inputhalf half">
  361. <div class="toolbar">
  362. <details>
  363. <summary>Readers</summary>
  364. <div id="readercontainer"></div>
  365. </details>
  366. </div>
  367. <textarea id="markdowninput">## Block Formats {#top}
  368. A regular paragraph.
  369. -# Sub text
  370. * Unordered
  371. * Lists
  372. * Sub list
  373. 1. Ordered
  374. 2. Lists
  375. 6. Sub list
  376. A blockquote:
  377. &gt; "The only thing we have to fear is fear itself—nameless, unreasoning, unjustified terror which paralyzes needed efforts to convert retreat into advance." - Franklin D. Roosevelt's First Inaugural Address
  378. Some definitions:
  379. word
  380. : a unit of meaning in a sentence
  381. sentence
  382. : a collection of words
  383. Code:
  384. ```javascript
  385. function foo() {
  386. return 'Hello world';
  387. }
  388. ```
  389. function bar() {
  390. return 'Indented code';
  391. }
  392. ### Heading, hash style
  393. Heading, underline style
  394. ---
  395. ### Modified Heading {style=color:red;}
  396. | Unit Price | Qty | Subtotal |
  397. | ---: | ---: | ---: |
  398. | $1.23 | 2 | =A*B FILL |
  399. | $4.99 | 6 | |
  400. | Total | | =SUM(C:C) |
  401. ---
  402. ## Inline Formats
  403. Normal, **double asterisk,** __double underscore,__ *asterisks,* _underscores,_ ~~double tildes,~~ ~single tildes,~ ^carets,^ ==double equals==, ``double backticks``, `single backticks`.
  404. Lorem ipsum dolor sit amet,[^1] consectetur adipiscing elit.[^abc] Sed sodales in odio eget porta. Proin et erat sit amet erat semper mollis vitae ut turpis.[^foo] Ut ipsum dui, maximus sit amet suscipit at, dictum id nulla.[^bar] Aenean efficitur rhoncus nulla non fermentum.
  405. [^1]: Donec ut felis volutpat, gravida ipsum scelerisque, accumsan est.
  406. [^abc]: Cras dictum rutrum quam.
  407. [^foo]: Donec maximus bibendum lorem.
  408. [^bar]: Praesent consectetur tristique leo. Morbi nec nisi sit amet quam imperdiet vehicula eu feugiat tortor.
  409. The HTML on the WWW is often full of JS and CSS.
  410. *[HTML]: Hypertext Markup Language
  411. *[WWW]: World Wide Web
  412. *[JS]: JavaScript
  413. *[CSS]: Cascading Style Sheets
  414. Click [here](#top) to return to the top. Referenced link to [Google][google].
  415. ![alt text](https://picsum.photos/100/75) ![alt text](https://picsum.photos/101/75 "With a title") ![][testimage]
  416. [testimage]: https://picsum.photos/102/75
  417. [google]: https://google.com "I prefer Duck Duck Go"
  418. Some verbatim &lt;span style="color:green;"&gt;HTML&lt;/span&gt;. A script tag will be ignored. &lt;script&gt;&lt;/script&gt;
  419. </textarea>
  420. </div>
  421. <div class="previewhalf half">
  422. <div id="preview">
  423. <p>Preview here</p>
  424. </div>
  425. </div>
  426. </body>
  427. </html>