compute note backlinks
This commit is contained in:
94
src/hooks/useComputeBacklinks.hook.ts
Normal file
94
src/hooks/useComputeBacklinks.hook.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { data } from '@/data/data'
|
||||
import { DataType } from '@/data/DataType.enum'
|
||||
import { useFile } from '@/hooks/useFile.hook'
|
||||
import { Backlink } from '@/modules/note/models/Backlink'
|
||||
import { BacklinkNote } from '@/modules/note/models/BacklinkNote'
|
||||
import { resolvePath } from '@/modules/repo/services/resolvePath'
|
||||
import { useUserRepoStore } from '@/modules/repo/store/userRepo.store'
|
||||
import { isExternalLink } from '@/utils/link'
|
||||
import { filenameToNoteTitle } from '@/utils/noteTitle'
|
||||
import { watch } from 'vue'
|
||||
|
||||
const isMarkdown = (filename?: string) => filename?.endsWith('.md') ?? false
|
||||
|
||||
export const useComputeBacklinks = () => {
|
||||
const store = useUserRepoStore()
|
||||
|
||||
watch(store, async () => {
|
||||
if (!store.userSettings?.backlink) {
|
||||
return
|
||||
}
|
||||
|
||||
const backlinks: Map<string, Backlink[]> = new Map()
|
||||
|
||||
for (const file of store.files) {
|
||||
if (!isMarkdown(file.path) || !file.sha) {
|
||||
continue
|
||||
}
|
||||
const fileBacklinkId = data.generateId(DataType.BacklinkNote, file.sha)
|
||||
const fileBacklink = await data.get<DataType.BacklinkNote, BacklinkNote>(
|
||||
fileBacklinkId
|
||||
)
|
||||
if (fileBacklink) {
|
||||
continue
|
||||
}
|
||||
backlinks.set(file.sha, [])
|
||||
const { getContent } = useFile(file.sha, false)
|
||||
const note = await getContent()
|
||||
|
||||
if (!note) {
|
||||
return
|
||||
}
|
||||
|
||||
const parser = new DOMParser()
|
||||
const htmlDoc = parser.parseFromString(note, 'text/html')
|
||||
|
||||
const links = htmlDoc.querySelectorAll('a')
|
||||
|
||||
for (const link of links) {
|
||||
const href = link.getAttribute('href') ?? ''
|
||||
|
||||
if (isExternalLink(href) || !isMarkdown(href)) {
|
||||
continue
|
||||
}
|
||||
|
||||
const path = resolvePath(file.path ?? '', href)
|
||||
const backlinkFile = store.files.find((file) => file.path === path)
|
||||
|
||||
if (!backlinkFile?.sha || !backlinkFile?.path) {
|
||||
continue
|
||||
}
|
||||
|
||||
const previousBacklinks = backlinks.get(backlinkFile.sha) ?? []
|
||||
backlinks.set(backlinkFile.sha, [
|
||||
...previousBacklinks,
|
||||
{
|
||||
sha: file.sha,
|
||||
title: filenameToNoteTitle(file.path ?? '')
|
||||
}
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
for (const [sha, fileBacklinks] of backlinks) {
|
||||
const fileBacklinkId = data.generateId(DataType.BacklinkNote, sha)
|
||||
const backlinkNote: BacklinkNote = {
|
||||
_id: fileBacklinkId,
|
||||
$type: DataType.BacklinkNote,
|
||||
sha: sha,
|
||||
links: fileBacklinks
|
||||
}
|
||||
|
||||
await data.add(backlinkNote)
|
||||
}
|
||||
|
||||
const backlinksInDb = await data.getAll<
|
||||
DataType.BacklinkNote,
|
||||
BacklinkNote
|
||||
>({
|
||||
prefix: DataType.BacklinkNote
|
||||
})
|
||||
|
||||
console.log(backlinksInDb.filter((b) => b.links.length))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user