Skip to content

Commit 19553ee

Browse files
committed
Setup Python code notes site with VitePress
1 parent aea2bbd commit 19553ee

5 files changed

Lines changed: 2697 additions & 1 deletion

File tree

‎.github/workflows/deploy-docs.yml‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Deploy VitePress to GitHub Pages
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
pages: write
11+
id-token: write
12+
13+
concurrency:
14+
group: "pages"
15+
cancel-in-progress: false
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v4
23+
24+
- name: Setup Node
25+
uses: actions/setup-node@v4
26+
with:
27+
node-version: 20
28+
cache: npm
29+
30+
- name: Install dependencies
31+
run: npm install
32+
33+
- name: Expand py-symbol blocks
34+
run: npm run docs:prebuild
35+
36+
- name: Build VitePress site
37+
run: npm run docs:build
38+
39+
- name: Upload artifact
40+
uses: actions/upload-pages-artifact@v3
41+
with:
42+
path: docs_generated/.vitepress/dist
43+
44+
deploy:
45+
needs: build
46+
runs-on: ubuntu-latest
47+
environment:
48+
name: github-pages
49+
url: ${{ steps.deployment.outputs.page_url }}
50+
steps:
51+
- name: Deploy to GitHub Pages
52+
id: deployment
53+
uses: actions/deploy-pages@v4
54+

‎.gitignore‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,10 @@ cython_debug/
205205
marimo/_static/
206206
marimo/_lsp/
207207
__marimo__/
208+
209+
# Node / frontend tooling
210+
node_modules/
211+
212+
# VitePress generated docs (build artifacts)
213+
docs_generated/
214+

‎README.md‎

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,122 @@
1-
# Code-Notes-Templete
1+
# Code-Notes-Templete
2+
3+
一个用于「边写代码边记笔记」的模板仓库,核心特点:
4+
5+
- **笔记用 Markdown 写在 `docs/` 下**
6+
- **Python 示例代码写在 `.py` 文件里(如 `python-demos/`)**
7+
- **在 Markdown 中通过自定义指令引用 `.py` 里的函数/类,构建时自动插入源码**
8+
9+
最终通过 **VitePress** 构建成一个静态网站,可以部署到 GitHub Pages 上浏览。
10+
11+
---
12+
13+
## 项目结构
14+
15+
大致目录结构如下:
16+
17+
- `docs/`:你亲自编写的 Markdown 笔记
18+
- `index.md`:首页
19+
- `.vitepress/config.ts`:VitePress 配置(导航、侧边栏、`base` 等)
20+
- `python-basics/default_args.md`:示例笔记,演示如何引用 Python 函数源码
21+
- `python-demos/`:Python 示例代码
22+
- `basics/default_args.py`:包含 `append_item` 函数的示例文件
23+
- `scripts/`:构建前的预处理脚本
24+
- `extract_snippet.py`:从 `.py` 文件中按函数/类名提取源码
25+
- `expand_py_symbols.py`:扫描 `docs/**/*.md`,将自定义指令替换为真正的代码块,生成到 `docs_generated/`
26+
- `docs_generated/`:由 `expand_py_symbols.py` 自动生成的 Markdown(不会提交到 Git)
27+
- `.github/workflows/deploy-docs.yml`:GitHub Pages 部署 workflow
28+
29+
---
30+
31+
## Markdown 中如何引用 `.py` 文件里的函数/类
32+
33+
在你自己的笔记中,只需要写一段自定义块指令 `py-symbol`,**不需要粘贴任何 Python 源码**:
34+
35+
```markdown
36+
::: py-symbol
37+
path: python-demos/basics/default_args.py
38+
symbol: append_item
39+
type: function # function 或 class
40+
:::
41+
```
42+
43+
含义说明:
44+
45+
- `path`:相对于仓库根目录的 `.py` 文件路径
46+
- `symbol`:要展示的函数名或类名(必须是顶层定义)
47+
- `type`:`function` 或 `class`
48+
49+
在构建前,会运行脚本:
50+
51+
```bash
52+
npm run docs:prebuild
53+
```
54+
55+
该脚本会:
56+
57+
1. 遍历 `docs/**/*.md`
58+
2. 找到所有 `::: py-symbol ... :::` 区块
59+
3. 用 `scripts/extract_snippet.py` 从对应 `.py` 文件中提取目标函数/类源码
60+
4. 在生成目录 `docs_generated/` 中,把这些指令替换成真正的 ```python 代码块
61+
62+
VitePress 最终使用 `docs_generated/` 作为内容源进行构建。
63+
64+
---
65+
66+
## 本地开发与预览
67+
68+
前置条件:已安装 Node.js(推荐 18+,本仓库使用 20 测试)和 Python 3.8+。
69+
70+
```bash
71+
# 安装依赖
72+
npm install
73+
74+
# 预处理 Markdown:从 .py 中提取代码到 docs_generated/
75+
npm run docs:prebuild
76+
77+
# 本地开发预览(默认 http://localhost:5173)
78+
npm run docs:dev
79+
80+
# 或直接构建静态站点(输出到 docs_generated/.vitepress/dist)
81+
npm run docs:build
82+
```
83+
84+
日常编辑建议流程:
85+
86+
1. 在 `python-demos/` 中新增或修改 Python 示例代码
87+
2. 在 `docs/` 中新增/修改 Markdown 笔记,并用 `py-symbol` 指令引用函数/类
88+
3. 运行 `npm run docs:prebuild && npm run docs:dev` 本地预览效果
89+
90+
---
91+
92+
## 部署到 GitHub Pages
93+
94+
仓库示例地址:`https://github.com/Systems-Intelligent-Lab/Code-Notes-Templete`
95+
96+
GitHub Pages 访问地址形如:
97+
98+
```text
99+
https://systems-intelligent-lab.github.io/Code-Notes-Templete/
100+
```
101+
102+
关键点:
103+
104+
- 在 `docs/.vitepress/config.ts` 中已经设置:
105+
106+
```ts
107+
base: '/Code-Notes-Templete/'
108+
```
109+
110+
- `.github/workflows/deploy-docs.yml` 会在推送到 `main` 分支时:
111+
1. 安装依赖
112+
2. 运行 `npm run docs:prebuild`
113+
3. 运行 `npm run docs:build`
114+
4. 将 `docs_generated/.vitepress/dist` 部署到 GitHub Pages
115+
116+
你需要在 GitHub 仓库中:
117+
118+
1. 打开 `Settings → Pages`
119+
2. 将 **Source** 设置为 **GitHub Actions**
120+
3. 推送代码到 `main`,等待 Actions 完成构建与部署
121+
122+
部署成功后,`Settings → Pages` 页面会显示站点访问链接。

‎docs/.vitepress/config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineConfig } from 'vitepress'
22

33
export default defineConfig({
4+
base: '/Code-Notes-Templete/',
45
title: 'Code Notes',
56
description: 'Python 代码笔记与示例',
67
lang: 'zh-CN',

0 commit comments

Comments
 (0)