mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2024-12-13 05:50:27 +00:00
Categories: Move them when a hypha is renamed
This commit is contained in:
parent
d44c4484de
commit
5b829f1d82
@ -94,3 +94,19 @@ func removeHyphaFromCategory(hyphaName, catName string) {
|
||||
mutex.Unlock()
|
||||
go saveToDisk()
|
||||
}
|
||||
|
||||
// RenameHyphaInAllCategories finds all mentions of oldName and replaces them with newName. Pass canonical names. Make sure newName is not taken. If oldName is not in any category, RenameHyphaInAllCategories is a no-op.
|
||||
func RenameHyphaInAllCategories(oldName, newName string) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
if node, ok := hyphaToCategories[oldName]; ok {
|
||||
hyphaToCategories[newName] = node
|
||||
delete(hyphaToCategories, oldName) // node should still be in memory 🙏
|
||||
for _, catName := range node.categoryList {
|
||||
if catNode, ok := categoryToHyphae[catName]; ok {
|
||||
catNode.removeHypha(oldName)
|
||||
catNode.storeHypha(newName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/bouncepaw/mycorrhiza/backlinks"
|
||||
"github.com/bouncepaw/mycorrhiza/categories"
|
||||
"regexp"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/history"
|
||||
@ -36,6 +37,7 @@ func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, u *us
|
||||
var (
|
||||
re = regexp.MustCompile(`(?i)` + oldHypha.CanonicalName())
|
||||
replaceName = func(str string) string {
|
||||
// Can we drop that util.CanonicalName?
|
||||
return re.ReplaceAllString(util.CanonicalName(str), newName)
|
||||
}
|
||||
hyphaeToRename = findHyphaeToRename(oldHypha, recursive)
|
||||
@ -67,9 +69,13 @@ func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, u *us
|
||||
}
|
||||
|
||||
for _, h := range hyphaeToRename {
|
||||
oldName := h.CanonicalName()
|
||||
hyphae.RenameHyphaTo(h, replaceName(h.CanonicalName()), replaceName)
|
||||
var (
|
||||
oldName = h.CanonicalName()
|
||||
newName = replaceName(oldName)
|
||||
)
|
||||
hyphae.RenameHyphaTo(h, newName, replaceName)
|
||||
backlinks.UpdateBacklinksAfterRename(h, oldName)
|
||||
categories.RenameHyphaInAllCategories(oldName, newName)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
Loading…
Reference in New Issue
Block a user