Skip to content

Commit 3b28ee3

Browse files
committed
feat(check): check plaint text urls, local images size and existence
1 parent e2cb7f5 commit 3b28ee3

3 files changed

Lines changed: 117 additions & 62 deletions

File tree

test.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ console.log("reading input...")
3939

4040
var version = process.version;
4141
console.log("used node version " + version);
42-
if(version.substring(0,2) === "v4" || version.substring(0,2) === "v5" || version.substring(0,2) === "v6" || version.substring(0,2) === "v8"){
43-
4442
if (options.file){
4543
inquirerprompt.setShowfilename(true);
4644
}
@@ -138,7 +136,3 @@ if(version.substring(0,2) === "v4" || version.substring(0,2) === "v5" || version
138136
}
139137
}
140138
}
141-
}
142-
else {
143-
log.error("please use node version 4.x through 6.x")
144-
}

test_modules/checkAll/check.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ module.exports = function(files, showprogressbar, callback) {
8989
//check file content
9090
var lineIndex = 0;
9191
fileContent.forEach(function(line, i) {
92-
checkContent(file, line, function(matchResult) {
92+
checkContent.check(file, line, function(matchResult) {
9393
//build error log
9494
if (matchResult !== null) {
9595
cntContent++;
@@ -98,10 +98,16 @@ module.exports = function(files, showprogressbar, callback) {
9898
"\n reason: " + matchResult;
9999
}
100100
});
101-
if (index === files.length && lineIndex === fileContent.length) {
102-
103-
}
104101
});
102+
const err = checkContent.checkLinksAndImages(file, fileContent);
103+
if(err && err.length) {
104+
cntContent += err.length;
105+
err.forEach(linkErr => {
106+
logContent += '\n\n > Error: \n line: ' + (linkErr.line) +
107+
"\n file: " + fname +
108+
"\n reason: " + linkErr.msg;
109+
});
110+
}
105111
} else {
106112
cntNotCheckedContent++;
107113
}

test_modules/checkContent/checkContent.js

Lines changed: 107 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,115 @@
11
'use strict';
22

33
var path = require('path');
4+
var fs = require('fs');
45

5-
module.exports = function(file, line, callback) {
6-
//test only files in tutorials and wip folder
7-
if (path.parse(file).dir.split(path.sep)[0] == "tutorials" || path.parse(file).dir.split(path.sep)[0] == "work-in-progress") {
8-
9-
//create regular expression
10-
var regexs = [
11-
new RegExp('\\[\\]\\((.*?)\\)'), //require alt tag for images
12-
new RegExp('^(# )\\w+'), // check for H1
13-
new RegExp('\\!\\[(.*\\.png.*)\\]\\(.*\\)'), // check for .png namings
14-
new RegExp('[^!]\\[.*?(here|there|file|folder|this|page)\\]\\((.*?)\\)'), // avoid useless url names
15-
new RegExp('\\[(.{1,2}|\\s{1,})\\]\\(.*\\)'), // conventions of alt-text for images are not observed (at least 3 characters, not only spaces)
16-
new RegExp('\\>###'), // avoid message box typo //(^\s*(\>){1,})\s*(\w){1,}
17-
new RegExp('\\[.*\]\\(.*\\.exe\\)'), // prohibit suspicious filetypes
18-
new RegExp('\\[.*\\]\\(\\)'), //\[.*\]\(\)
19-
new RegExp('\u201C'), //left double quote
20-
new RegExp('\u201D'), //right double quote
21-
new RegExp('\u2018'), //left single quote
22-
new RegExp('\u2019') //right single quote
23-
]
24-
25-
//messages for regular expressions
26-
var msg = [
27-
"empty alt-text tag for link/image",
28-
"no H1 (single #) allowed",
29-
"no filenames in alt-text for images allowed",
30-
"no useless hyperlinked terms",
31-
"conventions of alt-text for link/image are not observed (at least 3 characters, not only spaces)",
32-
"no message box typo",
33-
"no suspicious file types in links",
34-
"empty URL field",
35-
"curly quotes found. change to straight using quotes key",
36-
"curly quotes found. change to straight using quotes key",
37-
"curly single quotes found. change to straight using apostrophe key",
38-
"curly single quotes found. change to straight using apostrophe key"
39-
]
40-
41-
//check line with every regex
42-
regexs.forEach(function(regex, index) {
43-
var matchResult = line.match(regex);
44-
//if there is a match return error message and infos
45-
if (matchResult !== null) {
46-
if (index >= 2 && index <= 4) {
47-
callback(msg[index] + " -> " + matchResult[0].split("[", 2)[1].split("]", 2)[0]);
6+
var regexs = [
7+
new RegExp('\\[\\]\\((.*?)\\)'), //require alt tag for images
8+
new RegExp('^(# )\\w+'), // check for H1
9+
new RegExp('\\!\\[(.*\\.png.*)\\]\\(.*\\)'), // check for .png namings
10+
new RegExp('[^!]\\[.*?(here|there|file|folder|this|page)\\]\\((.*?)\\)'), // avoid useless url names
11+
new RegExp('\\[(.{1,2}|\\s{1,})\\]\\(.*\\)'), // conventions of alt-text for images are not observed (at least 3 characters, not only spaces)
12+
new RegExp('\\>###'), // avoid message box typo //(^\s*(\>){1,})\s*(\w){1,}
13+
new RegExp('\\[.*\]\\(.*\\.exe\\)'), // prohibit suspicious filetypes
14+
new RegExp('\\[.*\\]\\(\\)'), //\[.*\]\(\)
15+
new RegExp('\u201C'), //left double quote
16+
new RegExp('\u201D'), //right double quote
17+
new RegExp('\u2018'), //left single quote
18+
new RegExp('\u2019'), //right single quote
19+
];
20+
21+
//messages for regular expressions
22+
var msg = [
23+
"empty alt-text tag for link/image",
24+
"no H1 (single #) allowed",
25+
"no filenames in alt-text for images allowed",
26+
"no useless hyperlinked terms",
27+
"conventions of alt-text for link/image are not observed (at least 3 characters, not only spaces)",
28+
"no message box typo",
29+
"no suspicious file types in links",
30+
"empty URL field",
31+
"curly quotes found. change to straight using quotes key",
32+
"curly quotes found. change to straight using quotes key",
33+
"curly single quotes found. change to straight using apostrophe key",
34+
"curly single quotes found. change to straight using apostrophe key",
35+
];
36+
37+
const linkRegExp = new RegExp('(?<![`\\(\\[]|(href=")|(link=")|(src="))(http|ftp|https):\\/\\/([\\w_-]+(?:(?:\\.[\\w_-]+)+))([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])\\/?(?=([^`]*`[^`]*`)*[^`]*$)(?!(([^\\[]*\\])|([^\\<]*\\>)|([^\\(]*\\))))');
38+
const markdownImageRegExp = new RegExp('\\!\\[[^\\]]+\\]\\((?!http)(.+?)\\)');
39+
40+
const MB = Math.pow(2, 20);
41+
42+
const fileExistsSyncCS = (filePath) => {
43+
var dir = path.dirname(filePath);
44+
var fileNames = fs.readdirSync(dir);
45+
return fileNames.includes(path.basename(filePath));
46+
};
47+
48+
module.exports = {
49+
check: function(file, line, callback) {
50+
//test only files in tutorials and wip folder
51+
if (path.parse(file).dir.split(path.sep)[0] == "tutorials" || path.parse(file).dir.split(path.sep)[0] == "work-in-progress") {
52+
53+
//check line with every regex
54+
regexs.forEach(function(regex, index) {
55+
var matchResult = line.match(regex);
56+
//if there is a match return error message and infos
57+
if (matchResult !== null) {
58+
if (index >= 2 && index <= 4) {
59+
callback(msg[index] + " -> " + matchResult[0].split("[", 2)[1].split("]", 2)[0]);
60+
} else {
61+
callback(msg[index] + " -> " + matchResult[0]);
62+
}
63+
4864
} else {
49-
callback(msg[index] + " -> " + matchResult[0]);
65+
callback(null);
5066
}
51-
52-
} else {
53-
callback(null);
54-
}
55-
});
56-
} else {
57-
callback(null);
67+
});
68+
} else {
69+
callback(null);
70+
}
71+
72+
},
73+
checkLinksAndImages: (file, lines) => {
74+
if (path.parse(file).dir.split(path.sep)[0] == "tutorials" || path.parse(file).dir.split(path.sep)[0] == "work-in-progress") {
75+
let isCodeBlock = false;
76+
const result = [];
77+
const dir = path.dirname(file);
78+
lines.forEach((line, index) => {
79+
if(line.includes('```')) {
80+
isCodeBlock = !isCodeBlock;
81+
}
82+
if(!isCodeBlock) {
83+
const match = line.match(linkRegExp);
84+
if(match) {
85+
result.push({
86+
line: index + 1,
87+
msg: `plain text URL -> ${match[0]}`
88+
});
89+
}
90+
}
91+
const match = line.match(markdownImageRegExp);
92+
if(match) {
93+
const filePath = path.join(dir, match[1]);
94+
const exists = fileExistsSyncCS(filePath);
95+
if(exists) {
96+
const { size } = fs.statSync(filePath);
97+
if(size > MB) {
98+
result.push({
99+
line: index + 1,
100+
msg: `file size is more than 1 MB -> ${match[1]}`
101+
});
102+
}
103+
} else {
104+
result.push({
105+
line: index + 1,
106+
msg: `missed local image -> ${match[1]}`
107+
});
108+
}
109+
}
110+
});
111+
return result;
112+
}
58113
}
114+
};
59115

60-
}

0 commit comments

Comments
 (0)