jointrashposs/components/content/ProseA.vue

53 lines
1.7 KiB
Vue
Raw Normal View History

2023-07-11 19:19:32 +02:00
<script setup lang="ts">
import MiIco from '@/assets/svg/misskey_mi_bi.svg';
import ExtIco from 'bi/box-arrow-up-right.svg';
import { parseURL, isRelative, joinURL } from 'ufo';
2023-11-25 17:48:16 +01:00
import { isLocalPath, sanitizeInternalPath } from '@/assets/js/misc';
2023-07-11 19:19:32 +02:00
const runtimeConfig = useRuntimeConfig();
const rootDomain = parseURL(runtimeConfig.public.baseUrl);
2023-09-23 11:17:13 +02:00
const { resolve } = useRouter();
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-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
const url = parseURL(realHref.value);
2023-11-25 17:48:16 +01:00
if (isLocalPath(realHref.value)) {
// 相対パスの場合trailing slashがあるのでつくり下げる
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)) {
realHref.value = sanitizeInternalPath(realHref.value);
2023-12-24 16:57:53 +01:00
} else {
// 渡されたパスがローカライズされたルートでない場合はローカライズされたパスを返す
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">
<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>