Skip to content

Commit

Permalink
add: template.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
anpigon committed Aug 17, 2022
1 parent 2bc88e6 commit b4afe4f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 54 deletions.
17 changes: 6 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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<void>;

Expand Down
43 changes: 43 additions & 0 deletions src/utils/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { App, normalizePath, Notice } from 'obsidian';

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 '';
}
}

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');
},
);
}
43 changes: 0 additions & 43 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -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]*$/;
Expand Down Expand Up @@ -143,45 +142,3 @@ 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 '';
}
}

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');
},
);
}

0 comments on commit b4afe4f

Please sign in to comment.