Skip to content

Commit 84b9e80

Browse files
authoredSep 28, 2020
[CI skip] Some code refactoring
1 parent e67e51b commit 84b9e80

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed
 

‎src/missing.js

+40-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
// Imports from default node modules
12
const fs = require('fs');
23
const http = require("https");
34

5+
// This is the URL to the deployed wiki
46
const url = "https://github.com/Slimefun/Slimefun4/wiki/";
7+
8+
// The regular expression to check for links that lead to a wiki page
59
const regex = /\(https:\/\/github\.com\/Slimefun\/Slimefun4\/wiki\/[A-Za-z-]+\)/g;
610

711
/**
@@ -14,13 +18,18 @@ const options = {
1418
path: "/repos/Slimefun/Wiki/issues/2",
1519
headers: {
1620
"User-Agent": "Slimefun Wiki Action",
17-
"authorization": "token " + process.env.ACCESS_TOKEN,
21+
"authorization": `token ${process.env.ACCESS_TOKEN}`,
1822
"content-type": "application/x-www-form-urlencoded"
1923
}
2024
}
2125

26+
// The queue of file scan tasks
2227
const queue = [];
28+
29+
// All found pages
2330
const pages = [];
31+
32+
// All missing pages
2433
const missing = [];
2534

2635
// This is our placeholder text for any page that is missing
@@ -36,25 +45,13 @@ https://github.com/Slimefun/Slimefun4/wiki/Expanding-the-Wiki
3645
3746
`;
3847

39-
// Read the contents of our /pages/ directory.
48+
// Read the contents of our /pages/ directory as an async promise
4049
fs.promises.readdir("pages").then(files => {
50+
51+
// Loop through all files in that directory
4152
for (let i in files) {
42-
// Queue another task to find the file
43-
queue.push(fs.promises.readFile("pages/" + files[i], "UTF-8").then(content => {
44-
let match;
45-
46-
// Continue as long as a match can be found
47-
do {
48-
// Update our variable
49-
match = regex.exec(content);
50-
51-
// If we found a match, handle it
52-
if (match) {
53-
let page = match[0].substring(1 + url.length, match[0].length - 1);
54-
findFile(`${page}.md`);
55-
}
56-
} while(match);
57-
}));
53+
// Queue another task to find linked pages
54+
queue.push(fs.promises.readFile(`pages/${files[i]}`, "UTF-8").then(scanFile));
5855
}
5956

6057
// Finish working off the queue and evaluate afterwards
@@ -103,6 +100,31 @@ fs.promises.readdir("pages").then(files => {
103100
});
104101
});
105102

103+
/**
104+
* This method scans the given input for links to wiki pages.
105+
*
106+
* @param {string} content The file content
107+
*/
108+
function scanFile(content) {
109+
let match;
110+
111+
// This scans the document for any linked pages.
112+
// This continues as long as there are matches for our regular expression
113+
do {
114+
// Update our match variable
115+
match = regex.exec(content);
116+
117+
// If we found a match, handle it
118+
if (match) {
119+
// We will crop out the url portion of the match
120+
let page = match[0].substring(1 + url.length, match[0].length - 1);
121+
122+
// Start an attempt to find the .md file for the linked page
123+
findFile(`${page}.md`);
124+
}
125+
} while(match);
126+
}
127+
106128
/**
107129
* This method attempts to find the given page.
108130
* If the page could not be found, it will push it to the missing pages array.

0 commit comments

Comments
 (0)
Please sign in to comment.