@@ -50,13 +50,38 @@ const detectLang = (tags, preferLang) => {
5050 const selectedLang = targetLangs [ 0 ] ;
5151 return selectedLang [ 1 ] ;
5252} ;
53+
54+ /**
55+ * Add current year to date string if missing.
56+ * @param {string } dateText
57+ * @param {number } year
58+ * @param {string } lang
59+ * @returns {string }
60+ */
61+ function addYearToDateText ( dateText , year , lang ) {
62+ // Japanese: 4月23日(月) → 2024年4月23日(月)
63+ if ( lang === "ja" ) {
64+ return `${ year } 年${ dateText } ` ;
65+ }
66+ // Slash: 4/23(月) → 2024/4/23(月)
67+ if ( / ^ [ 0 - 9 ] { 1 , 2 } \/ [ 0 - 9 ] { 1 , 2 } / . test ( dateText ) ) {
68+ return `${ year } /${ dateText } ` ;
69+ }
70+ // Dash: 4-23(Mon) → 2024-4-23(Mon)
71+ if ( / ^ [ 0 - 9 ] { 1 , 2 } - [ 0 - 9 ] { 1 , 2 } / . test ( dateText ) ) {
72+ return `${ year } -${ dateText } ` ;
73+ }
74+ // Default: prepend year and a space
75+ return `${ year } ${ dateText } ` ;
76+ }
5377/**
5478 *
5579 * @param context
5680 * @param {Object } [config]
5781 */
5882function reporter ( context , config = { } ) {
5983 const preferLang = config . lang ;
84+ const useCurrentYearIfMissing = config . useCurrentYearIfMissing ;
6085 const { Syntax, RuleError, report, fixer, getSource} = context ;
6186 if ( typeof Intl === "undefined" ) {
6287 throw new Error ( "Not support your Node.js/browser. should be use latest version." ) ;
@@ -65,7 +90,29 @@ function reporter(context, config = {}) {
6590 return {
6691 [ Syntax . Str ] ( node ) {
6792 const text = getSource ( node ) ;
68- const chronoDates = chrono . parse ( text ) ;
93+ let chronoDates = chrono . parse ( text ) ;
94+ // Add current year if missing and option is enabled
95+ if ( useCurrentYearIfMissing ) {
96+ const currentYear = ( new Date ( ) ) . getFullYear ( ) ;
97+ chronoDates . forEach ( chronoDate => {
98+ // If year is not specified in the parsed result
99+ if (
100+ chronoDate . start &&
101+ chronoDate . start . knownValues . year === undefined
102+ ) {
103+ // Detect language for the date string
104+ const lang = detectLang ( Object . keys ( chronoDate . tags ) , preferLang ) ;
105+ if ( ! lang ) return ;
106+ // Re-parse the text with the year added
107+ const newText = addYearToDateText ( chronoDate . text , currentYear , lang ) ;
108+ const reparsed = chrono . parse ( newText , undefined , { forwardDate : true } ) ;
109+ // If reparsed successfully, update knownValues with year/month/day
110+ if ( reparsed && reparsed [ 0 ] && reparsed [ 0 ] . start && reparsed [ 0 ] . start . knownValues ) {
111+ Object . assign ( chronoDate . start . knownValues , reparsed [ 0 ] . start . knownValues ) ;
112+ }
113+ }
114+ } ) ;
115+ }
69116 // ignore "今日" text
70117 // ignore not valid data
71118 const filteredChronoDates = chronoDates . filter ( textIncludesNumber ) . filter ( yearMonthDayShouldKnownValues ) ;
0 commit comments