|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | var path = require('path'); |
| 4 | +var fs = require('fs'); |
4 | 5 |
|
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 | + |
48 | 64 | } else { |
49 | | - callback(msg[index] + " -> " + matchResult[0]); |
| 65 | + callback(null); |
50 | 66 | } |
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 | + } |
58 | 113 | } |
| 114 | +}; |
59 | 115 |
|
60 | | -} |
|
0 commit comments