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 {findItemEDBID} from '../lib/api/lodestone.js';
import {getMediawikiClient} from '../lib/config.js'; import {getMediawikiClient} from '../lib/config.js';
import {diff} from '../lib/util/diff.js'; import {diff} from '../lib/util/diff.js';
import {
/** addParameterBesideExistingParameter,
* Matches an empty `id-edb` infobox parameter which can just have a value setEmptyParameter,
* inserted after it. } from '../lib/util/template-parameters.js';
*/
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;
/** /**
* Inserts the `id-edb` item infobox parameter into the given page contents. * Inserts the `id-edb` item infobox parameter into the given page contents.
@ -35,20 +19,16 @@ function insertInfoboxEDBID (pageContent, edbID) {
console.log('what????', pageContent); console.log('what????', pageContent);
throw new Error('Page seems to already contain its own EDB ID??'); 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}`)
}
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'); throw new Error('Dunno how to insert the parameter into this page');
}
return result;
} }
const mw = await getMediawikiClient(); const mw = await getMediawikiClient();

View file

@ -1,11 +1,6 @@
// Utilities for scraping data from the Lodestone // Utilities for scraping data from the Lodestone
/** import {regExpEscape} from '../util/regexp.js';
* @see https://stackoverflow.com/a/6969486
* @param {string}
* @returns {string}
*/
const regExpEscape = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
/** /**
* Creates a regular expression that matches a link to the named item and * Creates a regular expression that matches a link to the named item and

6
lib/util/regexp.js Normal file
View file

@ -0,0 +1,6 @@
/**
* @see https://stackoverflow.com/a/6969486
* @param {string}
* @returns {string}
*/
export const regExpEscape = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');