Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ But, You can specify `2016-12-30` is `ja-JP` text by options
}
```

- `useCurrentYearIfMissing`: boolean
- Default: false
- If true, when the year is missing in the date string (e.g. `4月23日(月)`), the current year will be automatically added for validation.
- This is useful for documents that often omit the year in dates.

```json
{
"rules": {
"date-weekday-mismatch": {
"useCurrentYearIfMissing": true
}
}
}
```

language format following ISO 639-1.

e.g.) `en-US`, `en`, `ja` etc..
Expand Down
49 changes: 48 additions & 1 deletion src/textlint-rule-date-weekday-mismatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,38 @@ const detectLang = (tags, preferLang) => {
const selectedLang = targetLangs[0];
return selectedLang[1];
};

/**
* Add current year to date string if missing.
* @param {string} dateText
* @param {number} year
* @param {string} lang
* @returns {string}
*/
function addYearToDateText(dateText, year, lang) {
// Japanese: 4月23日(月) → 2024年4月23日(月)
if (lang === "ja") {
return `${year}年${dateText}`;
}
// Slash: 4/23(月) → 2024/4/23(月)
if (/^[0-9]{1,2}\/[0-9]{1,2}/.test(dateText)) {
return `${year}/${dateText}`;
}
// Dash: 4-23(Mon) → 2024-4-23(Mon)
if (/^[0-9]{1,2}-[0-9]{1,2}/.test(dateText)) {
return `${year}-${dateText}`;
}
// Default: prepend year and a space
return `${year} ${dateText}`;
}
/**
*
* @param context
* @param {Object} [config]
*/
function reporter(context, config = {}) {
const preferLang = config.lang;
const useCurrentYearIfMissing = config.useCurrentYearIfMissing;
const {Syntax, RuleError, report, fixer, getSource} = context;
if (typeof Intl === "undefined") {
throw new Error("Not support your Node.js/browser. should be use latest version.");
Expand All @@ -65,7 +90,29 @@ function reporter(context, config = {}) {
return {
[Syntax.Str](node){
const text = getSource(node);
const chronoDates = chrono.parse(text);
let chronoDates = chrono.parse(text);
// Add current year if missing and option is enabled
if (useCurrentYearIfMissing) {
const currentYear = (new Date()).getFullYear();
chronoDates.forEach(chronoDate => {
// If year is not specified in the parsed result
if (
chronoDate.start &&
chronoDate.start.knownValues.year === undefined
) {
// Detect language for the date string
const lang = detectLang(Object.keys(chronoDate.tags), preferLang);
if (!lang) return;
// Re-parse the text with the year added
const newText = addYearToDateText(chronoDate.text, currentYear, lang);
const reparsed = chrono.parse(newText, undefined, {forwardDate: true});
// If reparsed successfully, update knownValues with year/month/day
if (reparsed && reparsed[0] && reparsed[0].start && reparsed[0].start.knownValues) {
Object.assign(chronoDate.start.knownValues, reparsed[0].start.knownValues);
}
}
});
}
// ignore "今日" text
// ignore not valid data
const filteredChronoDates = chronoDates.filter(textIncludesNumber).filter(yearMonthDayShouldKnownValues);
Expand Down
39 changes: 37 additions & 2 deletions test/textlint-rule-date-weekday-mismatch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ tester.run("rule", rule, {
// invalid date should be ignored
"11月 25日 (火曜日) ",
// ignore relative word
"今日(火曜日)はどうしよう"
"今日(火曜日)はどうしよう",
// useCurrentYearIfMissing option: valid
{
text: "4月23日(水)",
options: { useCurrentYearIfMissing: true, lang: "ja" },
// 2025年4月23日は水曜日
},
{
text: "4/23(Wed)",
options: { useCurrentYearIfMissing: true, lang: "en" },
// 2025-04-23 is Wednesday
},
],
invalid: [
// single match
Expand Down Expand Up @@ -122,6 +133,30 @@ tester.run("rule", rule, {
}
]
},

// useCurrentYearIfMissing option: invalid
{
text: "4月23日(金)",
output: "4月23日(水)",
options: { useCurrentYearIfMissing: true, lang: "ja" },
errors: [
{
message: "4月23日(金) mismatch weekday.\n4月23日(金) => 4月23日(水)",
line: 1,
column: 7
}
]
},
{
text: "4/23(Fri)",
output: "4/23(Wed)",
options: { useCurrentYearIfMissing: true, lang: "en" },
errors: [
{
message: "4/23(Fri) mismatch weekday.\n4/23(Fri) => 4/23(Wed)",
line: 1,
column: 6
}
]
},
]
});