Skip to content

Commit

Permalink
Implement templater note creation
Browse files Browse the repository at this point in the history
  • Loading branch information
H3mul committed Jun 3, 2024
1 parent b5b87a2 commit 6c5abb0
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 37 deletions.
66 changes: 37 additions & 29 deletions src/lib/dataApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
type DataValue,
type Optional,
} from "./dataframe/dataframe";
import { nextUniqueProjectName, notEmpty, getNameFromPath } from "./helpers";
import { nextUniqueProjectName, notEmpty, getNameFromPath, getTemplater } from "./helpers";
import { decodeFrontMatter, encodeFrontMatter } from "./metadata";
import { i18n } from "./stores/i18n";
import { settings } from "./stores/settings";
Expand Down Expand Up @@ -94,38 +94,44 @@ export class DataApi {
);
}

async createNote(record: DataRecord, templatePath: string): Promise<void> {
let content = "";
async createNote(record: DataRecord, templatePath: string, useTemplater: boolean): Promise<void> {
const templater = getTemplater();
let file;
let content;

if (templatePath) {
const file = this.fileSystem.getFile(templatePath);
if (file) {
content = await file.read();
content = interpolateTemplate(content, {
title: () => getNameFromPath(record.id),
date: (format) => moment().format(format || "YYYY-MM-DD"),
time: (format) => moment().format(format || "HH:mm"),
});
if (record.values["tags"]) {
const templateTags = F.pipe(
content,
decodeFrontMatter,
E.map((frontmatter) => frontmatter["tags"]),
E.fold(
() => [],
(right) => right ?? [] // handle `null`
)
);
//@ts-ignore explict input in `createDataRecord()`
const tagSet: Set<string> = new Set(
templateTags.concat(record.values["tags"])
);
record.values["tags"] = [...tagSet];
}
}
content = await this.fileSystem.getFile(templatePath)?.read();
}

const file = await this.fileSystem.create(record.id, content);
if (!content) {
file = await this.fileSystem.create(record.id, "");
} else if (useTemplater && templater) {
const newTemplaterFile = await templater.templater.create_new_note_from_template(content, record.path, record.name, false);
file = this.fileSystem.getFile(newTemplaterFile.path)!;

Check warning on line 110 in src/lib/dataApi.ts

View workflow job for this annotation

GitHub Actions / build

Forbidden non-null assertion
} else {
content = interpolateTemplate(content, {
title: () => getNameFromPath(record.id),
date: (format) => moment().format(format || "YYYY-MM-DD"),
time: (format) => moment().format(format || "HH:mm"),
});
if (record.values["tags"]) {
const templateTags = F.pipe(
content,
decodeFrontMatter,
E.map((frontmatter) => frontmatter["tags"]),
E.fold(
() => [],
(right) => right ?? [] // handle `null`
)
);
//@ts-ignore explict input in `createDataRecord()`
const tagSet: Set<string> = new Set(
templateTags.concat(record.values["tags"])
);
record.values["tags"] = [...tagSet];
}
file = await this.fileSystem.create(record.id, content);
}

await this.updateFile(file, (data) => doUpdateRecord(data, [], record))();
}
Expand Down Expand Up @@ -282,6 +288,8 @@ export function createDataRecord(
}

return {
name: name,
path: normalizePath(path),
id: normalizePath(path + "/" + name + ".md"),
values: values ?? {},
};
Expand Down
2 changes: 2 additions & 0 deletions src/lib/dataframe/dataframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export enum DataFieldType {
}

export type DataRecord = {
readonly name?: string;
readonly path?: string;
readonly id: string;
readonly values: Record<string, Optional<DataValue>>;
};
Expand Down
11 changes: 11 additions & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,14 @@ export function makeContext<T>(): Context<T> {
set: (value: T) => setContext(key, value),
};
}

export function getTemplater(): any {
return get(app).plugins.plugins["templater-obsidian"];
}

export async function createNoteFromTemplaterTemplate(templateFile: string, folderPath: string, filename: string): Promise<void> {
const templater = getTemplater();
if (!templater) return;

await templater.templater.create_new_note_from_template();
}
4 changes: 2 additions & 2 deletions src/lib/viewApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import type { DataSource } from "./datasources";
export class ViewApi {
constructor(readonly dataSource: DataSource, readonly dataApi: DataApi) {}

addRecord(record: DataRecord, templatePath: string) {
addRecord(record: DataRecord, templatePath: string, useTemplater: boolean) {
if (this.dataSource.includes(record.id)) {
dataFrame.addRecord(record);
}
this.dataApi.createNote(record, templatePath);
this.dataApi.createNote(record, templatePath, useTemplater);
}

updateRecord(record: DataRecord, fields: DataField[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default class ProjectsPlugin extends Plugin {
project,
async (name, templatePath, project) => {
const record = createDataRecord(name, project);
await get(api).createNote(record, templatePath);
await get(api).createNote(record, templatePath, project.useTemplater);

// Open the created note in a new tab.
const file = this.app.vault.getAbstractFileByPath(record.id);
Expand Down
4 changes: 3 additions & 1 deletion src/ui/modals/components/CreateProject.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { FileListInput } from "src/ui/components/FileListInput";
import { Accordion, AccordionItem } from "src/ui/components/Accordion";
import { notEmpty } from "src/lib/helpers";
import { getTemplater, notEmpty } from "src/lib/helpers";
import { getFoldersInFolder, isValidPath } from "src/lib/obsidian";
import { capabilities } from "src/lib/stores/capabilities";
import { i18n } from "src/lib/stores/i18n";
Expand Down Expand Up @@ -325,6 +325,7 @@
/>
</SettingItem>

{#if !!getTemplater()}
<SettingItem
name={$i18n.t("modals.project.templater.name")}
description={$i18n.t("modals.project.templater.description") ?? ""}
Expand All @@ -335,6 +336,7 @@
(project = { ...project, useTemplater })}
/>
</SettingItem>
{/if}

<SettingItem
name={$i18n.t("modals.project.exclude.name")}
Expand Down
3 changes: 2 additions & 1 deletion src/ui/views/Board/BoardView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@
}
: {}
),
templatePath
templatePath,
project.useTemplater
);
}).open();
};
Expand Down
3 changes: 2 additions & 1 deletion src/ui/views/Calendar/CalendarView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@
createDataRecord(name, project, {
[dateField.name]: date.toDate(),
}),
templatePath
templatePath,
project.useTemplater
);
}
}).open();
Expand Down
2 changes: 1 addition & 1 deletion src/ui/views/Gallery/GalleryView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
size="lg"
onClick={() => {
new CreateNoteModal($app, project, (name, templatePath, project) => {
api.addRecord(createDataRecord(name, project), templatePath);
api.addRecord(createDataRecord(name, project), templatePath, project.useTemplater);
}).open();
}}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/ui/views/Table/TableView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
}}
onRowAdd={() => {
new CreateNoteModal($app, project, (name, templatePath, project) => {
api.addRecord(createDataRecord(name, project), templatePath);
api.addRecord(createDataRecord(name, project), templatePath, project.useTemplater);
}).open();
}}
onRowEdit={(id, values) => {
Expand Down

0 comments on commit 6c5abb0

Please sign in to comment.