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) : ''; const anchor = props.href.includes('#') ? props.href.substring(props.href.indexOf('#'), props.href.length) : '';
let content = ref<ParsedContentExtended>(), loading = ref(true); let content = ref<ParsedContentExtended>(), loading = ref(true);
queryContent().where({ _path: new RegExp("/" + sluggify(link) + '$', 'i') }).findOne().then((result: ParsedContentExtended) => { if(props.href !== '')
content.value = result; {
loading.value = false; queryContent().where({ _path: new RegExp("/" + sluggify(link) + '$', 'i') }).findOne().then((result: ParsedContentExtended) => {
let body = result.body; content.value = result;
loading.value = false;
let body = result.body;
if(anchor && result && body) if (anchor && result && body) {
{ if (result._type == 'markdown' && (body as MarkdownRoot).children) {
if(result._type == 'markdown' && (body as MarkdownRoot).children) body = body as MarkdownRoot;
{ const id = flatten(body?.toc?.links ?? []).find(e => "#" + sluggify(e.id) === anchor);
body = body as MarkdownRoot; const tag = `h${id?.depth ?? 0}`;
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 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 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') }).catch(() => {
{ loading.value = false;
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);
});
const hovered = ref(false), pos = ref<any>(); const hovered = ref(false), pos = ref<any>();
let timeout: NodeJS.Timeout; let timeout: NodeJS.Timeout;
@ -94,36 +92,39 @@ function hidePreview(e: Event) {
</script> </script>
<template > <template >
<NuxtLink custom no-prefetch v-slot="{ href: hrefSlot, navigate }" :href="{path: content?._path ?? link, hash: anchor}" :target="target"> <template v-if="href !== ''">
<a :href="hrefSlot" @click="navigate" @mouseenter="(e) => showPreview(e, true)" @mouseleave="hidePreview" v-bind="$attrs"><slot ></slot></a> <NuxtLink custom no-prefetch v-slot="{ href: hrefSlot, navigate }" :href="{ path: content?._path ?? link, hash: anchor }" :target="target">
</NuxtLink> <a :href="hrefSlot" @click="navigate" @mouseenter="(e) => showPreview(e, true)" @mouseleave="hidePreview" v-bind="$attrs"><slot ></slot></a>
<Teleport to="body" v-if="hovered && (loading || !!content)"> </NuxtLink>
<div class="popover hover-popover is-loaded" :style="pos" @mouseenter="(e) => showPreview(e, false)" @mouseleave="hidePreview"> <Teleport to="body" v-if="hovered && (loading || !!content)">
<template v-if="!!content"> <div class="popover hover-popover is-loaded" :style="pos" @mouseenter="(e) => showPreview(e, false)" @mouseleave="hidePreview">
<div class="markdown-embed" v-if="content._type == 'markdown' && ((content as ParsedContent)?.body?.children?.length ?? 0) > 0"> <template v-if="!!content">
<div class="markdown-embed-content"> <div class="markdown-embed" v-if="content._type == 'markdown' && ((content as ParsedContent)?.body?.children?.length ?? 0) > 0">
<div class="markdown-preview-view markdown-rendered node-insert-event hide-title"> <div class="markdown-embed-content">
<div class="markdown-preview-sizer markdown-preview-section" style="padding-bottom: 0px;"> <div class="markdown-preview-view markdown-rendered node-insert-event hide-title">
<h1 v-if="content?.title">{{ content?.title }}</h1> <div class="markdown-preview-sizer markdown-preview-section" style="padding-bottom: 0px;">
<ContentRenderer :key="content._id" :value="content"/> <h1 v-if="content?.title">{{ content?.title }}</h1>
<ContentRenderer :key="content._id" :value="content"/>
</div>
</div> </div>
</div> </div>
</div> </div>
</div> <div v-else-if="content._type == 'canvas'" class="canvas-embed is-loaded">
<div v-else-if="content._type == 'canvas'" class="canvas-embed is-loaded"> <CanvasRenderer :key="content._id" :canvas="(content as Canvas)" />
<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> </div>
</div> <div class="markdown-embed" v-else>
</template> <div class="not-found-container">
<div v-else> <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>
</div> </Teleport>
</Teleport> </template>
<a v-bind="$attrs" v-else><slot></slot></a>
</template> </template>