Skip to content

Commit 625fe29

Browse files
committed
Incorporate downloading template HTML file from GitHub
1 parent 306d226 commit 625fe29

4 files changed

Lines changed: 75 additions & 1 deletion

File tree

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "commonplace-notes",
33
"name": "Commonplace Notes",
4-
"version": "0.1.15",
4+
"version": "0.1.16",
55
"minAppVersion": "1.0.0",
66
"description": "Publish your notes with sliding panes and link to others notes",
77
"author": "Zach Mueller",

src/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { FrontmatterManager } from './utils/frontmatter';
1717
import { ContentIndexManager } from './utils/contentIndex';
1818
import { MappingManager } from './utils/mappings';
1919
import { NoticeManager } from './utils/notice';
20+
import { TemplateManager } from './utils/templateManager';
2021
import { Publisher } from './publish/publisher';
2122
import { Logger } from './utils/logging';
2223

@@ -70,6 +71,7 @@ export default class CommonplaceNotesPlugin extends Plugin {
7071
frontmatterManager: FrontmatterManager;
7172
contentIndexManager: ContentIndexManager;
7273
mappingManager: MappingManager;
74+
templateManager: TemplateManager;
7375
publisher: Publisher;
7476

7577
async onload() {
@@ -85,6 +87,7 @@ export default class CommonplaceNotesPlugin extends Plugin {
8587
this.contentIndexManager = new ContentIndexManager(this);
8688
this.mappingManager = new MappingManager(this);
8789
this.publisher = new Publisher(this);
90+
this.templateManager = new TemplateManager(this);
8891

8992
// Initialize indicator updates
9093
// Targeted indicator refresh upon file open events

src/publish/local.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ export async function publishLocalNotes(
99
profileId: string
1010
): Promise<boolean> {
1111
try {
12+
// Ensure template exists before proceeding
13+
const templateReady = await plugin.templateManager.ensureLocalTemplate();
14+
if (!templateReady) {
15+
throw new Error('Could not prepare template file');
16+
}
17+
1218
const profile = plugin.settings.publishingProfiles.find(p => p.id === profileId);
1319

1420
if (!profile) {

src/utils/templateManager.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import CommonplaceNotesPlugin from '../main';
2+
import { Logger } from './logging';
3+
import { PathUtils } from './path';
4+
import { NoticeManager } from './notice';
5+
6+
export class TemplateManager {
7+
private plugin: CommonplaceNotesPlugin;
8+
private readonly DEFAULT_TEMPLATE_URL = 'https://raw.githubusercontent.com/zachmueller/commonplace-notes/main/templates/local-template.html';
9+
10+
constructor(plugin: CommonplaceNotesPlugin) {
11+
this.plugin = plugin;
12+
}
13+
14+
async ensureLocalTemplate(): Promise<boolean> {
15+
const templatePath = this.plugin.profileManager.getLocalTemplateHtmlPath();
16+
17+
// Check if template already exists
18+
if (await this.plugin.app.vault.adapter.exists(templatePath)) {
19+
Logger.debug('Local template file already exists');
20+
return true;
21+
}
22+
23+
// Try to download default template
24+
Logger.info('Local template not found, attempting to download default template from GitHub');
25+
26+
try {
27+
const { success, error } = await NoticeManager.showProgress(
28+
'Downloading default template from GitHub',
29+
this.downloadDefaultTemplate(templatePath),
30+
'Successfully downloaded default template',
31+
'Failed to download template from GitHub'
32+
);
33+
34+
return success;
35+
} catch (error) {
36+
Logger.error('Failed to download default template:', error);
37+
NoticeManager.showNotice('Failed to download HTML template.');
38+
return false;
39+
}
40+
}
41+
42+
private async downloadDefaultTemplate(templatePath: string): Promise<void> {
43+
// Ensure template directory exists
44+
const templateDir = this.plugin.profileManager.getTemplateDir();
45+
await PathUtils.ensureDirectory(this.plugin, templateDir);
46+
47+
// Download the template
48+
const response = await fetch(this.DEFAULT_TEMPLATE_URL);
49+
50+
if (!response.ok) {
51+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
52+
}
53+
54+
const templateContent = await response.text();
55+
56+
// Validate that it looks like an HTML template
57+
if (!templateContent.includes('{{NOTES_JSON}}')) {
58+
throw new Error('Downloaded file does not appear to be a valid template');
59+
}
60+
61+
// Write to local file
62+
await this.plugin.app.vault.adapter.write(templatePath, templateContent);
63+
Logger.info(`Downloaded default template to ${templatePath}`);
64+
}
65+
}

0 commit comments

Comments
 (0)