手把手带你拿下vitepress

335 阅读2分钟

先看看我的

  • 偷用一个博主的主题插件,喜欢这种简洁直观的风格

微信图片_20240512202407.png

前期准备(官方说的)

第一步

  • 随便选择一种方式
npm add -D vitepress
pnpm add -D vitepress
yarn add -D vitepress
bun add -D vitepress

第二步

  • 构建项目
npx vitepress init
pnpm vitepress init
yarn vitepress init
bun vitepress init
  • 回答几个问题,后期都可以改,随便填
┌  Welcome to VitePress!
│
◇  Where should VitePress initialize the config?
│  ./docs
│
◇  Site title:
│  My Awesome Project
│
◇  Site description:
│  A VitePress Site
│
◆  Theme:
│  ● Default Theme (Out of the box, good-looking docs)
│  ○ Default Theme + Customization
│  ○ Custom Theme
└

第三步

  • 运行
npm run docs:dev
  • 界面如下,有点太素了,稍微美化一下下

微信图片_20240512211758.png

第四步

pnpm add @sugarat/theme
pnpm add @element-plus/icons-vue element-plus vue sass
  • 在配置文件.vitepress/config.ts (.js,mts等等均可) 中引入主题配置
import { defineConfig } from 'vitepress'

// 导入生成配置工具方法
import { getThemeConfig } from '@sugarat/theme/node'

// 主题独有配置,所有配置项,详见文档: https://theme.sugarat.top/
const blogTheme = getThemeConfig({}) 

export default defineConfig({
  // 继承博客主题配置
  extends: blogTheme, 
  // 省略VitePress其他配置
})
  • 在布局配置文件.vitepress/theme/index.ts中引入主题布局
import BlogTheme from '@sugarat/theme'

export default BlogTheme
  • 现在会变成这个样子,前提你得写好几篇文章。

微信图片_20240512202407.png

第五步

npm install vite-plugin-vitepress-auto-sidebar

//在config中添加
import AutoSidebar from 'vite-plugin-vitepress-auto-sidebar';
export default defineConfig({
 vite:{
    plugins:[
      // add plugin
      AutoSidebar(),
    ],
  },
})

第五步(搜索配置可有可无)

  • 我使用的是algolia,配置如下
import { defineConfig } from 'vitepress'
import { getThemeConfig } from '@sugarat/theme/node'
export default defineConfig({
  themeConfig: {
    algolia:{
      appId: '',
      apiKey: '',
      indexName: '',
      placeholder: '搜索',
    },
  }
})

微信图片_20240512222337.png

第六步