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.

markdownjs.html 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 blockReaderClasses = {
  258. 'Heading (underline)': MDUnderlinedHeadingReader,
  259. 'Heading (hash)': MDHashHeadingReader,
  260. 'Subtext': MDSubtextReader,
  261. 'Block quote': MDBlockQuoteReader,
  262. 'Unordered list': MDUnorderedListReader,
  263. 'Ordered list': MDOrderedListReader,
  264. 'Code block (fenced)': MDFencedCodeBlockReader,
  265. 'Code block (indented)': MDIndentedCodeBlockReader,
  266. 'Horizontal rule': MDHorizontalRuleReader,
  267. 'Table': MDTableReader,
  268. 'Definition list': MDDefinitionListReader,
  269. 'Paragraph': MDParagraphReader,
  270. 'Spreadsheets': MDSpreadsheetReader,
  271. };
  272. var inlineReaderClasses = {
  273. 'Emphasis': MDEmphasisReader,
  274. 'Strong': MDStrongReader,
  275. 'Strikethrough': MDStrikethroughReader,
  276. 'Underline': MDUnderlineReader,
  277. 'Highlight': MDHighlightReader,
  278. 'Links': MDLinkReader,
  279. 'Referenced links': MDReferencedLinkReader,
  280. 'Images': MDImageReader,
  281. 'Referenced images': MDReferencedImageReader,
  282. 'Code span': MDCodeSpanReader,
  283. 'Subscript': MDSubscriptReader,
  284. 'Superscript': MDSuperscriptReader,
  285. 'Footnotes': MDFootnoteReader,
  286. 'Abbrevations': MDAbbreviationReader,
  287. 'HTML tags': MDHTMLTagReader,
  288. 'Modifiers': MDModifierReader,
  289. 'Line breaks': MDLineBreakReader,
  290. };
  291. var activeReaders = [];
  292. var parser = null;
  293. function onDocumentLoad() {
  294. activeReaders = [ ...Markdown.allReaders, new MDSpreadsheetReader() ];
  295. parser = new Markdown(activeReaders);
  296. document.getElementById('markdowninput').addEventListener('input', onMarkdownChange);
  297. populateReaderCheckboxes();
  298. setTimeout(onMarkdownChange, 0);
  299. }
  300. function onMarkdownChange() {
  301. const textarea = document.getElementById('markdowninput');
  302. let markdown = textarea.value;
  303. let html = parser.toHTML(markdown, 'foo-');
  304. document.getElementById('preview').innerHTML = html;
  305. }
  306. function populateReaderCheckboxes() {
  307. const container = document.getElementById('readercontainer');
  308. var header = document.createElement('div');
  309. header.classList.add('reader-header');
  310. header.append(document.createTextNode('Block Readers'));
  311. container.append(header);
  312. const blockContainer = document.createElement('div');
  313. container.append(blockContainer);
  314. for (const name of Object.keys(blockReaderClasses).sort()) {
  315. const readerClass = blockReaderClasses[name];
  316. const checkbox = makeReaderCheckbox(name, readerClass);
  317. blockContainer.append(checkbox);
  318. }
  319. header = document.createElement('div');
  320. header.classList.add('reader-header');
  321. header.append(document.createTextNode('Inline Readers'));
  322. container.append(header);
  323. const inlineContainer = document.createElement('div');
  324. container.append(inlineContainer);
  325. for (const name of Object.keys(inlineReaderClasses).sort()) {
  326. const readerClass = inlineReaderClasses[name];
  327. const checkbox = makeReaderCheckbox(name, readerClass);
  328. inlineContainer.append(checkbox);
  329. }
  330. }
  331. function makeReaderCheckbox(name, readerClass) {
  332. var isSelected = false;
  333. for (const elem of activeReaders) {
  334. if (elem.constructor === readerClass) {
  335. isSelected = true;
  336. break;
  337. }
  338. }
  339. const check = document.createElement('input');
  340. check.type = 'checkbox';
  341. check.checked = isSelected;
  342. check.addEventListener('change', () => {
  343. handleCheckChanged(readerClass, check);
  344. });
  345. check.id = `reader-${readerClass.name}`;
  346. const label = document.createElement('label');
  347. label.htmlFor = check.id;
  348. label.append(document.createTextNode(name));
  349. const div = document.createElement('div');
  350. div.classList.add('reader-check');
  351. div.append(check);
  352. div.append(label);
  353. return div;
  354. }
  355. function handleCheckChanged(readerClass, check) {
  356. console.info(`${readerClass.name}: ${check.checked}`);
  357. if (check.checked) {
  358. activeReaders.push(new readerClass());
  359. } else {
  360. activeReaders = activeReaders.filter((reader) => reader.constructor.name !== readerClass.name);
  361. }
  362. parser = new Markdown(activeReaders);
  363. onMarkdownChange();
  364. }
  365. document.addEventListener('DOMContentLoaded', onDocumentLoad);
  366. </script>
  367. </head>
  368. <body>
  369. <div class="inputhalf half">
  370. <div class="toolbar">
  371. <details>
  372. <summary>Readers</summary>
  373. <div id="readercontainer"></div>
  374. </details>
  375. </div>
  376. <textarea id="markdowninput">## Block Formats {#top}
  377. A regular paragraph.
  378. -# Sub text
  379. * Unordered
  380. * Lists
  381. * Sub list
  382. 1. Ordered
  383. 2. Lists
  384. 6. Sub list
  385. A blockquote:
  386. &gt; "The only thing we have to fear is fear itself—nameless, unreasoning,
  387. &gt; unjustified terror which paralyzes needed efforts to convert retreat into
  388. &gt; advance." - Franklin D. Roosevelt's First Inaugural Address
  389. Some definitions:
  390. word
  391. : a unit of meaning in a sentence
  392. sentence
  393. : a collection of words
  394. Code:
  395. ```javascript
  396. function foo() {
  397. return 'Hello world';
  398. }
  399. ```
  400. function bar() {
  401. return 'Indented code';
  402. }
  403. ### Heading, hash style
  404. Heading, underline style
  405. ---
  406. ### Modified Heading {style=color:red;}
  407. | Unit Price | Qty | Subtotal |
  408. | ---: | ---: | ---: |
  409. | $1.23 | 2 | =A*B FILL |
  410. | $4.99 | 6 | |
  411. | Total | | =SUM(C:C) |
  412. ---
  413. ## Inline Formats
  414. Normal, **double asterisk,** __double underscore,__ *asterisks,* _underscores,_
  415. ~~double tildes,~~ ~single tildes,~ ^carets,^ ==double equals==, ``double backticks``,
  416. `single backticks`.
  417. Lorem ipsum dolor sit amet,[^1] consectetur adipiscing elit.[^abc] Sed sodales
  418. in odio eget porta. Proin et erat sit amet erat semper mollis vitae ut
  419. turpis.[^foo] Ut ipsum dui, maximus sit amet suscipit at, dictum id nulla.[^bar]
  420. Aenean efficitur rhoncus nulla non fermentum.
  421. [^1]: Donec ut felis volutpat, gravida ipsum scelerisque, accumsan est.
  422. [^abc]: Cras dictum rutrum quam.
  423. [^foo]: Donec maximus bibendum lorem.
  424. [^bar]: Praesent consectetur tristique leo. Morbi nec nisi sit amet quam
  425. imperdiet vehicula eu feugiat tortor.
  426. The HTML on the WWW is often full of JS and CSS.
  427. *[HTML]: Hypertext Markup Language
  428. *[WWW]: World Wide Web
  429. *[JS]: JavaScript
  430. *[CSS]: Cascading Style Sheets
  431. Click [here](#top) to return to the top. Referenced link to [Google][google].
  432. ![alt text](https://picsum.photos/100/75) ![alt text](https://picsum.photos/101/75 "With a title")
  433. ![][testimage]
  434. [testimage]: https://picsum.photos/102/75
  435. [google]: https://google.com "I prefer Duck Duck Go"
  436. Some verbatim &lt;span style="color:green;"&gt;HTML&lt;/span&gt;. A script tag
  437. will be ignored. &lt;script&gt;&lt;/script&gt;</textarea>
  438. </div>
  439. <div class="previewhalf half">
  440. <div id="preview">
  441. <p>Preview here</p>
  442. </div>
  443. </div>
  444. </body>
  445. </html>