1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-05 23:10:28 +00:00

feat: transcludeblock

This commit is contained in:
lin onetwo 2024-08-10 01:31:43 +08:00
parent 3cf93d08e5
commit 589241a406
2 changed files with 43 additions and 27 deletions

View File

@ -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;
};
})();

View File

@ -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",