From 78ad0761b322929de18c1cf6ce445bf80d533427 Mon Sep 17 00:00:00 2001 From: ewin Date: Tue, 19 Aug 2025 23:23:41 -0600 Subject: [PATCH] uhh apparently i just never committed these --- lib/util/template-parameters.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/util/template-parameters.js b/lib/util/template-parameters.js index 21cdb22..788ddc4 100644 --- a/lib/util/template-parameters.js +++ b/lib/util/template-parameters.js @@ -27,7 +27,37 @@ export function setEmptyParameter (pageContent, paramName, value) { * name. */ const existingParameterRegExp = paramName => - new RegExp(`^( *)\\|( *)${regExpEscape(paramName)}( *)=( *).*$`, 'm'); + new RegExp(`^( *)\\|( *)${regExpEscape(paramName)}( *)=( *)(.*)$`, 'm'); + +/** + * Gets the value of an existing parameter. + * @param {string} pageContent + * @param {string} paramName + * @returns {string | null} The value, or `null` if the parameter doesn't exist + */ +export function readExistingParameter (pageContent, paramName) { + let regexp = existingParameterRegExp(paramName); + let match; + if ((match = pageContent.match(regexp))) { + return match[5]; + } + return null; +} + +/** + * Sets the value of an existing parameter. + * @param {string} pageContent + * @param {string} paramName + * @param {string} value + * @returns {string | null} + */ +export function setExistingParameter (pageContent, paramName, value) { + const regexp = existingParameterRegExp(paramName); + if (pageContent.match(regexp)) { + return pageContent.replace(regexp, `$1|$2${paramName}$3=$4${value}`) + } + return null; +} /** * Inserts a new parameter before or after an existing parameter.