From 1a6a071a29336dadfe0dfde0dba0cc2373568d1a Mon Sep 17 00:00:00 2001
From: lin onetwo <linonetwo012@gmail.com>
Date: Tue, 30 Jul 2024 00:43:55 +0800
Subject: [PATCH] feat: add rule: 'parseBlock' metadata

---
 core/modules/parsers/wikiparser/wikiparser.js |  2 +-
 core/modules/utils/parsetree.js               | 12 ++++++------
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/core/modules/parsers/wikiparser/wikiparser.js b/core/modules/parsers/wikiparser/wikiparser.js
index 09866b064..10acf91ec 100644
--- a/core/modules/parsers/wikiparser/wikiparser.js
+++ b/core/modules/parsers/wikiparser/wikiparser.js
@@ -259,7 +259,7 @@ WikiParser.prototype.parseBlock = function(terminatorRegExpString) {
 	var start = this.pos;
 	var children = this.parseInlineRun(terminatorRegExp);
 	var end = this.pos;
-	return [{type: "element", tag: "p", children: children, start: start, end: end }];
+	return [{type: "element", tag: "p", children: children, start: start, end: end, rule: 'parseBlock' }];
 };
 
 /*
diff --git a/core/modules/utils/parsetree.js b/core/modules/utils/parsetree.js
index ae548de0e..f5b2c12d0 100644
--- a/core/modules/utils/parsetree.js
+++ b/core/modules/utils/parsetree.js
@@ -124,9 +124,10 @@ exports.getParseTreeText = function getParseTreeText(tree) {
 };
 
 /*
-Utility to get the (similarly but not 1:1 equal) original wikitext of a parse tree node or array of nodes
+Utility to get the (similarly but not 1:1 equal) original wikitext of a parse tree node or array of nodes.
+Based on `node.rule` metadata added in `wikiparser.js`.
 */
-exports.serializeParseTree = function serializeParseTree(tree, tiddlerType) {
+exports.serializeParseTree = function serializeParseTree(tree,tiddlerType) {
 	var output = [];
 	if($tw.utils.isArray(tree)) {
 		$tw.utils.each(tree,function(node) {
@@ -141,12 +142,11 @@ exports.serializeParseTree = function serializeParseTree(tree, tiddlerType) {
 				Parser.prototype.inlineRuleClasses[tree.rule] ||
 				Parser.prototype.pragmaRuleClasses[tree.rule];
 			if(Rule && Rule.prototype.serialize) {
-				output.push(Rule.prototype.serialize(tree));
+				output.push(Rule.prototype.serialize(tree,serializeParseTree));
+			} else if(tree.rule === "parseBlock") {
+				output.push(serializeParseTree(tree.children,tiddlerType),"\n\n");
 			}
 		}
-		if(tree.children) {
-			return serializeParseTree(tree.children);
-		}
 	}
 	return output.join("");
 };