Skip to content

Commit 9663f96

Browse files
committed
fix: ignore relative word like "今日", "明日"
1 parent 626ca00 commit 9663f96

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/textlint-rule-date-weekday-mismatch.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ const supportedLang = [
1313
[/^FR/i, "fr"],
1414
[/^ZH/i, "zh"]
1515
];
16-
16+
/**
17+
* text should be includes number
18+
* @param {Object} chronoDate
19+
* @returns {boolean}
20+
*/
21+
const textIncludesNumber = (chronoDate) => {
22+
return /[0-9-]/.test(chronoDate.text);
23+
};
1724
/**
1825
* detect lang and return language string
1926
* @param {string[]} tags
@@ -44,19 +51,27 @@ function reporter(context, config = {}) {
4451
if (typeof Intl === "undefined") {
4552
throw new Error("Not support your Node.js/browser. should be use latest version.");
4653
}
54+
4755
return {
4856
[Syntax.Str](node){
4957
const text = getSource(node);
5058
const chronoDate = chrono.parse(text);
51-
chronoDate.forEach(chronoDate => {
59+
// ignore "今日" text
60+
chronoDate.filter(textIncludesNumber).forEach(chronoDate => {
5261
const lang = detectLang(Object.keys(chronoDate.tags), preferLang);
5362
if (!lang) {
5463
// not found lang
5564
return;
5665
}
5766
// get weekday from actual date string
5867
const kV = chronoDate.start.knownValues;
59-
const $moment = moment(`${kV.year}-${kV.month}-${kV.day}`, "YYYY-MM-DD", lang);
68+
let $moment;
69+
try {
70+
$moment = moment(`${kV.year}-${kV.month}-${kV.day}`, "YYYY-MM-DD", lang);
71+
} catch (error) {
72+
// parse error is ignore
73+
return;
74+
}
6075
if (!$moment.isValid()) {
6176
return;
6277
}

test/textlint-rule-date-weekday-mismatch-test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ tester.run("rule", rule, {
1010
"2016-12-29(Thursday)",
1111
"2016-12-29(Tsu)",
1212
// invalid date should be ignored
13-
"11月 25日 (火曜日) "
13+
"11月 25日 (火曜日) ",
14+
// ignore relative word
15+
"今日は日曜 (火曜日) "
1416
],
1517
invalid: [
1618
// single match

0 commit comments

Comments
 (0)