diff --git a/bin/add-edb-ids b/bin/add-edb-ids index 0daff36..da6a554 100755 --- a/bin/add-edb-ids +++ b/bin/add-edb-ids @@ -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(); diff --git a/lib/api/lodestone.js b/lib/api/lodestone.js index d1fe5e3..fcd2738 100644 --- a/lib/api/lodestone.js +++ b/lib/api/lodestone.js @@ -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 diff --git a/lib/util/regexp.js b/lib/util/regexp.js new file mode 100644 index 0000000..b7f6d1e --- /dev/null +++ b/lib/util/regexp.js @@ -0,0 +1,6 @@ +/** + * @see https://stackoverflow.com/a/6969486 + * @param {string} + * @returns {string} + */ +export const regExpEscape = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');