From 3a6c7bc3c80d265141231272aba37cfd833e9c61 Mon Sep 17 00:00:00 2001 From: azu Date: Wed, 22 Mar 2017 21:38:53 +0900 Subject: [PATCH] fix(rule): add "ignoreLinkEnd" options --- README.md | 4 ++++ package.json | 3 ++- src/textlint-rule-period-in-list-item.js | 24 +++++++++++++++++++ .../textlint-rule-period-in-list-item-test.js | 21 ++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a14d68e..6143e3b 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,10 @@ textlint --rule period-in-list-item README.md // Built-in recognized period mark list // if the period of the text is not `periodMark` and it is a string in the `periodMarks`, "periodMarks": [".", "。", "."], + // Ignore only link tag + // - [text](link) + // It is not needed period mark + "ignoreLinkEnd": true, // allow exception period mark list at end of the list item // Ignore this period mark "allowPeriodMarks": [], diff --git a/package.json b/package.json index de93a2b..e922665 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "textlint-scripts": "^1.2.2" }, "dependencies": { - "check-ends-with-period": "^1.0.1" + "check-ends-with-period": "^1.0.1", + "unist-util-select": "^1.5.0" } } diff --git a/src/textlint-rule-period-in-list-item.js b/src/textlint-rule-period-in-list-item.js index 3c18517..134f42f 100644 --- a/src/textlint-rule-period-in-list-item.js +++ b/src/textlint-rule-period-in-list-item.js @@ -1,5 +1,6 @@ // MIT © 2017 azu "use strict"; +const select = require('unist-util-select'); const checkEndsWithPeriod = require("check-ends-with-period"); /** * check `text` that the end is not periodMark @@ -34,6 +35,10 @@ const defaultOptions = { // Built-in recognized period mark list // if the period of the text is not `periodMark` and it is a string in the `periodMarks`, "periodMarks": [".", "。", "."], + // Ignore only link tag + // - [text](link) + // It is not needed period mark + "ignoreLinkEnd": true, // allow exception period mark list at end of the list item // Ignore this period mark "allowPeriodMarks": [], @@ -47,10 +52,14 @@ const reporter = (context, options = {}) => { const { Syntax, RuleError, report, fixer, getSource } = context; const preferPeriodMark = options.periodMark || defaultOptions.periodMark; const isNotNeededPeriodMark = preferPeriodMark === ""; + // always `preferPeriodMark` is added to periodMarks const periodMarks = (options.periodMarks || defaultOptions.periodMarks).concat(preferPeriodMark); const allowPeriodMarks = options.allowPeriodMarks !== undefined ? options.allowPeriodMarks : defaultOptions.allowPeriodMarks; + const ignoreLinkEnd = options.ignoreLinkEnd !== undefined + ? options.ignoreLinkEnd + : defaultOptions.ignoreLinkEnd; const allowEmoji = options.allowEmoji !== undefined ? options.allowEmoji : defaultOptions.allowEmoji; @@ -73,6 +82,14 @@ const reporter = (context, options = {}) => { })); return; } + // - [link](http://example) + // should be ignored + if (ignoreLinkEnd) { + const linkNodes = select(node, `${Syntax.Paragraph} > *`); + if (linkNodes.length === 1 && linkNodes[0].type === Syntax.Link) { + return; + } + } const { valid, periodMark, index } = checkEndsWithPeriod(text, { periodMarks, allowPeriodMarks, @@ -81,6 +98,13 @@ const reporter = (context, options = {}) => { // Prefer to use period if (valid) { // but exist difference period + const isPeriodMarkAtEnd = periodMarks.indexOf(periodMark) !== -1; + // exception case that should not report + // !? + if (!isPeriodMarkAtEnd) { + return; + } + // periodMark is expected, then exit if (periodMark === preferPeriodMark) { return; } diff --git a/test/textlint-rule-period-in-list-item-test.js b/test/textlint-rule-period-in-list-item-test.js index b2b25b5..296e9ab 100644 --- a/test/textlint-rule-period-in-list-item-test.js +++ b/test/textlint-rule-period-in-list-item-test.js @@ -21,6 +21,13 @@ tester.run("textlint-rule-period-in-list-item", rule, { periodMark: "" } }, + { + text: `- [text](http://example.com)`, + options: { + ignoreOnlyLink: true, + periodMark: "." + } + }, { text: ` - item1。 @@ -87,6 +94,20 @@ tester.run("textlint-rule-period-in-list-item", rule, { } ] }, + { + text: `- [text](http://example.com) is bad`, + options: { + ignoreOnlyLink: true, + periodMark: "." + }, + errors: [ + { + "message": `Not exist period mark(".") at end of list item.`, + line: 1, + column: 35 + } + ] + }, // multiple match { text: `