jointrashposs/components/content/ProseA.vue

53 lines
1.6 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';
2023-09-23 12:07:45 +02:00
import { $URL, 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();
2023-09-23 11:17:13 +02:00
const rootDomain = new $URL(runtimeConfig.public.baseUrl);
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
2023-09-23 11:17:13 +02:00
const url = new $URL(realHref.value);
2023-11-25 17:48:16 +01:00
if (isLocalPath(realHref.value)) {
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(url.fullpath);
} else {
// 渡されたパスがローカライズされたルートでない場合はローカライズされたパスを返す
2023-11-25 17:48:16 +01:00
realHref.value = sanitizeInternalPath(localePath(url.fullpath));
2023-07-11 19:19:32 +02:00
}
2023-09-23 11:17:13 +02:00
// 相対パスの場合trailing slashがあるのでつくり下げる
if (isRelative(realHref.value)) {
realHref.value = joinURL('../', realHref.value);
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>