From 2d508c398a85a7250ed4a13c89c4ece211b1e926 Mon Sep 17 00:00:00 2001 From: moguraion Date: Thu, 28 Aug 2025 03:40:23 +0000 Subject: [PATCH 1/2] feature: add checkImage option --- src/textlint-rule-prh.js | 31 +++++++++++++++++++++++++++++++ test/prh-rule-tester-test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/textlint-rule-prh.js b/src/textlint-rule-prh.js index 6979349..088e730 100644 --- a/src/textlint-rule-prh.js +++ b/src/textlint-rule-prh.js @@ -12,6 +12,7 @@ const untildify = (filePath) => { }; const defaultOptions = { checkLink: false, + checkImage: false, checkBlockQuote: false, checkEmphasis: false, checkHeader: true, @@ -82,6 +83,9 @@ const createIgnoreNodeTypes = (options, Syntax) => { if (!options.checkLink) { nodeTypes.push(Syntax.Link); } + if (!options.checkImage) { + nodeTypes.push(Syntax.Image); + } if (!options.checkBlockQuote) { nodeTypes.push(Syntax.BlockQuote); } @@ -290,6 +294,33 @@ function reporter(context, userOptions = {}) { report(node, new RuleError(error.message)); } } + }, + [Syntax.Image](node) { + if (!options.checkImage) { + return; + } + const text = node.alt; + const n_index_offset = 2; + const dummyFilePath = ""; + const makeChangeSet = prhEngine.makeChangeSet(dummyFilePath, text); + forEachChange(makeChangeSet, text, ({ matchStartIndex, matchEndIndex, actual, expected, prh }) => { + // If result is not changed, should not report + if (actual === expected) { + return; + } + const suffix = prh !== null ? "\n" + prh : ""; + const messages = actual + " => " + expected + suffix; + report( + node, + new RuleError(messages, { + index: matchStartIndex + n_index_offset, + fix: fixer.replaceTextRange( + [matchStartIndex + n_index_offset, matchEndIndex + n_index_offset], + expected + ) + }) + ); + }); } }; } diff --git a/test/prh-rule-tester-test.js b/test/prh-rule-tester-test.js index 49bc786..7281afc 100644 --- a/test/prh-rule-tester-test.js +++ b/test/prh-rule-tester-test.js @@ -19,6 +19,13 @@ tester.run("prh", rule, { rulePaths: [__dirname + "/fixtures/rule.yaml"] } }, + { + text: "![jQuery](/path/to/logo.png)", + options: { + rulePaths: [__dirname + "/fixtures/rule.yaml"], + checkImage: true + } + }, { text: "# ディフォルト設定", options: { @@ -184,6 +191,29 @@ ${CODE_START_JS}`, } ] }, + { + text: "![jquery](/path/to/logo.png)", + output: "![jQuery](/path/to/logo.png)", + options: { + rulePaths: [__dirname + "/fixtures/rule.yaml"], + checkImage: true + }, + errors: [ + { + type: "lint", + ruleId: "prh", + message: "jquery => jQuery", + index: 2, + line: 1, + column: 3, + severity: 2, + fix: { + range: [2, 10], + text: "jQuery" + } + } + ] + }, { text: "> JQUERY", output: "> jQuery", From adb3f931307f2b0a03ed1bef286e8196ad07bb83 Mon Sep 17 00:00:00 2001 From: moguraion Date: Thu, 28 Aug 2025 03:53:44 +0000 Subject: [PATCH 2/2] chore: update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b3a2436..97676ba 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ You can use `~` as Home directory abbreviation. ``` - `checkLink`(optional) : Check `Link` node type (default: `false`) +- `checkImage`(optional) : Check `Image` node type (default: `false`) + - Only alt text of image is scoped - `checkBlockQuote`(optional) : Check `BlockQuote` node type (default: `false`) - `checkEmphasis`(optional) : Check `Emphasis` node type (default: `false`) - `checkHeader`(optional) : Check `Header` node type (default: `true`)