2023-07-11 19:19:32 +02:00
|
|
|
|
<script setup lang="ts">
|
2023-12-13 15:14:07 +01:00
|
|
|
|
import MiIco from '@/assets/svg/misskey_mi_bi.svg';
|
2023-07-13 17:38:17 +02:00
|
|
|
|
import ExtIco from 'bi/box-arrow-up-right.svg';
|
2023-12-30 05:16:36 +01:00
|
|
|
|
import { parseURL, isRelative, joinURL } from 'ufo';
|
2023-11-25 17:48:16 +01:00
|
|
|
|
import { isLocalPath, sanitizeInternalPath } from '@/assets/js/misc';
|
2023-07-13 17:38:17 +02:00
|
|
|
|
|
2023-07-11 19:19:32 +02:00
|
|
|
|
const runtimeConfig = useRuntimeConfig();
|
2023-12-30 05:16:36 +01:00
|
|
|
|
const rootDomain = parseURL(runtimeConfig.public.baseUrl);
|
2023-09-23 11:17:13 +02:00
|
|
|
|
const { resolve } = useRouter();
|
2023-12-22 15:53:24 +01:00
|
|
|
|
const localePath = useGLocalePath();
|
2023-07-11 19:19:32 +02:00
|
|
|
|
|
|
|
|
|
const props = defineProps({
|
|
|
|
|
href: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: ''
|
|
|
|
|
},
|
|
|
|
|
target: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: undefined,
|
|
|
|
|
required: false
|
2023-07-15 10:32:48 +02:00
|
|
|
|
},
|
2023-07-11 19:19:32 +02:00
|
|
|
|
})
|
|
|
|
|
|
2023-09-23 11:17:13 +02:00
|
|
|
|
const realHref = ref(props.href);
|
|
|
|
|
const realTarget = ref(props.target);
|
2023-07-11 19:19:32 +02:00
|
|
|
|
|
2023-12-30 05:16:36 +01:00
|
|
|
|
const url = parseURL(realHref.value);
|
2023-12-13 15:14:07 +01:00
|
|
|
|
|
2023-11-25 17:48:16 +01:00
|
|
|
|
if (isLocalPath(realHref.value)) {
|
2023-12-30 05:16:36 +01:00
|
|
|
|
// 相対パスの場合(trailing slashがあるので1つくり下げる)
|
|
|
|
|
if (isRelative(realHref.value)) {
|
|
|
|
|
realHref.value = joinURL('../', realHref.value.replace(/^\.\//, ''));
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-23 11:17:13 +02:00
|
|
|
|
// 内部リンクの場合
|
2023-12-24 16:57:53 +01:00
|
|
|
|
if (/^\/[a-z]{2}\//.test(realHref.value)) {
|
2023-12-30 05:16:36 +01:00
|
|
|
|
realHref.value = sanitizeInternalPath(realHref.value);
|
2023-12-24 16:57:53 +01:00
|
|
|
|
} else {
|
|
|
|
|
// 渡されたパスがローカライズされたルートでない場合はローカライズされたパスを返す
|
2023-12-30 05:16:36 +01:00
|
|
|
|
realHref.value = sanitizeInternalPath(localePath(resolve(realHref.value).fullPath));
|
2023-07-11 19:19:32 +02:00
|
|
|
|
}
|
2023-09-23 11:17:13 +02:00
|
|
|
|
} else if (rootDomain.host !== url.host) {
|
|
|
|
|
realTarget.value = '_blank';
|
2023-07-11 19:19:32 +02:00
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2023-12-24 16:57:53 +01:00
|
|
|
|
<GNuxtLink :href="realHref" :target="realTarget">
|
2023-12-13 15:14:07 +01:00
|
|
|
|
<slot></slot>
|
|
|
|
|
<MiIco v-if="realHref.startsWith('x-mi-web://')" class="text-xs mx-1" />
|
|
|
|
|
<ExtIco v-else-if="realTarget === '_blank'" class="text-xs mx-1" />
|
2023-07-11 19:19:32 +02:00
|
|
|
|
</GNuxtLink>
|
|
|
|
|
</template>
|