-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (27 loc) · 1.06 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
import { lintRule } from 'unified-lint-rule'
import { visit } from 'unist-util-visit'
import { parents } from 'unist-util-parents'
function findIndentDifference(codeNode, listNode) {
const listIndent = listNode.children[0].children[0].position.start.column - 1
const codeIndent = codeNode.position.start.column - 1
return listIndent - codeIndent
}
function codeBlockSplitList(tree, file) {
visit(parents(tree), 'code', node => {
const parentCodeNode = node.parent
const codeIndex = parentCodeNode.children.indexOf(node)
const prevSibling = parentCodeNode.children[codeIndex - 1]
if (prevSibling && prevSibling.type === 'list') {
const spacesToAdd = findIndentDifference(node, prevSibling)
const message = `Add ${spacesToAdd} ${
spacesToAdd === 1 ? 'space' : 'spaces'
} to the beginning of the code block to align with the list`
file.message(message, node)
}
})
}
const remarkLintCodeBlockSplitList = lintRule(
'remark-lint:code-block-split-list',
codeBlockSplitList
)
export default remarkLintCodeBlockSplitList