From 15e153f4636d6c9e516255a2313f3909657efb6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=A3=A8=EB=B0=80LuMir?= Date: Mon, 29 Sep 2025 19:20:14 +0900 Subject: [PATCH 1/4] refactor: use `getLocFromIndex` in `no-multiple-h1` --- src/rules/no-multiple-h1.js | 50 ++++++++----------------------------- 1 file changed, 11 insertions(+), 39 deletions(-) diff --git a/src/rules/no-multiple-h1.js b/src/rules/no-multiple-h1.js index bc1d9ff8..6bff9bd9 100644 --- a/src/rules/no-multiple-h1.js +++ b/src/rules/no-multiple-h1.js @@ -7,11 +7,7 @@ // Imports //----------------------------------------------------------------------------- -import { - findOffsets, - frontmatterHasTitle, - stripHtmlComments, -} from "../util.js"; +import { frontmatterHasTitle, stripHtmlComments } from "../util.js"; //----------------------------------------------------------------------------- // Type Definitions @@ -28,7 +24,7 @@ import { // Helpers //----------------------------------------------------------------------------- -const h1TagPattern = /]*>[\s\S]*?<\/h1>/giu; +const h1TagPattern = /]*>[\s\S]*?<\/h1>/dgiu; //----------------------------------------------------------------------------- // Rule Definition @@ -70,6 +66,7 @@ export default { }, create(context) { + const { sourceCode } = context; const [{ frontmatterTitle }] = context.options; const titlePattern = frontmatterTitle === "" ? null : new RegExp(frontmatterTitle, "iu"); @@ -97,45 +94,20 @@ export default { html(node) { const text = stripHtmlComments(node.value); + /** @type {RegExpExecArray} */ let match; + while ((match = h1TagPattern.exec(text)) !== null) { + const [startOffset, endOffset] = match.indices[0].map( + index => index + node.position.start.offset, + ); // Adjust `h1TagPattern` match indices to the full source code. + h1Count++; if (h1Count > 1) { - const { - lineOffset: startLineOffset, - columnOffset: startColumnOffset, - } = findOffsets(node.value, match.index); - - const { - lineOffset: endLineOffset, - columnOffset: endColumnOffset, - } = findOffsets( - node.value, - match.index + match[0].length, - ); - - const nodeStartLine = node.position.start.line; - const nodeStartColumn = node.position.start.column; - const startLine = nodeStartLine + startLineOffset; - const endLine = nodeStartLine + endLineOffset; - const startColumn = - (startLine === nodeStartLine - ? nodeStartColumn - : 1) + startColumnOffset; - const endColumn = - (endLine === nodeStartLine ? nodeStartColumn : 1) + - endColumnOffset; - context.report({ loc: { - start: { - line: startLine, - column: startColumn, - }, - end: { - line: endLine, - column: endColumn, - }, + start: sourceCode.getLocFromIndex(startOffset), + end: sourceCode.getLocFromIndex(endOffset), }, messageId: "multipleH1", }); From c4c92edf3f7f010045e31564072ac486cf673288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=A3=A8=EB=B0=80LuMir?= Date: Mon, 29 Sep 2025 19:24:26 +0900 Subject: [PATCH 2/4] wip --- src/rules/no-multiple-h1.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/rules/no-multiple-h1.js b/src/rules/no-multiple-h1.js index 6bff9bd9..19902f46 100644 --- a/src/rules/no-multiple-h1.js +++ b/src/rules/no-multiple-h1.js @@ -98,12 +98,12 @@ export default { let match; while ((match = h1TagPattern.exec(text)) !== null) { - const [startOffset, endOffset] = match.indices[0].map( - index => index + node.position.start.offset, - ); // Adjust `h1TagPattern` match indices to the full source code. - h1Count++; if (h1Count > 1) { + const [startOffset, endOffset] = match.indices[0].map( + index => index + node.position.start.offset, + ); // Adjust `h1TagPattern` match indices to the full source code. + context.report({ loc: { start: sourceCode.getLocFromIndex(startOffset), From a421db145ab6a5e10c371fb081e6ffe7bab5f9a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=A3=A8=EB=B0=80LuMir?= Date: Mon, 29 Sep 2025 22:11:50 +0900 Subject: [PATCH 3/4] wip --- src/rules/no-multiple-h1.js | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/rules/no-multiple-h1.js b/src/rules/no-multiple-h1.js index 19902f46..d21eda46 100644 --- a/src/rules/no-multiple-h1.js +++ b/src/rules/no-multiple-h1.js @@ -14,7 +14,8 @@ import { frontmatterHasTitle, stripHtmlComments } from "../util.js"; //----------------------------------------------------------------------------- /** - * @import { MarkdownRuleDefinition } from "../types.js"; + * @import { Yaml } from "mdast"; + * @import { MarkdownRuleDefinition, Toml, Json } from "../types.js"; * @typedef {"multipleH1"} NoMultipleH1MessageIds * @typedef {[{ frontmatterTitle?: string }]} NoMultipleH1Options * @typedef {MarkdownRuleDefinition<{ RuleOptions: NoMultipleH1Options, MessageIds: NoMultipleH1MessageIds }>} NoMultipleH1RuleDefinition @@ -24,7 +25,7 @@ import { frontmatterHasTitle, stripHtmlComments } from "../util.js"; // Helpers //----------------------------------------------------------------------------- -const h1TagPattern = /]*>[\s\S]*?<\/h1>/dgiu; +const h1TagPattern = /]*>[\s\S]*?<\/h1>/giu; //----------------------------------------------------------------------------- // Rule Definition @@ -73,19 +74,9 @@ export default { let h1Count = 0; return { - yaml(node) { - if (frontmatterHasTitle(node.value, titlePattern)) { - h1Count++; - } - }, - - toml(node) { - if (frontmatterHasTitle(node.value, titlePattern)) { - h1Count++; - } - }, - - json(node) { + ":matches(yaml, toml, json)"( + /** @type {Yaml | Toml | Json} */ node, + ) { if (frontmatterHasTitle(node.value, titlePattern)) { h1Count++; } @@ -100,9 +91,9 @@ export default { while ((match = h1TagPattern.exec(text)) !== null) { h1Count++; if (h1Count > 1) { - const [startOffset, endOffset] = match.indices[0].map( - index => index + node.position.start.offset, - ); // Adjust `h1TagPattern` match indices to the full source code. + const startOffset = // Adjust `h1TagPattern` match index to the full source code. + match.index + node.position.start.offset; + const endOffset = startOffset + match[0].length; context.report({ loc: { From 00049522179edd6f8872c48fcdf564d258b59bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=A3=A8=EB=B0=80LuMir?= Date: Mon, 29 Sep 2025 22:39:42 +0900 Subject: [PATCH 4/4] wip --- src/rules/no-multiple-h1.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/rules/no-multiple-h1.js b/src/rules/no-multiple-h1.js index d21eda46..451172e5 100644 --- a/src/rules/no-multiple-h1.js +++ b/src/rules/no-multiple-h1.js @@ -74,9 +74,7 @@ export default { let h1Count = 0; return { - ":matches(yaml, toml, json)"( - /** @type {Yaml | Toml | Json} */ node, - ) { + "yaml, toml, json"(/** @type {Yaml | Toml | Json} */ node) { if (frontmatterHasTitle(node.value, titlePattern)) { h1Count++; }