Add empty link security

This commit is contained in:
Peaceultime 2024-01-10 00:09:43 +01:00
parent 51e20b99c1
commit a55b98e07a
1 changed files with 55 additions and 54 deletions

View File

@ -34,15 +34,15 @@ const link = (props.href.startsWith('/') ? '' : '/') + (props.href.includes('#')
const anchor = props.href.includes('#') ? props.href.substring(props.href.indexOf('#'), props.href.length) : '';
let content = ref<ParsedContentExtended>(), loading = ref(true);
queryContent().where({ _path: new RegExp("/" + sluggify(link) + '$', 'i') }).findOne().then((result: ParsedContentExtended) => {
if(props.href !== '')
{
queryContent().where({ _path: new RegExp("/" + sluggify(link) + '$', 'i') }).findOne().then((result: ParsedContentExtended) => {
content.value = result;
loading.value = false;
let body = result.body;
if(anchor && result && body)
{
if(result._type == 'markdown' && (body as MarkdownRoot).children)
{
if (anchor && result && body) {
if (result._type == 'markdown' && (body as MarkdownRoot).children) {
body = body as MarkdownRoot;
const id = flatten(body?.toc?.links ?? []).find(e => "#" + sluggify(e.id) === anchor);
const tag = `h${id?.depth ?? 0}`;
@ -52,8 +52,7 @@ queryContent().where({ _path: new RegExp("/" + sluggify(link) + '$', 'i') }).fin
body.children = body.children.splice(startIdx, (nbr === -1 ? body.children.length : nbr) - startIdx);
}
else if(result._type == 'canvas')
{
else if (result._type == 'canvas') {
body = body as CanvasContent;
const nodes = body?.groups?.find(e => "#" + sluggify(e.name) === anchor)?.nodes;
@ -61,11 +60,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(() => {
}).catch(() => {
loading.value = false;
console.error("Cannot find ", props.href);
});
});
}
const hovered = ref(false), pos = ref<any>();
let timeout: NodeJS.Timeout;
@ -94,7 +92,8 @@ function hidePreview(e: Event) {
</script>
<template >
<NuxtLink custom no-prefetch v-slot="{ href: hrefSlot, navigate }" :href="{path: content?._path ?? link, hash: anchor}" :target="target">
<template v-if="href !== ''">
<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)">
@ -126,4 +125,6 @@ function hidePreview(e: Event) {
</div>
</div>
</Teleport>
</template>
<a v-bind="$attrs" v-else><slot></slot></a>
</template>