Skip to content

Commit

Permalink
feat: use a template for a book search
Browse files Browse the repository at this point in the history
  • Loading branch information
anpigon committed Aug 14, 2022
1 parent 19b03e1 commit b712c57
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CursorJumper } from './editor/cursor_jumper';
import { Book } from './models/book.model';

import { BookSearchSettingTab, BookSearchPluginSettings, DEFAULT_SETTINGS } from './settings/settings';
import { replaceVariableSyntax, makeFileName, makeFrontMater } from './utils/utils';
import { replaceVariableSyntax, makeFileName, makeFrontMater, getTemplateContents } from './utils/utils';

type MetadataWriter = (book: Book, metadata: string) => Promise<void>;

Expand Down Expand Up @@ -48,10 +48,18 @@ export default class BookSearchPlugin extends Plugin {
}
frontMatter = frontMatter.trim();

const content = replaceVariableSyntax(book, this.settings.content);
const fileContent = frontMatter ? `---\n${frontMatter}\n---\n${content}` : content;
let renderedContents = '';

await writer(book, fileContent);
const templateFile = this.settings.templateFile?.trim();
if (templateFile) {
const templateContents = await getTemplateContents(this.app, templateFile);
renderedContents = replaceVariableSyntax(book, templateContents);
} else {
const content = replaceVariableSyntax(book, this.settings.content);
renderedContents = frontMatter ? `---\n${frontMatter}\n---\n${content}` : content;
}

await writer(book, renderedContents);

// cursor focus
await new CursorJumper(this.app).jumpToNextCursorLocation();
Expand Down

0 comments on commit b712c57

Please sign in to comment.