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,38 +34,36 @@ 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) => {
content.value = result;
loading.value = false;
let body = result.body;
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)
{
body = body as MarkdownRoot;
const id = flatten(body?.toc?.links ?? []).find(e => "#" + sluggify(e.id) === anchor);
const tag = `h${id?.depth ?? 0}`;
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}`;
const startIdx = body.children.findIndex(e => e.tag === tag && e?.props?.id === id?.id) ?? 0;
const nbr = body.children.findIndex((e, i) => i > startIdx && e.tag?.match(new RegExp(`h[1-${id?.depth ?? 1}]`)));
const startIdx = body.children.findIndex(e => e.tag === tag && e?.props?.id === id?.id) ?? 0;
const nbr = body.children.findIndex((e, i) => i > startIdx && e.tag?.match(new RegExp(`h[1-${id?.depth ?? 1}]`)));
body.children = body.children.splice(startIdx, (nbr === -1 ? body.children.length : nbr) - startIdx);
body.children = body.children.splice(startIdx, (nbr === -1 ? body.children.length : nbr) - startIdx);
}
else if (result._type == 'canvas') {
body = body as CanvasContent;
const nodes = body?.groups?.find(e => "#" + sluggify(e.name) === anchor)?.nodes;
body.nodes = body.nodes.filter(e => nodes?.includes(e.id));
body.edges = body.edges.filter(e => nodes?.includes(e.fromNode) && nodes?.includes(e.toNode));
}
}
else if(result._type == 'canvas')
{
body = body as CanvasContent;
const nodes = body?.groups?.find(e => "#" + sluggify(e.name) === anchor)?.nodes;
body.nodes = body.nodes.filter(e => nodes?.includes(e.id));
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);
});
}).catch(() => {
loading.value = false;
});
}
const hovered = ref(false), pos = ref<any>();
let timeout: NodeJS.Timeout;
@ -94,36 +92,39 @@ function hidePreview(e: Event) {
</script>
<template >
<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)">
<div class="popover hover-popover is-loaded" :style="pos" @mouseenter="(e) => showPreview(e, false)" @mouseleave="hidePreview">
<template v-if="!!content">
<div class="markdown-embed" v-if="content._type == 'markdown' && ((content as ParsedContent)?.body?.children?.length ?? 0) > 0">
<div class="markdown-embed-content">
<div class="markdown-preview-view markdown-rendered node-insert-event hide-title">
<div class="markdown-preview-sizer markdown-preview-section" style="padding-bottom: 0px;">
<h1 v-if="content?.title">{{ content?.title }}</h1>
<ContentRenderer :key="content._id" :value="content"/>
<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)">
<div class="popover hover-popover is-loaded" :style="pos" @mouseenter="(e) => showPreview(e, false)" @mouseleave="hidePreview">
<template v-if="!!content">
<div class="markdown-embed" v-if="content._type == 'markdown' && ((content as ParsedContent)?.body?.children?.length ?? 0) > 0">
<div class="markdown-embed-content">
<div class="markdown-preview-view markdown-rendered node-insert-event hide-title">
<div class="markdown-preview-sizer markdown-preview-section" style="padding-bottom: 0px;">
<h1 v-if="content?.title">{{ content?.title }}</h1>
<ContentRenderer :key="content._id" :value="content"/>
</div>
</div>
</div>
</div>
</div>
<div v-else-if="content._type == 'canvas'" class="canvas-embed is-loaded">
<CanvasRenderer :key="content._id" :canvas="(content as Canvas)" />
</div>
<div class="markdown-embed" v-else>
<div class="not-found-container">
<div class="not-found-image"></div>
<div class="not-found-title">Impossible de prévisualiser</div>
<div class="not-found-description">Cliquez sur le lien pour accéder au contenu</div>
<div v-else-if="content._type == 'canvas'" class="canvas-embed is-loaded">
<CanvasRenderer :key="content._id" :canvas="(content as Canvas)" />
</div>
</div>
</template>
<div v-else>
<div class="markdown-embed" v-else>
<div class="not-found-container">
<div class="not-found-image"></div>
<div class="not-found-title">Impossible de prévisualiser</div>
<div class="not-found-description">Cliquez sur le lien pour accéder au contenu</div>
</div>
</div>
</template>
<div v-else>
</div>
</div>
</div>
</Teleport>
</Teleport>
</template>
<a v-bind="$attrs" v-else><slot></slot></a>
</template>