Skip to content

Commit

Permalink
fix(rule): add "ignoreLinkEnd" options
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Mar 22, 2017
1 parent 53598bd commit 3a6c7bc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
24 changes: 24 additions & 0 deletions src/textlint-rule-period-in-list-item.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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": [],
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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;
}
Expand Down
21 changes: 21 additions & 0 deletions test/textlint-rule-period-in-list-item-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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。
Expand Down Expand Up @@ -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: `
Expand Down

0 comments on commit 3a6c7bc

Please sign in to comment.