From 72dd18dfa539d1773231377d7dc7ca703a6e32ec Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Sun, 13 Oct 2024 21:31:29 +0800 Subject: [PATCH] fix: positional attribute in macro call --- core/modules/parsers/parseutils.js | 19 ++++++++++++------- .../wikiparser/rules/macrocallblock.js | 6 +++--- .../tests/data/serialize/MacroCallBlock.tid | 4 +++- .../tests/data/serialize/MacroCallInline.tid | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/core/modules/parsers/parseutils.js b/core/modules/parsers/parseutils.js index e2e95c684..8d143c5e9 100644 --- a/core/modules/parsers/parseutils.js +++ b/core/modules/parsers/parseutils.js @@ -388,28 +388,33 @@ exports.parseAttribute = function(source,pos) { /* 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) { 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.value === "true") { return attributeString; } - attributeString += '="' + node.value + '"'; + attributeString += assign + '"' + node.value + '"'; } else if(node.type === "filtered") { - attributeString += "={{{" + node.filter + "}}}"; + attributeString += assign + "{{{" + node.filter + "}}}"; } else if(node.type === "indirect") { - attributeString += "={{" + node.textReference + "}}"; + attributeString += assign + "{{" + node.textReference + "}}"; } else if(node.type === "substituted") { - attributeString += "=`" + node.rawValue + "`"; + attributeString += assign + "`" + node.rawValue + "`"; } else if(node.type === "macro") { if(node.value && typeof node.value === "object" && node.value.type === "macrocall") { var params = node.value.params.map(function(param) { return param.value; }).join(" "); - attributeString += "=<<" + node.value.name + " " + params + ">>"; + attributeString += assign + "<<" + node.value.name + " " + params + ">>"; } else { // Unsupported macro structure return null; diff --git a/core/modules/parsers/wikiparser/rules/macrocallblock.js b/core/modules/parsers/wikiparser/rules/macrocallblock.js index 1572cce63..a684a2d28 100644 --- a/core/modules/parsers/wikiparser/rules/macrocallblock.js +++ b/core/modules/parsers/wikiparser/rules/macrocallblock.js @@ -64,9 +64,9 @@ exports.serialize = function (node) { } // Append ordered arguments if any if(node.orderedAttributes) { - node.orderedAttributes.forEach(function (attr) { - if(attr.name !== "$variable") { - result += " " + '"' + attr.value + '"'; + node.orderedAttributes.forEach(function (attribute) { + if(attribute.name !== "$variable") { + result += " " + $tw.utils.serializeAttribute(attribute,{assignmentSymbol:":"}); } }); } diff --git a/editions/test/tiddlers/tests/data/serialize/MacroCallBlock.tid b/editions/test/tiddlers/tests/data/serialize/MacroCallBlock.tid index 723e23edd..4bd30d451 100644 --- a/editions/test/tiddlers/tests/data/serialize/MacroCallBlock.tid +++ b/editions/test/tiddlers/tests/data/serialize/MacroCallBlock.tid @@ -4,4 +4,6 @@ type: text/vnd.tiddlywiki <> -<<.def "macro calls">> \ No newline at end of file +<<.def "macro calls">> + +<> diff --git a/editions/test/tiddlers/tests/data/serialize/MacroCallInline.tid b/editions/test/tiddlers/tests/data/serialize/MacroCallInline.tid index 0b6b4fc50..09127dce0 100644 --- a/editions/test/tiddlers/tests/data/serialize/MacroCallInline.tid +++ b/editions/test/tiddlers/tests/data/serialize/MacroCallInline.tid @@ -2,4 +2,4 @@ tags: $:/tags/wikitext-serialize-test-spec title: Serialize/MacroCallInline type: text/vnd.tiddlywiki -These are macro calls in a line: <> and <<.def "macro calls">> \ No newline at end of file +These are macro calls in a line: <> and <<.def "macro calls">> <>