diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index 36b529193c..6c89f1e54a 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -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; diff --git a/test/tests/attachmentsTest.js b/test/tests/attachmentsTest.js index 7a0be85f25..365cbbfcb2 100644 --- a/test/tests/attachmentsTest.js +++ b/test/tests/attachmentsTest.js @@ -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'