address comments from Obsidian review

This commit is contained in:
Chris Gwilliams 2023-09-03 17:30:50 +01:00
parent 187c0005ac
commit 49d5cf100f
No known key found for this signature in database
GPG key ID: 67189FF5888B48F4
8 changed files with 24 additions and 112 deletions

View file

@ -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<string, string>,
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<string, string>
) => _setFrontmatterKeys(file, records, app);
return {
getFrontmatterKey,
setFrontmatterKeys,
};
}

View file

@ -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

View file

@ -15,7 +15,7 @@ const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["main.ts"],
entryPoints: ["src/main.ts"],
bundle: true,
external: [
"obsidian",

View file

@ -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
}
}

View file

@ -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": {}
}
}

View file

@ -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;
})

View file

@ -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<string, string>) {
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<string, string> = { 'writeas_url': res['url'].replace('http', 'https'), '_writeas_id': res['id'] }
this.updateFrontmatter(file, keys)
// const keys: Record<string, string> = { 'writeas_url': res['url'], '_writeas_id': res['id'] }
app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter["writeas_url"] = res['url'];
frontmatter["_writeas_id"] = res['id'];
});
}
}

View file

@ -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"
}