1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-10-04 18:00:44 +00:00

Add setquerystring operator

This commit is contained in:
jeremy@jermolene.com 2023-01-13 11:34:39 +00:00
parent 920b83e59d
commit 3c6f4ac0e8
2 changed files with 4 additions and 16 deletions

View File

@ -13,25 +13,11 @@ Filter operators for URL operations
"use strict"; "use strict";
exports["setquerystring"] = function(source,operator,options) { exports["setquerystring"] = function(source,operator,options) {
var URL = $tw.browser ? window.URL : require("url").URL, var name = operator.operands.length >= 1 ? operator.operands[0] : null,
URLSearchParams = $tw.browser ? window.URLSearchParams : require("url").URLSearchParams,
name = operator.operands.length >= 1 ? operator.operands[0] : null,
value = operator.operands.length >= 2 ? operator.operands[1] : "", value = operator.operands.length >= 2 ? operator.operands[1] : "",
results = []; results = [];
source(function(tiddler,title) { source(function(tiddler,title) {
var url; results.push($tw.utils.setQueryStringParameter(title,name,value));
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());
}
}); });
return results; return results;
}; };

View File

@ -10,6 +10,7 @@ title: Output
<$let lf={{{ [charcode[10],[13]]}}}> <$let lf={{{ [charcode[10],[13]]}}}>
<$text text={{{ <$text text={{{
[[$$$]setquerystring[habitat],[desert]]
[<url>setquerystring[]] [<url>setquerystring[]]
[<url>setquerystring[animal],[elephant]] [<url>setquerystring[animal],[elephant]]
[<url>setquerystring[animal],[elephant]setquerystring[animal],[ostrich]] [<url>setquerystring[animal],[elephant]setquerystring[animal],[ostrich]]
@ -21,6 +22,7 @@ title: Output
+ +
title: ExpectedResult title: ExpectedResult
$$$
https://tiddlywiki.com/ https://tiddlywiki.com/
https://tiddlywiki.com/?animal=elephant https://tiddlywiki.com/?animal=elephant
https://tiddlywiki.com/?animal=ostrich https://tiddlywiki.com/?animal=ostrich