From 589241a406b7cfae935e6b61e4fb17d7bf8219ba Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Sat, 10 Aug 2024 01:31:43 +0800 Subject: [PATCH] feat: transcludeblock --- .../wikiparser/rules/transcludeblock.js | 64 ++++++++++++------- .../tiddlers/tests/test-wikitext-serialize.js | 6 +- 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/core/modules/parsers/wikiparser/rules/transcludeblock.js b/core/modules/parsers/wikiparser/rules/transcludeblock.js index 9add8f6ad..88519e84d 100644 --- a/core/modules/parsers/wikiparser/rules/transcludeblock.js +++ b/core/modules/parsers/wikiparser/rules/transcludeblock.js @@ -87,37 +87,53 @@ exports.parse = function() { }; exports.serialize = function(tree, serialize) { - var serialized = "{{"; - // Check for tiddler attribute - if(tree.attributes.$tiddler) { - serialized += tree.attributes.$tiddler.value; - // Check for field attribute - if(tree.attributes.$field) { - serialized += "##" + tree.attributes.$field.value; + var result = "{{"; + function handleTransclude(transcludeNode) { + // Handle field + if(transcludeNode.attributes.$field) { + result += "!!" + transcludeNode.attributes.$field.value; } // Check for index attribute - if(tree.attributes.$index) { - serialized += "!!" + tree.attributes.$index.value; + if(transcludeNode.attributes.$index) { + result += "##" + transcludeNode.attributes.$index.value; + } + // Handle template + var tiddlerTitle = tree.attributes.tiddler ? tree.attributes.tiddler.value : undefined; + if(transcludeNode.attributes.$tiddler && transcludeNode.attributes.$tiddler.value !== tiddlerTitle) { + result += "||" + transcludeNode.attributes.$tiddler.value; + } + // Check for parameters + var params = []; + var excludedAttributes = ["tiddler", "$tiddler", "$field", "$index", "$template"]; + for(var key in transcludeNode.attributes) { + if(excludedAttributes.indexOf(key) === -1) { + params.push(transcludeNode.attributes[key].value); + } + } + if(params.length > 0) { + result += "|" + params.join("|"); } } - // Check for template attribute - if(tree.attributes.$template) { - serialized += "||" + tree.attributes.$template.value; - } - // Check for parameters - var params = []; - for(var key in tree.attributes) { - if(key !== "$tiddler" && key !== "$field" && key !== "$index" && key !== "$template") { - params.push(tree.attributes[key].value); + function handleTiddler(tiddlerNode) { + // Check for tiddler attribute + if(tree.attributes.tiddler.value) { + result += tree.attributes.tiddler.value; } + $tw.utils.each(tree.children, function(child) { + if(child.type === "transclude") { + handleTransclude(child); + } + }); } - if(params.length > 0) { - serialized += "|" + params.join("|"); + if(tree.type === "tiddler") { + handleTiddler(tree); + } else if(tree.type === "transclude") { + handleTransclude(tree); } - // Close the serialized string - serialized += "}}"; - // Return the complete serialized string - return serialized; + // Close the result string + result += "}}\n\n"; + // Return the complete result string + return result; }; })(); diff --git a/editions/test/tiddlers/tests/test-wikitext-serialize.js b/editions/test/tiddlers/tests/test-wikitext-serialize.js index bfc4523ad..b2a35e8bc 100644 --- a/editions/test/tiddlers/tests/test-wikitext-serialize.js +++ b/editions/test/tiddlers/tests/test-wikitext-serialize.js @@ -354,11 +354,10 @@ describe("WikiAST serialization unit tests", function () { var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("TableTest").tree).trimEnd(); expect(serialized).toBe(wiki.getTiddlerText("TableTest").trimEnd()); }); - return; wiki.addTiddler({ title: "TranscludeBlockTest", - text: "{{MyTiddler}}\n{{MyTiddler||TemplateTitle}}", + text: "{{MyTiddler}}\n\n{{MyTiddler||TemplateTitle}}\n\n{{||TemplateTitle}}\n\n{{MyTiddler|Parameter}}\n\n{{MyTiddler||TemplateTitle|Parameter|SecondParameter}}\n\n{{MyTiddler!!field}}\n\n{{!!field}}\n\n{{MyTiddler##index}}\n\n{{##index}}", }); it("should serialize block-level transclusions correctly", function () { var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("TranscludeBlockTest").tree).trimEnd(); @@ -367,12 +366,13 @@ describe("WikiAST serialization unit tests", function () { wiki.addTiddler({ title: "TranscludeInlineTest", - text: "{{MyTiddler}}\n{{MyTiddler||TemplateTitle}}", + text: "{{MyTiddler}} {{MyTiddler||TemplateTitle}}", }); it("should serialize inline-level transclusions correctly", function () { var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("TranscludeInlineTest").tree).trimEnd(); expect(serialized).toBe(wiki.getTiddlerText("TranscludeInlineTest").trimEnd()); }); + return; wiki.addTiddler({ title: "TypedBlockTest",