#
VitePress真是太好用了
2025-08-28
链接卡片
创建 Linkcard 组件,展示带 Logo、标题、描述的链接卡片样式。参考 Vuejs 官方中文仓库的实现方式,可以在文档中插入美观的外链卡片,包含网站 Logo、标题、描述和背景色,提升外部链接的展示效果和点击率。
:::tip
代码参考自 vuejs官网 的 中文仓库
:::
在theme/components文件夹,创建Linkcard.vue。
docs
├─ .vitepress
│ └─ config.mts
│ └─ theme
│ │ ├─ components
│ │ │ └─ Linkcard.vue
│ │ └─ index.ts
└─ index.md粘贴如下代码,保存
Linkcard.vue
<script setup lang="ts">
interface Props {
url: string
title: string
description: string
logo: string
}
const props = withDefaults(defineProps<Props>(), {
url: '',
title: '',
description: '',
logo: '',
})
</script>
<template>
<div class="linkcard">
<a :href="props.url" target="_blank">
<p class="description">{{ props.title }}<br><span>{{ props.description }}</span></p>
<div class="logo">
<img alt="logo" width="70px" height="70px" :src="props.logo" />
</div>
</a>
</div>
</template>
<style>
/* 卡片背景 */
.linkcard {
background-color: var(--vp-c-bg-soft);
border-radius: 8px;
padding: 8px 16px 8px 8px;
transition: color 0.5s, background-color 0.5s;
margin-top: 15px;
}
/* 卡片鼠标悬停 */
.linkcard:hover {
background-color: var(--vp-c-yellow-soft);
}
/* 链接样式 */
.linkcard a {
display: flex;
align-items: center;
}
/* 描述链接文字 */
.linkcard .description {
flex: 1;
font-weight: 500;
font-size: 16px;
line-height: 25px;
color: var(--vp-c-text-1);
margin: 0 0 0 16px;
transition: color 0.5s;
}
/* 描述链接文字2 */
.linkcard .description span {
font-size: 14px;
}
/* logo图片 */
.linkcard .logo img {
width: 80px;
object-fit: contain;
}
/* 链接下划线去除 */
.vp-doc a {
text-decoration: none;
}
</style>然后,在 index.ts 中注册全局组件
docs
├─ .vitepress
│ └─ config.mts
│ └─ theme
│ │ ├─ components
│ │ │ └─ Linkcard.vue
│ │ └─ index.ts
└─ index.md/* .vitepress/theme/index.ts */
import DefaultTheme from 'vitepress/theme'
import Linkcard from "./components/Linkcard.vue"
export default {
extends: DefaultTheme,
enhanceApp({app}) {
// 注册全局组件
app.component('Linkcard' , Linkcard)
}使用上,注意格式
<Linkcard url="你的网址" title="标题" description="描述" logo="logo图片路径"/>
比如:
<Linkcard url="https://vitepress.yiov.top/" title="Vitepress中文搭建教程" description="https://vitepress.yiov.top/" logo="https://vitepress.yiov.top/logo.png"/> 本文由 DoubleSpirit121 原创
采用 CC BY-NC-SA 4.0 协议进行许可
TAGS:
VitePress 1.6.3
0 评论