Skip to content

Commit 43217ee

Browse files
committed
vercel部署优化,修复首页报错找不到readme.md的问题
readme.md文件修改后,需要执行一次node utils/md2html.js来更新index.html的内容
1 parent b917954 commit 43217ee

File tree

7 files changed

+1573
-21
lines changed

7 files changed

+1573
-21
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,6 @@ dist
130130
.pnp.*
131131
/local/
132132
!README.md
133+
134+
.idea
135+
.vercel

controllers/root.js

+34-21
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,60 @@
11
import path from 'path';
22
import {readdirSync, readFileSync, existsSync} from 'fs';
33
import '../utils/marked.min.js';
4+
const LOG_WITH_FILE = process.env.LOG_WITH_FILE;
45

56
export default (fastify, options, done) => {
67
// 添加 / 接口
78
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+
}
1518
}
16-
}
1719

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+
}
2426

25-
// 读取 README.md 文件内容
26-
const markdownContent = readFileSync(readmePath, 'utf-8');
27+
// 读取 README.md 文件内容
28+
const markdownContent = readFileSync(readmePath, 'utf-8');
2729

28-
// 将 Markdown 转换为 HTML
29-
const htmlContent = marked.parse(markdownContent);
30+
// 将 Markdown 转换为 HTML
31+
const htmlContent = marked.parse(markdownContent);
3032

31-
// 返回 HTML 内容
32-
reply.type('text/html').send(`
33+
// 返回 HTML 内容
34+
reply.type('text/html').send(`
3335
<!DOCTYPE html>
3436
<html lang="en">
3537
<head>
3638
<meta charset="UTF-8">
3739
<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>
3942
</head>
4043
<body>
4144
${htmlContent}
4245
</body>
4346
</html>
4447
`);
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+
}
4558
});
4659

4760
// 新增 /favicon.ico 路由

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"fastify": "^4.15.0",
2424
"fastq": "^1.17.1",
2525
"lodash": "^4.17.21",
26+
"marked": "^15.0.4",
2627
"mime-types": "^2.1.35",
2728
"p-queue": "^8.0.1",
2829
"qs": "^6.13.1",

0 commit comments

Comments
 (0)