fix: drop relative format for now, chrono parse
This commit is contained in:
+3
-26
@@ -152,44 +152,23 @@ const parseNaturalLanguageDate = (query: string, language: string): DateRange |
|
||||
if (!query.trim()) return null
|
||||
|
||||
try {
|
||||
// Get language-specific parser
|
||||
const parser = getChronoParser(language)
|
||||
|
||||
// Try to parse the date using chrono with language-specific parser first
|
||||
let parsed = parser.parse(query)
|
||||
|
||||
// If failed and not already using English parser, try English as fallback
|
||||
if ((!parsed || parsed.length === 0) && parser !== chrono.en) {
|
||||
parsed = chrono.en.parse(query)
|
||||
}
|
||||
|
||||
if (!parsed || parsed.length === 0) return null
|
||||
|
||||
const result = parsed[0]
|
||||
if (!result) return null
|
||||
|
||||
// Get the start date
|
||||
const startDate = result.start.date()
|
||||
const start = dayjs(startDate).startOf("day")
|
||||
const start = dayjs(result.start.date())
|
||||
const end = result.end ? dayjs(result.end.date()) : dayjs()
|
||||
|
||||
// Check if there's an end date (for ranges)
|
||||
let end: dayjs.Dayjs
|
||||
if (result.end) {
|
||||
const endDate = result.end.date()
|
||||
end = dayjs(endDate).startOf("day")
|
||||
} else {
|
||||
// If no end date, use the start date as a single day
|
||||
end = start
|
||||
}
|
||||
|
||||
// Validate the dates are valid
|
||||
if (!start.isValid() || !end.isValid()) return null
|
||||
|
||||
// Ensure start is before or equal to end
|
||||
if (start.isAfter(end)) {
|
||||
return { start: end, end: start }
|
||||
}
|
||||
|
||||
return { start, end }
|
||||
} catch {
|
||||
return null
|
||||
@@ -205,15 +184,13 @@ export const createDateMentionBuilder = (context: DateMentionBuilderContext) =>
|
||||
const today = dayjs().startOf("day")
|
||||
const mentions: MentionData[] = []
|
||||
|
||||
// Try to parse as natural language date first
|
||||
if (normalized) {
|
||||
const naturalDateRange = parseNaturalLanguageDate(normalized, context.language)
|
||||
if (naturalDateRange) {
|
||||
// Successfully parsed a natural language date
|
||||
const chronoMention = createDateMentionData({
|
||||
range: naturalDateRange,
|
||||
translate: context.t,
|
||||
displayName: formatLocalizedRange(naturalDateRange, context.language),
|
||||
displayName: query,
|
||||
})
|
||||
mentions.push(chronoMention)
|
||||
}
|
||||
|
||||
+1
-32
@@ -125,13 +125,7 @@ export const createDateMentionData = ({
|
||||
translate: LabelTranslator
|
||||
displayName?: string
|
||||
}): DateMentionData => {
|
||||
// For relative dates with an id, use the relative format
|
||||
// For absolute dates without an id, use the absolute range format
|
||||
const isRelativeDate = id?.startsWith("date:relative:")
|
||||
const value =
|
||||
isRelativeDate && id
|
||||
? `<mention-date relative="${id.replace("date:relative:", "")}"></mention-date>`
|
||||
: formatRangeValue(range)
|
||||
const value = formatRangeValue(range)
|
||||
const text = value // Use the same value for text
|
||||
|
||||
const resolvedName = displayName ?? resolveMentionLabel(label, translate) ?? value
|
||||
@@ -147,32 +141,7 @@ export const createDateMentionData = ({
|
||||
}
|
||||
}
|
||||
|
||||
const parseRelativeDateRange = (value: string): DateRange | null => {
|
||||
const relativeMatch = value.match(/relative="([^"]+)"/)
|
||||
if (!relativeMatch) return null
|
||||
|
||||
const [, relativeKey] = relativeMatch
|
||||
if (!relativeKey) return null
|
||||
|
||||
// Add back the prefix to match with definition id
|
||||
const relativeId = `date:relative:${relativeKey}`
|
||||
const today = dayjs().startOf("day")
|
||||
const definition = RELATIVE_DATE_DEFINITIONS.find((def) => def.id === relativeId)
|
||||
if (!definition) return null
|
||||
|
||||
const range = definition.range(today)
|
||||
if (!range) return null
|
||||
|
||||
return range
|
||||
}
|
||||
|
||||
export const parseRangeValue = (value: string): DateRange | null => {
|
||||
// Handle relative date format: <mention-date relative="date:relative:today"></mention-date>
|
||||
const relativeResult = parseRelativeDateRange(value)
|
||||
if (relativeResult) {
|
||||
return relativeResult
|
||||
}
|
||||
|
||||
// Parse XML format: <mention-date start="YYYY-MM-DD" end="YYYY-MM-DD"></mention-date>
|
||||
const match = value.match(/start="([^"]+)"\s+end="([^"]+)"/)
|
||||
if (!match) return null
|
||||
|
||||
Reference in New Issue
Block a user