From 3c6f4ac0e8d050afc9afd2733ffba456e04554c9 Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Fri, 13 Jan 2023 11:34:39 +0000 Subject: [PATCH] Add setquerystring operator --- core/modules/filters/url-ops.js | 18 ++---------------- .../tests/data/filters/querystrings.tid | 2 ++ 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/core/modules/filters/url-ops.js b/core/modules/filters/url-ops.js index 392e510be..ce8817539 100644 --- a/core/modules/filters/url-ops.js +++ b/core/modules/filters/url-ops.js @@ -13,25 +13,11 @@ Filter operators for URL operations "use strict"; exports["setquerystring"] = function(source,operator,options) { - var URL = $tw.browser ? window.URL : require("url").URL, - URLSearchParams = $tw.browser ? window.URLSearchParams : require("url").URLSearchParams, - name = operator.operands.length >= 1 ? operator.operands[0] : null, + var name = operator.operands.length >= 1 ? operator.operands[0] : null, value = operator.operands.length >= 2 ? operator.operands[1] : "", results = []; source(function(tiddler,title) { - var url; - try { - url = new URL(title); - } catch(e) { - } - if(url) { - var params = new URLSearchParams(url.search); - if(name) { - params.set(name,value); - } - url.search = params.toString(); - results.push(url.toString()); - } + results.push($tw.utils.setQueryStringParameter(title,name,value)); }); return results; }; diff --git a/editions/test/tiddlers/tests/data/filters/querystrings.tid b/editions/test/tiddlers/tests/data/filters/querystrings.tid index fb3dd7687..bdd3c72d9 100644 --- a/editions/test/tiddlers/tests/data/filters/querystrings.tid +++ b/editions/test/tiddlers/tests/data/filters/querystrings.tid @@ -10,6 +10,7 @@ title: Output <$let lf={{{ [charcode[10],[13]]}}}> <$text text={{{ + [[$$$]setquerystring[habitat],[desert]] [setquerystring[]] [setquerystring[animal],[elephant]] [setquerystring[animal],[elephant]setquerystring[animal],[ostrich]] @@ -21,6 +22,7 @@ title: Output + title: ExpectedResult +$$$ https://tiddlywiki.com/ https://tiddlywiki.com/?animal=elephant https://tiddlywiki.com/?animal=ostrich