2021-12-30 19:59:28 +00:00
|
|
|
package backlinks
|
|
|
|
|
|
|
|
import (
|
2022-08-21 20:52:10 +00:00
|
|
|
"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"
|
2021-12-30 19:59:28 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
2022-06-10 15:45:27 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/mycoopts"
|
2021-12-30 19:59:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// UpdateBacklinksAfterEdit is a creation/editing hook for backlinks index
|
2022-02-19 08:26:38 +00:00
|
|
|
func UpdateBacklinksAfterEdit(h hyphae.Hypha, oldText string) {
|
2022-02-03 22:29:01 +00:00
|
|
|
oldLinks := extractHyphaLinksFromContent(h.CanonicalName(), oldText)
|
2021-12-30 19:59:28 +00:00
|
|
|
newLinks := extractHyphaLinks(h)
|
2022-02-03 22:29:01 +00:00
|
|
|
backlinkConveyor <- backlinkIndexEdit{h.CanonicalName(), oldLinks, newLinks}
|
2021-12-30 19:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateBacklinksAfterDelete is a deletion hook for backlinks index
|
2022-02-19 08:26:38 +00:00
|
|
|
func UpdateBacklinksAfterDelete(h hyphae.Hypha, oldText string) {
|
2022-02-03 22:29:01 +00:00
|
|
|
oldLinks := extractHyphaLinksFromContent(h.CanonicalName(), oldText)
|
|
|
|
backlinkConveyor <- backlinkIndexDeletion{h.CanonicalName(), oldLinks}
|
2021-12-30 19:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateBacklinksAfterRename is a renaming hook for backlinks index
|
2022-02-19 08:26:38 +00:00
|
|
|
func UpdateBacklinksAfterRename(h hyphae.Hypha, oldName string) {
|
2021-12-30 19:59:28 +00:00
|
|
|
actualLinks := extractHyphaLinks(h)
|
2022-02-03 22:29:01 +00:00
|
|
|
backlinkConveyor <- backlinkIndexRenaming{oldName, h.CanonicalName(), actualLinks}
|
2021-12-30 19:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// extractHyphaLinks extracts hypha links from a desired hypha
|
2022-02-19 08:26:38 +00:00
|
|
|
func extractHyphaLinks(h hyphae.Hypha) []string {
|
2022-02-03 22:29:01 +00:00
|
|
|
return extractHyphaLinksFromContent(h.CanonicalName(), fetchText(h))
|
2021-12-30 19:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// extractHyphaLinksFromContent extracts local hypha links from the provided text.
|
|
|
|
func extractHyphaLinksFromContent(hyphaName string, contents string) []string {
|
2022-06-10 15:45:27 +00:00
|
|
|
ctx, _ := mycocontext.ContextFromStringInput(contents, mycoopts.MarkupOptions(hyphaName))
|
2021-12-30 19:59:28 +00:00
|
|
|
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 {
|
2022-06-05 12:34:30 +00:00
|
|
|
switch link := link.(type) {
|
|
|
|
case *links.LocalLink:
|
|
|
|
result = append(result, link.Target(ctx))
|
2021-12-30 19:59:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|