mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-01-22 06:56:52 +00:00
feat: transcludeblock
This commit is contained in:
parent
3cf93d08e5
commit
589241a406
@ -87,37 +87,53 @@ exports.parse = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.serialize = function(tree, serialize) {
|
exports.serialize = function(tree, serialize) {
|
||||||
var serialized = "{{";
|
var result = "{{";
|
||||||
// Check for tiddler attribute
|
function handleTransclude(transcludeNode) {
|
||||||
if(tree.attributes.$tiddler) {
|
// Handle field
|
||||||
serialized += tree.attributes.$tiddler.value;
|
if(transcludeNode.attributes.$field) {
|
||||||
// Check for field attribute
|
result += "!!" + transcludeNode.attributes.$field.value;
|
||||||
if(tree.attributes.$field) {
|
|
||||||
serialized += "##" + tree.attributes.$field.value;
|
|
||||||
}
|
}
|
||||||
// Check for index attribute
|
// Check for index attribute
|
||||||
if(tree.attributes.$index) {
|
if(transcludeNode.attributes.$index) {
|
||||||
serialized += "!!" + tree.attributes.$index.value;
|
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
|
function handleTiddler(tiddlerNode) {
|
||||||
if(tree.attributes.$template) {
|
// Check for tiddler attribute
|
||||||
serialized += "||" + tree.attributes.$template.value;
|
if(tree.attributes.tiddler.value) {
|
||||||
}
|
result += tree.attributes.tiddler.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);
|
|
||||||
}
|
}
|
||||||
|
$tw.utils.each(tree.children, function(child) {
|
||||||
|
if(child.type === "transclude") {
|
||||||
|
handleTransclude(child);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if(params.length > 0) {
|
if(tree.type === "tiddler") {
|
||||||
serialized += "|" + params.join("|");
|
handleTiddler(tree);
|
||||||
|
} else if(tree.type === "transclude") {
|
||||||
|
handleTransclude(tree);
|
||||||
}
|
}
|
||||||
// Close the serialized string
|
// Close the result string
|
||||||
serialized += "}}";
|
result += "}}\n\n";
|
||||||
// Return the complete serialized string
|
// Return the complete result string
|
||||||
return serialized;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
@ -354,11 +354,10 @@ describe("WikiAST serialization unit tests", function () {
|
|||||||
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("TableTest").tree).trimEnd();
|
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("TableTest").tree).trimEnd();
|
||||||
expect(serialized).toBe(wiki.getTiddlerText("TableTest").trimEnd());
|
expect(serialized).toBe(wiki.getTiddlerText("TableTest").trimEnd());
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
|
|
||||||
wiki.addTiddler({
|
wiki.addTiddler({
|
||||||
title: "TranscludeBlockTest",
|
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 () {
|
it("should serialize block-level transclusions correctly", function () {
|
||||||
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("TranscludeBlockTest").tree).trimEnd();
|
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("TranscludeBlockTest").tree).trimEnd();
|
||||||
@ -367,12 +366,13 @@ describe("WikiAST serialization unit tests", function () {
|
|||||||
|
|
||||||
wiki.addTiddler({
|
wiki.addTiddler({
|
||||||
title: "TranscludeInlineTest",
|
title: "TranscludeInlineTest",
|
||||||
text: "{{MyTiddler}}\n{{MyTiddler||TemplateTitle}}",
|
text: "{{MyTiddler}} {{MyTiddler||TemplateTitle}}",
|
||||||
});
|
});
|
||||||
it("should serialize inline-level transclusions correctly", function () {
|
it("should serialize inline-level transclusions correctly", function () {
|
||||||
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("TranscludeInlineTest").tree).trimEnd();
|
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("TranscludeInlineTest").tree).trimEnd();
|
||||||
expect(serialized).toBe(wiki.getTiddlerText("TranscludeInlineTest").trimEnd());
|
expect(serialized).toBe(wiki.getTiddlerText("TranscludeInlineTest").trimEnd());
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
|
|
||||||
wiki.addTiddler({
|
wiki.addTiddler({
|
||||||
title: "TypedBlockTest",
|
title: "TypedBlockTest",
|
||||||
|
Loading…
Reference in New Issue
Block a user