vitepress+github Pages笔记 2025/10/18
vscode配置git
打开VScode设置,搜索git.path ,打开 settings.json 文件,添加
"git.path": "D:\\codetools\\Git\\cmd\\git.exe"前期配置
- node.js
- pnpm
pnpm -v
node -v新建一个文件夹,在这个文件夹目录下打开cmd
pnpm add -D vitepress //安装vitepress
pnpm vitepress init //初始化vitepress用vscode打开此文件夹
pnpm run docs:dev美化
index.md文件
---
# https://vitepress.dev/reference/default-theme-home-page
layout: home
hero:
name: "我的文档网站"
# text: "A VitePress Site"
tagline: 持续学习,持续进步
image:
src: /background.png
alt: 背景图
# actions:
# - theme: brand
# text: Markdown 示例
# link: /markdown-examples
# - theme: alt
# text: API 示例
# link: /api-examples
features:
- title: 💡 小建议
details: 选择永远大于努力
- title: 🧗 努力
details: 如果做一件事就努力把它做好
- title: 🤔 思考
details: 学会思考,不要人云亦云
---config.mjs文件
import { defineConfig } from 'vitepress';
import { set_sidebar } from "./utils/auto-sidebar.mjs"; // 改成自己的路径,util文件是自己建的,在.vitepress目录下
// https://vitepress.dev/reference/site-config
export default defineConfig({
base: "/myvitepress/", //github仓库名
head: [["link", { rel: "icon", href: "/myvitepress/logo.png" }]],//页面标签图标
title: "我的文档网站",
description: "A VitePress Site",
themeConfig: {
sidebar: false, // 关闭侧边栏
aside: "left", // 设置右侧侧边栏在左侧显示
outlineTitle:"目录",
outline:[2,6],
logo: "logo.png", //这些图片放在public目录下,public文件是自己创建的
// 设置搜索框的样式
search: {
provider: "local",
options: {
translations: {
button: {
buttonText: "搜索文档",
buttonAriaLabel: "搜索文档",
},
modal: {
noResultsText: "无法找到相关结果",
resetButtonTitle: "清除查询条件",
footer: {
selectText: "选择",
navigateText: "切换",
},
},
},
},
},
// https://vitepress.dev/reference/default-theme-config
nav: [
{ text: "MD笔记",
items: [
{text:"vitepress", link: "/MD/Java/"},
]
},
],
// sidebar: [
// {
// text: 'Examples',
// items: [
// { text: 'Markdown 演示', link: '/markdown-examples' },
// { text: '运行 API 演示', link: '/api-examples' }
// ]
// },
// {
// text: 'Examples2',
// items: [
// { text: 'Markdown 演示2', link: '/markdown-examples' },
// { text: '运行 API 演示2', link: '/api-examples' }
// ]
// },
// ],
// sidebar: {
// "front-end/react": set_sidebar("/front-end/react"),
// "backend/rabbitmq": set_sidebar("/backend/rabbitmq"),
// },
socialLinks: [
{ icon: 'github', link: 'https://github.com/vuejs/vitepress' },
{icon: {
svg: '<svg t="1760773809273" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4556" width="200" height="200"><path d="M0 0m184.32 0l655.36 0q184.32 0 184.32 184.32l0 655.36q0 184.32-184.32 184.32l-655.36 0q-184.32 0-184.32-184.32l0-655.36q0-184.32 184.32-184.32Z" fill="#EC5D85" p-id="4557"></path><path d="M512 241.96096h52.224l65.06496-96.31744c49.63328-50.31936 89.64096 0.43008 63.85664 45.71136l-34.31424 51.5072c257.64864 5.02784 257.64864 43.008 257.64864 325.03808 0 325.94944 0 336.46592-404.48 336.46592S107.52 893.8496 107.52 567.90016c0-277.69856 0-318.80192 253.14304-324.95616l-39.43424-58.368c-31.26272-54.90688 37.33504-90.40896 64.68608-42.37312l60.416 99.80928c18.18624-0.0512 41.18528-0.0512 65.66912-0.0512z" fill="#EF85A7" p-id="4558"></path><path d="M512 338.5856c332.8 0 332.8 0 332.8 240.64s0 248.39168-332.8 248.39168-332.8-7.75168-332.8-248.39168 0-240.64 332.8-240.64z" fill="#EC5D85" p-id="4559"></path><path d="M281.6 558.08a30.72 30.72 0 0 1-27.47392-16.97792 30.72 30.72 0 0 1 13.73184-41.216l122.88-61.44a30.72 30.72 0 0 1 41.216 13.74208 30.72 30.72 0 0 1-13.74208 41.216l-122.88 61.44a30.59712 30.59712 0 0 1-13.73184 3.23584zM752.64 558.08a30.60736 30.60736 0 0 1-12.8512-2.83648l-133.12-61.44a30.72 30.72 0 0 1-15.04256-40.7552 30.72 30.72 0 0 1 40.76544-15.02208l133.12 61.44A30.72 30.72 0 0 1 752.64 558.08zM454.656 666.88a15.36 15.36 0 0 1-12.288-6.1952 15.36 15.36 0 0 1 3.072-21.49376l68.5056-50.91328 50.35008 52.62336a15.36 15.36 0 0 1-22.20032 21.23776l-31.5904-33.024-46.71488 34.72384a15.28832 15.28832 0 0 1-9.13408 3.04128z" fill="#EF85A7" p-id="4560"></path><path d="M65.536 369.31584c15.03232 101.90848 32.84992 147.17952 44.544 355.328 14.63296 2.18112 177.70496 10.04544 204.05248-74.62912a16.14848 16.14848 0 0 0 1.64864-10.87488c-30.60736-80.3328-169.216-60.416-169.216-60.416s-10.36288-146.50368-11.49952-238.83776zM362.25024 383.03744l34.816 303.17568h34.64192L405.23776 381.1328zM309.52448 536.28928h45.48608l16.09728 158.6176-31.82592 1.85344zM446.86336 542.98624h45.80352V705.3312h-33.87392zM296.6016 457.97376h21.39136l5.2736 58.99264-18.91328 2.26304zM326.99392 457.97376h21.39136l2.53952 55.808-17.408 1.61792zM470.62016 459.88864h19.456v62.27968h-19.456zM440.23808 459.88864h22.20032v62.27968h-16.62976z" fill="#FFFFFF" p-id="4561"></path><path d="M243.56864 645.51936a275.456 275.456 0 0 1-28.4672 23.74656 242.688 242.688 0 0 1-29.53216 17.52064 2.70336 2.70336 0 0 1-4.4032-1.95584 258.60096 258.60096 0 0 1-5.12-29.57312c-1.41312-12.1856-1.95584-25.68192-2.16064-36.36224 0-0.3072 0-2.5088 3.01056-1.90464a245.92384 245.92384 0 0 1 34.22208 9.5744 257.024 257.024 0 0 1 32.3584 15.17568c0.52224 0.256 2.51904 1.4848 0.09216 3.77856z" fill="#EB5480" p-id="4562"></path><path d="M513.29024 369.31584c15.03232 101.90848 32.84992 147.17952 44.544 355.328 14.63296 2.18112 177.70496 10.04544 204.05248-74.62912a16.14848 16.14848 0 0 0 1.64864-10.87488c-30.60736-80.3328-169.216-60.416-169.216-60.416s-10.36288-146.50368-11.49952-238.83776zM810.00448 383.03744l34.816 303.17568h34.64192L852.992 381.1328zM757.27872 536.28928h45.48608l16.09728 158.6176-31.82592 1.85344zM894.6176 542.98624h45.80352V705.3312H906.5472zM744.35584 457.97376h21.39136l5.2736 58.99264-18.91328 2.26304zM774.74816 457.97376h21.39136l2.53952 55.808-17.408 1.61792zM918.3744 459.88864h19.456v62.27968h-19.456zM887.99232 459.88864h22.20032v62.27968h-16.62976z" fill="#FFFFFF" p-id="4563"></path><path d="M691.32288 645.51936a275.456 275.456 0 0 1-28.4672 23.74656 242.688 242.688 0 0 1-29.53216 17.52064 2.70336 2.70336 0 0 1-4.4032-1.95584 258.60096 258.60096 0 0 1-5.12-29.57312c-1.41312-12.1856-1.95584-25.68192-2.16064-36.36224 0-0.3072 0-2.5088 3.01056-1.90464a245.92384 245.92384 0 0 1 34.22208 9.5744 257.024 257.024 0 0 1 32.3584 15.17568c0.52224 0.256 2.51904 1.4848 0.09216 3.77856z" fill="#EB5480" p-id="4564"></path></svg>'
},
link: "https://www.bilibili.com/",
},
],
footer: {
copyright: "Copyright@ 2023 cnm",
},
}
})auto-sidebar.mjs文件 这是自动生成侧边栏的脚本
import path from "node:path";
import fs from "node:fs";
// 文件根目录
const DIR_PATH = path.resolve();
// 白名单,过滤不是文章的文件和文件夹
const WHITE_LIST = [
"index.md",
".vitepress",
"node_modules",
".idea",
"assets",
];
// 判断是否是文件夹
const isDirectory = (path) => fs.lstatSync(path).isDirectory();
// 取差值
const intersections = (arr1, arr2) =>
Array.from(new Set(arr1.filter((item) => !new Set(arr2).has(item))));
// 把方法导出直接使用
function getList(params, path1, pathname) {
// 存放结果
const res = [];
// 开始遍历params
for (let file in params) {
// 拼接目录
const dir = path.join(path1, params[file]);
// 判断是否是文件夹
const isDir = isDirectory(dir);
if (isDir) {
// 如果是文件夹,读取之后作为下一次递归参数
const files = fs.readdirSync(dir);
res.push({
text: params[file],
collapsible: true,
items: getList(files, dir, `${pathname}/${params[file]}`),
});
} else {
// 获取名字
const name = path.basename(params[file]);
// 排除非 md 文件
const suffix = path.extname(params[file]);
if (suffix !== ".md") {
continue;
}
res.push({
text: name,
link: `${pathname}/${name}`,
});
}
}
// 对name做一下处理,把后缀删除
res.map((item) => {
item.text = item.text.replace(/\.md$/, "");
});
return res;
}
export const set_sidebar = (pathname) => {
// 获取pathname的路径
const dirPath = path.join(DIR_PATH, pathname);
// 读取pathname下的所有文件或者文件夹
const files = fs.readdirSync(dirPath);
// 过滤掉
const items = intersections(files, WHITE_LIST);
// getList 函数后面会讲到
return getList(items, dirPath, pathname);
};在.vitepress theme style.css中配置下面的css
/* 自定义侧边栏在最左边,右边撑满宽度 */
.VPDoc .container {
margin: 0 !important;
}
@media (min-width: 960px) {
.VPDoc:not(.has-sidebar) .content {
max-width: 1552px !important;
}
}
.VPDoc.has-aside .content-container {
max-width: 1488px !important;
}
@media (min-width: 960px) {
.VPDoc:not(.has-sidebar) .container {
display: flex;
justify-content: center;
max-width: 1562px !important;
}
}
.aside-container {
position: fixed;
top: 0;
padding-top: calc(
var(--vp-nav-height) + var(--vp-layout-top-height, 0px) +
var(--vp-doc-top-height, 0px) + 10px
) !important;
width: 224px;
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
scrollbar-width: none;
}
/* 自定义h2的间距 */
.vp-doc h2 {
margin: 0px 0 16px;
padding-top: 24px;
border: none;
}设置GitHub pages
需要在config.mjs里面配置base,名称为github仓库名称,注意不要忘记改之前的icon
base: "/docs-demo/"初始化git仓库 git init 在文件夹里创建.gitignore文件,添加如下内容
node_modules
.DS_Store
dist
dist-ssr
cache
.cache
.temp
*.local添加本地所有文件到git仓库
git add .创建第一次提交
git commit -m "first commit"添加远程仓库地址到本地,注意这个网址是自己的地址
git remote add origin https://github.com/AZCodingAccount/Demo.git推送项目到github git push -u origin master
打开仓库->settings->pages->选择github actions
下面设置工作流 Actions->选择设置你的工作流 重命名并设置deploy脚本
name: Deploy VitePress site to Pages
on:
push:
branches: [master]
# 设置tokenn访问权限
permissions:
contents: read
pages: write
id-token: write
# 只允许同时进行一次部署,跳过正在运行和最新队列之间的运行队列
# 但是,不要取消正在进行的运行,因为我们希望允许这些生产部署完成
concurrency:
group: pages
cancel-in-progress: false
jobs:
# 构建工作
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0 # 如果未启用 lastUpdated,则不需要
- name: Setup pnpm
uses: pnpm/action-setup@v2 # 安装pnpm并添加到环境变量
with:
version: 8.6.12 # 指定需要的 pnpm 版本
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18
cache: pnpm # 设置缓存
- name: Setup Pages
uses: actions/configure-pages@v3 # 在工作流程自动配置GithubPages
- name: Install dependencies
run: pnpm install # 安装依赖
- name: Build with VitePress
run: |
pnpm run docs:build # 启动项目
touch .nojekyll # 通知githubpages不要使用Jekyll处理这个站点,不知道为啥不生效,就手动搞了
- name: Upload artifact
uses: actions/upload-pages-artifact@v3 # 上传构建产物
with:
path: .vitepress/dist # 指定上传的路径,当前是根目录,如果是docs需要加docs/的前缀
# 部署工作
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }} # 从后续的输出中获取部署后的页面URL
needs: build # 在build后面完成
runs-on: ubuntu-latest # 运行在最新版本的ubuntu系统上
name: Deploy
steps:
- name: Deploy to GitHub Pages
id: deployment # 指定id
uses: actions/deploy-pages@v4 # 将之前的构建产物部署到github pages中若自定义域名另外需要把base(GitHub仓库地址)给注释掉(不然css文件和页面都找不到)
补充
如果你想要配置mermaid支持(这是一个可以使用md语法绘制流程图,饼状图的md扩展),需要按照下面的步骤操作。 安装
npm i vitepress-plugin-mermaid mermaid -D如果使用pnpm,还需要下面的配置改变pnpm的默认行为兼容插件
pnpm install --shamefully-hoist
# 或者在根目录新建.npmrc文件,配置
shamefully-hoist=true更改.vitepress/config.mjs配置项 导包
import { withMermaid } from "vitepress-plugin-mermaid";修改defineConfig—>withMermaid 根配置项下添加
mermaid: {
// refer https://mermaid.js.org/config/setup/modules/mermaidAPI.html#mermaidapi-configuration-defaults for options
},
mermaidPlugin: {
class: "mermaid my-class", // set additional css classes for parent container
},使用到的工具
icon网站 vscode与GitHub仓库connect失败,无法push 解决: 方法1-使用代理了
- 配置 Git 使用代理,设置 > 网络与互联网 > 代理,找到代理设置,并记录当前代理端口。假设端口号为 1234。
git config --global http.proxy http://127.0.0.1:1234
git config --global https.proxy http://127.0.0.1:1234- 验证代理设置是否生效
git config --global -l - 刷新 DNS 缓存
-windows用户
ipconfig /flushdns
-mac用户
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder方法2-没使用代理
- 取消 Git 配置的代理
git config --global --unset http.proxy
git config --global --unset https.proxy- 验证代理设置已成功移除
git config --global -l