Skip to content

Commit 1bd528f

Browse files
authored
refactor: use getLocFromIndex in no-multiple-h1 (#542)
* refactor: use `getLocFromIndex` in `no-multiple-h1` * wip * wip * wip
1 parent 104d4b7 commit 1bd528f

File tree

1 file changed

+12
-51
lines changed

1 file changed

+12
-51
lines changed

src/rules/no-multiple-h1.js

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@
77
// Imports
88
//-----------------------------------------------------------------------------
99

10-
import {
11-
findOffsets,
12-
frontmatterHasTitle,
13-
stripHtmlComments,
14-
} from "../util.js";
10+
import { frontmatterHasTitle, stripHtmlComments } from "../util.js";
1511

1612
//-----------------------------------------------------------------------------
1713
// Type Definitions
1814
//-----------------------------------------------------------------------------
1915

2016
/**
21-
* @import { MarkdownRuleDefinition } from "../types.js";
17+
* @import { Yaml } from "mdast";
18+
* @import { MarkdownRuleDefinition, Toml, Json } from "../types.js";
2219
* @typedef {"multipleH1"} NoMultipleH1MessageIds
2320
* @typedef {[{ frontmatterTitle?: string }]} NoMultipleH1Options
2421
* @typedef {MarkdownRuleDefinition<{ RuleOptions: NoMultipleH1Options, MessageIds: NoMultipleH1MessageIds }>} NoMultipleH1RuleDefinition
@@ -70,25 +67,14 @@ export default {
7067
},
7168

7269
create(context) {
70+
const { sourceCode } = context;
7371
const [{ frontmatterTitle }] = context.options;
7472
const titlePattern =
7573
frontmatterTitle === "" ? null : new RegExp(frontmatterTitle, "iu");
7674
let h1Count = 0;
7775

7876
return {
79-
yaml(node) {
80-
if (frontmatterHasTitle(node.value, titlePattern)) {
81-
h1Count++;
82-
}
83-
},
84-
85-
toml(node) {
86-
if (frontmatterHasTitle(node.value, titlePattern)) {
87-
h1Count++;
88-
}
89-
},
90-
91-
json(node) {
77+
"yaml, toml, json"(/** @type {Yaml | Toml | Json} */ node) {
9278
if (frontmatterHasTitle(node.value, titlePattern)) {
9379
h1Count++;
9480
}
@@ -97,45 +83,20 @@ export default {
9783
html(node) {
9884
const text = stripHtmlComments(node.value);
9985

86+
/** @type {RegExpExecArray} */
10087
let match;
88+
10189
while ((match = h1TagPattern.exec(text)) !== null) {
10290
h1Count++;
10391
if (h1Count > 1) {
104-
const {
105-
lineOffset: startLineOffset,
106-
columnOffset: startColumnOffset,
107-
} = findOffsets(node.value, match.index);
108-
109-
const {
110-
lineOffset: endLineOffset,
111-
columnOffset: endColumnOffset,
112-
} = findOffsets(
113-
node.value,
114-
match.index + match[0].length,
115-
);
116-
117-
const nodeStartLine = node.position.start.line;
118-
const nodeStartColumn = node.position.start.column;
119-
const startLine = nodeStartLine + startLineOffset;
120-
const endLine = nodeStartLine + endLineOffset;
121-
const startColumn =
122-
(startLine === nodeStartLine
123-
? nodeStartColumn
124-
: 1) + startColumnOffset;
125-
const endColumn =
126-
(endLine === nodeStartLine ? nodeStartColumn : 1) +
127-
endColumnOffset;
92+
const startOffset = // Adjust `h1TagPattern` match index to the full source code.
93+
match.index + node.position.start.offset;
94+
const endOffset = startOffset + match[0].length;
12895

12996
context.report({
13097
loc: {
131-
start: {
132-
line: startLine,
133-
column: startColumn,
134-
},
135-
end: {
136-
line: endLine,
137-
column: endColumn,
138-
},
98+
start: sourceCode.getLocFromIndex(startOffset),
99+
end: sourceCode.getLocFromIndex(endOffset),
139100
},
140101
messageId: "multipleH1",
141102
});

0 commit comments

Comments
 (0)