Add regex replace feature for file renaming (#3562)
This commit is contained in:
parent
4187819cd1
commit
29f4aece24
2 changed files with 8 additions and 1 deletions
|
@ -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;
|
||||
|
|
|
@ -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'
|
||||
|
|
Loading…
Reference in a new issue