|
|
<!DOCTYPE html> |
|
|
<html lang="zh-CN"> |
|
|
<head> |
|
|
<meta charset="UTF-8" /> |
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
|
|
<title>文档管理系统</title> |
|
|
<style> |
|
|
* { margin: 0; padding: 0; box-sizing: border-box; } |
|
|
body { |
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; |
|
|
background: #f8fafc; |
|
|
color: #1e293b; |
|
|
line-height: 1.6; |
|
|
padding: 24px; |
|
|
} |
|
|
a { color: #2563eb; text-decoration: none; } |
|
|
a:hover { text-decoration: underline; } |
|
|
.container { |
|
|
max-width: 1200px; |
|
|
margin: 0 auto; |
|
|
background: #ffffff; |
|
|
border-radius: 16px; |
|
|
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06); |
|
|
padding: 32px 40px 48px; |
|
|
position: relative; |
|
|
} |
|
|
.breadcrumb { |
|
|
font-size: 14px; |
|
|
color: #64748b; |
|
|
margin-bottom: 24px; |
|
|
display: flex; |
|
|
align-items: center; |
|
|
gap: 8px; |
|
|
flex-wrap: wrap; |
|
|
} |
|
|
.breadcrumb a { color: #2563eb; } |
|
|
.breadcrumb .sep { color: #94a3b8; } |
|
|
.doc-grid { |
|
|
display: grid; |
|
|
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); |
|
|
gap: 20px; |
|
|
margin-top: 12px; |
|
|
} |
|
|
.doc-card { |
|
|
background: #f1f5f9; |
|
|
border-radius: 12px; |
|
|
padding: 20px 24px; |
|
|
border: 1px solid #e2e8f0; |
|
|
transition: background 0.2s, transform 0.1s; |
|
|
display: block; |
|
|
text-decoration: none; |
|
|
color: inherit; |
|
|
} |
|
|
.doc-card:hover { |
|
|
background: #e6edf6; |
|
|
transform: translateY(-2px); |
|
|
border-color: #bfdbfe; |
|
|
} |
|
|
.doc-card .name { |
|
|
font-size: 18px; |
|
|
font-weight: 600; |
|
|
color: #0f172a; |
|
|
display: flex; |
|
|
align-items: center; |
|
|
gap: 8px; |
|
|
} |
|
|
.doc-card .desc { |
|
|
font-size: 14px; |
|
|
color: #475569; |
|
|
margin-top: 4px; |
|
|
} |
|
|
.doc-card .meta { |
|
|
display: flex; |
|
|
flex-wrap: wrap; |
|
|
gap: 12px; |
|
|
margin-top: 10px; |
|
|
font-size: 13px; |
|
|
color: #64748b; |
|
|
} |
|
|
.doc-card .meta span { |
|
|
background: #ffffffcc; |
|
|
padding: 2px 12px; |
|
|
border-radius: 20px; |
|
|
border: 1px solid #d1d5db; |
|
|
} |
|
|
.badge { |
|
|
display: inline-block; |
|
|
font-size: 12px; |
|
|
background: #dbeafe; |
|
|
color: #1d4ed8; |
|
|
padding: 2px 12px; |
|
|
border-radius: 20px; |
|
|
font-weight: 500; |
|
|
} |
|
|
|
|
|
/* ----- 搜索框 ----- */ |
|
|
.search-box { |
|
|
margin: 16px 0 24px; |
|
|
width: 100%; |
|
|
max-width: 400px; |
|
|
padding: 10px 16px; |
|
|
border: 2px solid #e2e8f0; |
|
|
border-radius: 8px; |
|
|
font-size: 16px; |
|
|
outline: none; |
|
|
transition: border-color 0.2s; |
|
|
} |
|
|
.search-box:focus { |
|
|
border-color: #3b82f6; |
|
|
} |
|
|
.search-box::placeholder { |
|
|
color: #94a3b8; |
|
|
} |
|
|
.search-results-count { |
|
|
font-size: 14px; |
|
|
color: #64748b; |
|
|
margin-left: 8px; |
|
|
} |
|
|
|
|
|
/* ----- 文档阅读页:左侧目录 + 右侧正文布局 ----- */ |
|
|
.doc-layout { |
|
|
display: flex; |
|
|
gap: 30px; |
|
|
margin-top: 8px; |
|
|
} |
|
|
.doc-sidebar { |
|
|
flex: 0 0 240px; |
|
|
position: sticky; |
|
|
top: 24px; |
|
|
max-height: calc(100vh - 80px); |
|
|
overflow-y: auto; |
|
|
padding-right: 12px; |
|
|
border-right: 1px solid #e2e8f0; |
|
|
display: none; /* 默认隐藏,在宽屏显示 */ |
|
|
} |
|
|
.doc-sidebar h4 { |
|
|
font-size: 14px; |
|
|
color: #475569; |
|
|
margin-bottom: 12px; |
|
|
font-weight: 600; |
|
|
} |
|
|
.doc-sidebar ul { |
|
|
list-style: none; |
|
|
padding-left: 0; |
|
|
} |
|
|
.doc-sidebar li { |
|
|
margin: 4px 0; |
|
|
} |
|
|
.doc-sidebar a { |
|
|
display: block; |
|
|
font-size: 13px; |
|
|
color: #475569; |
|
|
padding: 4px 8px; |
|
|
border-radius: 4px; |
|
|
transition: background 0.2s, color 0.2s; |
|
|
text-decoration: none; |
|
|
white-space: nowrap; |
|
|
overflow: hidden; |
|
|
text-overflow: ellipsis; |
|
|
} |
|
|
.doc-sidebar a:hover { |
|
|
background: #eef2f6; |
|
|
color: #1e293b; |
|
|
} |
|
|
.doc-sidebar a.active { |
|
|
color: #2563eb; |
|
|
background: #dbeafe; |
|
|
} |
|
|
.doc-sidebar .level-2 { padding-left: 16px; font-size: 12px; } |
|
|
.doc-sidebar .level-3 { padding-left: 32px; font-size: 12px; color: #94a3b8; } |
|
|
.doc-main { |
|
|
flex: 1; |
|
|
min-width: 0; /* 防止 Flex 溢出 */ |
|
|
} |
|
|
@media (min-width: 1024px) { |
|
|
.doc-sidebar { display: block; } |
|
|
} |
|
|
@media (max-width: 1024px) { |
|
|
.doc-layout { flex-direction: column; } |
|
|
.doc-sidebar { |
|
|
position: static; |
|
|
max-height: none; |
|
|
border-right: none; |
|
|
border-bottom: 1px solid #e2e8f0; |
|
|
margin-bottom: 16px; |
|
|
padding-bottom: 12px; |
|
|
display: block; /* 在窄屏依然显示,但改为水平排列 */ |
|
|
} |
|
|
.doc-sidebar ul { |
|
|
display: flex; |
|
|
flex-wrap: wrap; |
|
|
gap: 8px; |
|
|
} |
|
|
.doc-sidebar li { margin: 0; } |
|
|
.doc-sidebar a { white-space: nowrap; } |
|
|
.doc-sidebar .level-2, .doc-sidebar .level-3 { padding-left: 0; } |
|
|
} |
|
|
|
|
|
.markdown-body { |
|
|
font-size: 16px; |
|
|
color: #1e293b; |
|
|
} |
|
|
.markdown-body h1 { |
|
|
font-size: 28px; |
|
|
font-weight: 700; |
|
|
margin: 0 0 12px 0; |
|
|
padding-bottom: 8px; |
|
|
border-bottom: 2px solid #e2e8f0; |
|
|
} |
|
|
.markdown-body h2 { |
|
|
font-size: 22px; |
|
|
font-weight: 600; |
|
|
margin: 28px 0 12px 0; |
|
|
} |
|
|
.markdown-body h3 { |
|
|
font-size: 18px; |
|
|
font-weight: 600; |
|
|
margin: 20px 0 10px 0; |
|
|
} |
|
|
.markdown-body p { margin: 12px 0; } |
|
|
.markdown-body ul, .markdown-body ol { margin: 8px 0 8px 24px; } |
|
|
.markdown-body li { margin: 4px 0; } |
|
|
.markdown-body blockquote { |
|
|
border-left: 4px solid #3b82f6; |
|
|
padding: 8px 16px; |
|
|
background: #f1f5f9; |
|
|
margin: 16px 0; |
|
|
border-radius: 0 8px 8px 0; |
|
|
} |
|
|
.markdown-body code { |
|
|
background: #f1f5f9; |
|
|
padding: 2px 8px; |
|
|
border-radius: 4px; |
|
|
font-size: 14px; |
|
|
} |
|
|
.markdown-body pre { |
|
|
background: #1e293b; |
|
|
color: #e2e8f0; |
|
|
padding: 16px; |
|
|
border-radius: 10px; |
|
|
overflow-x: auto; |
|
|
margin: 16px 0; |
|
|
} |
|
|
.markdown-body pre code { |
|
|
background: transparent; |
|
|
padding: 0; |
|
|
color: inherit; |
|
|
} |
|
|
.markdown-body img { |
|
|
max-width: 100%; |
|
|
height: auto; |
|
|
border-radius: 10px; |
|
|
margin: 16px 0; |
|
|
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06); |
|
|
display: block; |
|
|
} |
|
|
.markdown-body table { |
|
|
width: 100%; |
|
|
border-collapse: collapse; |
|
|
margin: 16px 0; |
|
|
font-size: 14px; |
|
|
} |
|
|
.markdown-body th, .markdown-body td { |
|
|
border: 1px solid #e2e8f0; |
|
|
padding: 8px 12px; |
|
|
text-align: left; |
|
|
} |
|
|
.markdown-body th { background: #f1f5f9; font-weight: 600; } |
|
|
.loading { text-align: center; padding: 60px 0; color: #64748b; } |
|
|
.error { text-align: center; padding: 40px 0; color: #ef4444; } |
|
|
.text-muted { color: #64748b; font-size: 14px; } |
|
|
|
|
|
/* ----- 回到顶部按钮 ----- */ |
|
|
.back-to-top { |
|
|
position: fixed; |
|
|
bottom: 30px; |
|
|
right: 30px; |
|
|
width: 44px; |
|
|
height: 44px; |
|
|
background: #2563eb; |
|
|
color: #ffffff; |
|
|
border: none; |
|
|
border-radius: 50%; |
|
|
font-size: 20px; |
|
|
cursor: pointer; |
|
|
box-shadow: 0 2px 8px rgba(37, 99, 235, 0.3); |
|
|
transition: opacity 0.3s, transform 0.2s; |
|
|
opacity: 0; |
|
|
visibility: hidden; |
|
|
z-index: 999; |
|
|
display: flex; |
|
|
align-items: center; |
|
|
justify-content: center; |
|
|
text-decoration: none; |
|
|
} |
|
|
.back-to-top.show { |
|
|
opacity: 1; |
|
|
visibility: visible; |
|
|
} |
|
|
.back-to-top:hover { |
|
|
transform: translateY(-2px); |
|
|
background: #1d4ed8; |
|
|
} |
|
|
@media (max-width: 640px) { |
|
|
.back-to-top { |
|
|
bottom: 20px; |
|
|
right: 20px; |
|
|
width: 40px; |
|
|
height: 40px; |
|
|
font-size: 18px; |
|
|
} |
|
|
} |
|
|
|
|
|
/* ----- 搜索结果高亮 ----- */ |
|
|
.highlight { background-color: #fef08a; padding: 0 2px; } |
|
|
</style> |
|
|
</head> |
|
|
<body> |
|
|
<div class="container" id="app"> |
|
|
<div class="loading">⏳ 加载中...</div> |
|
|
</div> |
|
|
|
|
|
<!-- 回到顶部按钮(HTML结构直接放在 body 内) --> |
|
|
<a href="#" class="back-to-top" id="backToTopBtn" title="回到顶部">↑</a> |
|
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"> |
|
|
</script> |
|
|
<script src="https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js"> |
|
|
</script> |
|
|
<script> |
|
|
// ============================================================ |
|
|
// 1. Mermaid 初始化(深色主题美化版) |
|
|
// ============================================================ |
|
|
mermaid.initialize({ |
|
|
startOnLoad: false, |
|
|
theme: 'base', |
|
|
flowchart: { |
|
|
useMaxWidth: true, |
|
|
curve: 'basis', |
|
|
padding: 20, |
|
|
htmlLabels: true |
|
|
}, |
|
|
themeVariables: { |
|
|
// ---------- 全局背景 ---------- |
|
|
'background': '#1e293b', // 整体背景(深灰蓝,接近您截图) |
|
|
|
|
|
// ---------- 流程图样式 ---------- |
|
|
'lineColor': '#93c5fd', // 线条颜色(亮蓝色,清晰) |
|
|
'lineWidth': '2px', |
|
|
'edgeLabelBackground': '#1e293b', // 连线标签背景(与背景同色,挡住线) |
|
|
|
|
|
'nodeBorder': '#93c5fd', // 节点边框(亮蓝色) |
|
|
'mainBkg': '#334155', // 节点背景(比整体背景稍亮) |
|
|
'nodeTextColor': '#f8fafc', // 节点文字(白色) |
|
|
'tertiaryColor': '#475569', // 次要颜色(子图背景等) |
|
|
|
|
|
// ---------- 时序图样式 ---------- |
|
|
'actorBkg': '#1e293b', // 参与者背景(与整体背景一致) |
|
|
'actorBorder': '#93c5fd', // 参与者边框(亮蓝色) |
|
|
'actorTextColor': '#f8fafc', // 参与者文字(白色) |
|
|
'actorLineColor': '#94a3b8', // 参与者竖线(浅灰) |
|
|
|
|
|
'signalColor': '#93c5fd', // 消息线颜色(亮蓝色) |
|
|
'signalTextColor': '#f8fafc', // 消息文字颜色(白色) |
|
|
|
|
|
'loopTextColor': '#f8fafc', // 循环/备注文字 |
|
|
'loopBorder': '#64748b', // 循环边框 |
|
|
'loopLineColor': '#64748b', // 循环线颜色 |
|
|
|
|
|
'noteBkgColor': '#334155', // 备注背景(与节点背景一致) |
|
|
'noteBorderColor': '#64748b', // 备注边框 |
|
|
'noteTextColor': '#f8fafc' // 备注文字 |
|
|
} |
|
|
}); |
|
|
// ============================================================ |
|
|
// 2. 全局变量 & 工具函数 |
|
|
// ============================================================ |
|
|
const app = document.getElementById('app'); |
|
|
const backToTopBtn = document.getElementById('backToTopBtn'); |
|
|
|
|
|
function getPath() { return window.location.pathname; } |
|
|
|
|
|
function getDocNameFromPath(path) { |
|
|
const match = path.match(/^\/view\/(.+)$/); |
|
|
return match ? match[1] : null; |
|
|
} |
|
|
|
|
|
function viewLink(filename) { return '/view/' + encodeURIComponent(filename); } |
|
|
|
|
|
// 滚动回到顶部按钮逻辑 |
|
|
function handleScroll() { |
|
|
if (window.scrollY > 300) { |
|
|
backToTopBtn.classList.add('show'); |
|
|
} else { |
|
|
backToTopBtn.classList.remove('show'); |
|
|
} |
|
|
} |
|
|
window.addEventListener('scroll', handleScroll); |
|
|
backToTopBtn.addEventListener('click', function(e) { |
|
|
e.preventDefault(); |
|
|
window.scrollTo({ top: 0, behavior: 'smooth' }); |
|
|
}); |
|
|
|
|
|
// ============================================================ |
|
|
// 3. API 调用 |
|
|
// ============================================================ |
|
|
async function fetchDocList() { |
|
|
const res = await fetch('/api/list'); |
|
|
if (!res.ok) throw new Error('获取文档列表失败 (状态码: ' + res.status + ')'); |
|
|
return res.json(); |
|
|
} |
|
|
|
|
|
async function fetchDocContent(filename) { |
|
|
if (!filename || filename.includes('..') || filename.includes('/') || filename.includes('\\')) { |
|
|
throw new Error('文件名不合法'); |
|
|
} |
|
|
const res = await fetch('/api/read/' + encodeURIComponent(filename)); |
|
|
if (!res.ok) { |
|
|
let errorDetail = ''; |
|
|
try { |
|
|
errorDetail = await res.text(); |
|
|
} catch (e) {} |
|
|
throw new Error(`读取文档失败 (状态码: ${res.status}) ${errorDetail}`); |
|
|
} |
|
|
return res.text(); |
|
|
} |
|
|
|
|
|
// ============================================================ |
|
|
// 4. 渲染函数 |
|
|
// ============================================================ |
|
|
|
|
|
// 4.1 首页(带搜索框) |
|
|
function renderHome(docs) { |
|
|
// 按模块分组 |
|
|
const grouped = {}; |
|
|
docs.forEach(doc => { |
|
|
const module = doc.module || '通用文档'; |
|
|
if (!grouped[module]) { |
|
|
grouped[module] = []; |
|
|
} |
|
|
grouped[module].push(doc); |
|
|
}); |
|
|
|
|
|
// 自定义模块顺序(可在此调整) |
|
|
const customOrder = ['智慧车行', '智慧人行', '智慧安防']; |
|
|
|
|
|
let html = ` |
|
|
<div class="breadcrumb"><span>🏠 文档中心</span></div> |
|
|
<h1 style="font-size:26px; font-weight:700; margin-bottom:4px;">📚 服务实施文档</h1> |
|
|
<p class="text-muted" style="margin-bottom:8px;">点击文档查看详细内容 · 支持图片 & GIF</p> |
|
|
|
|
|
<!-- 搜索框 --> |
|
|
<input type="text" id="docSearchInput" class="search-box" placeholder="🔍 搜索文档名称或模块..." /> |
|
|
<span id="searchResultsCount" class="search-results-count"></span> |
|
|
<div id="docListContainer"> |
|
|
`; |
|
|
|
|
|
if (Object.keys(grouped).length === 0) { |
|
|
html += `<p style="text-align:center; color:#94a3b8; padding:40px 0;">📭 未找到 .md 文档</p>`; |
|
|
} |
|
|
|
|
|
// 先渲染自定义顺序的模块 |
|
|
for (const moduleName of customOrder) { |
|
|
if (grouped[moduleName]) { |
|
|
const files = grouped[moduleName]; |
|
|
html += renderModule(moduleName, files); |
|
|
delete grouped[moduleName]; |
|
|
} |
|
|
} |
|
|
// 剩下的模块按字母顺序渲染 |
|
|
const remainingModules = Object.keys(grouped).sort(); |
|
|
for (const moduleName of remainingModules) { |
|
|
const files = grouped[moduleName]; |
|
|
html += renderModule(moduleName, files); |
|
|
} |
|
|
|
|
|
html += ` |
|
|
</div> |
|
|
<hr style="margin:32px 0 16px;" /> |
|
|
<div class="text-muted" style="font-size:13px;"> |
|
|
⚡ 图片引用格式:<code></code> · 建议将图片放在对应模块的 <code>image/</code> 目录下 |
|
|
</div> |
|
|
`; |
|
|
app.innerHTML = html; |
|
|
|
|
|
// 绑定搜索事件(使用事件委托) |
|
|
const searchInput = document.getElementById('docSearchInput'); |
|
|
if (searchInput) { |
|
|
searchInput.addEventListener('input', function(e) { |
|
|
const keyword = e.target.value.trim().toLowerCase(); |
|
|
filterDocuments(keyword); |
|
|
}); |
|
|
} |
|
|
// 初始显示搜索结果数量 |
|
|
updateSearchCount(); |
|
|
} |
|
|
|
|
|
// 渲染单个模块 |
|
|
function renderModule(moduleName, files) { |
|
|
let html = ` |
|
|
<div class="module-group" data-module="${moduleName}"> |
|
|
<h2 style="font-size:20px; font-weight:600; margin-top:16px; margin-bottom:12px; padding:8px 0; border-bottom:2px solid #e2e8f0;"> |
|
|
📁 ${moduleName} |
|
|
</h2> |
|
|
<div class="doc-grid"> |
|
|
`; |
|
|
files.forEach(doc => { |
|
|
let serviceName = doc.name || doc.filename.replace('.md', '').replace('实施文档', '').trim() || '文档'; |
|
|
html += ` |
|
|
<a href="${viewLink(doc.filename)}" class="doc-card" data-search="${serviceName} ${moduleName} ${doc.filename}"> |
|
|
<div class="name"><span>📄</span> ${serviceName}</div> |
|
|
<div class="desc">${doc.filename}</div> |
|
|
<div class="meta"><span>📁 ${moduleName}</span><span class="badge">.md</span></div> |
|
|
</a> |
|
|
`; |
|
|
}); |
|
|
html += `</div></div>`; |
|
|
return html; |
|
|
} |
|
|
|
|
|
// 搜索过滤函数 |
|
|
function filterDocuments(keyword) { |
|
|
const cards = document.querySelectorAll('.doc-card'); |
|
|
const modules = document.querySelectorAll('.module-group'); |
|
|
let visibleCount = 0; |
|
|
|
|
|
cards.forEach(card => { |
|
|
const searchText = card.getAttribute('data-search').toLowerCase(); |
|
|
const match = searchText.includes(keyword); |
|
|
card.style.display = match ? 'block' : 'none'; |
|
|
if (match) visibleCount++; |
|
|
}); |
|
|
|
|
|
// 隐藏空模块 |
|
|
modules.forEach(module => { |
|
|
const visibleCards = module.querySelectorAll('.doc-card[style*="display: block"]'); |
|
|
const allCards = module.querySelectorAll('.doc-card'); |
|
|
if (visibleCards.length === 0) { |
|
|
module.style.display = 'none'; |
|
|
} else { |
|
|
module.style.display = 'block'; |
|
|
} |
|
|
}); |
|
|
|
|
|
updateSearchCount(visibleCount); |
|
|
} |
|
|
|
|
|
function updateSearchCount(count) { |
|
|
const countEl = document.getElementById('searchResultsCount'); |
|
|
if (!countEl) return; |
|
|
const totalCards = document.querySelectorAll('.doc-card').length; |
|
|
if (count === undefined) { |
|
|
count = totalCards; |
|
|
} |
|
|
countEl.textContent = count === totalCards ? '' : `显示 ${count} / ${totalCards} 个文档`; |
|
|
} |
|
|
|
|
|
// 4.2 文档阅读页(带侧边栏目录) |
|
|
function renderDocView(filename, content) { |
|
|
// 解码文件名 |
|
|
let decodedFilename = filename; |
|
|
try { |
|
|
decodedFilename = decodeURIComponent(filename); |
|
|
if (decodedFilename.includes('%')) { |
|
|
decodedFilename = decodeURIComponent(decodedFilename); |
|
|
} |
|
|
} catch (e) {} |
|
|
|
|
|
// 解析 Markdown |
|
|
let rawHtml = ''; |
|
|
try { |
|
|
rawHtml = marked.parse(content); |
|
|
} catch (e) { |
|
|
renderError('Markdown解析失败: ' + e.message); |
|
|
return; |
|
|
} |
|
|
|
|
|
// 处理 DOM(图片路径 + 标题锚点) |
|
|
const parser = new DOMParser(); |
|
|
const doc = parser.parseFromString(rawHtml, 'text/html'); |
|
|
const images = doc.querySelectorAll('img'); |
|
|
const docDir = decodedFilename.includes('/') ? decodedFilename.substring(0, decodedFilename.lastIndexOf('/') + 1) : ''; |
|
|
images.forEach(img => { |
|
|
let src = img.getAttribute('src'); |
|
|
if (src && src.startsWith('image/')) { |
|
|
img.setAttribute('src', '/' + docDir + src); |
|
|
} |
|
|
}); |
|
|
|
|
|
const headings = doc.querySelectorAll('h1, h2, h3, h4, h5, h6'); |
|
|
headings.forEach(h => { |
|
|
const rawText = h.textContent.trim(); |
|
|
if (rawText) { |
|
|
const slug = rawText.toLowerCase() |
|
|
.replace(/[^a-z0-9\u4e00-\u9fa5]+/g, '-') |
|
|
.replace(/^-+|-+$/g, ''); |
|
|
h.id = slug; |
|
|
} |
|
|
}); |
|
|
|
|
|
const htmlContent = doc.body.innerHTML; |
|
|
|
|
|
// 提取服务名 |
|
|
let serviceName = decodedFilename |
|
|
.replace(/\.md$/i, '') |
|
|
.replace(/实施文档$/i, '') |
|
|
.replace(/模块实施文档$/i, '') |
|
|
.replace(/模块文档$/i, '') |
|
|
.replace(/文档$/i, '') |
|
|
.trim(); |
|
|
if (!serviceName) serviceName = '文档'; |
|
|
|
|
|
// 构建侧边栏目录(扫描已生成的 HTML 中的标题) |
|
|
// 我们先把 HTML 插进去,再扫描,这样更可靠 |
|
|
const breadcrumb = ` |
|
|
<div class="breadcrumb"> |
|
|
<a href="/">🏠 首页</a> |
|
|
<span class="sep">›</span> |
|
|
<span style="font-weight:500;">${serviceName}</span> |
|
|
</div> |
|
|
`; |
|
|
|
|
|
const footer = ` |
|
|
<div style="margin-top:32px; border-top:1px solid #e2e8f0; padding-top:16px; font-size:13px; color:#94a3b8;"> |
|
|
📄 文档:${decodedFilename} · 图片目录:<code>/${docDir}image/</code> |
|
|
<br /><a href="/" style="display:inline-block; margin-top:6px;">← 返回文档列表</a> |
|
|
</div> |
|
|
`; |
|
|
|
|
|
// 使用 doc-layout 布局 |
|
|
app.innerHTML = ` |
|
|
${breadcrumb} |
|
|
<div class="doc-layout"> |
|
|
<div class="doc-sidebar" id="tocSidebar"> |
|
|
<h4>📖 目录</h4> |
|
|
<ul id="tocList"></ul> |
|
|
</div> |
|
|
<div class="doc-main"> |
|
|
<div class="markdown-body">${htmlContent}</div> |
|
|
${footer} |
|
|
</div> |
|
|
</div> |
|
|
`; |
|
|
|
|
|
// 生成目录 |
|
|
const tocList = document.getElementById('tocList'); |
|
|
if (tocList) { |
|
|
const mainBody = document.querySelector('.doc-main .markdown-body'); |
|
|
if (mainBody) { |
|
|
const headingsInBody = mainBody.querySelectorAll('h1, h2, h3'); |
|
|
let tocHtml = ''; |
|
|
headingsInBody.forEach(h => { |
|
|
const level = parseInt(h.tagName[1]); |
|
|
const text = h.textContent.trim(); |
|
|
const id = h.id; |
|
|
if (id) { |
|
|
const className = `level-${level}`; |
|
|
tocHtml += `<li><a href="#${id}" class="${className}">${text}</a></li>`; |
|
|
} |
|
|
}); |
|
|
tocList.innerHTML = tocHtml; |
|
|
|
|
|
// 点击目录链接时平滑滚动,并高亮当前项 |
|
|
tocList.querySelectorAll('a').forEach(link => { |
|
|
link.addEventListener('click', function(e) { |
|
|
e.preventDefault(); |
|
|
const targetId = this.getAttribute('href').substring(1); |
|
|
const targetEl = document.getElementById(targetId); |
|
|
if (targetEl) { |
|
|
targetEl.scrollIntoView({ behavior: 'smooth', block: 'start' }); |
|
|
// 高亮当前目录项 |
|
|
tocList.querySelectorAll('a').forEach(a => a.classList.remove('active')); |
|
|
this.classList.add('active'); |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
// 渲染 Mermaid |
|
|
if (typeof mermaid !== 'undefined') { |
|
|
try { |
|
|
const mermaidBlocks = document.querySelectorAll('.markdown-body pre code.language-mermaid, .markdown-body pre code.mermaid'); |
|
|
if (mermaidBlocks.length > 0) { |
|
|
mermaid.run({ nodes: mermaidBlocks }); |
|
|
} |
|
|
} catch (e) { |
|
|
console.warn('Mermaid 渲染失败:', e); |
|
|
} |
|
|
} |
|
|
|
|
|
// 滚动监听:高亮当前可见的目录项(可选增强) |
|
|
const tocLinks = document.querySelectorAll('#tocList a'); |
|
|
if (tocLinks.length > 0) { |
|
|
const headings = document.querySelectorAll('.doc-main .markdown-body h1, .doc-main .markdown-body h2, .doc-main .markdown-body h3'); |
|
|
const observer = new IntersectionObserver((entries) => { |
|
|
entries.forEach(entry => { |
|
|
if (entry.isIntersecting) { |
|
|
const id = entry.target.id; |
|
|
const link = document.querySelector(`#tocList a[href="#${id}"]`); |
|
|
if (link) { |
|
|
tocLinks.forEach(l => l.classList.remove('active')); |
|
|
link.classList.add('active'); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}, { rootMargin: '0px 0px -40% 0px' }); |
|
|
headings.forEach(h => observer.observe(h)); |
|
|
} |
|
|
} |
|
|
|
|
|
function renderError(msg) { |
|
|
app.innerHTML = ` |
|
|
<div class="breadcrumb"><a href="/">🏠 首页</a></div> |
|
|
<div class="error"> |
|
|
<p style="font-size:48px; margin-bottom:8px;">⚠️</p> |
|
|
<h2 style="color:#ef4444;">${msg}</h2> |
|
|
<p class="text-muted" style="margin-top:12px;"><a href="/">返回首页</a></p> |
|
|
</div> |
|
|
`; |
|
|
} |
|
|
|
|
|
// ============================================================ |
|
|
// 5. 路由 |
|
|
// ============================================================ |
|
|
async function router() { |
|
|
const path = getPath(); |
|
|
if (path === '/' || path === '/index.html' || path === '') { |
|
|
try { |
|
|
const docs = await fetchDocList(); |
|
|
renderHome(docs); |
|
|
} catch (err) { |
|
|
renderError('无法加载文档列表: ' + err.message); |
|
|
} |
|
|
return; |
|
|
} |
|
|
if (path.startsWith('/view/')) { |
|
|
const filename = getDocNameFromPath(path); |
|
|
if (!filename) { renderError('无效的文档路径'); return; } |
|
|
try { |
|
|
const content = await fetchDocContent(filename); |
|
|
renderDocView(filename, content); |
|
|
} catch (err) { |
|
|
renderError('无法加载文档: ' + err.message); |
|
|
} |
|
|
return; |
|
|
} |
|
|
renderError('页面未找到: ' + path); |
|
|
} |
|
|
|
|
|
// ============================================================ |
|
|
// 6. 启动 |
|
|
// ============================================================ |
|
|
window.addEventListener('popstate', router); |
|
|
window.addEventListener('DOMContentLoaded', router); |
|
|
|
|
|
document.addEventListener('click', function(e) { |
|
|
const link = e.target.closest('a'); |
|
|
if (!link) return; |
|
|
const href = link.getAttribute('href'); |
|
|
if (!href) return; |
|
|
if (href.startsWith('/') && !href.startsWith('//') && !href.startsWith('/image/')) { |
|
|
e.preventDefault(); |
|
|
if (window.location.pathname !== href) { |
|
|
window.history.pushState({}, '', href); |
|
|
router(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
</script> |
|
|
</body> |
|
|
</html> |