Refactor parameter replacement logic
This commit is contained in:
parent
7fac86adbb
commit
0db95cad3d
3 changed files with 20 additions and 39 deletions
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -1,11 +1,6 @@
|
|||
// Utilities for scraping data from the Lodestone
|
||||
|
||||
/**
|
||||
* @see https://stackoverflow.com/a/6969486
|
||||
* @param {string}
|
||||
* @returns {string}
|
||||
*/
|
||||
const regExpEscape = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
import {regExpEscape} from '../util/regexp.js';
|
||||
|
||||
/**
|
||||
* Creates a regular expression that matches a link to the named item and
|
||||
|
|
|
|||
6
lib/util/regexp.js
Normal file
6
lib/util/regexp.js
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
/**
|
||||
* @see https://stackoverflow.com/a/6969486
|
||||
* @param {string}
|
||||
* @returns {string}
|
||||
*/
|
||||
export const regExpEscape = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
Loading…
Add table
Add a link
Reference in a new issue