From 7a2889dd6c0c87e2d3af697ecbcb5f60d8925a4c Mon Sep 17 00:00:00 2001 From: Ivan Solyankin Date: Tue, 18 Sep 2018 14:49:32 +0300 Subject: [PATCH] added multiple and numbered %s support for search engines closes #987 --- src/url_util.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/url_util.ts b/src/url_util.ts index eeb6b3cd..68631ecb 100644 --- a/src/url_util.ts +++ b/src/url_util.ts @@ -354,6 +354,8 @@ export function graftUrlPath(url: URL, newTail: string, level: number) { export function interpolateSearchItem(urlPattern: URL, query: string): URL { const hasInterpolationPoint = urlPattern.href.includes("%s") + let queryWords = query.split(" ") + // percent-encode if theres a %s in the query string, or if we're apppending // and there's a query string if ( @@ -361,11 +363,21 @@ export function interpolateSearchItem(urlPattern: URL, query: string): URL { urlPattern.search !== "" ) { query = encodeURIComponent(query) + queryWords = queryWords.map(w => encodeURIComponent(w)) } // replace or append as needed if (hasInterpolationPoint) { - return new URL(urlPattern.href.replace("%s", query)) + let resultingURL = new URL (urlPattern.href.replace(/%s\d+/g, function(x) { + const index = parseInt(x.slice(2)) - 1 + if (index >= queryWords.length) { + return "" + } + + return queryWords[index] + })) + + return new URL(resultingURL.href.replace("%s", query)) } else { return new URL(urlPattern.href + query) }