diff --git a/src/main.ts b/src/main.ts index 7fef828..8411542 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,17 +1,12 @@ import { MarkdownView, Notice, Plugin } from 'obsidian'; + import { BookSearchModal } from './book_search_modal'; import { BookSuggestModal } from './book_suggest_modal'; -import { CursorJumper } from './editor/cursor_jumper'; -import { Book } from './models/book.model'; - -import { BookSearchSettingTab, BookSearchPluginSettings, DEFAULT_SETTINGS } from './settings/settings'; -import { - replaceVariableSyntax, - makeFileName, - makeFrontMater, - getTemplateContents, - applyTemplateTransformations, -} from './utils/utils'; +import { CursorJumper } from '@editor/cursor_jumper'; +import { Book } from '@models/book.model'; +import { BookSearchSettingTab, BookSearchPluginSettings, DEFAULT_SETTINGS } from '@settings/settings'; +import { getTemplateContents, applyTemplateTransformations } from '@utils/template'; +import { replaceVariableSyntax, makeFileName, makeFrontMater } from '@utils/utils'; type MetadataWriter = (book: Book, metadata: string) => Promise; diff --git a/src/utils/template.ts b/src/utils/template.ts new file mode 100644 index 0000000..9146773 --- /dev/null +++ b/src/utils/template.ts @@ -0,0 +1,43 @@ +import { App, normalizePath, Notice } from 'obsidian'; + +export async function getTemplateContents(app: App, templatePath: string | undefined): Promise { + const { metadataCache, vault } = app; + const normalizedTemplatePath = normalizePath(templatePath ?? ''); + if (templatePath === '/') { + return Promise.resolve(''); + } + + try { + const templateFile = metadataCache.getFirstLinkpathDest(normalizedTemplatePath, ''); + return templateFile ? vault.cachedRead(templateFile) : ''; + } catch (err) { + console.error(`Failed to read the daily note template '${normalizedTemplatePath}'`, err); + new Notice('Failed to read the daily note template'); + return ''; + } +} + +export function applyTemplateTransformations(rawTemplateContents: string): string { + return rawTemplateContents.replace( + /{{\s*(date|time)\s*(([+-]\d+)([yqmwdhs]))?\s*(:.+?)?}}/gi, + (_, _timeOrDate, calc, timeDelta, unit, momentFormat) => { + const now = window.moment(); + const currentDate = window + .moment() + .clone() + .set({ + hour: now.get('hour'), + minute: now.get('minute'), + second: now.get('second'), + }); + if (calc) { + currentDate.add(parseInt(timeDelta, 10), unit); + } + + if (momentFormat) { + return currentDate.format(momentFormat.substring(1).trim()); + } + return currentDate.format('YYYY-MM-DD'); + }, + ); +} diff --git a/src/utils/utils.ts b/src/utils/utils.ts index ca8250c..3f9b46b 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,6 +1,5 @@ import { Book, FrontMatter } from '@models/book.model'; import { DefaultFrontmatterKeyType } from '@settings/settings'; -import { App, normalizePath, Notice } from 'obsidian'; // == Format Syntax == // export const NUMBER_REGEX = /^-?[0-9]*$/; @@ -143,45 +142,3 @@ function replacer(str: string, reg: RegExp, replaceValue) { return replaceValue; }); } - -export async function getTemplateContents(app: App, templatePath: string | undefined): Promise { - const { metadataCache, vault } = app; - const normalizedTemplatePath = normalizePath(templatePath ?? ''); - if (templatePath === '/') { - return Promise.resolve(''); - } - - try { - const templateFile = metadataCache.getFirstLinkpathDest(normalizedTemplatePath, ''); - return templateFile ? vault.cachedRead(templateFile) : ''; - } catch (err) { - console.error(`Failed to read the daily note template '${normalizedTemplatePath}'`, err); - new Notice('Failed to read the daily note template'); - return ''; - } -} - -export function applyTemplateTransformations(rawTemplateContents: string): string { - return rawTemplateContents.replace( - /{{\s*(date|time)\s*(([+-]\d+)([yqmwdhs]))?\s*(:.+?)?}}/gi, - (_, _timeOrDate, calc, timeDelta, unit, momentFormat) => { - const now = window.moment(); - const currentDate = window - .moment() - .clone() - .set({ - hour: now.get('hour'), - minute: now.get('minute'), - second: now.get('second'), - }); - if (calc) { - currentDate.add(parseInt(timeDelta, 10), unit); - } - - if (momentFormat) { - return currentDate.format(momentFormat.substring(1).trim()); - } - return currentDate.format('YYYY-MM-DD'); - }, - ); -}