forked from arantes555/check-md
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
476 lines (425 loc) · 13.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
const glob = require('globby');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const url = require('url');
const assert = require('assert');
const headingRE = /(?:\r?\n|^)#+([^\n]+)/g;
const imgTitleRE = /^(.*?) ".*?"$/;
const imgSizeRE = /^(.*?) =[\dx]+$/;
const matchUrlStr = c => `([^${c}]*)`;
const matchAnchorStr = `((?:\\!)?\\[[^\\]\\r\\n]+\\])(?:(?:\\: *${matchUrlStr('\\r\\n')})|(?:\\(${matchUrlStr('\\)')}\\)))`;
const matchAnchorRE = new RegExp(`(?:\\r?\\n|\`\`\`|${matchAnchorStr})`);
// eslint-disable-next-line no-control-regex
const rControl = /[\u0000-\u001f]/g;
const rSpecial = /[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'“”‘’–—<>,.?/]+/g;
const rCombining = /[\u0300-\u036F]/g;
const LOG_LEVELS = {
none: 0,
info: 1,
warn: 2,
error: 3,
};
/** @type {Map<String, CacheObj>} */
let contentCache;
let dirtyContentList;
const presetConfig = {
vuepress: {
root: [ './', './.vuepress/public' ],
slugify: defaultSlugify,
cwd: path.resolve(process.cwd(), './docs'),
},
default: {
defaultIndex: [ 'README.md', 'readme.md' ],
root: [ './' ],
pattern: '**/*.md',
ignore: [ '**/node_modules' ],
aliases: [],
ignoreFootnotes: false,
uniqueSlugStartIndex: 2,
cwd: process.cwd(),
exitLevel: 'error',
slugify: defaultSlugify,
},
};
/**
* @typedef {Object} CacheObj
* @property {String} CacheObj.content
* @property {Boolean} CacheObj.dirty
* @property {String} CacheObj.fileUrl
* @property {Array<String>} [CacheObj.headings]
*/
/**
* @typedef {Object} CheckOption
* @property {String} CheckOption.cwd
* @property {Boolean} [CheckOption.fix]
* @property {keyof LOG_LEVELS} [CheckOption.exitLevel]
* @property {Array<String>} [CheckOption.root]
* @property {Array<String>} [CheckOption.defaultIndex]
* @property {String} [CheckOption.preset]
* @property {String | Array<String>} [CheckOption.pattern]
* @property {String | Array<String>} [CheckOption.ignore]
* @property {Array<String>} [CheckOption.aliases]
* @property {Boolean} [CheckOption.ignoreFootnotes]
* @property {Number} [CheckOption.uniqueSlugStartIndex]
* @property {typeof defaultSlugify} [CheckOption.slugify]
*/
/**
* @typedef {Object} ReportListItem
* @property {String} ReportResult.errMsg
* @property {String} ReportResult.matchUrl
* @property {String} ReportResult.fullText
* @property {String} ReportResult.fileUrl
* @property {Number} ReportResult.line
* @property {Number} ReportResult.col
*/
/**
* @typedef {Object} ReportResult
* @property {String} ReportResult.msg
* @property {Array<ReportListItem>} ReportResult.list
* @property {keyof LOG_LEVELS} ReportResult.type
*/
/**
* check md's heading
* @param {String} fileUrl - fileUrl
* @param {String} heading - heading
* @param {Function} slugify - slugify
* @param {Number} uniqueSlugStartIndex - uniqueSlugStartIndex
* @return {Boolean} - check result
*/
function hasHeading(fileUrl, heading, slugify, uniqueSlugStartIndex) {
const cacheObj = getContent(fileUrl);
if (!cacheObj.headings) {
cacheObj.headings = [];
cacheObj.content.replace(headingRE, (_, hash) => {
const slug = slugify(hash.trim());
let i = uniqueSlugStartIndex;
let uniq = slug;
while (cacheObj.headings.includes(uniq)) {
uniq = `${slug}-${i}`;
i++;
}
cacheObj.headings.push(uniq);
});
}
heading = heading.toLowerCase();
return cacheObj.headings.includes(heading);
}
// slugify
function defaultSlugify(str, lower = true) {
// Split accented characters into components
str = str.normalize('NFKD')
// Remove accents
.replace(rCombining, '')
// Remove control characters
.replace(rControl, '')
// Replace special characters
.replace(rSpecial, '-')
// Remove continuous separators
.replace(/\-{2,}/g, '-')
// Remove prefixing and trailing separators
.replace(/^\-+|\-+$/g, '')
// ensure it doesn't start with a number (#121)
.replace(/^(\d)/, '_$1');
if (lower) {
return str.toLowerCase();// lowercase
}
return str;
}
/**
* get content with cache
* @param {String} fileUrl - fileUrl
* @return {CacheObj} - CacheObj
*/
function getContent(fileUrl) {
if (contentCache.has(fileUrl)) {
return contentCache.get(fileUrl);
}
const content = fs.readFileSync(fileUrl, { encoding: 'utf-8' });
const cacheObj = { fileUrl, content, dirty: false };
contentCache.set(fileUrl, cacheObj);
return cacheObj;
}
/**
* set content with cache
* @param {String} fileUrl - fileUrl
* @param {String} content - content
*/
function setContent(fileUrl, content) {
const contentResult = getContent(fileUrl);
if (contentResult.content === content) {
return;
}
contentResult.content = content;
if (!contentResult.dirty) {
contentResult.dirty = true;
dirtyContentList.push(contentResult);
}
}
// flush set content
function flushSetContent() {
dirtyContentList.forEach(item => {
fs.writeFileSync(item.fileUrl, item.content);
item.dirty = false;
});
dirtyContentList.length = 0;
}
/**
* @param {Object} options - options
* @param {ReportResult['type']} options.type - options.type
* @param {(p: ReportResult) => ReportResult['msg']} options.msgFn - options.msgFn
* @return {ReportResult} - result in ReportResult Format
*/
function createReportResult({ type, msgFn }) {
return {
type,
list: [],
get msg() { return msgFn(this); },
};
}
// check file exist
const existCache = new Map();
function fileExist(fileUrl) {
if (existCache.has(fileUrl)) {
return existCache.get(fileUrl);
}
const isExist = fs.existsSync(fileUrl);
existCache.set(fileUrl, isExist);
return isExist;
}
// get file stat
function getFileStat(fileUrl) {
if (!fileExist(fileUrl)) {
return;
}
return fs.statSync(fileUrl);
}
/**
* init option
* @param {CheckOption} options - options
*/
function initOption(options) {
if (options.__init__) return options;
if (options.preset && presetConfig[options.preset]) {
options = Object.assign({}, presetConfig.default, presetConfig[options.preset], options);
} else {
options = Object.assign({}, presetConfig.default, options);
}
options.__init__ = true;
return options;
}
/**
* check markdown
* @param {CheckOption} options - options
*/
async function check(options) {
// Clearing cache (this new check could have different options)
contentCache = new Map();
dirtyContentList = [];
options = initOption(options);
const { cwd, defaultIndex, root, fix, pattern, ignore, ignoreFootnotes, uniqueSlugStartIndex } = options;
assert(Array.isArray(root), 'options.root must be array');
const globPattern = (Array.isArray(pattern) ? pattern : [ pattern ]).concat(
(Array.isArray(ignore) ? ignore : [ ignore ]).map(p => `!${p}`)
);
const files = await glob(globPattern, { cwd });
const result = {
warning: createReportResult({
msgFn(r) { return `${r.list.length} warning was found`; },
type: 'warn',
}),
deadlink: createReportResult({
msgFn(r) { return `${r.list.length} dead links was found`; },
type: 'error',
}),
};
const aliases = new Map();
assert(Array.isArray(options.aliases), 'options.aliases must be array');
for (const alias of options.aliases) {
const split = alias.split('=');
assert(split.length === 2, 'aliases must be of the form \'alias=./actual/path/\'');
aliases.set(split[0], path.resolve(process.cwd(), split[1]));
}
// normalize url
const normalizeUrl = (fileUrl, ext) => {
ext = ext || path.extname(fileUrl);
if (ext === '.html') {
// convert html to md
return `${fileUrl.substring(0, fileUrl.length - 4)}md`;
} else if (!ext) {
const stat = getFileStat(fileUrl);
if (fileUrl.endsWith('/') || (stat && stat.isDirectory())) {
// directory, try to find file with defaultIndex
return defaultIndex.map(index => `${fileUrl}/${index}`).find(f => fileExist(f));
}
return `${fileUrl}.md`;
}
return fileUrl;
};
// each files
files.forEach(file => {
const fileUrl = path.resolve(cwd, file);
const dirname = path.dirname(fileUrl);
let { content } = getContent(fileUrl);
let matches;
let line = 1;
let newContent = '';
let lineIndex = 0;
let scanIndex = 0;
let collectContent = '';
let inBlock = false;
while ((matches = content.match(matchAnchorRE))) {
const char = matches[0];
let matchUrl = (matches[2] || matches[3] || '').trim();
const isVariable = !!matches[2];
const beforeContent = content.substring(0, matches.index);
let newChar = char;
collectContent += beforeContent + char;
if (char === '\n' || char === '\r\n') {
// new line
line++;
lineIndex = scanIndex + matches.index + char.length;
} else if (char === '```') {
// code block
inBlock = !inBlock;
} else if (!inBlock) {
// Support image alt attribute
const imgTitleMatch = matchUrl.match(imgTitleRE);
if (imgTitleMatch) {
matchUrl = imgTitleMatch[1];
}
// Support image alt attribute
const imgSizeMatch = matchUrl.match(imgSizeRE);
if (imgSizeMatch) {
matchUrl = imgSizeMatch[1];
}
const col = collectContent.length - char.length - lineIndex + 1;
const baseReportObj = { matchUrl, fullText: char, fileUrl, line, col };
const urlObj = url.parse(matchUrl);
if (urlObj.protocol) {
// do nothing with remote url
} else if (ignoreFootnotes && char.startsWith('[^')) {
// do nothing with footnote
} else if (!matchUrl) {
// empty url
result.deadlink.list.push({ ...baseReportObj, errMsg: 'Url link is empty' });
} else {
// only handle local url
let pathname = urlObj.pathname || '';
let ext = path.extname(pathname);
let matchAbUrl;
if (pathname) {
if (pathname.charAt(0) === '/') {
// find exist file
matchAbUrl = root.map(r => normalizeUrl(path.join(cwd, r, pathname.substring(1)), ext))
.find(f => fileExist(f));
} else {
const firstSegment = pathname.split('/')[0];
if (aliases.has(firstSegment)) {
pathname = pathname.replace(firstSegment, aliases.get(firstSegment));
}
matchAbUrl = path.resolve(dirname, pathname);
}
} else { // this is when there is only a hash
matchAbUrl = fileUrl;
ext = path.extname(matchAbUrl);
}
matchAbUrl = matchAbUrl && normalizeUrl(matchAbUrl, ext);
if (ext === '.html') {
// warning
if (fix) {
// replace .html to .md
urlObj.pathname = `${urlObj.pathname.substring(0, urlObj.pathname.length - 4)}md`;
} else {
result.warning.list.push({ ...baseReportObj, errMsg: 'Should use .md instead of .html' });
}
}
if (!matchAbUrl || !fileExist(matchAbUrl)) {
// file is not found
result.deadlink.list.push({ ...baseReportObj, errMsg: 'File is not found' });
} else if (urlObj.hash) {
let hash = decodeURIComponent(urlObj.hash.substring(1));
const slugify = options.slugify || defaultSlugify;
// check slugify
const slugHash = slugify(hash, false);
if (slugHash !== hash) {
if (fix) {
urlObj.hash = slugHash;
} else {
result.deadlink.list.push({ ...baseReportObj, errMsg: 'Hash should slugify' });
}
hash = slugHash;
}
if (!hasHeading(matchAbUrl, hash, slugify, uniqueSlugStartIndex)) {
// hash is not found
result.deadlink.list.push({ ...baseReportObj, errMsg: 'Hash is not found' });
}
}
if (fix) {
const newUrl = url.format(urlObj);
if (newUrl !== matchUrl) {
newChar = `${matches[1]}${isVariable ? `: ${newUrl}` : `(${newUrl})`}`;
}
}
}
}
scanIndex += matches.index + char.length;
content = content.substring(matches.index + char.length);
newContent += beforeContent + newChar;
}
newContent += content;
if (fix) setContent(fileUrl, newContent);
});
flushSetContent();
return result;
}
/**
* check and throw
* @param {CheckOption} options - options
*/
async function checkAndThrow(options) {
options = initOption(options);
console.info('Checking markdown...');
const result = await check(options);
const errorLevels = [];
Object.keys(result).forEach(k => {
/** @type {ReportResult} */
const item = result[k];
if (!item.list.length) {
return;
}
const level = LOG_LEVELS[item.type];
errorLevels.push(level);
if (level > LOG_LEVELS.none) {
console[item.type](convertErrMsg(item));
}
});
// tips for fix
if (errorLevels.length) {
console.info(chalk.gray('Executes with --fix to fix automatically\n'));
}
// should not exit if exitLevel is none
const exitLevel = LOG_LEVELS[options.exitLevel.toLowerCase()];
if (exitLevel !== LOG_LEVELS.none && errorLevels.find(level => level >= exitLevel)) {
console.error(chalk.red('Checking failed\n'));
process.exit(1);
} else {
console.info(chalk.green('Checking passed\n'));
}
}
/**
* @param {ReportResult} obj - obj
*/
function convertErrMsg(obj) {
return `\n${obj.type === 'error' ? chalk.red(obj.msg) : (obj.type === 'warn' ? chalk.yellow(obj.msg) : obj.msg)}\n\n` +
obj.list
.map(item => ` ${chalk.red(item.errMsg)}: ${item.fullText} ${chalk.gray(`(${item.fileUrl}:${item.line}:${item.col})`)}`)
.join('\n') +
'\n';
}
// export list
exports.check = check;
exports.checkAndThrow = checkAndThrow;
exports.presetConfig = presetConfig;
exports.setContent = setContent;
exports.getContent = getContent;