以下是使用 VuePress 搭建个人博客或技术文档网站的详细教程,涵盖基础搭建、配置优化和部署流程:
node -v
和 npm -v
,显示版本号即成功。npm
(v9.0+)、yarn
或 pnpm
(推荐 pnpm
,速度快且节省磁盘空间)。bashmkdir vuepress-blog && cd vuepress-blognpm init -y # 生成 package.json
vuepress-theme-reco
):bashnpm install -D vuepress-theme-reco@2.xbashvuepress-blog/├── docs/ # 文档根目录│ ├── .vuepress/ # VuePress 配置目录│ │ ├── public/ # 静态资源(如图片、favicon)│ │ ├── config.js # 主配置文件│ │ └── styles/ # 自定义样式(可选)│ └── README.md # 首页或默认页面└── package.json
在 docs/README.md
中写入内容:
markdown# Hello VuePress!> 这是一个简单的 VuePress 博客示例。
package.json
添加启动脚本:
json{ "scripts": { "dev": "vuepress dev docs", "build": "vuepress build docs" }}
docs/.vuepress/config.js
)javascriptimport { defineUserConfig } from 'vuepress';import { defaultTheme } from '@vuepress/theme-default'; export default defineUserConfig({ lang: 'zh-CN', title: '我的博客', description: 'VuePress 搭建的静态博客', theme: defaultTheme({ logo: '/images/logo.png', // 左上角 logo navbar: [ // 顶部导航栏 { text: '首页', link: '/' }, { text: 'GitHub', link: 'https://github.com/yourname' } ], sidebar: { // 侧边栏配置 '/guide/': [ { text: '快速入门', link: '/guide/' }, { text: '进阶配置', link: '/guide/advanced.md' } ] } })});
docs/README.md
)使用 YAML 前缀自定义首页:
markdown---home: trueheroText: VuePress 博客tagline: 简洁、高效、Vue 驱动actions: - text: 开始阅读 → link: /guide/features: - title: Markdown 优先 details: 以 Markdown 为中心的项目结构,专注写作。 - title: Vue 驱动 details: 支持在 Markdown 中使用 Vue 组件。 - title: 高性能 details: 预渲染静态 HTML + SPA 加载。footer: MIT Licensed | Copyright © 2025-present YourName---
logo.png
、avatar.jpg
)放入 docs/.vuepress/public/images/
。以 vuepress-theme-reco
为例:
javascript// config.jsimport themeReco from 'vuepress-theme-reco'; export default defineUserConfig({ theme: themeReco({ style: '@vuepress-reco/style-default', logo: '/images/logo.png', nav: [ { text: '首页', link: '/' }, { text: '分类', link: '/categories/' } ], // 更多配置参考主题文档 })});
http://localhost:8080
查看效果。docs/.vuepress/dist/
。在 config.js
中添加:
javascriptexport default defineUserConfig({ base: '/repository-name/', // GitHub 仓库名(如 /vuepress-blog/)});
在项目根目录创建 .github/workflows/deploy.yml
:
yamlname: Deployon: push: branches: [main]jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: npm install - run: npm run build - name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v3 with: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./docs/.vuepress/dist
gh-pages
分支,目录为 /(root)
。https://yourname.github.io/repository-name/
。/images/logo.png
),并放在 public/
目录。docs/.vuepress/styles/index.scss
中自定义样式:scss:root { --accent-color: #42b983;}base
路径是否与仓库名一致。gh-pages
分支。docs/.vuepress/theme/Layout.vue
实现。Giscus
或 Valine
。config.js
中配置 head
标签:javascripthead: [ ['meta', { name: 'keywords', content: 'VuePress,博客,技术文档' }], ['link', { rel: 'icon', href: '/images/logo.png' }]]原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。