forked from mdx-js/eslint-mdx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remark.ts
67 lines (59 loc) · 1.75 KB
/
remark.ts
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
import type { Linter } from 'eslint'
import { processors } from 'eslint-plugin-markdown'
import { meta } from '../meta'
import type { RemarkLintMessage } from '../rules'
import { getShortLang } from './helpers'
import { processorOptions as defaultProcessorOptions } from './options'
export const createRemarkProcessor = (
processorOptions = defaultProcessorOptions,
): Linter.Processor => ({
meta: {
name: 'mdx/remark',
version: meta.version,
},
supportsAutofix: true,
preprocess(text, filename) {
if (!processorOptions.lintCodeBlocks) {
return [text]
}
return [
text,
...processors.markdown
.preprocess(text, filename)
.map(({ text, filename }) => ({
text,
filename:
filename.slice(0, filename.lastIndexOf('.')) +
'.' +
getShortLang(filename, processorOptions.languageMapper),
})),
]
},
postprocess([mdxMessages, ...markdownMessages], filename) {
return [
...mdxMessages,
...processors.markdown.postprocess(markdownMessages, filename),
]
.sort((a, b) => a.line - b.line || a.column - b.column)
.map(lintMessage => {
const {
message,
ruleId: eslintRuleId,
severity: eslintSeverity,
} = lintMessage
if (eslintRuleId !== 'mdx/remark') {
return lintMessage
}
const { source, ruleId, reason, severity } = JSON.parse(
message,
) as RemarkLintMessage
return {
...lintMessage,
ruleId: `${source}-${ruleId}`,
message: reason,
severity: Math.max(eslintSeverity, severity) as Linter.Severity,
}
})
},
})
export const remark: Linter.Processor = createRemarkProcessor()