From 3749588b0b1561df4df8948a2c3923b6f517cc4d Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Sun, 21 Mar 2021 15:26:27 +0100 Subject: [PATCH] =?UTF-8?q?:sparkles:=20(path)=20create=20a=20resolve=20pa?= =?UTF-8?q?th=20service=20to=20manage=20=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit resolve path like ../.. --- src/hooks/useMarkdown.hook.ts | 10 ++--- src/hooks/useNote.hook.ts | 22 ++--------- src/modules/repo/services/resolvePath.ts | 32 ++++++++++++++++ src/views/DraftNotes.vue | 8 +++- src/views/FleetingNotes.vue | 8 +++- tests/unit/example.spec.ts | 12 ------ .../modules/repo/services/resolvePath.spec.ts | 37 +++++++++++++++++++ 7 files changed, 91 insertions(+), 38 deletions(-) create mode 100644 src/modules/repo/services/resolvePath.ts delete mode 100644 tests/unit/example.spec.ts create mode 100644 tests/unit/modules/repo/services/resolvePath.spec.ts diff --git a/src/hooks/useMarkdown.hook.ts b/src/hooks/useMarkdown.hook.ts index 9b8e5d7..984c955 100644 --- a/src/hooks/useMarkdown.hook.ts +++ b/src/hooks/useMarkdown.hook.ts @@ -3,11 +3,11 @@ import markdownItClass from '@toycode/markdown-it-class' const md = new MarkdownIt().use(markdownItClass, { h1: ['title', 'is-1'], - h2: ['subtitle', 'is-2'], - h3: ['subtitle', 'is-3'], - h4: ['subtitle', 'is-4'], - h5: ['subtitle', 'is-5'], - h6: ['subtitle', 'is-6'], + h2: ['title', 'is-2'], + h3: ['title', 'is-3'], + h4: ['title', 'is-4'], + h5: ['title', 'is-5'], + h6: ['title', 'is-6'], table: ['table', 'is-striped', 'is-hoverable', 'is-fullwidth'] }) diff --git a/src/hooks/useNote.hook.ts b/src/hooks/useNote.hook.ts index aaa7c4c..59361b7 100644 --- a/src/hooks/useNote.hook.ts +++ b/src/hooks/useNote.hook.ts @@ -15,13 +15,7 @@ import { useOverlay } from '@/hooks/useOverlay.hook' import { useQueryStackedNotes } from '@/hooks/useQueryStackedNotes.hook' import { useRepo } from '@/hooks/useRepo.hook' import { useRouter } from 'vue-router' - -const sanitizePath = (path: string) => { - if (path.startsWith('./')) { - return decodeURIComponent(path.replace('./', '')) - } - return decodeURIComponent(path) -} +import { resolvePath } from '@/modules/repo/services/resolvePath' export const useNote = ( containerClass: string, @@ -59,19 +53,9 @@ export const useNote = ( ({ path, currentNoteSHA }) => { const currentFile = tree.value.find((file) => file.sha === currentNoteSHA) - const absolutePathArray = currentFile?.path?.split('/') ?? [] - absolutePathArray?.pop() - const absolutePath = absolutePathArray.join('/') + const finalPath = resolvePath(currentFile?.path ?? '', path) - const finalPath = absolutePath - ? `${sanitizePath(absolutePath)}/${sanitizePath(path)}` - : sanitizePath(path) - - const relativePath = sanitizePath(path) - - const file = tree.value.find( - (file) => file.path === finalPath || file.path === relativePath - ) + const file = tree.value.find((file) => file.path === finalPath) if (!file?.sha || stackedNotes.value.includes(file.sha)) { scrollToFocusedNote(file?.sha) diff --git a/src/modules/repo/services/resolvePath.ts b/src/modules/repo/services/resolvePath.ts new file mode 100644 index 0000000..1b37cec --- /dev/null +++ b/src/modules/repo/services/resolvePath.ts @@ -0,0 +1,32 @@ +const sanitizePath = (path: string) => { + if (path.startsWith('./')) { + return decodeURIComponent(path.replace('./', '')) + } + return decodeURIComponent(path) +} + +const removeNoteFilename = (pathNote: string) => { + const path = pathNote.split('/') + path.pop() + + return sanitizePath(path.join('/')) +} + +export const resolvePath = ( + currentAbsolutePathNote: string, + pathToResolve: string +) => { + let currentAbsolutePath = removeNoteFilename(currentAbsolutePathNote) + pathToResolve = sanitizePath(pathToResolve) + + while (pathToResolve.startsWith('../')) { + const adjustedAbsolutePath = currentAbsolutePath.split('/') + adjustedAbsolutePath.pop() + currentAbsolutePath = adjustedAbsolutePath.join('/') + pathToResolve = pathToResolve.replace('../', '') + } + + return currentAbsolutePath + ? `${currentAbsolutePath}/${pathToResolve}` + : pathToResolve +} diff --git a/src/views/DraftNotes.vue b/src/views/DraftNotes.vue index 6d3527e..c5d1409 100644 --- a/src/views/DraftNotes.vue +++ b/src/views/DraftNotes.vue @@ -9,8 +9,9 @@