Migrate backlinks

This commit is contained in:
Timur Ismagilov 2024-09-07 22:32:41 +03:00
parent 10bcfe65b2
commit 98469b3c49
2 changed files with 13 additions and 12 deletions

View File

@ -2,8 +2,8 @@
package backlinks package backlinks
import ( import (
hyphae2 "github.com/bouncepaw/mycorrhiza/internal/hyphae" "github.com/bouncepaw/mycorrhiza/internal/hyphae"
"log" "log/slog"
"os" "os"
"sort" "sort"
@ -14,7 +14,7 @@ import (
func yieldHyphaBacklinks(hyphaName string) <-chan string { func yieldHyphaBacklinks(hyphaName string) <-chan string {
hyphaName = util.CanonicalName(hyphaName) hyphaName = util.CanonicalName(hyphaName)
out := make(chan string) out := make(chan string)
sorted := hyphae2.PathographicSort(out) sorted := hyphae.PathographicSort(out)
go func() { go func() {
backlinks, exists := backlinkIndex[hyphaName] backlinks, exists := backlinkIndex[hyphaName]
if exists { if exists {
@ -43,7 +43,7 @@ var backlinkIndex = make(map[string]linkSet)
// IndexBacklinks traverses all text hyphae, extracts links from them and forms an initial index. Call it when indexing and reindexing hyphae. // IndexBacklinks traverses all text hyphae, extracts links from them and forms an initial index. Call it when indexing and reindexing hyphae.
func IndexBacklinks() { func IndexBacklinks() {
// It is safe to ignore the mutex, because there is only one worker. // It is safe to ignore the mutex, because there is only one worker.
for h := range hyphae2.FilterHyphaeWithText(hyphae2.YieldExistingHyphae()) { for h := range hyphae.FilterHyphaeWithText(hyphae.YieldExistingHyphae()) {
foundLinks := extractHyphaLinksFromContent(h.CanonicalName(), fetchText(h)) foundLinks := extractHyphaLinksFromContent(h.CanonicalName(), fetchText(h))
for _, link := range foundLinks { for _, link := range foundLinks {
if _, exists := backlinkIndex[link]; !exists { if _, exists := backlinkIndex[link]; !exists {
@ -72,7 +72,7 @@ func BacklinksFor(hyphaName string) []string {
func Orphans() []string { func Orphans() []string {
var orphans []string var orphans []string
for h := range hyphae2.YieldExistingHyphae() { for h := range hyphae.YieldExistingHyphae() {
if BacklinksCount(h.CanonicalName()) == 0 { if BacklinksCount(h.CanonicalName()) == 0 {
orphans = append(orphans, h.CanonicalName()) orphans = append(orphans, h.CanonicalName())
} }
@ -92,14 +92,14 @@ func toLinkSet(xs []string) linkSet {
return result return result
} }
func fetchText(h hyphae2.Hypha) string { func fetchText(h hyphae.Hypha) string {
var path string var path string
switch h := h.(type) { switch h := h.(type) {
case *hyphae2.EmptyHypha: case *hyphae.EmptyHypha:
return "" return ""
case *hyphae2.TextualHypha: case *hyphae.TextualHypha:
path = h.TextFilePath() path = h.TextFilePath()
case *hyphae2.MediaHypha: case *hyphae.MediaHypha:
if !h.HasTextFile() { if !h.HasTextFile() {
return "" return ""
} }
@ -108,7 +108,7 @@ func fetchText(h hyphae2.Hypha) string {
text, err := os.ReadFile(path) text, err := os.ReadFile(path)
if err != nil { if err != nil {
log.Println(err) slog.Error("Failed to read file", "path", path, "err", err, "hyphaName", h.CanonicalName())
return "" return ""
} }
return string(text) return string(text)

View File

@ -1,12 +1,13 @@
package backlinks package backlinks
import ( import (
"github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/mycoopts"
"git.sr.ht/~bouncepaw/mycomarkup/v5" "git.sr.ht/~bouncepaw/mycomarkup/v5"
"git.sr.ht/~bouncepaw/mycomarkup/v5/links" "git.sr.ht/~bouncepaw/mycomarkup/v5/links"
"git.sr.ht/~bouncepaw/mycomarkup/v5/mycocontext" "git.sr.ht/~bouncepaw/mycomarkup/v5/mycocontext"
"git.sr.ht/~bouncepaw/mycomarkup/v5/tools" "git.sr.ht/~bouncepaw/mycomarkup/v5/tools"
"github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/mycoopts"
) )
// UpdateBacklinksAfterEdit is a creation/editing hook for backlinks index // UpdateBacklinksAfterEdit is a creation/editing hook for backlinks index