|
1 | 1 | import path from 'path'; |
2 | 2 | import {readdirSync, readFileSync, existsSync} from 'fs'; |
3 | 3 | import '../utils/marked.min.js'; |
| 4 | +const LOG_WITH_FILE = process.env.LOG_WITH_FILE; |
4 | 5 |
|
5 | 6 | export default (fastify, options, done) => { |
6 | 7 | // 添加 / 接口 |
7 | 8 | fastify.get('/', async (request, reply) => { |
8 | | - let readmePath = null; |
9 | | - const files = readdirSync(options.rootDir); |
10 | | - // console.log(files); |
11 | | - for (const file of files) { |
12 | | - if (/^readme\.md$/i.test(file)) { |
13 | | - readmePath = path.join(options.rootDir, file); |
14 | | - break; |
| 9 | + if (LOG_WITH_FILE){ |
| 10 | + let readmePath = null; |
| 11 | + const files = readdirSync(options.rootDir); |
| 12 | + // console.log(files); |
| 13 | + for (const file of files) { |
| 14 | + if (/^readme\.md$/i.test(file)) { |
| 15 | + readmePath = path.join(options.rootDir, file); |
| 16 | + break; |
| 17 | + } |
15 | 18 | } |
16 | | - } |
17 | 19 |
|
18 | | - // 如果未找到 README.md 文件 |
19 | | - if (!readmePath) { |
20 | | - let fileHtml = files.map(file => `<li>${file}</li>`).join(''); |
21 | | - reply.code(404).type('text/html;charset=utf-8').send(`<h1>README.md not found</h1><ul>${fileHtml}</ul>`); |
22 | | - return; |
23 | | - } |
| 20 | + // 如果未找到 README.md 文件 |
| 21 | + if (!readmePath) { |
| 22 | + let fileHtml = files.map(file => `<li>${file}</li>`).join(''); |
| 23 | + reply.code(404).type('text/html;charset=utf-8').send(`<h1>README.md not found</h1><ul>${fileHtml}</ul>`); |
| 24 | + return; |
| 25 | + } |
24 | 26 |
|
25 | | - // 读取 README.md 文件内容 |
26 | | - const markdownContent = readFileSync(readmePath, 'utf-8'); |
| 27 | + // 读取 README.md 文件内容 |
| 28 | + const markdownContent = readFileSync(readmePath, 'utf-8'); |
27 | 29 |
|
28 | | - // 将 Markdown 转换为 HTML |
29 | | - const htmlContent = marked.parse(markdownContent); |
| 30 | + // 将 Markdown 转换为 HTML |
| 31 | + const htmlContent = marked.parse(markdownContent); |
30 | 32 |
|
31 | | - // 返回 HTML 内容 |
32 | | - reply.type('text/html').send(` |
| 33 | + // 返回 HTML 内容 |
| 34 | + reply.type('text/html').send(` |
33 | 35 | <!DOCTYPE html> |
34 | 36 | <html lang="en"> |
35 | 37 | <head> |
36 | 38 | <meta charset="UTF-8"> |
37 | 39 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
38 | | - <title>README</title> |
| 40 | + <link href='/public/css/main.css' rel='stylesheet' type='text/css'/> |
| 41 | + <title>drpyS(drpy-node)</title> |
39 | 42 | </head> |
40 | 43 | <body> |
41 | 44 | ${htmlContent} |
42 | 45 | </body> |
43 | 46 | </html> |
44 | 47 | `); |
| 48 | + }else{ |
| 49 | + // 设置文件路径 |
| 50 | + const indexPath = path.join(options.rootDir, 'public', 'index.html'); |
| 51 | + |
| 52 | + // 读取 index.md 文件内容 |
| 53 | + const indexContent = readFileSync(indexPath, 'utf-8'); |
| 54 | + |
| 55 | + // 返回 HTML 内容 |
| 56 | + reply.type('text/html').send(indexContent); |
| 57 | + } |
45 | 58 | }); |
46 | 59 |
|
47 | 60 | // 新增 /favicon.ico 路由 |
|
0 commit comments