Browse Source

'代码优化'

admincjy 1 month ago
parent
commit
a374964c8b
3 changed files with 21 additions and 50 deletions
  1. 2 1
      .gitignore
  2. 0 30
      public/css/style.css
  3. 19 19
      server.js

+ 2 - 1
.gitignore

@@ -4,4 +4,5 @@
 docs/*
 docs/*
 node_modules/
 node_modules/
 .idea/
 .idea/
-.DS_Store
+.DS_Store
+nul

+ 0 - 30
public/css/style.css

@@ -91,22 +91,6 @@ body {
     font-size: 0.95rem;
     font-size: 0.95rem;
 }
 }
 
 
-.doc-preview {
-    list-style: none;
-    margin: 16px 0;
-    padding: 0;
-}
-
-.doc-preview li {
-    padding: 8px 0;
-    color: #656d76;
-    border-bottom: 1px solid #f3f4f6;
-    font-size: 0.9rem;
-}
-
-.doc-preview li:last-child {
-    border-bottom: none;
-}
 
 
 .read-btn {
 .read-btn {
     width: 100%;
     width: 100%;
@@ -473,16 +457,6 @@ body {
     font-weight: 600;
     font-weight: 600;
 }
 }
 
 
-.nav-number {
-    color: #656d76;
-    font-size: 0.85rem;
-    margin-right: 6px;
-}
-
-.nav-item.active .nav-number {
-    color: rgba(255, 255, 255, 0.8);
-}
-
 .nav-item.level-1 {
 .nav-item.level-1 {
     margin-left: 16px;
     margin-left: 16px;
 }
 }
@@ -825,10 +799,6 @@ body {
         box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
         box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
     }
     }
 
 
-    .left-sidebar.show {
-        transform: translateX(0);
-    }
-
     .right-sidebar {
     .right-sidebar {
         position: fixed;
         position: fixed;
         right: 0;
         right: 0;

+ 19 - 19
server.js

@@ -6,6 +6,11 @@ const cors = require('cors');
 const app = express();
 const app = express();
 const PORT = 3000;
 const PORT = 3000;
 
 
+// index.md 缓存
+let indexCache = null;
+let indexCacheTime = 0;
+const CACHE_DURATION = 5000; // 5秒缓存
+
 // 路径安全验证函数 - 防止路径遍历攻击
 // 路径安全验证函数 - 防止路径遍历攻击
 function sanitizePath(input) {
 function sanitizePath(input) {
     if (!input || typeof input !== 'string') {
     if (!input || typeof input !== 'string') {
@@ -33,25 +38,24 @@ app.use(express.json({ limit: '10mb' })); // 增加请求体大小限制到10MB
 app.use(express.urlencoded({ limit: '10mb', extended: true }));
 app.use(express.urlencoded({ limit: '10mb', extended: true }));
 app.use(express.static('public'));
 app.use(express.static('public'));
 
 
-// API: 获取 index.md 内容
-app.get('/api/index', async (req, res) => {
-  try {
-    const indexPath = path.join(__dirname, 'docs', 'index.md');
-    const content = await fs.readFile(indexPath, 'utf-8');
-    res.json({ content });
-  } catch (error) {
-    res.status(500).json({ error: '无法读取 index.md', details: error.message });
+// 获取 index.md 结构(带缓存)
+async function getIndexStructure() {
+  const now = Date.now();
+  if (indexCache && (now - indexCacheTime < CACHE_DURATION)) {
+    return indexCache;
   }
   }
-});
+
+  const indexPath = path.join(__dirname, 'docs', 'index.md');
+  const content = await fs.readFile(indexPath, 'utf-8');
+  indexCache = parseIndexMd(content);
+  indexCacheTime = now;
+  return indexCache;
+}
 
 
 // API: 解析 index.md 结构
 // API: 解析 index.md 结构
 app.get('/api/structure', async (req, res) => {
 app.get('/api/structure', async (req, res) => {
   try {
   try {
-    const indexPath = path.join(__dirname, 'docs', 'index.md');
-    const content = await fs.readFile(indexPath, 'utf-8');
-
-    // 解析结构
-    const structure = parseIndexMd(content);
+    const structure = await getIndexStructure();
     res.json(structure);
     res.json(structure);
   } catch (error) {
   } catch (error) {
     res.status(500).json({ error: '无法解析文档结构', details: error.message });
     res.status(500).json({ error: '无法解析文档结构', details: error.message });
@@ -67,11 +71,7 @@ app.get('/api/category/:category', async (req, res) => {
       return res.status(400).json({ error: '无效的分类名称' });
       return res.status(400).json({ error: '无效的分类名称' });
     }
     }
 
 
-    const indexPath = path.join(__dirname, 'docs', 'index.md');
-    const content = await fs.readFile(indexPath, 'utf-8');
-
-    // 解析并获取指定分类的文档
-    const structure = parseIndexMd(content);
+    const structure = await getIndexStructure();
     const categoryData = structure.find(cat => cat.name === category);
     const categoryData = structure.find(cat => cat.name === category);
 
 
     if (!categoryData) {
     if (!categoryData) {