compute note backlinks

This commit is contained in:
Julien Calixte
2021-06-06 09:57:17 +02:00
parent 203db4c00a
commit 4f13c18573
11 changed files with 122 additions and 24 deletions

View File

@@ -1,14 +0,0 @@
import { useUserRepoStore } from '@/modules/repo/store/userRepo.store'
import { watch } from 'vue'
export const useBackLinks = () => {
const store = useUserRepoStore()
watch(store, () => {
if (!store.userSettings?.backlink) {
return
}
console.log("let's go backlinks!")
})
}

View 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))
})
}

View File

@@ -18,6 +18,7 @@ export const useFile = (sha: string, retrieveContent = true) => {
return
}
content.value = render(fileContent)
return content.value
}
const getCachedFileContent = async (): Promise<string | null> => {

View File

@@ -1,9 +1,8 @@
import { noteEventBus } from '@/bus/noteEventBus'
import { useUserRepoStore } from '@/modules/repo/store/userRepo.store'
import { isExternalLink } from '@/utils/link'
import { onUnmounted } from '@vue/runtime-core'
const LINKS = ['http://', 'https://']
export const useLinks = (className: string, sha?: string) => {
const store = useUserRepoStore()
@@ -16,7 +15,7 @@ export const useLinks = (className: string, sha?: string) => {
return
}
if (LINKS.some((link) => href.startsWith(link))) {
if (isExternalLink(href)) {
window.open(href, '_blank')
return
}
@@ -50,9 +49,7 @@ export const useLinks = (className: string, sha?: string) => {
return
}
const isExternalLink = LINKS.some((link) => href.startsWith(link))
if (isExternalLink) {
if (isExternalLink(href)) {
element.classList.add('external-link')
}
})