-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathfenced-code-meta.js
More file actions
101 lines (86 loc) · 2.5 KB
/
Copy pathfenced-code-meta.js
File metadata and controls
101 lines (86 loc) · 2.5 KB
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
/**
* @fileoverview Rule to require or disallow metadata for fenced code blocks.
* @author TKDev7
*/
//-----------------------------------------------------------------------------
// Type Definitions
//-----------------------------------------------------------------------------
/**
* @import { MarkdownRuleDefinition } from "../types.js";
* @typedef {"missingMetadata" | "disallowedMetadata"} FencedCodeMetaMessageIds
* @typedef {["always" | "never"]} FencedCodeMetaOptions
* @typedef {MarkdownRuleDefinition<{ RuleOptions: FencedCodeMetaOptions, MessageIds: FencedCodeMetaMessageIds }>} FencedCodeMetaRuleDefinition
*/
//-----------------------------------------------------------------------------
// Rule Definition
//-----------------------------------------------------------------------------
export default /** @satisfies {FencedCodeMetaRuleDefinition} */ ({
meta: {
type: "problem",
docs: {
recommended: false,
description: "Require or disallow metadata for fenced code blocks",
url: "https://github.com/eslint/markdown/blob/main/docs/rules/fenced-code-meta.md",
},
messages: {
missingMetadata: "Missing code block metadata.",
disallowedMetadata: "Code block metadata is not allowed.",
},
schema: [
{
enum: ["always", "never"],
},
],
defaultOptions: ["always"],
},
create(context) {
const [mode] = context.options;
const { sourceCode } = context;
return {
code(node) {
const lineText = sourceCode.lines[node.position.start.line - 1];
const fenceLineText = lineText.slice(
node.position.start.column - 1,
);
if (mode === "always") {
if (node.lang && !node.meta) {
const langIndex = fenceLineText.indexOf(node.lang);
context.report({
loc: {
start: node.position.start,
end: {
line: node.position.start.line,
column:
node.position.start.column +
langIndex +
node.lang.length,
},
},
messageId: "missingMetadata",
});
}
return;
}
if (node.meta) {
const metaIndex = fenceLineText.lastIndexOf(node.meta);
context.report({
loc: {
start: {
line: node.position.start.line,
column: node.position.start.column + metaIndex,
},
end: {
line: node.position.start.line,
column:
node.position.start.column +
metaIndex +
node.meta.trimEnd().length,
},
},
messageId: "disallowedMetadata",
});
}
},
};
},
});