Files
ONE-OS/axhub-make/vite-plugins/virtualHtml/handlers/docsHtmlHandler.ts
王冕 a27e3b8e43 feat: sync full workspace including web modules, docs, and configurations to Gitea
Optimized the root .gitignore to exclude virtual environments, node modules,
and temp folders to ensure clean and lightweight version tracking.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-09 18:12:25 +08:00

46 lines
1.6 KiB
TypeScript

import type { IncomingMessage, ServerResponse } from 'http';
import fs from 'fs';
import path from 'path';
import { logVirtualHtmlDebug } from '../logger';
export function handleDocsHtml(req: IncomingMessage, res: ServerResponse, specTemplate: string): boolean {
if (!req.url?.includes('/docs.html')) {
return false;
}
const urlWithoutQuery = req.url.split('?')[0];
const urlPath = urlWithoutQuery.replace('/docs.html', '');
const pathParts = urlPath.split('/').filter(Boolean);
logVirtualHtmlDebug('Docs 请求路径:', req.url, '解析部分:', pathParts);
// 处理 /docs/* 的 docs.html 请求
if (pathParts.length >= 1 && pathParts[0] === 'docs') {
const docName = pathParts.slice(1).join('/');
const mdPath = path.resolve(process.cwd(), 'src/docs' + (docName ? '/' + docName : '') + '.md');
logVirtualHtmlDebug('检查 docs markdown 文件:', mdPath, '存在:', fs.existsSync(mdPath));
if (fs.existsSync(mdPath)) {
const title = `Docs: ${docName || 'Index'}`;
const specMdUrl = `${urlPath}.md`;
let html = specTemplate.replace(/\{\{TITLE\}\}/g, title);
html = html.replace(/\{\{SPEC_URL\}\}/g, specMdUrl);
html = html.replace(/\{\{DOCS_CONFIG\}\}/g, '[]');
html = html.replace(/\{\{MULTI_DOC\}\}/g, 'false');
logVirtualHtmlDebug('返回 Docs 虚拟 HTML:', req.url);
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.statusCode = 200;
res.end(html);
return true;
} else {
logVirtualHtmlDebug('docs markdown 不存在:', mdPath);
}
}
return false;
}