fix(quick-add-magic): prevent parsing partial keywords in words (#2140)

- Fixed date parser incorrectly extracting date components from within
words
- "Renovation - 2nd Floor Bath" no longer becomes "Reation - Floor Bath"
with a due date of November 2nd

## Changes
- `getMonthFromText` now requires word boundaries, preventing "nov" from
matching inside "Renovation" or "mar" inside "Remark"
- `getDayFromText` now only matches ordinals when followed by
end-of-string, time expressions, or month names, preventing "2nd Floor"
from being parsed as a date

Resolves
https://community.vikunja.io/t/quick-add-magic-unintended-date-parsing/4259
This commit is contained in:
kolaente 2026-01-23 23:23:42 +01:00 committed by GitHub
parent a86bbfa121
commit 8419794b65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 3 deletions

View File

@ -316,7 +316,9 @@ const getDateFromWeekday = (text: string, date: Date = new Date()): dateFoundRes
}
const getDayFromText = (text: string, now: Date = new Date()) => {
const matcher = /(^| )(([1-2][0-9])|(3[01])|(0?[1-9]))(st|nd|rd|th|\.)($| )/ig
// Only match ordinals when followed by end-of-string, time expressions, or month names
// This prevents matching "2nd Floor" or "13th floor" as dates
const matcher = new RegExp('(^| )(([1-2][0-9])|(3[01])|(0?[1-9]))(st|nd|rd|th|\\.)(?=$| at | @ | ' + monthsRegexGroup + ')', 'ig')
const results = matcher.exec(text)
if (results === null) {
return {
@ -348,7 +350,7 @@ const getDayFromText = (text: string, now: Date = new Date()) => {
}
const getMonthFromText = (text: string, date: Date) => {
const matcher = new RegExp(monthsRegexGroup, 'ig')
const matcher = new RegExp('\\b' + monthsRegexGroup + '\\b', 'ig')
const results = matcher.exec(text)
if (results === null) {

View File

@ -315,7 +315,7 @@ describe('Parse Task Text', () => {
expect(result.text).toBe('Lorem Ipsum github')
expect(result.date).toBeNull()
})
describe('Should not recognize weekdays in words', () => {
describe('Should not recognize partial keywords in words', () => {
const cases = [
'renewed',
'github',
@ -328,6 +328,11 @@ describe('Parse Task Text', () => {
'monitor blood pressure',
'Monitor blood pressure',
'buy almonds',
'Renovation',
'Remark',
'Renovation - 2nd Floor Bath',
'Remark - 13th floor',
'13th floor - remark',
]
cases.forEach(c => {
@ -349,6 +354,12 @@ describe('Parse Task Text', () => {
expect(result.text).toBe(`Lorem Ipsum ${c} dolor`)
expect(result.date).toBeNull()
})
it(`should not recognize text with ${c} as special keywords`, () => {
const result = parseTaskText(c)
expect(result.text).toBe(c)
expect(result.date).toBeNull()
})
})
})
it('should not recognize date number with no spacing around them', () => {