forked from oasisprotocol/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.markdownlint-cli2.cjs
83 lines (80 loc) · 3.12 KB
/
.markdownlint-cli2.cjs
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
// @ts-check
/** @type {import('markdownlint').Options} */
const options = {
config: {
// TODO: enable some of these rules
'line-length': false,
'no-multiple-blanks': false,
'no-hard-tabs': false,
'no-space-in-links': false,
'no-space-in-emphasis': false,
'blanks-around-fences': false,
'list-marker-space': false,
'ol-prefix': false,
'ul-style': false,
'ul-indent': false,
'no-duplicate-header': false,
'no-emphasis-as-heading': false,
'no-trailing-punctuation': false,
'heading-increment': false,
'first-line-heading': false,
'fenced-code-language': false,
'commands-show-output': false,
'no-inline-html': false,
'no-alt-text': false,
},
customRules: [{
// Check relative links have a file extension, and file exists
names: ['relative-links-have-ext'],
description: 'for Docusaurus routing',
tags: ['links'],
information: new URL('https://github.com/oasisprotocol/docs/issues/4'),
function: ({ name, tokens }, onError) => {
const fs = require('fs');
const path = require('path');
const dir = path.dirname(name);
for (const token of tokens) {
if (token.type === 'inline') {
for (const child of token.children) {
if (child.type === 'link_open') {
const [_key, href] = child.attrs.find(([key, value]) => key === 'href');
const isAbsoluteUrl = new URL(href, 'relative://a.b').protocol !== 'relative:';
const isAbsolutePathOrNetworkPath = href.startsWith('/');
const isFragment = href.startsWith('#');
const isRelativePath = !isAbsoluteUrl && !isAbsolutePathOrNetworkPath && !isFragment;
if (isRelativePath) {
const relativePath = href.split('#')[0];
const missingExtension = !path.extname(relativePath);
const missingFile = !fs.existsSync(path.join(dir, relativePath));
if (missingExtension) {
const postfixSuggestions = ['.md', '.mdx', 'README.md', 'README.mdx', '/README.md', '/README.mdx', 'index.md', '/index.md'];
const goodPostfix = postfixSuggestions.find(
postfix => fs.existsSync(path.join(dir, relativePath + postfix))
);
const postfixColumn = 1 + child.line.indexOf(href) + relativePath.length;
const canFix = goodPostfix && child.line.indexOf(href) >= 0;
onError({
lineNumber: child.lineNumber,
context: href,
detail: 'Filename extension missing',
fixInfo: !canFix ? undefined : {
insertText: goodPostfix,
editColumn: postfixColumn,
},
});
} else if (missingFile) {
onError({
lineNumber: child.lineNumber,
context: href,
detail: 'File missing',
});
}
}
}
}
}
}
},
}],
};
module.exports = options;