Browse Source

Adding MDLineBreakReader

main
Rocketsoup 1 year ago
parent
commit
66d859d250
3 changed files with 53 additions and 12 deletions
  1. 23
    0
      js/markdown.js
  2. 1
    1
      js/markdown.min.js
  3. 29
    11
      markdownjs.html

+ 23
- 0
js/markdown.js View File

3090
 }
3090
 }
3091
 
3091
 
3092
 /**
3092
 /**
3093
+ * Converts line breaks within blocks into line breaks in the HTML. Not
3094
+ * included in any of the default reader sets since most flavors ignore
3095
+ * line breaks within blocks.
3096
+ */
3097
+class MDLineBreakReader extends MDReader {
3098
+	postProcess(state, blocks) {
3099
+		MDNode.replaceNodes(state, blocks, (original) => {
3100
+			if (!(original instanceof MDTextNode)) return null;
3101
+			const lines = original.text.split("\n");
3102
+			if (lines.length == 1) return null;
3103
+			var nodes = [];
3104
+			for (const [i, line] of lines.entries()) {
3105
+				if (i > 0) {
3106
+					nodes.push(new MDLineBreakNode());
3107
+				}
3108
+				nodes.push(new MDTextNode(line));
3109
+			}
3110
+			return new MDNode(nodes);
3111
+		});
3112
+	}
3113
+}
3114
+
3115
+/**
3093
  * Reads a verbatim HTML tag, and if it passes validation by `MDState.tagFilter`,
3116
  * Reads a verbatim HTML tag, and if it passes validation by `MDState.tagFilter`,
3094
  * will be rendered in the final HTML document. Disallowed tags will be rendered
3117
  * will be rendered in the final HTML document. Disallowed tags will be rendered
3095
  * as plain text in the resulting document.
3118
  * as plain text in the resulting document.

+ 1
- 1
js/markdown.min.js
File diff suppressed because it is too large
View File


+ 29
- 11
markdownjs.html View File

257
 		<script src="js/markdown.js"></script>
257
 		<script src="js/markdown.js"></script>
258
 		<script src="js/spreadsheet.js"></script>
258
 		<script src="js/spreadsheet.js"></script>
259
 		<script>
259
 		<script>
260
-			var parser = new Markdown([ ...Markdown.allReaders, new MDSpreadsheetReader() ]);
260
+			var activeReaders = [];
261
+			var parser = null;
261
 			function onDocumentLoad() {
262
 			function onDocumentLoad() {
263
+				activeReaders = Markdown.standardReaders.slice();
264
+				parser = new Markdown(activeReaders);
262
 				document.getElementById('markdowninput').addEventListener('input', onMarkdownChange);
265
 				document.getElementById('markdowninput').addEventListener('input', onMarkdownChange);
263
 				populateReaderCheckboxes();
266
 				populateReaderCheckboxes();
264
 				setTimeout(onMarkdownChange, 0);
267
 				setTimeout(onMarkdownChange, 0);
300
 				'Abbrevations': MDAbbreviationReader,
303
 				'Abbrevations': MDAbbreviationReader,
301
 				'HTML tags': MDHTMLTagReader,
304
 				'HTML tags': MDHTMLTagReader,
302
 				'Modifiers': MDModifierReader,
305
 				'Modifiers': MDModifierReader,
306
+				'Line breaks': MDLineBreakReader,
303
 			};
307
 			};
304
-			var activeReaders = [];
305
 			function populateReaderCheckboxes() {
308
 			function populateReaderCheckboxes() {
306
 				const container = document.getElementById('readercontainer');
309
 				const container = document.getElementById('readercontainer');
307
 				var header = document.createElement('div');
310
 				var header = document.createElement('div');
312
 				container.append(blockContainer);
315
 				container.append(blockContainer);
313
 				for (const name of Object.keys(blockReaderClasses).sort()) {
316
 				for (const name of Object.keys(blockReaderClasses).sort()) {
314
 					const readerClass = blockReaderClasses[name];
317
 					const readerClass = blockReaderClasses[name];
315
-					activeReaders.push(new readerClass());
316
 					const checkbox = makeReaderCheckbox(name, readerClass);
318
 					const checkbox = makeReaderCheckbox(name, readerClass);
317
 					blockContainer.append(checkbox);
319
 					blockContainer.append(checkbox);
318
 				}
320
 				}
324
 				container.append(inlineContainer);
326
 				container.append(inlineContainer);
325
 				for (const name of Object.keys(inlineReaderClasses).sort()) {
327
 				for (const name of Object.keys(inlineReaderClasses).sort()) {
326
 					const readerClass = inlineReaderClasses[name];
328
 					const readerClass = inlineReaderClasses[name];
327
-					activeReaders.push(new readerClass());
328
 					const checkbox = makeReaderCheckbox(name, readerClass);
329
 					const checkbox = makeReaderCheckbox(name, readerClass);
329
 					inlineContainer.append(checkbox);
330
 					inlineContainer.append(checkbox);
330
 				}
331
 				}
331
 			}
332
 			}
332
 			function makeReaderCheckbox(name, readerClass) {
333
 			function makeReaderCheckbox(name, readerClass) {
334
+				var isSelected = false;
335
+				for (const elem of activeReaders) {
336
+					if (elem.constructor === readerClass) {
337
+						isSelected = true;
338
+						break;
339
+					}
340
+				}
333
 				const check = document.createElement('input');
341
 				const check = document.createElement('input');
334
 				check.type = 'checkbox';
342
 				check.type = 'checkbox';
335
-				check.checked = true;
343
+				check.checked = isSelected;
336
 				check.addEventListener('change', () => {
344
 				check.addEventListener('change', () => {
337
 					handleCheckChanged(readerClass, check);
345
 					handleCheckChanged(readerClass, check);
338
 				});
346
 				});
386
 
394
 
387
 A blockquote:
395
 A blockquote:
388
 
396
 
389
-&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
397
+&gt; "The only thing we have to fear is fear itself—nameless, unreasoning,
398
+&gt; unjustified terror which paralyzes needed efforts to convert retreat into
399
+&gt; advance." - Franklin D. Roosevelt's First Inaugural Address
390
 
400
 
391
 Some definitions:
401
 Some definitions:
392
 
402
 
424
 
434
 
425
 ## Inline Formats
435
 ## Inline Formats
426
 
436
 
427
-Normal, **double asterisk,** __double underscore,__ *asterisks,* _underscores,_ ~~double tildes,~~ ~single tildes,~ ^carets,^ ==double equals==, ``double backticks``, `single backticks`.
437
+Normal, **double asterisk,** __double underscore,__ *asterisks,* _underscores,_
438
+~~double tildes,~~ ~single tildes,~ ^carets,^ ==double equals==, ``double backticks``,
439
+`single backticks`.
428
 
440
 
429
-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.
441
+Lorem ipsum dolor sit amet,[^1] consectetur adipiscing elit.[^abc] Sed sodales
442
+in odio eget porta. Proin et erat sit amet erat semper mollis vitae ut
443
+turpis.[^foo] Ut ipsum dui, maximus sit amet suscipit at, dictum id nulla.[^bar]
444
+Aenean efficitur rhoncus nulla non fermentum.
430
 
445
 
431
 [^1]: Donec ut felis volutpat, gravida ipsum scelerisque, accumsan est.
446
 [^1]: Donec ut felis volutpat, gravida ipsum scelerisque, accumsan est.
432
 [^abc]: Cras dictum rutrum quam.
447
 [^abc]: Cras dictum rutrum quam.
433
 [^foo]: Donec maximus bibendum lorem.
448
 [^foo]: Donec maximus bibendum lorem.
434
-[^bar]: Praesent consectetur tristique leo. Morbi nec nisi sit amet quam imperdiet vehicula eu feugiat tortor. 
449
+[^bar]: Praesent consectetur tristique leo. Morbi nec nisi sit amet quam
450
+ imperdiet vehicula eu feugiat tortor. 
435
 
451
 
436
 The HTML on the WWW is often full of JS and CSS.
452
 The HTML on the WWW is often full of JS and CSS.
437
 
453
 
442
 
458
 
443
 Click [here](#top) to return to the top. Referenced link to [Google][google].
459
 Click [here](#top) to return to the top. Referenced link to [Google][google].
444
 
460
 
445
-![alt text](https://picsum.photos/100/75) ![alt text](https://picsum.photos/101/75 "With a title") ![][testimage]
461
+![alt text](https://picsum.photos/100/75) ![alt text](https://picsum.photos/101/75 "With a title")
462
+![][testimage]
446
 
463
 
447
 [testimage]: https://picsum.photos/102/75
464
 [testimage]: https://picsum.photos/102/75
448
 [google]: https://google.com  "I prefer Duck Duck Go"
465
 [google]: https://google.com  "I prefer Duck Duck Go"
449
 
466
 
450
-Some verbatim &lt;span style="color:green;"&gt;HTML&lt;/span&gt;. A script tag will be ignored. &lt;script&gt;&lt;/script&gt;</textarea>
467
+Some verbatim &lt;span style="color:green;"&gt;HTML&lt;/span&gt;. A script tag
468
+will be ignored. &lt;script&gt;&lt;/script&gt;</textarea>
451
 		</div>
469
 		</div>
452
 		<div class="previewhalf half">
470
 		<div class="previewhalf half">
453
 			<div id="preview">
471
 			<div id="preview">

Loading…
Cancel
Save