Fix links without leading slash

This commit is contained in:
Peaceultime 2024-01-09 23:47:17 +01:00
parent f19a6a65da
commit 51e20b99c1
2 changed files with 5 additions and 2 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@
.nitro
.cache
dist
content
# Node dependencies
node_modules

View File

@ -30,7 +30,7 @@ function flatten(val: TocLink[]): TocLink[] {
return val.flatMap ? val?.flatMap((e: TocLink) => e.children ? [e, ...flatten(e.children)] : e) : val;
}
const link = (props.href.includes('#') ? props.href.substring(0, props.href.indexOf('#')) : props.href).replace(/\..*$/, '');
const link = (props.href.startsWith('/') ? '' : '/') + (props.href.includes('#') ? props.href.substring(0, props.href.indexOf('#')) : props.href).replace(/\..*$/, '').replace("/index", '');
const anchor = props.href.includes('#') ? props.href.substring(props.href.indexOf('#'), props.href.length) : '';
let content = ref<ParsedContentExtended>(), loading = ref(true);
@ -61,8 +61,10 @@ queryContent().where({ _path: new RegExp("/" + sluggify(link) + '$', 'i') }).fin
body.edges = body.edges.filter(e => nodes?.includes(e.fromNode) && nodes?.includes(e.toNode));
}
}
console.error("Found ", props.href);
}).catch(() => {
loading.value = false;
console.error("Cannot find ", props.href);
});
const hovered = ref(false), pos = ref<any>();
@ -92,7 +94,7 @@ function hidePreview(e: Event) {
</script>
<template >
<NuxtLink custom no-prefetch v-slot="{ href: hrefSlot, navigate }" :to="(content?._path ?? link + anchor) ?? href" :target="target">
<NuxtLink custom no-prefetch v-slot="{ href: hrefSlot, navigate }" :href="{path: content?._path ?? link, hash: anchor}" :target="target">
<a :href="hrefSlot" @click="navigate" @mouseenter="(e) => showPreview(e, true)" @mouseleave="hidePreview" v-bind="$attrs"><slot ></slot></a>
</NuxtLink>
<Teleport to="body" v-if="hovered && (loading || !!content)">