Skip to content

Commit

Permalink
feat: Adding a command to update obs2mk setting using obsidian
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Apr 13, 2022
1 parent f38d356 commit 0496bed
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
18 changes: 17 additions & 1 deletion mkdocsPublisher/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,23 @@ export default class mkdocsPublication extends Plugin {
}
},
});

this.addCommand({
id: 'obs2mk-update-settings',
name: 'Update settings workflow',
callback: async () => {
try {
const {vault, metadataCache} = this.app;
const publish = new MkdocsPublish(vault, metadataCache, this.settings);
const successUpdate=await publish.updateSettings();
if (successUpdate) {
new Notice('Successfully updated settings.')
}
} catch (e) {
console.error(e)
new Notice('Unable to update settings, something went wrong.')
}
},
});
}
onunload() {
console.log('Mkdocs Publication unloaded');
Expand Down
62 changes: 61 additions & 1 deletion src/settings.ts → mkdocsPublisher/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export interface mkdocsPublicationSettings {
shareKey: string;
ExcludedFolder: string;
fileMenu: boolean;
categoryKey: string;
categoryDefault: string;
indexFolder: string;
}

export const DEFAULT_SETTINGS: mkdocsPublicationSettings = {
Expand All @@ -17,6 +20,9 @@ export const DEFAULT_SETTINGS: mkdocsPublicationSettings = {
shareKey: "share",
ExcludedFolder: "",
fileMenu: false,
categoryKey: "category",
categoryDefault: "notes",
indexFolder: "(i)"
};

export class mkdocsSettingsTab extends PluginSettingTab {
Expand Down Expand Up @@ -73,7 +79,7 @@ export class mkdocsSettingsTab extends PluginSettingTab {
await this.plugin.saveSettings();
})
);
containerEl.createEl("h3", {text: "Settings for sharing"});
containerEl.createEl("h3", {text: "Sharing Settings"});
new Setting(containerEl)
.setName("Share Key")
.setDesc("The frontmatter key to publish your file on the website.")
Expand Down Expand Up @@ -110,5 +116,59 @@ export class mkdocsSettingsTab extends PluginSettingTab {
})
);

containerEl.createEl("h3", {text: "OBS2MK settings"});

new Setting(containerEl)
.setName("Category Key")
.setDesc("The frontmatter key to set the category of your file.")
.addText((text) =>
text
.setPlaceholder("category")
.setValue(this.plugin.settings.categoryKey)
.onChange(async(value)=>{
this.plugin.settings.categoryKey = value;
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Default category")
.setDesc("The default folder where you note will be published.")
.addText((text) =>
text
.setPlaceholder("Notes")
.setValue(this.plugin.settings.categoryDefault)
.onChange(async(value)=>{
this.plugin.settings.categoryDefault = value;
await this.plugin.saveSettings();
})
);

const desc_index=document.createDocumentFragment();
desc_index.createEl("span", null, (span)=>{
span.innerText="The index key is used for the citation of the folder note. See ";
span.createEl("a", null, (link)=> {
link.innerText = "documentation";
link.href = "https://mara-li.github.io/mkdocs_obsidian_template/documentation/blog%20customization/#folder-note";
})
span.innerText=" for more information.";
});

new Setting(containerEl)
.setName("Index Folder Note Key")
.setDesc(desc_index)
.addText((text) =>
text
.setPlaceholder("(i)")
.setValue(this.plugin.settings.indexFolder)
.onChange(async(value)=>{
this.plugin.settings.indexFolder = value;
await this.plugin.saveSettings();
})
);




}
}
17 changes: 17 additions & 0 deletions mkdocsPublisher/utils/publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ export default class MkdocsPublish {
repo: this.settings.githubRepo,
path
});
// @ts-ignore
if (response.status === 200 && response.data.type === "file") {
// @ts-ignore
payload.sha = response.data.sha;
}
} catch (e) {
Expand Down Expand Up @@ -163,4 +165,19 @@ export default class MkdocsPublish {
ref: 'main'
});
}

async updateSettings() {
let newSettings = `index_key=${this.settings.indexFolder}
default_blog=${this.settings.categoryDefault}
category_key=${this.settings.categoryKey}
`
newSettings=newSettings.replace(/\t/gi, '').trim();
try {
await this.uploadText('.github-actions', newSettings, '.github-actions');
return true
}
catch {
return false
}
}
}

0 comments on commit 0496bed

Please sign in to comment.