feat: AppLink support custom target, close #560

This commit is contained in:
YunYouJun 2025-07-12 17:19:48 +08:00
parent f8f0e8df22
commit 7f87c55bfa
2 changed files with 10 additions and 4 deletions

View File

@ -1,26 +1,32 @@
<script setup lang="ts">
// https://router.vuejs.org/guide/advanced/extending-router-link.html#extending-routerlink
import { computed } from 'vue'
import { RouterLink } from 'vue-router'
import { EXTERNAL_URL_RE } from '../../node/constants'
const props = defineProps<{
showExternalIcon?: boolean
to?: string
href?: string
target?: string
}>()
const link = computed(() => props.href || props.to || '#')
const isExternalLink = computed(() => {
return typeof link.value === 'string' && link.value.startsWith('http')
return (link.value && EXTERNAL_URL_RE.test(link.value)) || props.target === '_blank'
})
</script>
<template>
<a v-if="isExternalLink" v-bind="$attrs" :href="link" target="_blank">
<a
v-if="isExternalLink" class="va-link" v-bind="$attrs" :href="link"
:target="target ?? (isExternalLink ? '_blank' : undefined)"
>
<slot />
<div v-if="showExternalIcon" class="icon-link inline-block" i-ri-arrow-right-up-line />
</a>
<RouterLink v-else v-bind="$attrs" :to="link">
<RouterLink v-else class="va-link" v-bind="$attrs" :to="link">
<slot />
</RouterLink>
</template>

View File

@ -2,7 +2,7 @@ import type { UserConfig } from 'vite'
export const EXCERPT_SEPARATOR = '<!-- more -->'
export const EXTERNAL_URL_RE = /^https?:/i
export const EXTERNAL_URL_RE = /^(?:[a-z]+:|\/\/)/i
export const PATHNAME_PROTOCOL_RE = /^pathname:\/\//
export const ALL_ROUTE = '/:all(.*)*'