Skip to content

Commit

Permalink
Fix #966; Time trigger may be recognized as a date trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmeyers committed May 29, 2024
1 parent d9abb7e commit fbd8d65
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/components/Editor/suggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ export class TimeSuggest extends EditorSuggest<string> {
if (!stateManager) return null;

const timeTrigger = stateManager.getSetting('time-trigger');
const timeTriggerRegex = new RegExp(`\\B${escapeRegExpStr(timeTrigger as string)}{?([^}]*)$`);
const timeTriggerRegex = new RegExp(
`(?:^|\\s)${escapeRegExpStr(timeTrigger as string)}{?([^}]*)$`
);
const textCtx = (editor.getLine(cursor.line) || '').slice(0, cursor.ch);
const match = textCtx.match(timeTriggerRegex);

Expand Down
24 changes: 12 additions & 12 deletions src/parsers/helpers/hydrateBoard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,44 @@ export function preprocessTitle(stateManager: StateManager, title: string) {
};

title = title.replace(
new RegExp(`${escapeRegExpStr(dateTrigger)}\\[\\[([^\\]]+)\\]\\]`, 'g'),
(match, content) => {
new RegExp(`(^|\\s)${escapeRegExpStr(dateTrigger)}\\[\\[([^\\]]+)\\]\\]`, 'g'),
(match, space, content) => {
const parsed = moment(content, dateFormat);
if (!parsed.isValid()) return match;
date = parsed;
const linkPath = app.metadataCache.getFirstLinkpathDest(content, stateManager.file.path);
if (!dateColor) dateColor = getDateColor(parsed);
const { wrapperClass, wrapperStyle } = getWrapperStyles(c('preview-date-wrapper'));
return `<span data-date="${date.toISOString()}" class="${wrapperClass} ${c('date')} ${c('preview-date-link')}"${wrapperStyle}><a class="${c('preview-date')} internal-link" data-href="${linkPath?.path ?? content}" href="${linkPath?.path ?? content}" target="_blank" rel="noopener">${parsed.format(dateDisplayFormat)}</a></span>`;
return `${space}<span data-date="${date.toISOString()}" class="${wrapperClass} ${c('date')} ${c('preview-date-link')}"${wrapperStyle}><a class="${c('preview-date')} internal-link" data-href="${linkPath?.path ?? content}" href="${linkPath?.path ?? content}" target="_blank" rel="noopener">${parsed.format(dateDisplayFormat)}</a></span>`;
}
);
title = title.replace(
new RegExp(`${escapeRegExpStr(dateTrigger)}\\[([^\\]]+)\\]\\([^)]+\\)`, 'g'),
(match, content) => {
new RegExp(`(^|\\s)${escapeRegExpStr(dateTrigger)}\\[([^\\]]+)\\]\\([^)]+\\)`, 'g'),
(match, space, content) => {
const parsed = moment(content, dateFormat);
if (!parsed.isValid()) return match;
date = parsed;
const linkPath = app.metadataCache.getFirstLinkpathDest(content, stateManager.file.path);
if (!dateColor) dateColor = getDateColor(parsed);
const { wrapperClass, wrapperStyle } = getWrapperStyles(c('preview-date-wrapper'));
return `<span data-date="${date.toISOString()}" class="${wrapperClass} ${c('date')} ${c('preview-date-link')}"${wrapperStyle}><a class="${c('preview-date')} internal-link" data-href="${linkPath?.path ?? content}" href="${linkPath?.path ?? content}" target="_blank" rel="noopener">${parsed.format(dateDisplayFormat)}</a></span>`;
return `${space}<span data-date="${date.toISOString()}" class="${wrapperClass} ${c('date')} ${c('preview-date-link')}"${wrapperStyle}><a class="${c('preview-date')} internal-link" data-href="${linkPath?.path ?? content}" href="${linkPath?.path ?? content}" target="_blank" rel="noopener">${parsed.format(dateDisplayFormat)}</a></span>`;
}
);
title = title.replace(
new RegExp(`${escapeRegExpStr(dateTrigger)}{([^}]+)}`, 'g'),
(match, content) => {
new RegExp(`(^|\\s)${escapeRegExpStr(dateTrigger)}{([^}]+)}`, 'g'),
(match, space, content) => {
const parsed = moment(content, dateFormat);
if (!parsed.isValid()) return match;
date = parsed;
if (!dateColor) dateColor = getDateColor(parsed);
const { wrapperClass, wrapperStyle } = getWrapperStyles(c('preview-date-wrapper'));
return `<span data-date="${date.toISOString()}" class="${wrapperClass} ${c('date')}"${wrapperStyle}><span class="${c('preview-date')} ${c('item-metadata-date')}">${parsed.format(dateDisplayFormat)}</span></span>`;
return `${space}<span data-date="${date.toISOString()}" class="${wrapperClass} ${c('date')}"${wrapperStyle}><span class="${c('preview-date')} ${c('item-metadata-date')}">${parsed.format(dateDisplayFormat)}</span></span>`;
}
);

title = title.replace(
new RegExp(`${escapeRegExpStr(timeTrigger)}{([^}]+)}`, 'g'),
(match, content) => {
new RegExp(`(^|\\s)${escapeRegExpStr(timeTrigger)}{([^}]+)}`, 'g'),
(match, space, content) => {
const parsed = moment(content, timeFormat);
if (!parsed.isValid()) return match;

Expand All @@ -89,7 +89,7 @@ export function preprocessTitle(stateManager: StateManager, title: string) {
}

const { wrapperClass, wrapperStyle } = getWrapperStyles(c('preview-time-wrapper'));
return `<span data-date="${date.toISOString()}" class="${wrapperClass} ${c('date')}"${wrapperStyle}><span class="${c('preview-time')} ${c('item-metadata-time')}">${parsed.format(timeFormat)}</span></span>`;
return `${space}<span data-date="${date.toISOString()}" class="${wrapperClass} ${c('date')}"${wrapperStyle}><span class="${c('preview-time')} ${c('item-metadata-time')}">${parsed.format(timeFormat)}</span></span>`;
}
);

Expand Down

0 comments on commit fbd8d65

Please sign in to comment.