vitepress的使用

创建项目并启动项目

js 复制代码
// 1.创建项目,直接在空项目下安装vitepress(npm/yarn等都可以,这个可以看官网,官网给了好几种安装方式)
yarn add -D vitepress
// 2.初始化配置项目(npm/官网也给了多种包管理工具的安装方式)
npx 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 //使用自定义主题
◇  Use TypeScript for config and theme files? //是否使用ts作为模版(我选的是no)
│  ● Yes / ○ No
◆  Add VitePress npm scripts to package.json? //是否将package.json作为配置文件(这里选择yes)
│  ● Yes / ○ No
└
// 整体为:./docs 回车 回车 第二个 no yes

这样就创建完毕了,目前项目的结构应该是如下所示:

js 复制代码
├─ docs
│  ├─ .vitepress //当前目录所在的位置就是文档的根目录
│  │  └─ config.js //项目的配置文件,最重要
│  ├─ api-examples.md
│  ├─ markdown-examples.md
│  └─ index.md
└─ package.json

项目启动

复制代码
yarn docs:dev

得到如下页面(这样项目就搭建完成了)

运行原理:项目启动后会允许根目录下的index.md,在项目里面所有的md文档均会被编译为html,这里项目的根目录被设置为了./docs,因此会先运行/docs/index.md以下所有介绍的根目录均代表./docs目录下

配置介绍

首页

js 复制代码
---
# https://vitepress.dev/reference/default-theme-home-page
layout: home  //指定以home格式呈现(一共有三种模式呈现doc,page,home,默认以doc格式呈现)

//下面的这些都是页面的显示各位可以去参考页面
hero:
  name: "My Awesome Project"
  text: "A VitePress Site"
  tagline: My great project tagline
  image:
  	scr:/logo.svg  //默认找根目录下的public文件夹内的图片
  	alt:VitePress
  actions:
    - theme: brand
      text: Markdown Examples
      link: /markdown-examples
    - theme: alt
      text: API Examples
      link: /api-examples

features:
  - title: Feature A
    details: Lorem ipsum dolor sit amet, consectetur adipiscing elit
  - title: Feature B
    details: Lorem ipsum dolor sit amet, consectetur adipiscing elit
  - title: Feature C
    details: Lorem ipsum dolor sit amet, consectetur adipiscing elit
---

文件路由

根目录下的config.mjs里面

js 复制代码
import { defineConfig } from 'vitepress'

// https://vitepress.dev/reference/site-config
export default defineConfig({
  title: "萧寂博客园",
  description: "A VitePress Site",
  themeConfig: {
    // https://vitepress.dev/reference/default-theme-config
    nav: [  //右上角导航栏
      {
        text: '前端', items: [
          { text: '前端子页面', link: '/web/index' },
          {
            text: '',  //这个写东西了就代表有标题了
            items: [
              { text: '前端子页面', link: '/web/进阶基础' },
            ]
          }
        ]
      },
      { text: '标签页', link: '/markdown-examples' }
    ],

    sidebar: {
      // 下面这个当匹配到对应的路由时会动态加载侧边栏,例如下面我这个就是匹配到/web/index这个时才展示侧边栏
      // 下面这种方法可以保证页面切换左边侧边栏不会消失
      '/web/index': {
        title: 'web前端',
        items: [
          { text: '前端首页', link: '/web/index' },
          { text: '前端侧边栏', link: '/web/进阶基础' }
        ]
      },
      '/web/进阶基础': {
        title: '进阶基础',
        items: [
          { text: '前端首页', link: '/web/index' },
          { text: '前端侧边栏', link: '/web/进阶基础' },
        ]
      }
    },

    socialLinks: [  // 右上角的图标(如github这种)
      { icon: 'github', link: 'https://github.com/vuejs/vitepress' }
    ]
  }
})

文档模式

一共有三种模式呈现doc,page,home,默认以doc格式呈现

更改文档模式如下(在当前指定的md文件最上面填写如下代码)

js 复制代码
---
layout:home
---

图片资源

图片资源放在根目录下的public文件夹内,可以直接写名字,默认这个名字的资源是去找public文件夹第一层去找同名文件

相关推荐
代码煮茶11 小时前
Vite 5.0 新特性深度解析:更快、更干净、更未来的前端构建利器
vue.js
Pu_Nine_915 小时前
IntersectionObserver 详解:封装 Vue 指令实现图片懒加载
前端·javascript·vue.js·性能优化
前端那点事16 小时前
Vue nextTick 超全解析|作用、使用场景、底层原理、Vue2/Vue3区别
前端·vue.js
前端那点事16 小时前
Vue面试高频:子组件能直接修改父组件数据吗?单向数据流原理+正确写法全覆盖
前端·vue.js
前端那点事16 小时前
为什么 Vue 的 template 标签不能用 v-show?底层机制+踩坑复盘+生产级解决方案
前端·vue.js
北风toto18 小时前
为什么 IntelliJ IDEA Community 无法开发 Vue?——附解决方案
java·vue.js·intellij-idea
跟着珅聪学java18 小时前
Element UI 的 Tabs 标签页开发教程
javascript·vue.js·elementui
jiayong2319 小时前
前端面试题库 - Vue框架篇
前端·vue.js·面试
三*一19 小时前
Mapbox GL JS 前端多边形分割实战:从踩坑到优雅实现
开发语言·前端·javascript·vue.js
xChive20 小时前
前端请求取消:用 AbortController 从 fetch 到 axios
前端·vue.js·axios·fetch·abortcontroller