1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-30 05:19:57 +00:00

feat: use void node to carry important info for typedblock

This commit is contained in:
lin onetwo 2024-08-10 02:33:50 +08:00
parent 0adf638c1a
commit c4f6136fa7
2 changed files with 39 additions and 22 deletions

View File

@ -63,36 +63,46 @@ exports.parse = function() {
var parser = this.parser.wiki.parseText(parseType,text,{defaultType: "text/plain"});
// If there's no render type, just return the parse tree
if(!renderType) {
return parser.tree;
return [{
type: "void",
children: $tw.utils.isArray(parser.tree) ? parser.tree : [parser.tree],
parseType: parseType,
renderType: renderType,
text: text,
start: start,
end: this.parser.pos
}];
} else {
// Otherwise, render to the rendertype and return in a <PRE> tag
var widgetNode = this.parser.wiki.makeWidget(parser),
container = $tw.fakeDocument.createElement("div");
widgetNode.render(container,null);
text = renderType === "text/html" ? container.innerHTML : container.textContent;
var renderResult = renderType === "text/html" ? container.innerHTML : container.textContent;
return [{
type: "element",
tag: "pre",
type: "void",
children: [{
type: "text",
text: text,
start: start,
end: this.parser.pos
}]
type: "element",
tag: "pre",
children: [{
type: "text",
text: renderResult,
}]
}],
parseType: parseType,
renderType: renderType,
text: text,
start: start,
end: this.parser.pos
}];
}
};
exports.serialize = function (tree, serialize) {
var serialized = '$$$'; // Extract the type from the tree node (assuming it's stored in a specific attribute)
if(tree.attributes && tree.attributes.type) {
serialized += tree.attributes.type.value;
if(tree.type === "void") {
// Directly process the tree's text content
return "$$$" + tree.parseType + (tree.renderType ? " > " + tree.renderType : "") + "\n" + tree.text + "\n$$$\n\n";
}
serialized += '\n'; // Serialize the children of the block
serialized += serialize(tree.children); // Close the serialized string
serialized += "\n$$$";
// Return the complete serialized string
return serialized;
return "";
};
})();

View File

@ -372,16 +372,23 @@ describe("WikiAST serialization unit tests", function () {
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("TranscludeInlineTest").tree).trimEnd();
expect(serialized).toBe(wiki.getTiddlerText("TranscludeInlineTest").trimEnd());
});
return;
wiki.addTiddler({
title: "TypedBlockTest",
text: '$$$.js\nThis will be rendered as JavaScript\n$$$\n$$$.svg\n<svg xmlns="http://www.w3.org/2000/svg" width="150" height="100">\n <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red" />\n</svg>\n$$$\n$$$text/vnd.tiddlywiki>text/html\nThis will be rendered as an //HTML representation// of WikiText\n$$$',
title: "TypedBlockTest1",
text: "$$$text/vnd.tiddlywiki > text/plain\nThis is ''some'' wikitext\n$$$\n\n$$$text/unknown\nSome plain text, which will not be //formatted//.\n\n$$$text/vnd.tiddlywiki > text/html\nThis is ''some'' wikitext\n$$$\n\n",
});
wiki.addTiddler({
title: "TypedBlockTest2",
text: '$$$.js\nThis will be rendered as JavaScript\n$$$\n\n$$$.svg\n<svg xmlns="http://www.w3.org/2000/svg" width="150" height="100">\n <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red" />\n</svg>\n$$$\n\n$$$image/svg+xml\n<svg xmlns="http://www.w3.org/2000/svg" width="150" height="100">\n <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="green" />\n</svg>\n$$$',
});
it("should serialize typed blocks correctly", function () {
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("TypedBlockTest").tree).trimEnd();
expect(serialized).toBe(wiki.getTiddlerText("TypedBlockTest").trimEnd());
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("TypedBlockTest1").tree).trimEnd();
expect(serialized).toBe(wiki.getTiddlerText("TypedBlockTest1").trimEnd());
serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("TypedBlockTest2").tree).trimEnd();
expect(serialized).toBe(wiki.getTiddlerText("TypedBlockTest2").trimEnd());
});
return;
wiki.addTiddler({
title: "WikiLinkTest",