1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-09-29 07:20:47 +00:00

Add setquerystring operator

This commit is contained in:
jeremy@jermolene.com 2023-01-13 11:34:39 +00:00
parent 261f0f5261
commit 9288420c67
2 changed files with 4 additions and 16 deletions

View File

@ -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;
};

View File

@ -10,6 +10,7 @@ title: Output
<$let lf={{{ [charcode[10],[13]]}}}>
<$text text={{{
[[$$$]setquerystring[habitat],[desert]]
[<url>setquerystring[]]
[<url>setquerystring[animal],[elephant]]
[<url>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