(path) create a resolve path service to manage …

resolve path like ../..
This commit is contained in:
2021-03-21 15:26:27 +01:00
parent 299bc75529
commit 3749588b0b
7 changed files with 91 additions and 38 deletions

View File

@@ -3,11 +3,11 @@ import markdownItClass from '@toycode/markdown-it-class'
const md = new MarkdownIt().use(markdownItClass, { const md = new MarkdownIt().use(markdownItClass, {
h1: ['title', 'is-1'], h1: ['title', 'is-1'],
h2: ['subtitle', 'is-2'], h2: ['title', 'is-2'],
h3: ['subtitle', 'is-3'], h3: ['title', 'is-3'],
h4: ['subtitle', 'is-4'], h4: ['title', 'is-4'],
h5: ['subtitle', 'is-5'], h5: ['title', 'is-5'],
h6: ['subtitle', 'is-6'], h6: ['title', 'is-6'],
table: ['table', 'is-striped', 'is-hoverable', 'is-fullwidth'] table: ['table', 'is-striped', 'is-hoverable', 'is-fullwidth']
}) })

View File

@@ -15,13 +15,7 @@ import { useOverlay } from '@/hooks/useOverlay.hook'
import { useQueryStackedNotes } from '@/hooks/useQueryStackedNotes.hook' import { useQueryStackedNotes } from '@/hooks/useQueryStackedNotes.hook'
import { useRepo } from '@/hooks/useRepo.hook' import { useRepo } from '@/hooks/useRepo.hook'
import { useRouter } from 'vue-router' import { useRouter } from 'vue-router'
import { resolvePath } from '@/modules/repo/services/resolvePath'
const sanitizePath = (path: string) => {
if (path.startsWith('./')) {
return decodeURIComponent(path.replace('./', ''))
}
return decodeURIComponent(path)
}
export const useNote = ( export const useNote = (
containerClass: string, containerClass: string,
@@ -59,19 +53,9 @@ export const useNote = (
({ path, currentNoteSHA }) => { ({ path, currentNoteSHA }) => {
const currentFile = tree.value.find((file) => file.sha === currentNoteSHA) const currentFile = tree.value.find((file) => file.sha === currentNoteSHA)
const absolutePathArray = currentFile?.path?.split('/') ?? [] const finalPath = resolvePath(currentFile?.path ?? '', path)
absolutePathArray?.pop()
const absolutePath = absolutePathArray.join('/')
const finalPath = absolutePath const file = tree.value.find((file) => file.path === finalPath)
? `${sanitizePath(absolutePath)}/${sanitizePath(path)}`
: sanitizePath(path)
const relativePath = sanitizePath(path)
const file = tree.value.find(
(file) => file.path === finalPath || file.path === relativePath
)
if (!file?.sha || stackedNotes.value.includes(file.sha)) { if (!file?.sha || stackedNotes.value.includes(file.sha)) {
scrollToFocusedNote(file?.sha) scrollToFocusedNote(file?.sha)

View File

@@ -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
}

View File

@@ -9,8 +9,9 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { useQueryStackedNotes } from '@/hooks/useQueryStackedNotes.hook'
import { useFolderNotes } from '@/modules/note/hooks/useFolderNotes' import { useFolderNotes } from '@/modules/note/hooks/useFolderNotes'
import { defineAsyncComponent, defineComponent, toRefs } from 'vue' import { defineAsyncComponent, defineComponent, onMounted, toRefs } from 'vue'
const FluxNote = defineAsyncComponent(() => import('@/components/FluxNote.vue')) const FluxNote = defineAsyncComponent(() => import('@/components/FluxNote.vue'))
@@ -32,6 +33,11 @@ export default defineComponent({
refProps.user, refProps.user,
refProps.repo refProps.repo
) )
const { resetStackedNotes } = useQueryStackedNotes()
onMounted(() => {
resetStackedNotes()
})
return { return {
content content

View File

@@ -9,8 +9,9 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { useQueryStackedNotes } from '@/hooks/useQueryStackedNotes.hook'
import { useFolderNotes } from '@/modules/note/hooks/useFolderNotes' import { useFolderNotes } from '@/modules/note/hooks/useFolderNotes'
import { defineAsyncComponent, defineComponent, toRefs } from 'vue' import { defineAsyncComponent, defineComponent, onMounted, toRefs } from 'vue'
const FluxNote = defineAsyncComponent(() => import('@/components/FluxNote.vue')) const FluxNote = defineAsyncComponent(() => import('@/components/FluxNote.vue'))
@@ -32,6 +33,11 @@ export default defineComponent({
refProps.user, refProps.user,
refProps.repo refProps.repo
) )
const { resetStackedNotes } = useQueryStackedNotes()
onMounted(() => {
resetStackedNotes()
})
return { return {
content content

View File

@@ -1,12 +0,0 @@
import { shallowMount } from '@vue/test-utils'
import HelloWorld from '@/components/HelloWorld.vue'
describe('HelloWorld.vue', () => {
it('renders props.msg when passed', () => {
const msg = 'new message'
const wrapper = shallowMount(HelloWorld, {
props: { msg }
})
expect(wrapper.text()).toMatch(msg)
})
})

View File

@@ -0,0 +1,37 @@
import { resolvePath } from '@/modules/repo/services/resolvePath'
describe('resolve path service', () => {
it('set the absolute path if path to resolve is empty', () => {
expect(resolvePath('standard/README.md', '')).toEqual('standard/')
})
it('returns the path sanitized if there is no absolute path', () => {
expect(resolvePath('', './here/note.md')).toEqual('here/note.md')
})
it('set the absolute path from the current path', () => {
expect(resolvePath('standard/README.md', './other-note.md')).toEqual(
'standard/other-note.md'
)
})
it('set the absolute path from the current path with multiple level', () => {
expect(
resolvePath('standard/you/are/here/README.md', './other-note.md')
).toEqual('standard/you/are/here/other-note.md')
})
it('set the absolute path from the current path with a go back in the relative path', () => {
expect(
resolvePath('standard/you/are/here/README.md', '../other-note.md')
).toEqual('standard/you/are/other-note.md')
expect(
resolvePath('standard/you/are/here/README.md', '../../other-note.md')
).toEqual('standard/you/other-note.md')
expect(
resolvePath('standard/you/are/here/README.md', './../../other-note.md')
).toEqual('standard/you/other-note.md')
})
})