fix: don't parse dates in urls

Resolves #2353
This commit is contained in:
kolaente 2022-09-15 10:23:37 +02:00
parent 49217889b5
commit 92f24e59a7
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
2 changed files with 32 additions and 25 deletions

View file

@ -118,7 +118,7 @@ const addTimeToDate = (text: string, date: Date, match: string | null): datePars
}
export const getDateFromText = (text: string, now: Date = new Date()) => {
const fullDateRegex: RegExp = /([0-9][0-9]?\/[0-9][0-9]?\/[0-9][0-9]([0-9][0-9])?|[0-9][0-9][0-9][0-9]\/[0-9][0-9]?\/[0-9][0-9]?|[0-9][0-9][0-9][0-9]-[0-9][0-9]?-[0-9][0-9]?)/ig
const fullDateRegex: RegExp = / ([0-9][0-9]?\/[0-9][0-9]?\/[0-9][0-9]([0-9][0-9])?|[0-9][0-9][0-9][0-9]\/[0-9][0-9]?\/[0-9][0-9]?|[0-9][0-9][0-9][0-9]-[0-9][0-9]?-[0-9][0-9]?)/ig
// 1. Try parsing the text as a "usual" date, like 2021-06-24 or 06/24/2021
let results: string[] | null = fullDateRegex.exec(text)
@ -127,7 +127,7 @@ export const getDateFromText = (text: string, now: Date = new Date()) => {
let containsYear: boolean = true
if (result === null) {
// 2. Try parsing the date as something like "jan 21" or "21 jan"
const monthRegex: RegExp = /((jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec) [0-9][0-9]?|[0-9][0-9]? (jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec))/ig
const monthRegex: RegExp = / ((jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec) [0-9][0-9]?|[0-9][0-9]? (jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec))/ig
results = monthRegex.exec(text)
result = results === null ? null : `${results[0]} ${now.getFullYear()}`
foundText = results === null ? '' : results[0]
@ -135,7 +135,7 @@ export const getDateFromText = (text: string, now: Date = new Date()) => {
if (result === null) {
// 3. Try parsing the date as "27/01" or "01/27"
const monthNumericRegex: RegExp = /([0-9][0-9]?\/[0-9][0-9]?)/ig
const monthNumericRegex: RegExp = / ([0-9][0-9]?\/[0-9][0-9]?)/ig
results = monthNumericRegex.exec(text)
// Put the year before or after the date, depending on what works

View file

@ -356,6 +356,13 @@ describe('Parse Task Text', () => {
expect(result.text).toBe('Lorem Ispum v1.1.1')
expect(result.date).toBeNull()
})
it('should not recognize dates in urls', () => {
const text = 'https://some-url.org/blog/2019/1/233526-some-more-text'
const result = parseTaskText(text)
expect(result.text).toBe(text)
expect(result.date).toBeNull()
})
describe('Parse weekdays', () => {
@ -432,28 +439,28 @@ describe('Parse Task Text', () => {
const cases = {
'Lorem Ipsum 06/08/2021 ad': '2021-6-8',
'Lorem Ipsum 6/7/21 ad': '2021-6-7',
'27/07/2021,': null,
'2021/07/06,': '2021-7-6',
'2021-07-06': '2021-7-6',
'27 jan': '2022-1-27',
'27/1': '2022-1-27',
'27/01': '2022-1-27',
'16/12': '2021-12-16',
'01/27': '2022-1-27',
'1/27': '2022-1-27',
'Jan 27': '2022-1-27',
'jan 27': '2022-1-27',
'feb 21': '2022-2-21',
'mar 21': '2022-3-21',
'apr 21': '2022-4-21',
'may 21': '2022-5-21',
'jun 21': '2022-6-21',
'jul 21': '2021-7-21',
'aug 21': '2021-8-21',
'sep 21': '2021-9-21',
'oct 21': '2021-10-21',
'nov 21': '2021-11-21',
'dec 21': '2021-12-21',
'dolor sit amet 27/07/2021,': null,
'dolor sit amet 2021/07/06,': '2021-7-6',
'dolor sit amet 2021-07-06': '2021-7-6',
'dolor sit amet 27 jan': '2022-1-27',
'dolor sit amet 27/1': '2022-1-27',
'dolor sit amet 27/01': '2022-1-27',
'dolor sit amet 16/12': '2021-12-16',
'dolor sit amet 01/27': '2022-1-27',
'dolor sit amet 1/27': '2022-1-27',
'dolor sit amet Jan 27': '2022-1-27',
'dolor sit amet jan 27': '2022-1-27',
'dolor sit amet feb 21': '2022-2-21',
'dolor sit amet mar 21': '2022-3-21',
'dolor sit amet apr 21': '2022-4-21',
'dolor sit amet may 21': '2022-5-21',
'dolor sit amet jun 21': '2022-6-21',
'dolor sit amet jul 21': '2021-7-21',
'dolor sit amet aug 21': '2021-8-21',
'dolor sit amet sep 21': '2021-9-21',
'dolor sit amet oct 21': '2021-10-21',
'dolor sit amet nov 21': '2021-11-21',
'dolor sit amet dec 21': '2021-12-21',
}
for (const c in cases) {