@real1ty-obsidian-plugins/utils 2.19.2 → 2.20.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.
- package/dist/file/index.d.ts +1 -0
- package/dist/file/index.d.ts.map +1 -1
- package/dist/file/index.js +1 -0
- package/dist/file/index.js.map +1 -1
- package/dist/file/templater-service.d.ts +18 -0
- package/dist/file/templater-service.d.ts.map +1 -0
- package/dist/file/templater-service.js +70 -0
- package/dist/file/templater-service.js.map +1 -0
- package/package.json +1 -1
- package/src/file/index.ts +1 -0
- package/src/file/templater-service.ts +93 -0
package/dist/file/index.d.ts
CHANGED
package/dist/file/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/file/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,QAAQ,CAAC;AACvB,cAAc,mBAAmB,CAAC;AAClC,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/file/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,QAAQ,CAAC;AACvB,cAAc,mBAAmB,CAAC;AAClC,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC"}
|
package/dist/file/index.js
CHANGED
package/dist/file/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/file/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,QAAQ,CAAC;AACvB,cAAc,mBAAmB,CAAC;AAClC,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC","sourcesContent":["export * from \"./child-reference\";\nexport * from \"./file\";\nexport * from \"./file-operations\";\nexport * from \"./file-utils\";\nexport * from \"./frontmatter\";\nexport * from \"./frontmatter-diff\";\nexport * from \"./frontmatter-propagation\";\nexport * from \"./link-parser\";\nexport * from \"./property-utils\";\nexport * from \"./templater\";\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/file/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,QAAQ,CAAC;AACvB,cAAc,mBAAmB,CAAC;AAClC,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC","sourcesContent":["export * from \"./child-reference\";\nexport * from \"./file\";\nexport * from \"./file-operations\";\nexport * from \"./file-utils\";\nexport * from \"./frontmatter\";\nexport * from \"./frontmatter-diff\";\nexport * from \"./frontmatter-propagation\";\nexport * from \"./link-parser\";\nexport * from \"./property-utils\";\nexport * from \"./templater\";\nexport * from \"./templater-service\";\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type App, TFile } from "obsidian";
|
|
2
|
+
import type { FileCreationOptions } from "./templater";
|
|
3
|
+
export type { FileCreationOptions };
|
|
4
|
+
export declare class TemplaterService {
|
|
5
|
+
private app;
|
|
6
|
+
constructor(app: App);
|
|
7
|
+
/**
|
|
8
|
+
* Checks if Templater plugin is installed and enabled.
|
|
9
|
+
*/
|
|
10
|
+
isAvailable(): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a file using Templater or falls back to manual creation.
|
|
13
|
+
*/
|
|
14
|
+
createFile(options: FileCreationOptions): Promise<TFile>;
|
|
15
|
+
private shouldUseTemplate;
|
|
16
|
+
private createManually;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=templater-service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"templater-service.d.ts","sourceRoot":"","sources":["../../src/file/templater-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAGvD,YAAY,EAAE,mBAAmB,EAAE,CAAC;AAMpC,qBAAa,gBAAgB;IAChB,OAAO,CAAC,GAAG;gBAAH,GAAG,EAAE,GAAG;IAE5B;;OAEG;IACH,WAAW,IAAI,OAAO;IAItB;;OAEG;IACG,UAAU,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,KAAK,CAAC;IAmC9D,OAAO,CAAC,iBAAiB;YAIX,cAAc;CA8B5B"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { __awaiter } from "tslib";
|
|
2
|
+
import { TFile } from "obsidian";
|
|
3
|
+
import { createFromTemplate, isTemplaterAvailable } from "./templater";
|
|
4
|
+
// ============================================================================
|
|
5
|
+
// Templater Service (Class-based wrapper)
|
|
6
|
+
// ============================================================================
|
|
7
|
+
export class TemplaterService {
|
|
8
|
+
constructor(app) {
|
|
9
|
+
this.app = app;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Checks if Templater plugin is installed and enabled.
|
|
13
|
+
*/
|
|
14
|
+
isAvailable() {
|
|
15
|
+
return isTemplaterAvailable(this.app);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Creates a file using Templater or falls back to manual creation.
|
|
19
|
+
*/
|
|
20
|
+
createFile(options) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
const { title, targetDirectory, filename, content, frontmatter, templatePath, useTemplater } = options;
|
|
23
|
+
const finalFilename = filename || title;
|
|
24
|
+
// If content is provided, use manual creation to preserve the content
|
|
25
|
+
if (content) {
|
|
26
|
+
return this.createManually(title, targetDirectory, finalFilename, content, frontmatter);
|
|
27
|
+
}
|
|
28
|
+
// Try to use Templater if requested and available
|
|
29
|
+
if (useTemplater && templatePath && this.shouldUseTemplate(templatePath)) {
|
|
30
|
+
const templateFile = yield createFromTemplate(this.app, templatePath, targetDirectory, finalFilename);
|
|
31
|
+
if (templateFile) {
|
|
32
|
+
// Apply frontmatter if provided
|
|
33
|
+
if (frontmatter && Object.keys(frontmatter).length > 0) {
|
|
34
|
+
yield this.app.fileManager.processFrontMatter(templateFile, (fm) => {
|
|
35
|
+
Object.assign(fm, frontmatter);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return templateFile;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Fallback to manual creation
|
|
42
|
+
return this.createManually(title, targetDirectory, finalFilename, content, frontmatter);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
shouldUseTemplate(templatePath) {
|
|
46
|
+
return !!(templatePath && this.isAvailable() && this.app.vault.getFileByPath(templatePath));
|
|
47
|
+
}
|
|
48
|
+
createManually(title, targetDirectory, filename, customContent, frontmatter) {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
const baseName = filename.replace(/\.md$/, "");
|
|
51
|
+
const filePath = `${targetDirectory}/${baseName}.md`;
|
|
52
|
+
// Check if file already exists
|
|
53
|
+
const existingFile = this.app.vault.getAbstractFileByPath(filePath);
|
|
54
|
+
if (existingFile instanceof TFile) {
|
|
55
|
+
return existingFile;
|
|
56
|
+
}
|
|
57
|
+
// Use custom content or default
|
|
58
|
+
const content = customContent || `# ${title}\n\n`;
|
|
59
|
+
const file = yield this.app.vault.create(filePath, content);
|
|
60
|
+
// Apply frontmatter if provided
|
|
61
|
+
if (frontmatter && Object.keys(frontmatter).length > 0) {
|
|
62
|
+
yield this.app.fileManager.processFrontMatter(file, (fm) => {
|
|
63
|
+
Object.assign(fm, frontmatter);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return file;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=templater-service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"templater-service.js","sourceRoot":"","sources":["../../src/file/templater-service.ts"],"names":[],"mappings":";AAAA,OAAO,EAAY,KAAK,EAAE,MAAM,UAAU,CAAC;AAE3C,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAIvE,+EAA+E;AAC/E,0CAA0C;AAC1C,+EAA+E;AAE/E,MAAM,OAAO,gBAAgB;IAC5B,YAAoB,GAAQ;QAAR,QAAG,GAAH,GAAG,CAAK;IAAG,CAAC;IAEhC;;OAEG;IACH,WAAW;QACV,OAAO,oBAAoB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACvC,CAAC;IAED;;OAEG;IACG,UAAU,CAAC,OAA4B;;YAC5C,MAAM,EAAE,KAAK,EAAE,eAAe,EAAE,QAAQ,EAAE,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,GAC3F,OAAO,CAAC;YAET,MAAM,aAAa,GAAG,QAAQ,IAAI,KAAK,CAAC;YAExC,sEAAsE;YACtE,IAAI,OAAO,EAAE,CAAC;gBACb,OAAO,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,OAAO,EAAE,WAAW,CAAC,CAAC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AC,qBAAqB,CAAC,QAAQ,CAAC,CAAC;YACpE,IAAI,YAAY,YAAY,KAAK,EAAE,CAAC;gBACnC,OAAO,YAAY,CAAC;YACrB,CAAC;YAED,gCAAgC;YAChC,MAAM,OAAO,GAAG,aAAa,IAAI,KAAK,KAAK,MAAM,CAAC;YAElD,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;YAE5D,gCAAgC;YAChC,IAAI,WAAW,IAAI,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACxD,MAAM,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,kBAAkB,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,EAAE;oBAC1D,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,WAAW,CAAC,CAAC;gBAChC,CAAC,CAAC,CAAC;YACJ,CAAC;YAED,OAAO,IAAI,CAAC;QACb,CAAC;KAAA;CACD","sourcesContent":["import { type App, TFile } from \"obsidian\";\nimport type { FileCreationOptions } from \"./templater\";\nimport { createFromTemplate, isTemplaterAvailable } from \"./templater\";\n\nexport type { FileCreationOptions };\n\n// ============================================================================\n// Templater Service (Class-based wrapper)\n// ============================================================================\n\nexport class TemplaterService {\n\tconstructor(private app: App) {}\n\n\t/**\n\t * Checks if Templater plugin is installed and enabled.\n\t */\n\tisAvailable(): boolean {\n\t\treturn isTemplaterAvailable(this.app);\n\t}\n\n\t/**\n\t * Creates a file using Templater or falls back to manual creation.\n\t */\n\tasync createFile(options: FileCreationOptions): Promise<TFile> {\n\t\tconst { title, targetDirectory, filename, content, frontmatter, templatePath, useTemplater } =\n\t\t\toptions;\n\n\t\tconst finalFilename = filename || title;\n\n\t\t// If content is provided, use manual creation to preserve the content\n\t\tif (content) {\n\t\t\treturn this.createManually(title, targetDirectory, finalFilename, content, frontmatter);\n\t\t}\n\n\t\t// Try to use Templater if requested and available\n\t\tif (useTemplater && templatePath && this.shouldUseTemplate(templatePath)) {\n\t\t\tconst templateFile = await createFromTemplate(\n\t\t\t\tthis.app,\n\t\t\t\ttemplatePath,\n\t\t\t\ttargetDirectory,\n\t\t\t\tfinalFilename\n\t\t\t);\n\n\t\t\tif (templateFile) {\n\t\t\t\t// Apply frontmatter if provided\n\t\t\t\tif (frontmatter && Object.keys(frontmatter).length > 0) {\n\t\t\t\t\tawait this.app.fileManager.processFrontMatter(templateFile, (fm) => {\n\t\t\t\t\t\tObject.assign(fm, frontmatter);\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\treturn templateFile;\n\t\t\t}\n\t\t}\n\n\t\t// Fallback to manual creation\n\t\treturn this.createManually(title, targetDirectory, finalFilename, content, frontmatter);\n\t}\n\n\tprivate shouldUseTemplate(templatePath: string): boolean {\n\t\treturn !!(templatePath && this.isAvailable() && this.app.vault.getFileByPath(templatePath));\n\t}\n\n\tprivate async createManually(\n\t\ttitle: string,\n\t\ttargetDirectory: string,\n\t\tfilename: string,\n\t\tcustomContent?: string,\n\t\tfrontmatter?: Record<string, unknown>\n\t): Promise<TFile> {\n\t\tconst baseName = filename.replace(/\\.md$/, \"\");\n\t\tconst filePath = `${targetDirectory}/${baseName}.md`;\n\n\t\t// Check if file already exists\n\t\tconst existingFile = this.app.vault.getAbstractFileByPath(filePath);\n\t\tif (existingFile instanceof TFile) {\n\t\t\treturn existingFile;\n\t\t}\n\n\t\t// Use custom content or default\n\t\tconst content = customContent || `# ${title}\\n\\n`;\n\n\t\tconst file = await this.app.vault.create(filePath, content);\n\n\t\t// Apply frontmatter if provided\n\t\tif (frontmatter && Object.keys(frontmatter).length > 0) {\n\t\t\tawait this.app.fileManager.processFrontMatter(file, (fm) => {\n\t\t\t\tObject.assign(fm, frontmatter);\n\t\t\t});\n\t\t}\n\n\t\treturn file;\n\t}\n}\n"]}
|
package/package.json
CHANGED
package/src/file/index.ts
CHANGED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { type App, TFile } from "obsidian";
|
|
2
|
+
import type { FileCreationOptions } from "./templater";
|
|
3
|
+
import { createFromTemplate, isTemplaterAvailable } from "./templater";
|
|
4
|
+
|
|
5
|
+
export type { FileCreationOptions };
|
|
6
|
+
|
|
7
|
+
// ============================================================================
|
|
8
|
+
// Templater Service (Class-based wrapper)
|
|
9
|
+
// ============================================================================
|
|
10
|
+
|
|
11
|
+
export class TemplaterService {
|
|
12
|
+
constructor(private app: App) {}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Checks if Templater plugin is installed and enabled.
|
|
16
|
+
*/
|
|
17
|
+
isAvailable(): boolean {
|
|
18
|
+
return isTemplaterAvailable(this.app);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates a file using Templater or falls back to manual creation.
|
|
23
|
+
*/
|
|
24
|
+
async createFile(options: FileCreationOptions): Promise<TFile> {
|
|
25
|
+
const { title, targetDirectory, filename, content, frontmatter, templatePath, useTemplater } =
|
|
26
|
+
options;
|
|
27
|
+
|
|
28
|
+
const finalFilename = filename || title;
|
|
29
|
+
|
|
30
|
+
// If content is provided, use manual creation to preserve the content
|
|
31
|
+
if (content) {
|
|
32
|
+
return this.createManually(title, targetDirectory, finalFilename, content, frontmatter);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Try to use Templater if requested and available
|
|
36
|
+
if (useTemplater && templatePath && this.shouldUseTemplate(templatePath)) {
|
|
37
|
+
const templateFile = await createFromTemplate(
|
|
38
|
+
this.app,
|
|
39
|
+
templatePath,
|
|
40
|
+
targetDirectory,
|
|
41
|
+
finalFilename
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
if (templateFile) {
|
|
45
|
+
// Apply frontmatter if provided
|
|
46
|
+
if (frontmatter && Object.keys(frontmatter).length > 0) {
|
|
47
|
+
await this.app.fileManager.processFrontMatter(templateFile, (fm) => {
|
|
48
|
+
Object.assign(fm, frontmatter);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return templateFile;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Fallback to manual creation
|
|
56
|
+
return this.createManually(title, targetDirectory, finalFilename, content, frontmatter);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private shouldUseTemplate(templatePath: string): boolean {
|
|
60
|
+
return !!(templatePath && this.isAvailable() && this.app.vault.getFileByPath(templatePath));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private async createManually(
|
|
64
|
+
title: string,
|
|
65
|
+
targetDirectory: string,
|
|
66
|
+
filename: string,
|
|
67
|
+
customContent?: string,
|
|
68
|
+
frontmatter?: Record<string, unknown>
|
|
69
|
+
): Promise<TFile> {
|
|
70
|
+
const baseName = filename.replace(/\.md$/, "");
|
|
71
|
+
const filePath = `${targetDirectory}/${baseName}.md`;
|
|
72
|
+
|
|
73
|
+
// Check if file already exists
|
|
74
|
+
const existingFile = this.app.vault.getAbstractFileByPath(filePath);
|
|
75
|
+
if (existingFile instanceof TFile) {
|
|
76
|
+
return existingFile;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Use custom content or default
|
|
80
|
+
const content = customContent || `# ${title}\n\n`;
|
|
81
|
+
|
|
82
|
+
const file = await this.app.vault.create(filePath, content);
|
|
83
|
+
|
|
84
|
+
// Apply frontmatter if provided
|
|
85
|
+
if (frontmatter && Object.keys(frontmatter).length > 0) {
|
|
86
|
+
await this.app.fileManager.processFrontMatter(file, (fm) => {
|
|
87
|
+
Object.assign(fm, frontmatter);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return file;
|
|
92
|
+
}
|
|
93
|
+
}
|