1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-02 14:29:55 +00:00

fix: positional attribute in macro call

This commit is contained in:
lin onetwo 2024-10-13 21:31:29 +08:00
parent 53296a484c
commit 72dd18dfa5
4 changed files with 19 additions and 12 deletions

View File

@ -388,28 +388,33 @@ exports.parseAttribute = function(source,pos) {
/* /*
Serialize a parsed attribute node Serialize a parsed attribute node
*/ */
exports.serializeAttribute = function(node) { exports.serializeAttribute = function(node,options) {
options = options || {};
if(!node || typeof node !== "object" || !node.name || !node.type) { if(!node || typeof node !== "object" || !node.name || !node.type) {
return null; return null;
} }
var attributeString = node.name; // If name is number, means it is a positional attribute and name is omitted
var positional = parseInt(node.name) >= 0,
// `=` in a widget and might be `:` in a macro
assign = positional ? "" : (options.assignmentSymbol || "="),
attributeString = positional ? "" : node.name;
if(node.type === "string") { if(node.type === "string") {
if(node.value === "true") { if(node.value === "true") {
return attributeString; return attributeString;
} }
attributeString += '="' + node.value + '"'; attributeString += assign + '"' + node.value + '"';
} else if(node.type === "filtered") { } else if(node.type === "filtered") {
attributeString += "={{{" + node.filter + "}}}"; attributeString += assign + "{{{" + node.filter + "}}}";
} else if(node.type === "indirect") { } else if(node.type === "indirect") {
attributeString += "={{" + node.textReference + "}}"; attributeString += assign + "{{" + node.textReference + "}}";
} else if(node.type === "substituted") { } else if(node.type === "substituted") {
attributeString += "=`" + node.rawValue + "`"; attributeString += assign + "`" + node.rawValue + "`";
} else if(node.type === "macro") { } else if(node.type === "macro") {
if(node.value && typeof node.value === "object" && node.value.type === "macrocall") { if(node.value && typeof node.value === "object" && node.value.type === "macrocall") {
var params = node.value.params.map(function(param) { var params = node.value.params.map(function(param) {
return param.value; return param.value;
}).join(" "); }).join(" ");
attributeString += "=<<" + node.value.name + " " + params + ">>"; attributeString += assign + "<<" + node.value.name + " " + params + ">>";
} else { } else {
// Unsupported macro structure // Unsupported macro structure
return null; return null;

View File

@ -64,9 +64,9 @@ exports.serialize = function (node) {
} }
// Append ordered arguments if any // Append ordered arguments if any
if(node.orderedAttributes) { if(node.orderedAttributes) {
node.orderedAttributes.forEach(function (attr) { node.orderedAttributes.forEach(function (attribute) {
if(attr.name !== "$variable") { if(attribute.name !== "$variable") {
result += " " + '"' + attr.value + '"'; result += " " + $tw.utils.serializeAttribute(attribute,{assignmentSymbol:":"});
} }
}); });
} }

View File

@ -5,3 +5,5 @@ type: text/vnd.tiddlywiki
<<name "value" "value2">> <<name "value" "value2">>
<<.def "macro calls">> <<.def "macro calls">>
<<alert "primary" "primary alert" width:"60%">>

View File

@ -2,4 +2,4 @@ tags: $:/tags/wikitext-serialize-test-spec
title: Serialize/MacroCallInline title: Serialize/MacroCallInline
type: text/vnd.tiddlywiki type: text/vnd.tiddlywiki
These are macro calls in a line: <<name "value" "value2">> and <<.def "macro calls">> These are macro calls in a line: <<name "value" "value2">> and <<.def "macro calls">> <<alert "primary" "primary alert" width:"60%">>