- 首页:`/` 未登录原地显示登录页(不再跳 /login),已登录直接进 `/dashboard`; 看板路由由 `/` 迁到 `/dashboard`,侧边栏「首页」与无权限回跳同步; 桌面端「工具 → 打开配置页」的 `/?token=` 免登录仍然可用 - 文档站:`/doc/**` 由后端整站生成静态 HTML(顶部分类 tab 全局切换、左侧栏目 → 内容页、 右侧本页目录);`/sitemap.txt` 新链接追加到末尾,下架 / 删除的会被清理 - 平台管理 · 文档管理(`/admin/doc`):分类 / 栏目 / 内容页(Markdown)简易 CMS, 内容页有编辑表单与列表,保存后自动重生成目录与静态页面 - 登录页右下角、顶栏(登录后)、看板右上角新增进入文档站的链接 - 部署:nginx 模板为 `/doc`、`/doc/`、`/sitemap.txt` 增加反代(否则会落到 SPA 回退); Vite dev server 增加同名代理;`middleware/frontend.js` 排除这些路径 - 登录页插画改为内联 SVG(沿用工作区已有的未提交改动) - 测试:新增 Markdown 渲染单测;后端 218 个用例通过,前端 typecheck / build 通过 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
74 lines
2.2 KiB
JavaScript
74 lines
2.2 KiB
JavaScript
'use strict';
|
||
|
||
const fs = require('fs');
|
||
const { Controller } = require('egg');
|
||
|
||
/**
|
||
* 文档站(公开,静态 HTML)
|
||
*
|
||
* 后端**不参与渲染**:`service/docSite` 在内容保存时把页面生成到 `run/doc-site`,
|
||
* 这里只做「路径 → 文件」的映射与缓存头控制。文件被人删了 / 刚部署上来时按需重新生成。
|
||
*
|
||
* - `GET /doc` → 301 到 `/doc/`
|
||
* - `GET /doc/…` → 生成好的 HTML(分类首页 `index.html` / 内容页 `<slug>.html`)
|
||
* - `GET /sitemap.txt` → 全站 URL(页面顺序稳定,新增的追加在末尾)
|
||
*/
|
||
class WebDocController extends Controller {
|
||
/** GET /doc(`/doc/` 会被 koa-router 的 strict:false 归到同一条路由,这里区分处理) */
|
||
async redirect() {
|
||
const { ctx } = this;
|
||
if (ctx.path === '/doc') {
|
||
ctx.redirect('/doc/');
|
||
return;
|
||
}
|
||
await this.page();
|
||
}
|
||
|
||
/** GET /doc/** */
|
||
async page() {
|
||
const { ctx } = this;
|
||
await ctx.service.docSite.ensureSite(ctx);
|
||
|
||
const file = ctx.service.docSite.resolveFile(this.relativePath(ctx.path));
|
||
if (!file || !this.isfile(file)) {
|
||
// 页面已经下架 / 改名,文件被清掉了:整站重生成一次再试
|
||
await ctx.service.docSite.generate(ctx);
|
||
if (!file || !this.isfile(file)) ctx.throw(404, '文档页面不存在');
|
||
}
|
||
|
||
ctx.set('Cache-Control', 'no-cache');
|
||
ctx.type = 'html';
|
||
ctx.body = fs.createReadStream(file);
|
||
}
|
||
|
||
/** GET /sitemap.txt */
|
||
async sitemap() {
|
||
const { ctx } = this;
|
||
await ctx.service.docSite.ensureSite(ctx);
|
||
|
||
const file = ctx.service.docSite.sitemapPath;
|
||
if (!this.isfile(file)) await ctx.service.docSite.generate(ctx);
|
||
|
||
ctx.set('Cache-Control', 'no-cache');
|
||
ctx.type = 'text/plain; charset=utf-8';
|
||
ctx.body = fs.createReadStream(file);
|
||
}
|
||
|
||
/** `/doc/guide/welcome.html` → `guide/welcome.html`;`/doc/guide/` → `guide/index.html` */
|
||
relativePath(pathname) {
|
||
const clean = String(pathname || '').replace(/^\/doc\/?/, '');
|
||
if (!clean) return 'index.html';
|
||
return clean.endsWith('/') ? `${clean}index.html` : clean;
|
||
}
|
||
|
||
isfile(file) {
|
||
try {
|
||
return fs.statSync(file).isFile();
|
||
} catch {
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
|
||
module.exports = WebDocController;
|