jointrashposs/components/g/NuxtLink.vue

30 lines
778 B
Vue
Raw Normal View History

2023-09-23 11:17:13 +02:00
<template>
2023-09-23 12:07:45 +02:00
<NuxtLink
v-bind="rawProps"
:to="realHref"
>
2023-09-23 11:17:13 +02:00
<slot></slot>
2023-09-23 12:07:45 +02:00
</NuxtLink>
2023-09-23 11:17:13 +02:00
</template>
<script setup lang="ts">
2023-09-23 12:07:45 +02:00
import { parseURL, cleanDoubleSlashes, withTrailingSlash } from 'ufo';
2023-09-23 11:17:13 +02:00
import { NuxtLinkProps } from 'nuxt/app';
2023-09-23 12:07:45 +02:00
const rawProps = defineProps<NuxtLinkProps>();
const realHref = ref(rawProps.to ?? rawProps.href);
2023-09-23 11:17:13 +02:00
2023-09-23 12:07:45 +02:00
if (realHref.value && typeof realHref.value === 'string') {
const runtimeConfig = useRuntimeConfig();
const rootDomain = parseURL(runtimeConfig.public.baseUrl);
const url = parseURL(realHref.value);
if (!url.host || rootDomain.host === url.host) {
realHref.value = withTrailingSlash(realHref.value, true);
}
2023-09-23 11:17:13 +02:00
2023-09-23 12:07:45 +02:00
realHref.value = cleanDoubleSlashes(realHref.value);
2023-09-23 11:17:13 +02:00
}
</script>