Bläddra i källkod

Refactor to unified MDReader, MDNode. More syntax readers.

main
Rocketsoup 1 år sedan
förälder
incheckning
f16c3fe63f
6 ändrade filer med 1214 tillägg och 1580 borttagningar
  1. 1169
    1531
      js/markdown.js
  2. 1
    1
      js/markdown.min.js
  3. 21
    25
      js/spreadsheet.js
  4. 1
    1
      js/spreadsheet.min.js
  5. 1
    1
      markdownjs.html
  6. 21
    21
      testjs.html

+ 1169
- 1531
js/markdown.js
Filskillnaden har hållits tillbaka eftersom den är för stor
Visa fil


+ 1
- 1
js/markdown.min.js
Filskillnaden har hållits tillbaka eftersom den är för stor
Visa fil


+ 21
- 25
js/spreadsheet.js Visa fil

2891
  * post-process step on any tables in the document tree. Must be used with
2891
  * post-process step on any tables in the document tree. Must be used with
2892
  * `MDTableBlockReader`. Tables without at least one formula will not be altered.
2892
  * `MDTableBlockReader`. Tables without at least one formula will not be altered.
2893
  */
2893
  */
2894
-class MDSpreadsheetBlockReader extends MDBlockReader {
2895
-	readBlock(state) {
2896
-		return null;
2897
-	}
2898
-
2899
-	postProcess(state, blocks) {
2900
-		for (const block of blocks) {
2901
-			if (block instanceof MDTableBlock) {
2902
-				this.#processTable(block, state);
2894
+class MDSpreadsheetReader extends MDReader {
2895
+	postProcess(state, nodes) {
2896
+		for (const node of nodes) {
2897
+			if (node instanceof MDTableNode) {
2898
+				this.#processTable(node, state);
2903
 			}
2899
 			}
2904
 		}
2900
 		}
2905
 	}
2901
 	}
2906
 
2902
 
2907
 	/**
2903
 	/**
2908
-	 * @param {MDTableBlock} tableBlock
2904
+	 * @param {MDTableNode} tableNode
2909
 	 * @param {MDState} state
2905
 	 * @param {MDState} state
2910
 	 */
2906
 	 */
2911
-	#processTable(tableBlock, state) {
2907
+	#processTable(tableNode, state) {
2912
 		// Measure table
2908
 		// Measure table
2913
-		const rowCount = tableBlock.bodyRows.length;
2909
+		const rowCount = tableNode.bodyRows.length;
2914
 		var columnCount = 0;
2910
 		var columnCount = 0;
2915
-		for (const row of tableBlock.bodyRows) {
2916
-			columnCount = Math.max(columnCount, row.cells.length);
2911
+		for (const row of tableNode.bodyRows) {
2912
+			columnCount = Math.max(columnCount, row.children.length);
2917
 		}
2913
 		}
2918
 
2914
 
2919
 		// Create and populate grid
2915
 		// Create and populate grid
2920
 		const grid = new SpreadsheetGrid(columnCount, rowCount);
2916
 		const grid = new SpreadsheetGrid(columnCount, rowCount);
2921
 		for (var c = 0; c < columnCount; c++) {
2917
 		for (var c = 0; c < columnCount; c++) {
2922
 			for (var r = 0; r < rowCount; r++) {
2918
 			for (var r = 0; r < rowCount; r++) {
2923
-				const cellBlock = tableBlock.bodyRows[r].cells[c];
2924
-				if (cellBlock === undefined) continue;
2925
-				const cellText = cellBlock.toPlaintext(state);
2919
+				const cellNode = tableNode.bodyRows[r].children[c];
2920
+				if (cellNode === undefined) continue;
2921
+				const cellText = cellNode.toPlaintext(state);
2926
 				const gridCell = grid.cells[c][r];
2922
 				const gridCell = grid.cells[c][r];
2927
 				gridCell.originalValue = CellValue.fromCellString(cellText);
2923
 				gridCell.originalValue = CellValue.fromCellString(cellText);
2928
 			}
2924
 			}
2947
 		// Copy results back to table
2943
 		// Copy results back to table
2948
 		for (var c = 0; c < columnCount; c++) {
2944
 		for (var c = 0; c < columnCount; c++) {
2949
 			for (var r = 0; r < rowCount; r++) {
2945
 			for (var r = 0; r < rowCount; r++) {
2950
-				const cellBlock = tableBlock.bodyRows[r].cells[c];
2951
-				if (cellBlock === undefined) continue;
2946
+				const cellNode = tableNode.bodyRows[r].children[c];
2947
+				if (cellNode === undefined) continue;
2952
 				const gridCell = grid.cells[c][r];
2948
 				const gridCell = grid.cells[c][r];
2953
 				const gridValue = gridCell.outputValue;
2949
 				const gridValue = gridCell.outputValue;
2954
 				const cellText = gridValue.formattedValue;
2950
 				const cellText = gridValue.formattedValue;
2955
-				cellBlock.content = new MDInlineBlock(new MDTextSpan(cellText));
2951
+				cellNode.children = [ new MDTextNode(cellText) ];
2956
 				if (gridCell.isCalculated) {
2952
 				if (gridCell.isCalculated) {
2957
-					cellBlock.cssClasses.push('calculated');
2953
+					cellNode.cssClasses.push('calculated');
2958
 				}
2954
 				}
2959
-				cellBlock.cssClasses.push(`spreadsheet-type-${gridValue.type}`);
2955
+				cellNode.cssClasses.push(`spreadsheet-type-${gridValue.type}`);
2960
 				if (gridValue.type == CellValue.TYPE_ERROR) {
2956
 				if (gridValue.type == CellValue.TYPE_ERROR) {
2961
-					cellBlock.attributes['title'] = gridValue.value;
2957
+					cellNode.attributes['title'] = gridValue.value;
2962
 				}
2958
 				}
2963
 				const gridNumber = gridValue.numericValue();
2959
 				const gridNumber = gridValue.numericValue();
2964
 				if (gridNumber !== null) {
2960
 				if (gridNumber !== null) {
2965
-					cellBlock.attributes['data-numeric-value'] = `${gridNumber}`;
2961
+					cellNode.attributes['data-numeric-value'] = `${gridNumber}`;
2966
 				}
2962
 				}
2967
 				const gridString = gridValue.stringValue(false);
2963
 				const gridString = gridValue.stringValue(false);
2968
 				if (gridString !== null) {
2964
 				if (gridString !== null) {
2969
-					cellBlock.attributes['data-string-value'] = gridString;
2965
+					cellNode.attributes['data-string-value'] = gridString;
2970
 				}
2966
 				}
2971
 			}
2967
 			}
2972
 		}
2968
 		}

+ 1
- 1
js/spreadsheet.min.js
Filskillnaden har hållits tillbaka eftersom den är för stor
Visa fil


+ 1
- 1
markdownjs.html Visa fil

86
 		<script src="js/markdown.js"></script>
86
 		<script src="js/markdown.js"></script>
87
 		<script src="js/spreadsheet.js"></script>
87
 		<script src="js/spreadsheet.js"></script>
88
 		<script>
88
 		<script>
89
-			var parser = new Markdown([ ...Markdown.allBlockReaders, new MDSpreadsheetBlockReader() ], Markdown.allInlineReaders);
89
+			var parser = new Markdown([ ...Markdown.allReaders, new MDSpreadsheetReader() ]);
90
 			function onDocumentLoad() {
90
 			function onDocumentLoad() {
91
 				document.getElementById('markdowninput').addEventListener('input', onMarkdownChange);
91
 				document.getElementById('markdowninput').addEventListener('input', onMarkdownChange);
92
 				setTimeout(onMarkdownChange, 0);
92
 				setTimeout(onMarkdownChange, 0);

+ 21
- 21
testjs.html Visa fil

737
 
737
 
738
 				test_strikethrough_single() {
738
 				test_strikethrough_single() {
739
 					let markdown = 'Lorem ~ipsum~ dolor';
739
 					let markdown = 'Lorem ~ipsum~ dolor';
740
-					let expected = 'Lorem <strike>ipsum</strike> dolor';
740
+					let expected = 'Lorem <s>ipsum</s> dolor';
741
 					let actual = this.md(markdown);
741
 					let actual = this.md(markdown);
742
 					this.assertEqual(actual, expected);
742
 					this.assertEqual(actual, expected);
743
 				}
743
 				}
744
 
744
 
745
 				test_strikethrough_double() {
745
 				test_strikethrough_double() {
746
 					let markdown = 'Lorem ~~ipsum~~ dolor';
746
 					let markdown = 'Lorem ~~ipsum~~ dolor';
747
-					let expected = 'Lorem <strike>ipsum</strike> dolor';
747
+					let expected = 'Lorem <s>ipsum</s> dolor';
748
 					let actual = this.md(markdown);
748
 					let actual = this.md(markdown);
749
 					this.assertEqual(actual, expected);
749
 					this.assertEqual(actual, expected);
750
 				}
750
 				}
850
 
850
 
851
 				test_htmlTags() {
851
 				test_htmlTags() {
852
 					let markdown = 'Lorem <strong title="value" foo=\'with " quote\' bar="with \' apostrophe" attr=unquoted checked>ipsum</strong> dolor';
852
 					let markdown = 'Lorem <strong title="value" foo=\'with " quote\' bar="with \' apostrophe" attr=unquoted checked>ipsum</strong> dolor';
853
-					let expected = markdown;
853
+					let expected = 'Lorem <strong title="value" foo="with &quot; quote" bar="with \' apostrophe" attr="unquoted" checked>ipsum</strong> dolor';
854
 					let actual = this.md(markdown);
854
 					let actual = this.md(markdown);
855
 					this.assertEqual(actual, expected);
855
 					this.assertEqual(actual, expected);
856
 				}
856
 				}
918
 
918
 
919
 				test_unorderedList_nested() {
919
 				test_unorderedList_nested() {
920
 					let markdown = "* Lorem\n + Ipsum\n* Dolor";
920
 					let markdown = "* Lorem\n + Ipsum\n* Dolor";
921
-					let expected = '<ul> <li>Lorem <ul> <li>Ipsum</li> </ul></li> <li>Dolor</li> </ul>';
921
+					let expected = '<ul> <li>Lorem<ul> <li>Ipsum</li> </ul> </li> <li>Dolor</li> </ul>';
922
 					let actual = this.md(markdown);
922
 					let actual = this.md(markdown);
923
 					this.assertEqual(actual, expected);
923
 					this.assertEqual(actual, expected);
924
 				}
924
 				}
950
 
950
 
951
 				test_orderedList_nested1() {
951
 				test_orderedList_nested1() {
952
 					let markdown = "1. Lorem\n 1. Ipsum\n1. Dolor";
952
 					let markdown = "1. Lorem\n 1. Ipsum\n1. Dolor";
953
-					let expected = '<ol> <li>Lorem <ol> <li>Ipsum</li> </ol></li> <li>Dolor</li> </ol>';
953
+					let expected = '<ol> <li>Lorem<ol> <li>Ipsum</li> </ol> </li> <li>Dolor</li> </ol>';
954
 					let actual = this.md(markdown);
954
 					let actual = this.md(markdown);
955
 					this.assertEqual(actual, expected);
955
 					this.assertEqual(actual, expected);
956
 				}
956
 				}
957
 
957
 
958
 				test_orderedList_nested2() {
958
 				test_orderedList_nested2() {
959
 					let markdown = "1. Lorem\n 1. Ipsum\n      1. Dolor\n 1. Sit\n1. Amet";
959
 					let markdown = "1. Lorem\n 1. Ipsum\n      1. Dolor\n 1. Sit\n1. Amet";
960
-					let expected = '<ol> <li>Lorem <ol> <li>Ipsum <ol> <li>Dolor</li> </ol></li> <li>Sit</li> </ol></li> <li>Amet</li> </ol>';
960
+					let expected = '<ol> <li>Lorem<ol> <li>Ipsum<ol> <li>Dolor</li> </ol> </li> <li>Sit</li> </ol> </li> <li>Amet</li> </ol>';
961
 					let actual = this.md(markdown);
961
 					let actual = this.md(markdown);
962
 					this.assertEqual(actual, expected);
962
 					this.assertEqual(actual, expected);
963
 				}
963
 				}
964
 
964
 
965
 				test_blockquote() {
965
 				test_blockquote() {
966
 					let markdown = '> Lorem ipsum dolor';
966
 					let markdown = '> Lorem ipsum dolor';
967
-					let expected = '<blockquote> Lorem ipsum dolor </blockquote>';
967
+					let expected = '<blockquote>Lorem ipsum dolor</blockquote>';
968
 					let actual = this.md(markdown);
968
 					let actual = this.md(markdown);
969
 					this.assertEqual(actual, expected);
969
 					this.assertEqual(actual, expected);
970
 				}
970
 				}
985
 
985
 
986
 				test_codeBlock_indented() {
986
 				test_codeBlock_indented() {
987
 					let markdown = "Code\n\n    function foo() {\n        return 'bar';\n    }\n\nend";
987
 					let markdown = "Code\n\n    function foo() {\n        return 'bar';\n    }\n\nend";
988
-					let expected = "<p>Code</p>\n\n<pre><code>function foo() {\n    return 'bar';\n}</code></pre>\n<p>end</p>\n";
989
-					let actual = this.parser.toHTML(markdown); // don't normalize whitespace
988
+					let expected = "<p>Code</p>\n\n<pre><code>function foo() {\n    return 'bar';\n}</code></pre>\n\n<p>end</p>";
989
+					let actual = this.parser.toHTML(markdown).trim(); // don't normalize whitespace
990
 					this.assertEqual(actual.replace(/ /g, '⎵'), expected.replace(/ /g, '⎵'));
990
 					this.assertEqual(actual.replace(/ /g, '⎵'), expected.replace(/ /g, '⎵'));
991
 				}
991
 				}
992
 
992
 
993
 				test_codeBlock_fenced() {
993
 				test_codeBlock_fenced() {
994
 					let markdown = "Code\n\n```\nfunction foo() {\n    return 'bar';\n}\n```\n\nend";
994
 					let markdown = "Code\n\n```\nfunction foo() {\n    return 'bar';\n}\n```\n\nend";
995
-					let expected = "<p>Code</p>\n\n<pre><code>function foo() {\n    return 'bar';\n}</code></pre>\n<p>end</p>\n";
996
-					let actual = this.parser.toHTML(markdown); // don't normalize whitespace
995
+					let expected = "<p>Code</p>\n\n<pre><code>function foo() {\n    return 'bar';\n}</code></pre>\n\n<p>end</p>";
996
+					let actual = this.parser.toHTML(markdown).trim(); // don't normalize whitespace
997
 					this.assertEqual(actual.replace(/ /g, '⎵'), expected.replace(/ /g, '⎵'));
997
 					this.assertEqual(actual.replace(/ /g, '⎵'), expected.replace(/ /g, '⎵'));
998
 				}
998
 				}
999
 
999
 
1026
 					let expected = '<table> ' +
1026
 					let expected = '<table> ' +
1027
 						'<thead> ' +
1027
 						'<thead> ' +
1028
 						'<tr> ' +
1028
 						'<tr> ' +
1029
-						'<th align="left">Column A</th> ' +
1030
-						'<th align="center">Column B</th> ' +
1031
-						'<th align="right">Column C</th> ' +
1029
+						'<th style="text-align: left;">Column A</th> ' +
1030
+						'<th style="text-align: center;">Column B</th> ' +
1031
+						'<th style="text-align: right;">Column C</th> ' +
1032
 						'</tr> ' +
1032
 						'</tr> ' +
1033
 						'</thead> ' +
1033
 						'</thead> ' +
1034
 						'<tbody> ' +
1034
 						'<tbody> ' +
1035
 						'<tr> ' +
1035
 						'<tr> ' +
1036
-						'<td align="left">1</td> ' +
1037
-						'<td align="center">2</td> ' +
1038
-						'<td align="right">3</td> ' +
1036
+						'<td style="text-align: left;">1</td> ' +
1037
+						'<td style="text-align: center;">2</td> ' +
1038
+						'<td style="text-align: right;">3</td> ' +
1039
 						'</tr> ' +
1039
 						'</tr> ' +
1040
 						'<tr> ' +
1040
 						'<tr> ' +
1041
-						'<td align="left">4</td> ' +
1042
-						'<td align="center">5</td> ' +
1043
-						'<td align="right">6</td> ' +
1041
+						'<td style="text-align: left;">4</td> ' +
1042
+						'<td style="text-align: center;">5</td> ' +
1043
+						'<td style="text-align: right;">6</td> ' +
1044
 						'</tr> ' +
1044
 						'</tr> ' +
1045
 						'</tbody> ' +
1045
 						'</tbody> ' +
1046
 						'</table>';
1046
 						'</table>';
1702
 				parser;
1702
 				parser;
1703
 
1703
 
1704
 				setUp() {
1704
 				setUp() {
1705
-					this.parser = new Markdown([ ...Markdown.allBlockReaders, new MDSpreadsheetBlockReader() ], Markdown.allInlineReaders);
1705
+					this.parser = new Markdown([ ...Markdown.allReaders, new MDSpreadsheetReader() ]);
1706
 				}
1706
 				}
1707
 
1707
 
1708
 				md(markdown) {
1708
 				md(markdown) {

Laddar…
Avbryt
Spara