Quellcode durchsuchen

Nested lists working

main
Rocketsoup vor 1 Jahr
Ursprung
Commit
6a8f5eeceb
1 geänderte Dateien mit 21 neuen und 2 gelöschten Zeilen
  1. 21
    2
      js/markdown.js

+ 21
- 2
js/markdown.js Datei anzeigen

7
 //     ---
7
 //     ---
8
 //     [link](url){.class}
8
 //     [link](url){.class}
9
 //     ``` {.class}
9
 //     ``` {.class}
10
+// TODO: Test broken/incomplete syntax thoroughly
11
+// TODO: Sanity checks on loops/recursion?
10
 
12
 
11
 class _MDHAlign {
13
 class _MDHAlign {
12
 	static Left = new _MDHAlign('Left');
14
 	static Left = new _MDHAlign('Left');
920
 	 * @param {String} line
922
 	 * @param {String} line
921
 	 */
923
 	 */
922
 	static #stripIndent(line, count=1) {
924
 	static #stripIndent(line, count=1) {
923
-		let regex = new RegExp(`^(: {1,4}|\\t){${count}}`);
925
+		let regex = new RegExp(`^(?: {1,4}|\\t){${count}}`);
924
 		return line.replace(regex, '');
926
 		return line.replace(regex, '');
925
 	}
927
 	}
926
 
928
 
1497
 		var seenBlankLine = false;
1499
 		var seenBlankLine = false;
1498
 		var needsBlocks = false;
1500
 		var needsBlocks = false;
1499
 		var lines = [];
1501
 		var lines = [];
1500
-		while (p < state.lines.length) {
1502
+		var hasNestedList = false;
1503
+		var firstNestedListLine = -1;
1504
+		while (state.hasLines(1, p)) {
1501
 			let line = state.lines[p++];
1505
 			let line = state.lines[p++];
1502
 			if (p == state.p + 1) {
1506
 			if (p == state.p + 1) {
1503
 				line = line.substring(firstLineStartPos);
1507
 				line = line.substring(firstLineStartPos);
1514
 				if (seenBlankLine) {
1518
 				if (seenBlankLine) {
1515
 					needsBlocks = true;
1519
 					needsBlocks = true;
1516
 				}
1520
 				}
1521
+				if (inList && /^\s*(?:\*|\+|\-|\d+\.)\s+/.exec(line)) {
1522
+					hasNestedList = true;
1523
+					if (firstNestedListLine < 0) {
1524
+						firstNestedListLine = lines.length;
1525
+					}
1526
+				}
1517
 				lines.push(this.#stripIndent(line));
1527
 				lines.push(this.#stripIndent(line));
1518
 			} else {
1528
 			} else {
1519
 				if (seenBlankLine) {
1529
 				if (seenBlankLine) {
1526
 		while (lines.length > 0 && lines[lines.length - 1].trim().length == 0) {
1536
 		while (lines.length > 0 && lines[lines.length - 1].trim().length == 0) {
1527
 			lines.pop();
1537
 			lines.pop();
1528
 		}
1538
 		}
1539
+		if (inList && hasNestedList) {
1540
+			let parentLines = lines.slice(0, firstNestedListLine);
1541
+			let parentContent = this.#readInline(state, parentLines.join("\n"));
1542
+			let nestedLines = lines.slice(firstNestedListLine);
1543
+			let substate = state.copy(nestedLines);
1544
+			let nestedContent = this.#readBlocks(substate);
1545
+			state.p = p;
1546
+			return new _MDMultiBlock([parentContent].concat(nestedContent));
1547
+		}
1529
 		if (needsBlocks) {
1548
 		if (needsBlocks) {
1530
 			let substate = state.copy(lines);
1549
 			let substate = state.copy(lines);
1531
 			let blocks = this.#readBlocks(substate);
1550
 			let blocks = this.#readBlocks(substate);

Laden…
Abbrechen
Speichern