Files
remanso/src/hooks/useNoteView.hook.ts
Julien Calixte 4ce8c30649 fix(navigation): support anchor fragments in note links
Links like `path/to/note.md#heading` previously errored with "Note not
found" because the full href (including `#hash`) was matched against
file paths. Split the fragment off in the link handler, plumb it through
the event bus, and scroll the matching heading into view once the
target note is in place. Headings now get GitHub-style ids via
markdown-it-anchor + github-slugger so the anchors actually exist.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-26 09:40:30 +02:00

53 lines
1.4 KiB
TypeScript

import { computed, onUnmounted } from "vue"
import { noteEventBus } from "@/bus/noteEventBus"
import { useRouteQueryStackedNotes } from "@/hooks/useRouteQueryStackedNotes.hook"
import { resolvePath } from "@/modules/repo/services/resolvePath"
import { useUserRepoStore } from "@/modules/repo/store/userRepo.store"
import { pathToNotePathTitle } from "@/utils/noteTitle"
import { errorMessage } from "@/utils/notif"
export const useNoteView = () => {
const store = useUserRepoStore()
const { stackedNotes, addStackedNote } = useRouteQueryStackedNotes()
const titles = computed(() =>
stackedNotes.value?.reduce((obj: Record<string, string>, note) => {
if (!note) {
return obj
}
const filePath = store.files.find((file) => file.sha === note)?.path ?? ""
obj[note] = pathToNotePathTitle(filePath)
return obj
}, {})
)
const unsubscribeLink = noteEventBus.addEventBusListener(
({ path, hash, currentNoteSHA }) => {
const currentFile = store.files.find(
(file) => file.sha === currentNoteSHA
)
const absolutePath = resolvePath(currentFile?.path ?? "", path)
const file = store.files.find((file) => file.path === absolutePath)
if (!file?.sha) {
errorMessage(`Note ${absolutePath} not found.`)
return
}
addStackedNote(currentNoteSHA ?? "", file.sha, undefined, hash)
}
)
onUnmounted(() => {
unsubscribeLink()
})
return {
titles
}
}