Refactor parameter replacement logic

This commit is contained in:
ewin 2025-07-29 21:38:46 -04:00
parent 7fac86adbb
commit 0db95cad3d
Signed by: erin
SSH key fingerprint: SHA256:swjoHhREbZPbWe+gyJNi24d4NAxJSyUIm3fpZj4z3wc
3 changed files with 20 additions and 39 deletions

View file

@ -3,26 +3,10 @@
import {findItemEDBID} from '../lib/api/lodestone.js';
import {getMediawikiClient} from '../lib/config.js';
import {diff} from '../lib/util/diff.js';
/**
* Matches an empty `id-edb` infobox parameter which can just have a value
* inserted after it.
*/
const existingEmptyEDBIDParameter = /^( *\| *id-edb *=) *$/m;
/**
* Matches an `id-gt` infobox parameter above which we can insert an `id-edb`
* parameter. Whitespace in capture groups so the inserted parameter can use the
* exact same formatting.
*/
const existingGTIDParameter = /^( *)\|( *)id-gt( *)=( *).*$/m;
/**
* Matches a `release` infobox parameter below which we can insert an `id-edb`
* parameter. Whitespace in capture groups so the inserted parameter can use the
* exact same formatting.
*/
const existingReleaseParameter = /^( *)\|( *)release( *)=( *).*$/m;
import {
addParameterBesideExistingParameter,
setEmptyParameter,
} from '../lib/util/template-parameters.js';
/**
* Inserts the `id-edb` item infobox parameter into the given page contents.
@ -35,20 +19,16 @@ function insertInfoboxEDBID (pageContent, edbID) {
console.log('what????', pageContent);
throw new Error('Page seems to already contain its own EDB ID??');
}
if (pageContent.match(existingEmptyEDBIDParameter)) {
console.log('Page has existing empty `id-edb` parameter');
return pageContent.replace(existingEmptyEDBIDParameter, `$1 ${edbID}`);
}
if (pageContent.match(existingGTIDParameter)) {
console.log('Page has `id-gt` parameter, inserting `id-edb` above that');
return pageContent.replace(existingGTIDParameter, `$1|$2id-edb$3=$4${edbID}\n$&`);
}
if (pageContent.match(existingReleaseParameter)) {
console.log('Page has no ID parameters, inserting `id-edb` below `release`');
return pageContent.replace(existingReleaseParameter, `$&\n$1|$2id-edb$3=$4${edbID}`)
}
throw new Error('Dunno how to insert the parameter into this page');
let result = setEmptyParameter(pageContent, 'id-edb', edbID)
?? addParameterBesideExistingParameter(pageContent, 'id-edb', edbID, 'before', 'id-gt')
?? addParameterBesideExistingParameter(pageContent, 'id-edb', edbID, 'after', 'release')
?? addParameterBesideExistingParameter(pageContent, 'id-edb', edbID, 'after', 'patch');
if (result == null) {
throw new Error('Dunno how to insert the parameter into this page');
}
return result;
}
const mw = await getMediawikiClient();