diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 287bbb5..ce664a9 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,5 +1,6 @@ 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]*$/; @@ -20,8 +21,8 @@ export function makeFileName(book: Book, fileNameFormat: string) { return titleForFileName; } const authorForFileName = replaceIllegalFileNameCharactersInString(book.author); - if(fileNameFormat) { - return replaceVariableSyntax(book, replaceDateInString(fileNameFormat)) + if (fileNameFormat) { + return replaceVariableSyntax(book, replaceDateInString(fileNameFormat)); } return `${titleForFileName} - ${authorForFileName}`; } @@ -142,3 +143,20 @@ 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 ''; + } +}