Kaynağa Gözat

Refactor to unified MDReader, MDNode. More syntax readers.

main
Rocketsoup 1 yıl önce
ebeveyn
işleme
f16c3fe63f
6 değiştirilmiş dosya ile 1214 ekleme ve 1580 silme
  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
Dosya farkı çok büyük olduğundan ihmal edildi
Dosyayı Görüntüle


+ 1
- 1
js/markdown.min.js
Dosya farkı çok büyük olduğundan ihmal edildi
Dosyayı Görüntüle


+ 21
- 25
js/spreadsheet.js Dosyayı Görüntüle

@@ -2891,38 +2891,34 @@ class SpreadsheetCell {
2891 2891
  * post-process step on any tables in the document tree. Must be used with
2892 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 2905
 	 * @param {MDState} state
2910 2906
 	 */
2911
-	#processTable(tableBlock, state) {
2907
+	#processTable(tableNode, state) {
2912 2908
 		// Measure table
2913
-		const rowCount = tableBlock.bodyRows.length;
2909
+		const rowCount = tableNode.bodyRows.length;
2914 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 2915
 		// Create and populate grid
2920 2916
 		const grid = new SpreadsheetGrid(columnCount, rowCount);
2921 2917
 		for (var c = 0; c < columnCount; c++) {
2922 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 2922
 				const gridCell = grid.cells[c][r];
2927 2923
 				gridCell.originalValue = CellValue.fromCellString(cellText);
2928 2924
 			}
@@ -2947,26 +2943,26 @@ class MDSpreadsheetBlockReader extends MDBlockReader {
2947 2943
 		// Copy results back to table
2948 2944
 		for (var c = 0; c < columnCount; c++) {
2949 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 2948
 				const gridCell = grid.cells[c][r];
2953 2949
 				const gridValue = gridCell.outputValue;
2954 2950
 				const cellText = gridValue.formattedValue;
2955
-				cellBlock.content = new MDInlineBlock(new MDTextSpan(cellText));
2951
+				cellNode.children = [ new MDTextNode(cellText) ];
2956 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 2956
 				if (gridValue.type == CellValue.TYPE_ERROR) {
2961
-					cellBlock.attributes['title'] = gridValue.value;
2957
+					cellNode.attributes['title'] = gridValue.value;
2962 2958
 				}
2963 2959
 				const gridNumber = gridValue.numericValue();
2964 2960
 				if (gridNumber !== null) {
2965
-					cellBlock.attributes['data-numeric-value'] = `${gridNumber}`;
2961
+					cellNode.attributes['data-numeric-value'] = `${gridNumber}`;
2966 2962
 				}
2967 2963
 				const gridString = gridValue.stringValue(false);
2968 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
Dosya farkı çok büyük olduğundan ihmal edildi
Dosyayı Görüntüle


+ 1
- 1
markdownjs.html Dosyayı Görüntüle

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

+ 21
- 21
testjs.html Dosyayı Görüntüle

@@ -737,14 +737,14 @@
737 737
 
738 738
 				test_strikethrough_single() {
739 739
 					let markdown = 'Lorem ~ipsum~ dolor';
740
-					let expected = 'Lorem <strike>ipsum</strike> dolor';
740
+					let expected = 'Lorem <s>ipsum</s> dolor';
741 741
 					let actual = this.md(markdown);
742 742
 					this.assertEqual(actual, expected);
743 743
 				}
744 744
 
745 745
 				test_strikethrough_double() {
746 746
 					let markdown = 'Lorem ~~ipsum~~ dolor';
747
-					let expected = 'Lorem <strike>ipsum</strike> dolor';
747
+					let expected = 'Lorem <s>ipsum</s> dolor';
748 748
 					let actual = this.md(markdown);
749 749
 					this.assertEqual(actual, expected);
750 750
 				}
@@ -850,7 +850,7 @@
850 850
 
851 851
 				test_htmlTags() {
852 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 854
 					let actual = this.md(markdown);
855 855
 					this.assertEqual(actual, expected);
856 856
 				}
@@ -918,7 +918,7 @@
918 918
 
919 919
 				test_unorderedList_nested() {
920 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 922
 					let actual = this.md(markdown);
923 923
 					this.assertEqual(actual, expected);
924 924
 				}
@@ -950,21 +950,21 @@
950 950
 
951 951
 				test_orderedList_nested1() {
952 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 954
 					let actual = this.md(markdown);
955 955
 					this.assertEqual(actual, expected);
956 956
 				}
957 957
 
958 958
 				test_orderedList_nested2() {
959 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 961
 					let actual = this.md(markdown);
962 962
 					this.assertEqual(actual, expected);
963 963
 				}
964 964
 
965 965
 				test_blockquote() {
966 966
 					let markdown = '> Lorem ipsum dolor';
967
-					let expected = '<blockquote> Lorem ipsum dolor </blockquote>';
967
+					let expected = '<blockquote>Lorem ipsum dolor</blockquote>';
968 968
 					let actual = this.md(markdown);
969 969
 					this.assertEqual(actual, expected);
970 970
 				}
@@ -985,15 +985,15 @@
985 985
 
986 986
 				test_codeBlock_indented() {
987 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 990
 					this.assertEqual(actual.replace(/ /g, '⎵'), expected.replace(/ /g, '⎵'));
991 991
 				}
992 992
 
993 993
 				test_codeBlock_fenced() {
994 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 997
 					this.assertEqual(actual.replace(/ /g, '⎵'), expected.replace(/ /g, '⎵'));
998 998
 				}
999 999
 
@@ -1026,21 +1026,21 @@
1026 1026
 					let expected = '<table> ' +
1027 1027
 						'<thead> ' +
1028 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 1032
 						'</tr> ' +
1033 1033
 						'</thead> ' +
1034 1034
 						'<tbody> ' +
1035 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 1039
 						'</tr> ' +
1040 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 1044
 						'</tr> ' +
1045 1045
 						'</tbody> ' +
1046 1046
 						'</table>';
@@ -1702,7 +1702,7 @@
1702 1702
 				parser;
1703 1703
 
1704 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 1708
 				md(markdown) {

Loading…
İptal
Kaydet