Skip to content

Commit a5bff28

Browse files
committed
Use modern js.
1 parent 8ccf997 commit a5bff28

File tree

2 files changed

+54
-64
lines changed

2 files changed

+54
-64
lines changed

.github/manual_lint.js

Lines changed: 53 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,53 @@
1-
// fetch all .md files.
2-
// Check it's content.
3-
// Console.LogError for errors.
4-
5-
6-
const glob = require("glob");
7-
const fs = require("fs");
8-
var path = require('path');
9-
10-
var errors = 0;
11-
12-
var getDirectories = function (src, callback) {
13-
glob(src + '../../dishes/**/*.md', callback);
14-
};
15-
16-
getDirectories(__dirname, function (err, res) {
17-
res.forEach(filePath => {
18-
// console.log("Linting file: " + filePath + " ...");
19-
20-
fs.readFile(filePath, 'utf8' , (err, data) => {
21-
data = data.replace('\r\n', '\n');
22-
data = data.replace('\r', '\n');
23-
24-
dataLines = data.split('\n');
25-
var filename = path.parse(filePath).base.replace(".md","");
26-
27-
titles = dataLines.filter(t => t.startsWith('#'));
28-
secondTitles = titles
29-
.filter(t => t.startsWith('## '));
30-
31-
if (titles[0].trim() != "# " + filename + "的做法") {
32-
console.error(`File ${filePath} is invalid! It's title should be: ${"# " + filename + "的做法"}! It was ${titles[0].trim()}!`);
33-
errors++;
34-
return;
35-
}
36-
if (secondTitles.length != 4) {
37-
console.error(`File ${filePath} is invalid! It doesn't has 4 second titles!`);
38-
errors++;
39-
return;
40-
}
41-
if (secondTitles[0].trim() != "## 必备原料和工具") {
42-
console.error(`File ${filePath} is invalid! The first title is NOT 必备原料和工具! It was ${secondTitles[0]}!`);
43-
errors++;
44-
}
45-
if (secondTitles[1].trim() != "## 计算") {
46-
console.error(`File ${filePath} is invalid! The second title is NOT 计算!`);
47-
errors++;
48-
}
49-
if (secondTitles[2].trim() != "## 操作") {
50-
console.error(`File ${filePath} is invalid! The thrid title is NOT 操作!`);
51-
errors++;
52-
}
53-
if (secondTitles[3].trim() != "## 附加内容") {
54-
console.error(`File ${filePath} is invalid! The fourth title is NOT 附加内容!`);
55-
errors++;
56-
}
57-
});
58-
});
59-
});
60-
61-
if (errors > 0) {
62-
throw `Found ${errors} errors! Please fix!`;
63-
}
1+
const util = require("util");
2+
const glob = util.promisify(require('glob'));
3+
const fs = require("fs").promises;
4+
const path = require('path');
5+
6+
7+
async function main() {
8+
var errors = [];
9+
var directories = await glob(__dirname + '../../dishes/**/*.md');
10+
11+
for (var filePath of directories) {
12+
var data = await fs.readFile(filePath, 'utf8');
13+
14+
dataLines = data.split('\n').map(t => t.trim());
15+
var filename = path.parse(filePath).base.replace(".md","");
16+
17+
titles = dataLines.filter(t => t.startsWith('#'));
18+
secondTitles = titles
19+
.filter(t => t.startsWith('## '));
20+
21+
if (titles[0].trim() != "# " + filename + "的做法") {
22+
errors.push(`File ${filePath} is invalid! It's title should be: ${"# " + filename + "的做法"}! It was ${titles[0].trim()}!`);
23+
continue;
24+
}
25+
if (secondTitles.length != 4) {
26+
errors.push(`File ${filePath} is invalid! It doesn't has 4 second titles!`);
27+
continue;
28+
}
29+
if (secondTitles[0].trim() != "## 必备原料和工具") {
30+
errors.push(`File ${filePath} is invalid! The first title is NOT 必备原料和工具! It was ${secondTitles[0]}!`);
31+
}
32+
if (secondTitles[1].trim() != "## 计算") {
33+
errors.push(`File ${filePath} is invalid! The second title is NOT 计算!`);
34+
}
35+
if (secondTitles[2].trim() != "## 操作") {
36+
errors.push(`File ${filePath} is invalid! The thrid title is NOT 操作!`);
37+
}
38+
if (secondTitles[3].trim() != "## 附加内容") {
39+
errors.push(`File ${filePath} is invalid! The fourth title is NOT 附加内容!`);
40+
}
41+
}
42+
43+
if (errors.length > 0) {
44+
for (var error of errors) {
45+
console.error(error + "\n");
46+
}
47+
48+
var message = `Found ${errors.length} errors! Please fix!`;
49+
throw new Error(message);
50+
}
51+
}
52+
53+
main();

dishes/breakfast/吐司果酱.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 吐司+果酱的做法
1+
# 吐司果酱的做法
22

33
饱腹感的懒人快速营养早餐,2min 搞定
44

0 commit comments

Comments
 (0)