Add regex replace feature for file renaming (#3562)

This commit is contained in:
Grace Dinh 2024-01-12 10:24:40 -08:00 committed by Dan Stillman
parent 4187819cd1
commit 29f4aece24
2 changed files with 8 additions and 1 deletions

View file

@ -2253,7 +2253,7 @@ Zotero.Attachments = new function () {
};
const common = (value, { truncate = false, prefix = '', suffix = '', case: textCase = '' } = {}) => {
const common = (value, { truncate = false, prefix = '', suffix = '', replaceFrom = '', replaceTo = '', regexOpts = '', case: textCase = '' } = {}) => {
if (value === '' || value === null || typeof value === 'undefined') {
return '';
}
@ -2283,6 +2283,9 @@ Zotero.Attachments = new function () {
let affixed = false;
if (replaceFrom) {
value = value.replace(new RegExp(replaceFrom, regexOpts), replaceTo);
}
if (prefix && !value.startsWith(prefix)) {
value = prefix + value;
affixed = true;

View file

@ -1430,6 +1430,10 @@ describe("Zotero.Attachments", function() {
Zotero.Attachments.getFileBaseNameFromItem(item, '{{firstCreator suffix=" - "}}{{year suffix=" - "}}{{title truncate="50" }}'),
'Barius and Pixelus - 1975 - Lorem Ipsum'
);
assert.equal(
Zotero.Attachments.getFileBaseNameFromItem(item, '{{firstCreator suffix=" - " replaceFrom=" *and *" replaceTo="&"}}{{year suffix=" - " replaceFrom="(\\d{2})(\\d{2})" replaceTo="$2"}}{{title truncate="50" replaceFrom=".m" replaceTo="a"}} - {{title truncate="50" replaceFrom=".m" replaceTo="a" regexOpts="g"}}'),
'Barius&Pixelus - 75 - Lora Ipsum - Lora Ipsa'
);
assert.equal(
Zotero.Attachments.getFileBaseNameFromItem(item, '{{year suffix="-"}}{{firstCreator truncate="10" suffix="-"}}{{title truncate="5" }}'),
'1975-Barius and-Lorem'