mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2024-10-30 11:46:16 +00:00
2381b6abfe
I don't really understand how it worked
52 lines
1.9 KiB
Go
52 lines
1.9 KiB
Go
package backlinks
|
|
|
|
import (
|
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
|
"github.com/bouncepaw/mycorrhiza/mycoopts"
|
|
"lesarbr.es/mycomarkup/v5"
|
|
"lesarbr.es/mycomarkup/v5/links"
|
|
"lesarbr.es/mycomarkup/v5/mycocontext"
|
|
"lesarbr.es/mycomarkup/v5/tools"
|
|
)
|
|
|
|
// UpdateBacklinksAfterEdit is a creation/editing hook for backlinks index
|
|
func UpdateBacklinksAfterEdit(h hyphae.Hypha, oldText string) {
|
|
oldLinks := extractHyphaLinksFromContent(h.CanonicalName(), oldText)
|
|
newLinks := extractHyphaLinks(h)
|
|
backlinkConveyor <- backlinkIndexEdit{h.CanonicalName(), oldLinks, newLinks}
|
|
}
|
|
|
|
// UpdateBacklinksAfterDelete is a deletion hook for backlinks index
|
|
func UpdateBacklinksAfterDelete(h hyphae.Hypha, oldText string) {
|
|
oldLinks := extractHyphaLinksFromContent(h.CanonicalName(), oldText)
|
|
backlinkConveyor <- backlinkIndexDeletion{h.CanonicalName(), oldLinks}
|
|
}
|
|
|
|
// UpdateBacklinksAfterRename is a renaming hook for backlinks index
|
|
func UpdateBacklinksAfterRename(h hyphae.Hypha, oldName string) {
|
|
actualLinks := extractHyphaLinks(h)
|
|
backlinkConveyor <- backlinkIndexRenaming{oldName, h.CanonicalName(), actualLinks}
|
|
}
|
|
|
|
// extractHyphaLinks extracts hypha links from a desired hypha
|
|
func extractHyphaLinks(h hyphae.Hypha) []string {
|
|
return extractHyphaLinksFromContent(h.CanonicalName(), fetchText(h))
|
|
}
|
|
|
|
// extractHyphaLinksFromContent extracts local hypha links from the provided text.
|
|
func extractHyphaLinksFromContent(hyphaName string, contents string) []string {
|
|
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
|
|
}
|