Skip to content

Commit

Permalink
add: function getTemplateContents in utils
Browse files Browse the repository at this point in the history
  • Loading branch information
anpigon committed Aug 14, 2022
1 parent 5300e6b commit 19b03e1
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -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]*$/;
Expand All @@ -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}`;
}
Expand Down Expand Up @@ -142,3 +143,20 @@ function replacer(str: string, reg: RegExp, replaceValue) {
return replaceValue;
});
}

export async function getTemplateContents(app: App, templatePath: string | undefined): Promise<string> {
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 '';
}
}

0 comments on commit 19b03e1

Please sign in to comment.