From 19b03e14b583f64971b29b9cc949e0db54376333 Mon Sep 17 00:00:00 2001 From: anpigon Date: Sun, 14 Aug 2022 12:47:26 +0900 Subject: [PATCH] add: function getTemplateContents in utils --- src/utils/utils.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) 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 ''; + } +}