1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-03-10 13:38:20 +00:00

50 lines
1.9 KiB
Go
Raw Permalink Normal View History

package backlinks
import (
"github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/mycoopts"
"git.sr.ht/~bouncepaw/mycomarkup/v5"
"git.sr.ht/~bouncepaw/mycomarkup/v5/links"
"git.sr.ht/~bouncepaw/mycomarkup/v5/mycocontext"
"git.sr.ht/~bouncepaw/mycomarkup/v5/tools"
)
// UpdateBacklinksAfterEdit is a creation/editing hook for backlinks index
2022-02-19 11:26:38 +03:00
func UpdateBacklinksAfterEdit(h hyphae.Hypha, oldText string) {
2022-02-04 03:29:01 +05:00
oldLinks := extractHyphaLinksFromContent(h.CanonicalName(), oldText)
2025-03-07 10:26:46 +00:00
contents := fetchText(h)
newLinks := extractHyphaLinksFromContent(h.CanonicalName(), contents)
backlinkConveyor <- backlinkIndexEdit{h.CanonicalName(), oldLinks, newLinks, contents, oldText}
}
// UpdateBacklinksAfterDelete is a deletion hook for backlinks index
2022-02-19 11:26:38 +03:00
func UpdateBacklinksAfterDelete(h hyphae.Hypha, oldText string) {
2022-02-04 03:29:01 +05:00
oldLinks := extractHyphaLinksFromContent(h.CanonicalName(), oldText)
2025-03-07 10:26:46 +00:00
backlinkConveyor <- backlinkIndexDeletion{h.CanonicalName(), oldLinks, oldText}
}
// UpdateBacklinksAfterRename is a renaming hook for backlinks index
2022-02-19 11:26:38 +03:00
func UpdateBacklinksAfterRename(h hyphae.Hypha, oldName string) {
2025-03-07 10:26:46 +00:00
contents := fetchText(h)
actualLinks := extractHyphaLinksFromContent(h.CanonicalName(), contents)
backlinkConveyor <- backlinkIndexRenaming{oldName, h.CanonicalName(), actualLinks, contents}
}
// extractHyphaLinksFromContent extracts local hypha links from the provided text.
func extractHyphaLinksFromContent(hyphaName string, contents string) []string {
2022-06-10 18:45:27 +03:00
ctx, _ := mycocontext.ContextFromStringInput(contents, mycoopts.MarkupOptions(hyphaName))
linkVisitor, getLinks := tools.LinkVisitor(ctx)
// Ignore the result of BlockTree because we call it for linkVisitor.
_ = mycomarkup.BlockTree(ctx, linkVisitor)
foundLinks := getLinks()
var result []string
for _, link := range foundLinks {
switch link := link.(type) {
case *links.LocalLink:
result = append(result, link.Target(ctx))
}
}
return result
}