jointrashposs/components/g/NuxtLink.vue

36 lines
842 B
Vue
Raw Normal View History

2023-09-23 11:17:13 +02:00
<template>
2023-09-23 12:07:45 +02:00
<NuxtLink
:to="realHref"
2023-09-23 20:02:14 +02:00
:href="undefined"
2023-09-23 12:07:45 +02:00
>
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 20:02:14 +02:00
import { RouteLocationRaw } from '#vue-router';
2023-09-23 11:17:13 +02:00
2023-09-23 20:02:14 +02:00
const rawProps = defineProps<{
to?: RouteLocationRaw | string;
href?: RouteLocationRaw | string;
}>();
2023-09-23 11:17:13 +02:00
2023-09-24 05:35:58 +02:00
const realHref = computed(() => {
const rhf = rawProps.to ?? rawProps.href;
2023-09-23 12:07:45 +02:00
2023-09-24 05:35:58 +02:00
if (rhf && typeof rhf === 'string') {
const runtimeConfig = useRuntimeConfig();
const rootDomain = parseURL(runtimeConfig.public.baseUrl);
const url = parseURL(rhf);
if (!url.host || rootDomain.host === url.host) {
return withTrailingSlash(rhf, true);
}
2023-09-23 11:17:13 +02:00
2023-09-24 05:35:58 +02:00
return cleanDoubleSlashes(rhf);
}
});
2023-09-23 11:17:13 +02:00
</script>