From 35f7a8ea06cb703ae3209be18cc41688bed5f8fa Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Fri, 14 Jan 2022 16:57:30 +0000 Subject: [PATCH] Fix renaming a tiddler can result in duplicate tags Fixes #6398 --- core/modules/wiki-bulkops.js | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/core/modules/wiki-bulkops.js b/core/modules/wiki-bulkops.js index f64beccf5..d730b3c2e 100644 --- a/core/modules/wiki-bulkops.js +++ b/core/modules/wiki-bulkops.js @@ -46,26 +46,31 @@ function relinkTiddler(fromTitle,toTitle,options) { if(!tiddler.fields["plugin-type"] && type !== "application/javascript") { var tags = tiddler.fields.tags ? tiddler.fields.tags.slice(0) : undefined, list = tiddler.fields.list ? tiddler.fields.list.slice(0) : undefined, - isModified = false; + isModified = false, + processList = function(listField) { + if(listField && listField.indexOf(fromTitle) !== -1) { + // Remove any existing instances of the toTitle + var p = listField.indexOf(toTitle); + while(p !== -1) { + listField.splice(p,1); + p = listField.indexOf(toTitle); + } + // Replace the fromTitle with toTitle + $tw.utils.each(listField,function (title,index) { + if(title === fromTitle) { + listField[index] = toTitle; + isModified = true; + } + }); + } + }; if(!options.dontRenameInTags) { // Rename tags - $tw.utils.each(tags,function (title,index) { - if(title === fromTitle) { -console.log("Renaming tag '" + tags[index] + "' to '" + toTitle + "' of tiddler '" + tiddler.fields.title + "'"); - tags[index] = toTitle; - isModified = true; - } - }); + processList(tags); } if(!options.dontRenameInLists) { // Rename lists - $tw.utils.each(list,function (title,index) { - if(title === fromTitle) { -console.log("Renaming list item '" + list[index] + "' to '" + toTitle + "' of tiddler '" + tiddler.fields.title + "'"); - list[index] = toTitle; - isModified = true; - } - }); + processList(list); } if(isModified) { var newTiddler = new $tw.Tiddler(tiddler,{tags: tags, list: list},self.getModificationFields())