@real1ty-obsidian-plugins/utils 2.20.0 → 2.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dist/core/indexer.d.ts +5 -0
  2. package/dist/core/indexer.d.ts.map +1 -1
  3. package/dist/core/indexer.js +6 -2
  4. package/dist/core/indexer.js.map +1 -1
  5. package/dist/file/file-utils.d.ts +5 -0
  6. package/dist/file/file-utils.d.ts.map +1 -1
  7. package/dist/file/file-utils.js +21 -0
  8. package/dist/file/file-utils.js.map +1 -1
  9. package/dist/file/file.d.ts +19 -0
  10. package/dist/file/file.d.ts.map +1 -1
  11. package/dist/file/file.js +40 -6
  12. package/dist/file/file.js.map +1 -1
  13. package/dist/file/frontmatter-serialization.d.ts +21 -0
  14. package/dist/file/frontmatter-serialization.d.ts.map +1 -0
  15. package/dist/file/frontmatter-serialization.js +57 -0
  16. package/dist/file/frontmatter-serialization.js.map +1 -0
  17. package/dist/file/index.d.ts +1 -0
  18. package/dist/file/index.d.ts.map +1 -1
  19. package/dist/file/index.js +1 -0
  20. package/dist/file/index.js.map +1 -1
  21. package/dist/file/templater-service.d.ts +1 -3
  22. package/dist/file/templater-service.d.ts.map +1 -1
  23. package/dist/file/templater-service.js +5 -38
  24. package/dist/file/templater-service.js.map +1 -1
  25. package/dist/file/templater.d.ts +10 -1
  26. package/dist/file/templater.d.ts.map +1 -1
  27. package/dist/file/templater.js +57 -22
  28. package/dist/file/templater.js.map +1 -1
  29. package/package.json +2 -1
  30. package/src/core/indexer.ts +12 -3
  31. package/src/file/file-utils.ts +26 -0
  32. package/src/file/file.ts +46 -8
  33. package/src/file/frontmatter-serialization.ts +72 -0
  34. package/src/file/index.ts +1 -0
  35. package/src/file/templater-service.ts +14 -51
  36. package/src/file/templater.ts +75 -25
@@ -1,4 +1,6 @@
1
1
  import { type App, Notice, normalizePath, TFile } from "obsidian";
2
+ import { waitForFileReady } from "./file-utils";
3
+ import { createFileContentWithFrontmatter } from "./frontmatter-serialization";
2
4
 
3
5
  const TEMPLATER_ID = "templater-obsidian";
4
6
 
@@ -45,12 +47,58 @@ export function isTemplaterAvailable(app: App): boolean {
45
47
  return !!instance;
46
48
  }
47
49
 
50
+ /**
51
+ * Checks if a template should be used based on availability and file existence.
52
+ */
53
+ export function shouldUseTemplate(app: App, templatePath: string | undefined): boolean {
54
+ return !!(
55
+ templatePath &&
56
+ templatePath.trim() !== "" &&
57
+ isTemplaterAvailable(app) &&
58
+ app.vault.getFileByPath(templatePath)
59
+ );
60
+ }
61
+
62
+ /**
63
+ * Creates a file manually with optional frontmatter and content.
64
+ * Returns existing file if it already exists.
65
+ */
66
+ export async function createFileManually(
67
+ app: App,
68
+ targetDirectory: string,
69
+ filename: string,
70
+ content?: string,
71
+ frontmatter?: Record<string, unknown>
72
+ ): Promise<TFile> {
73
+ const baseName = filename.replace(/\.md$/, "");
74
+ const filePath = `${targetDirectory}/${baseName}.md`;
75
+
76
+ // Check if file already exists
77
+ const existingFile = app.vault.getAbstractFileByPath(filePath);
78
+ if (existingFile instanceof TFile) {
79
+ return existingFile;
80
+ }
81
+
82
+ const bodyContent = content || "";
83
+
84
+ let fileContent: string;
85
+ if (frontmatter && Object.keys(frontmatter).length > 0) {
86
+ fileContent = createFileContentWithFrontmatter(frontmatter, bodyContent);
87
+ } else {
88
+ fileContent = bodyContent;
89
+ }
90
+
91
+ const file = await app.vault.create(filePath, fileContent);
92
+ return file;
93
+ }
94
+
48
95
  export async function createFromTemplate(
49
96
  app: App,
50
97
  templatePath: string,
51
98
  targetFolder?: string,
52
99
  filename?: string,
53
- openNewNote = false
100
+ openNewNote = false,
101
+ frontmatter?: Record<string, unknown>
54
102
  ): Promise<TFile | null> {
55
103
  const templater = await waitForTemplater(app);
56
104
  if (!templater) {
@@ -76,7 +124,22 @@ export async function createFromTemplate(
76
124
  openNewNote
77
125
  );
78
126
 
79
- return newFile ?? null;
127
+ if (!newFile) {
128
+ return null;
129
+ }
130
+
131
+ if (frontmatter && Object.keys(frontmatter).length > 0) {
132
+ const readyFile = await waitForFileReady(app, newFile.path);
133
+
134
+ if (readyFile) {
135
+ await app.fileManager.processFrontMatter(readyFile, (fm) => {
136
+ Object.assign(fm, frontmatter);
137
+ });
138
+ return readyFile;
139
+ }
140
+ }
141
+
142
+ return newFile;
80
143
  } catch (error) {
81
144
  console.error("Error creating file from template:", error);
82
145
  new Notice("Error creating file from template. Please ensure the template file is valid.");
@@ -92,40 +155,27 @@ export async function createFileWithTemplate(
92
155
  options;
93
156
 
94
157
  const finalFilename = filename || title;
95
- const baseName = finalFilename.replace(/\.md$/, "");
96
- const filePath = normalizePath(`${targetDirectory}/${baseName}.md`);
97
158
 
98
- const existingFile = app.vault.getAbstractFileByPath(filePath);
99
- if (existingFile instanceof TFile) {
100
- return existingFile;
159
+ // If content is provided, use manual creation to preserve the content
160
+ if (content) {
161
+ return createFileManually(app, targetDirectory, finalFilename, content, frontmatter);
101
162
  }
102
163
 
103
- if (useTemplater && templatePath && templatePath.trim() !== "" && isTemplaterAvailable(app)) {
164
+ // Try to use template if requested and available
165
+ if (useTemplater && shouldUseTemplate(app, templatePath)) {
104
166
  const templateFile = await createFromTemplate(
105
167
  app,
106
- templatePath,
168
+ templatePath!,
107
169
  targetDirectory,
108
- finalFilename
170
+ finalFilename,
171
+ false,
172
+ frontmatter
109
173
  );
110
174
 
111
175
  if (templateFile) {
112
- if (frontmatter && Object.keys(frontmatter).length > 0) {
113
- await app.fileManager.processFrontMatter(templateFile, (fm) => {
114
- Object.assign(fm, frontmatter);
115
- });
116
- }
117
176
  return templateFile;
118
177
  }
119
178
  }
120
179
 
121
- const fileContent = content || "";
122
- const file = await app.vault.create(filePath, fileContent);
123
-
124
- if (frontmatter && Object.keys(frontmatter).length > 0) {
125
- await app.fileManager.processFrontMatter(file, (fm) => {
126
- Object.assign(fm, frontmatter);
127
- });
128
- }
129
-
130
- return file;
180
+ return createFileManually(app, targetDirectory, finalFilename, content, frontmatter);
131
181
  }