This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
CJYDocs 是一个基于 Node.js + Express 的轻量级 Markdown 文档管理和渲染系统,采用前后端分离架构。核心特点是通过解析 docs/index.md 配置文件实现文档的层级化管理。
npm start # 生产模式启动
npm run dev # 开发模式启动(使用 nodemon 自动重启)
服务器默认运行在 http://localhost:3000
npm install
前端 (public/)
↓ REST API
后端 (server.js)
↓ File I/O
数据层 (docs/)
关键文件: docs/index.md
系统的文档结构完全由 index.md 定义,格式如下:
[分类名称]
1: 文档名.md
2: 文档名.md
2.1: 子文档名.md
2.2: 子文档名.md
解析逻辑 (server.js:200-236):
[分类名] → 对应 docs/ 下的目录名编号: 文档名.md → 文档项,支持多级编号(如 1.1.1)level 通过点号数量计算:1 = level 0, 1.1 = level 1, 1.1.1 = level 2{ name, docs: [{ number, name, level, fullName }] }重要:添加新文档时:
docs/分类名/ 下创建 .md 文件docs/index.md 中添加对应条目DOM 缓存策略 (reader.js:5-13):
const DOM = {
loading: null,
content: null,
docNav: null,
toc: null,
leftSidebar: null,
rightSidebar: null
};
所有频繁访问的 DOM 元素都通过此对象缓存,避免重复查询。
事件委托模式:
#doc-nav 父元素上监听,而非每个导航项#toc 父元素上监听所有 API 都在 server.js 中定义:
GET /api/structure - 获取完整文档结构(解析 index.md)GET /api/category/:category - 获取指定分类信息GET /api/doc/:category/:docName - 获取文档内容GET /api/search/:category?q=xxx¤tDoc=yyy - 搜索文档安全机制:
所有用户输入都经过 sanitizePath() 清理和 validatePath() 验证,防止路径遍历攻击。
使用 marked.js + highlight.js,配置在 reader.js:15-29
reader.js:176-221 - 遍历渲染后的 HTML,提取所有 h1-h6 标题,使用 IntersectionObserver 实现滚动监听
.md 文件,返回匹配的行号和上下文片段TreeWalker API 精确定位到匹配的文本节点并滚动localStorage,按分类隔离IntersectionObserver:替代 scroll 事件,性能提升 20-30%cjydocs/
├── server.js # Express 后端,包含所有 API 和 index.md 解析逻辑
├── docs/
│ ├── index.md # 文档结构配置文件(核心)
│ └── 分类名/
│ └── 文档.md # 实际文档内容
└── public/
├── index.html # 首页(显示分类列表)
├── reader.html # 阅读器页面(三栏布局)
├── css/style.css # 统一样式
└── js/
├── index.js # 首页逻辑
└── reader.js # 阅读器逻辑(DOM缓存、事件委托、搜索)
server.js 中定义新路由sanitizePath() 清理所有用户输入validatePath() 验证文件路径在 docs/ 目录内DOM 缓存对象获取元素classList.toggle() 简化条件判断docs/index.md.md 文件路径遍历防护:
sanitizePath() - 移除 ../、/、\ 等危险字符validatePath() - 确保解析后的路径在 docs/ 目录内示例:
const category = sanitizePath(req.params.category);
const docPath = path.join(docsDir, category, `${docName}.md`);
if (!validatePath(docPath, docsDir)) {
return res.status(403).json({ error: '拒绝访问' });
}
检查:
docs/index.md 中是否有对应条目index.md 中的名称完全一致(区分大小写)检查:
检查:
DOM 缓存对象)