Przeglądaj źródła

Adding MDLineBreakReader

main
Rocketsoup 1 rok temu
rodzic
commit
66d859d250
3 zmienionych plików z 53 dodań i 12 usunięć
  1. 23
    0
      js/markdown.js
  2. 1
    1
      js/markdown.min.js
  3. 29
    11
      markdownjs.html

+ 23
- 0
js/markdown.js Wyświetl plik

@@ -3090,6 +3090,29 @@ class MDReferencedImageReader extends MDReferencedLinkReader {
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 3116
  * Reads a verbatim HTML tag, and if it passes validation by `MDState.tagFilter`,
3094 3117
  * will be rendered in the final HTML document. Disallowed tags will be rendered
3095 3118
  * as plain text in the resulting document.

+ 1
- 1
js/markdown.min.js
Plik diff jest za duży
Wyświetl plik


+ 29
- 11
markdownjs.html Wyświetl plik

@@ -257,8 +257,11 @@
257 257
 		<script src="js/markdown.js"></script>
258 258
 		<script src="js/spreadsheet.js"></script>
259 259
 		<script>
260
-			var parser = new Markdown([ ...Markdown.allReaders, new MDSpreadsheetReader() ]);
260
+			var activeReaders = [];
261
+			var parser = null;
261 262
 			function onDocumentLoad() {
263
+				activeReaders = Markdown.standardReaders.slice();
264
+				parser = new Markdown(activeReaders);
262 265
 				document.getElementById('markdowninput').addEventListener('input', onMarkdownChange);
263 266
 				populateReaderCheckboxes();
264 267
 				setTimeout(onMarkdownChange, 0);
@@ -300,8 +303,8 @@
300 303
 				'Abbrevations': MDAbbreviationReader,
301 304
 				'HTML tags': MDHTMLTagReader,
302 305
 				'Modifiers': MDModifierReader,
306
+				'Line breaks': MDLineBreakReader,
303 307
 			};
304
-			var activeReaders = [];
305 308
 			function populateReaderCheckboxes() {
306 309
 				const container = document.getElementById('readercontainer');
307 310
 				var header = document.createElement('div');
@@ -312,7 +315,6 @@
312 315
 				container.append(blockContainer);
313 316
 				for (const name of Object.keys(blockReaderClasses).sort()) {
314 317
 					const readerClass = blockReaderClasses[name];
315
-					activeReaders.push(new readerClass());
316 318
 					const checkbox = makeReaderCheckbox(name, readerClass);
317 319
 					blockContainer.append(checkbox);
318 320
 				}
@@ -324,15 +326,21 @@
324 326
 				container.append(inlineContainer);
325 327
 				for (const name of Object.keys(inlineReaderClasses).sort()) {
326 328
 					const readerClass = inlineReaderClasses[name];
327
-					activeReaders.push(new readerClass());
328 329
 					const checkbox = makeReaderCheckbox(name, readerClass);
329 330
 					inlineContainer.append(checkbox);
330 331
 				}
331 332
 			}
332 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 341
 				const check = document.createElement('input');
334 342
 				check.type = 'checkbox';
335
-				check.checked = true;
343
+				check.checked = isSelected;
336 344
 				check.addEventListener('change', () => {
337 345
 					handleCheckChanged(readerClass, check);
338 346
 				});
@@ -386,7 +394,9 @@ A regular paragraph.
386 394
 
387 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 401
 Some definitions:
392 402
 
@@ -424,14 +434,20 @@ Heading, underline style
424 434
 
425 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 446
 [^1]: Donec ut felis volutpat, gravida ipsum scelerisque, accumsan est.
432 447
 [^abc]: Cras dictum rutrum quam.
433 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 452
 The HTML on the WWW is often full of JS and CSS.
437 453
 
@@ -442,12 +458,14 @@ The HTML on the WWW is often full of JS and CSS.
442 458
 
443 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 464
 [testimage]: https://picsum.photos/102/75
448 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 469
 		</div>
452 470
 		<div class="previewhalf half">
453 471
 			<div id="preview">

Ładowanie…
Anuluj
Zapisz