Преглед изворни кода

Exceptions while parsing markdown are caught and investigated and the minimal chunk of markdown is printed to console

main
Rocketsoup пре 1 година
родитељ
комит
f40d49c1a6
3 измењених фајлова са 48 додато и 3 уклоњено
  1. 45
    0
      js/markdown.js
  2. 1
    1
      js/markdown.min.js
  3. 2
    2
      markdownjs.html

+ 45
- 0
js/markdown.js Прегледај датотеку

@@ -4138,6 +4138,19 @@ class Markdown {
4138 4138
 	 */
4139 4139
 	toHTML(markdown, elementIdPrefix='') {
4140 4140
 		const lines = markdown.split(/(?:\n|\r|\r\n)/);
4141
+		try {
4142
+			return this.#parse(lines, elementIdPrefix);
4143
+		} catch (e) {
4144
+			this.#investigateException(lines, elementIdPrefix);
4145
+			throw e;
4146
+		}
4147
+	}
4148
+
4149
+	/**
4150
+	 * @param {string[]} lines
4151
+	 * @param {string} elementIdPrefix
4152
+	 */
4153
+	#parse(lines, elementIdPrefix) {
4141 4154
 		const state = new MDState(lines);
4142 4155
 		state.readersByBlockPriority = this.#readersByBlockPriority;
4143 4156
 		state.readersByTokenPriority = this.#readersByTokenPriority
@@ -4153,4 +4166,36 @@ class Markdown {
4153 4166
 		}
4154 4167
 		return MDNode.toHTML(nodes, state);
4155 4168
 	}
4169
+
4170
+	/**
4171
+	 * Keeps removing first and last lines of markdown to locate the source of
4172
+	 * an exception.
4173
+	 *
4174
+	 * @param {string[]} lines
4175
+	 * @param {string} elementIdPrefix
4176
+	 */
4177
+	#investigateException(lines, elementIdPrefix) {
4178
+		var startIndex = 0;
4179
+		var endIndex = lines.length;
4180
+		// Keep stripping away first line until an exception stops being thrown
4181
+		for (var i = 0; i < lines.length; i++) {
4182
+			try {
4183
+				this.#parse(lines.slice(i, endIndex), elementIdPrefix);
4184
+				break;
4185
+			} catch (e0) {
4186
+				startIndex = i;
4187
+			}
4188
+		}
4189
+		// Keep stripping away last line until an exception stops being thrown
4190
+		for (var i = lines.length; i > startIndex; i--) {
4191
+			try {
4192
+				this.#parse(lines.slice(startIndex, i), elementIdPrefix);
4193
+				break;
4194
+			} catch (e0) {
4195
+				endIndex = i;
4196
+			}
4197
+		}
4198
+		const problematicMarkdown = lines.slice(startIndex, endIndex).join("\n");
4199
+		console.error(`This portion of markdown caused an unexpected exception: ${problematicMarkdown}`);
4200
+	}
4156 4201
 }

+ 1
- 1
js/markdown.min.js
Разлика између датотеке није приказан због своје велике величине
Прегледај датотеку


+ 2
- 2
markdownjs.html Прегледај датотеку

@@ -378,11 +378,11 @@ A regular paragraph.
378 378
 
379 379
 * Unordered
380 380
 * Lists
381
-* Sub list
381
+    * Sub list
382 382
 
383 383
 1. Ordered
384 384
 2. Lists
385
-6. Sub list
385
+    6. Sub list
386 386
 
387 387
 A blockquote:
388 388
 

Loading…
Откажи
Сачувај