diff --git a/Frontmatter.ts b/Frontmatter.ts deleted file mode 100644 index 644c584..0000000 --- a/Frontmatter.ts +++ /dev/null @@ -1,82 +0,0 @@ -/** - * Original: https://github.com/mcndt/obsidian-quickshare - * Slightly modified for own purposes - */ - -import type { App, TFile } from "obsidian"; - -function _getFrontmatterKey( - file: TFile, - key: string, - app: App -): string { - const fmCache = app.metadataCache.getFileCache(file)?.frontmatter; - return fmCache?.[key] || undefined; -} - -function _setFrontmatterKey( - file: TFile, - key: string, - value: string, - content: string -): string { - if (_getFrontmatterKey(file, key, app) === value) { - console.log("returning"); - return content; - } - - if (_getFrontmatterKey(file, key, app) !== undefined) { - // replace the existing key. - content = content.replace( - new RegExp(`^(${key}):\\s*(.*)$`, "m"), - `${key}: ${value}` - ); - } else { - if (content.match(/^---/)) { - // add the key to the existing block - content = content.replace( - /^---/, - `---\n${key}: ${value}` - ); - } else { - // create a new block - content = `---\n${key}: ${value}\n---\n${content}`; - } - } - - return content; -} - -async function _setFrontmatterKeys( - file: TFile, - records: Record, - app: App -) { - let content = await app.vault.read(file); - for (const [key, value] of Object.entries(records)) { - if (_getFrontmatterKey(file, key, app) !== value) { - content = _setFrontmatterKey( - file, - key, - value, - content - ); - } - } - await app.vault.modify(file, content); -} - -export function useFrontmatterHelper(app: App) { - const getFrontmatterKey = (file: TFile, key: string) => - _getFrontmatterKey(file, key, app); - - const setFrontmatterKeys = ( - file: TFile, - records: Record - ) => _setFrontmatterKeys(file, records, app); - - return { - getFrontmatterKey, - setFrontmatterKeys, - }; -} \ No newline at end of file diff --git a/README.md b/README.md index 665fee8..748b10f 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## What? -An Obsidian plugin to publish posts to https://write.as. +Publish notes to https://write.as. ## Disclosure @@ -14,8 +14,9 @@ An Obsidian plugin to publish posts to https://write.as. 1. Set your username and password in the plugin settings 2. Add `writeas_collection` into your frontmatter, set it to a collection/blog that you own (this **must** be set) -3. Run `Publish/update to writeas` (or use alt+shift+p) -4. The plugin will add the ID and the post URL to your frontmatter. Subsequent publish commands will then update the same post. +3. Run `Publish/update` (or use alt+shift+p) +4. The plugin will add the ID and the post URL to your frontmatter. Subsequent + publish commands will then update the same post. ## API Documentation diff --git a/esbuild.config.mjs b/esbuild.config.mjs index b13282b..be2cafe 100644 --- a/esbuild.config.mjs +++ b/esbuild.config.mjs @@ -15,7 +15,7 @@ const context = await esbuild.context({ banner: { js: banner, }, - entryPoints: ["main.ts"], + entryPoints: ["src/main.ts"], bundle: true, external: [ "obsidian", diff --git a/manifest.json b/manifest.json index d5f1242..b319287 100644 --- a/manifest.json +++ b/manifest.json @@ -1,10 +1,10 @@ { "id": "writeas-publisher", - "version": "1.0.1", + "version": "1.0.2", "name": "Writeas Blog Publisher", "minAppVersion": "0.15.0", - "description": "This is a plugin for Obsidian to publish a note to a collection on write.as. Set your username and password in settings. Set `writeas_collection` as your alias in your frontmatter and publish!", + "description": "Publish your notes to write.as", "author": "encima", "authorUrl": "https://github.com/encima/obsidian-writeas-plugin", "isDesktopOnly": false -} +} \ No newline at end of file diff --git a/package.json b/package.json index 14f07e0..386ada3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "writeas-publisher", - "version": "1.0.1", + "version": "1.0.2", "description": "This is a plugin for Obsidian to publish to a collection on write.as. Set your username and password in settings. Set `writeas_collection` as your alias in your frontmatter and publish!", "main": "main.js", "scripts": { @@ -22,4 +22,4 @@ "typescript": "4.7.4" }, "dependencies": {} -} +} \ No newline at end of file diff --git a/Writeas.ts b/src/Writeas.ts similarity index 96% rename from Writeas.ts rename to src/Writeas.ts index d3ae3e0..de52d41 100644 --- a/Writeas.ts +++ b/src/Writeas.ts @@ -61,7 +61,6 @@ export class WriteasClient { return; }).then((resp) => { let res = resp?.json['data'] - console.dir(res); return res; }) } @@ -80,7 +79,6 @@ export class WriteasClient { return; }).then((resp) => { let res = resp?.json['data'] - console.dir(res); return res; }) } @@ -99,7 +97,6 @@ export class WriteasClient { return; }).then((resp) => { let res = resp?.json['data'] - console.dir(res); return res; }) @@ -119,7 +116,6 @@ export class WriteasClient { return; }).then((resp) => { let res = resp?.json['data'] - console.dir(res); return res; }) diff --git a/main.ts b/src/main.ts similarity index 82% rename from main.ts rename to src/main.ts index 04490a8..73e6e0f 100644 --- a/main.ts +++ b/src/main.ts @@ -1,6 +1,5 @@ -import { WriteasClient } from 'Writeas'; -import { App, Modal, Notice, Plugin, PluginSettingTab, Setting, TFile } from 'obsidian'; -import { useFrontmatterHelper } from './Frontmatter'; +import { App, Notice, Plugin, PluginSettingTab, Setting, TFile } from 'obsidian'; +import { WriteasClient } from './Writeas'; interface WriteasPluginSettings { writeasUser: string; @@ -26,9 +25,9 @@ export default class WriteasPlugin extends Plugin { await this.loadSettings(); this.addCommand({ - id: 'open-sample-modal-simple', - name: 'Publish/update on write.as', - hotkeys: [{ modifiers: ["Alt", "Shift"], key: "p" }], + id: 'open-publish-simple', + name: 'Publish/update', + hotkeys: [], callback: () => { var file = this.app.workspace.getActiveFile() if (file) { @@ -42,11 +41,6 @@ export default class WriteasPlugin extends Plugin { } - updateFrontmatter(file: TFile, keys: Record) { - let fm = useFrontmatterHelper(app) - fm.setFrontmatterKeys(file, keys) - } - async handleFile(file: TFile) { let c = new WriteasClient(this.settings.writeasUser, this.settings.writeasPassword); @@ -54,7 +48,6 @@ export default class WriteasPlugin extends Plugin { var content = this.app.metadataCache.getFileCache(file); if (content?.frontmatter && content.frontmatter[COLL_KEY]) { let coll = content.frontmatter[COLL_KEY] - console.log(coll); await c.login(); // TODO check if collection is valid? // TODO check if post exists and if collection it is in has changed @@ -65,7 +58,7 @@ export default class WriteasPlugin extends Plugin { // // return // } let post_body = { body: removeFrontMatter(lines), title: file.basename }; - let res; + let res: { [x: string]: any; }; if (content.frontmatter['_writeas_id']) { let id = content.frontmatter['_writeas_id']; res = await c.updatePost(post_body, id) @@ -80,8 +73,11 @@ export default class WriteasPlugin extends Plugin { new Notice("Failed to publish, does the collection exist?") return; } - const keys: Record = { 'writeas_url': res['url'].replace('http', 'https'), '_writeas_id': res['id'] } - this.updateFrontmatter(file, keys) + // const keys: Record = { 'writeas_url': res['url'], '_writeas_id': res['id'] } + app.fileManager.processFrontMatter(file, (frontmatter) => { + frontmatter["writeas_url"] = res['url']; + frontmatter["_writeas_id"] = res['id']; + }); } } diff --git a/versions.json b/versions.json index 189c17e..03d60fe 100644 --- a/versions.json +++ b/versions.json @@ -1,4 +1,5 @@ { "1.0.0": "0.15.0", - "1.0.1": "0.15.0" -} + "1.0.1": "0.15.0", + "1.0.2": "0.15.0" +} \ No newline at end of file