Skip to content

Commit

Permalink
fix: Dynamically set target attribute based on link type, using `_bla…
Browse files Browse the repository at this point in the history
…nk` for external links and `_self` for internal links
  • Loading branch information
Theo-Messi committed Aug 26, 2024
1 parent 9b057d3 commit 8baf412
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/guide/links.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export default {
:items="[
{
name: '支付宝',
link: 'https://www.alipay.com',
link: '',
icon: 'fab fa-alipay',
color: '#00a1e9'
},
Expand All @@ -135,7 +135,7 @@ export default {
},
{
name: '支付宝',
link: 'https://www.alipay.com',
link: '',
icon: 'https://i.theojs.cn/logo/github.svg'
},
{
Expand Down
10 changes: 9 additions & 1 deletion packages/Lumen/components/DocBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ export default defineComponent({
typeof url === 'string' &&
/\.(png|jpe?g|gif|svg|webp|bmp|tif?f|tiff|ico)(\?.*)?$/.test(url)
)
},
/**
* 判断给定的链接是否是外部链接。
* @param {string} link - 要判断的链接。
* @returns {boolean} 如果链接是外部链接,则返回 `true`,否则返回 `false`。
*/
isExternalLink(link: string): boolean {
return /^https?:\/\//.test(link)
}
}
})
Expand All @@ -70,7 +78,7 @@ export default defineComponent({
:name="item.name"
:title="item.name"
class="box"
target="_blank"
:target="isExternalLink(item.link) ? '_blank' : '_self'"
rel="noopener"
>
<div class="box-content">
Expand Down
10 changes: 9 additions & 1 deletion packages/Lumen/components/DocLinks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ export default defineComponent({
typeof url === 'string' &&
/\.(png|jpe?g|gif|svg|webp|bmp|tif?f|tiff|ico|avif)(\?.*)?$/.test(url)
)
},
/**
* 判断给定的链接是否是外部链接。
* @param {string} link - 要判断的链接。
* @returns {boolean} 如果链接是外部链接,则返回 `true`,否则返回 `false`。
*/
isExternalLink(link: string): boolean {
return /^https?:\/\//.test(link)
}
}
})
Expand All @@ -64,7 +72,7 @@ export default defineComponent({
:href="item.link"
:name="item.name"
:title="item.name"
target="_blank"
:target="isExternalLink(item.link) ? '_blank' : '_self'"
rel="noopener"
>
<!-- 渲染图标 -->
Expand Down

0 comments on commit 8baf412

Please sign in to comment.