-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy path.remarkrc.js
81 lines (69 loc) · 2.18 KB
/
.remarkrc.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
import remarkLint from 'remark-lint';
import remarkPresetLintRecommended from 'remark-preset-lint-recommended';
import { lintRule } from 'unified-lint-rule';
import { visitParents } from 'unist-util-visit-parents';
const htmlTagsWithMarkdownEquivalents = [
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', // Headings
'p', // Paragraphs
'br', // Line Breaks
'strong', 'b', // Bold
'em', 'i', // Italic
'blockquote', // Blockquote
'ol', 'ul', 'li', // Lists
'code', // Inline Code
'pre', // Code Block
'hr', // Horizontal Rule
'a', // Links
'img' // Images
];
const invalidAnchorAttributes = ['href', 'title'];
const invalidImageAttributes = ['src', 'alt', 'title'];
const checkIfHtml = (node, parents, file) => {
if (!node.position) return
// Is this html-ish?
if (/^[\t ]*<!--/.test(node.value)) return
const name = node.name;
// check if name starts with a capital letter
const isComponent = /^[A-Z]/.test(name);
// check if the tag has no markdown equivalent
const tagHasNoMarkdownEquivalent = !htmlTagsWithMarkdownEquivalents.includes(name);
if (isComponent || tagHasNoMarkdownEquivalent) return;
// check for attributes on the tags a and img
if (name === 'a' && node.attributes.find(attr => !invalidAnchorAttributes.includes(attr.name))) {
return;
}
if (name === 'img' && node.attributes.find(attr => !invalidImageAttributes.includes(attr.name))) {
return;
}
file.message(`Unexpected HTML tag [${name}], use markdown instead`, {
ancestors: [...parents, node],
place: node.position
});
}
// this is the lint rule definition
const noHtml = lintRule(
{
origin: 'remark-lint:no-html',
},
function (tree, file) {
//console.log(tree);
visitParents(tree, 'mdxJsxFlowElement', function (node, parents) {
checkIfHtml(node, parents, file);
})
visitParents(tree, 'paragraph', function (node, parents) {
[...node.children].forEach((child) => {
if (child.type === 'mdxJsxTextElement') {
checkIfHtml(child, parents, file);
}
});
})
}
)
const remarkConfig = {
plugins: [
remarkLint,
remarkPresetLintRecommended,
noHtml
]
};
export default remarkConfig;