update plugin

This commit is contained in:
Maxime Cannoodt 2022-06-26 22:46:39 +02:00
parent 12d79cdfba
commit 5d09dbad12
9 changed files with 144 additions and 122 deletions

View File

@ -1,52 +1,54 @@
import esbuild from "esbuild"; import esbuild from "esbuild";
import process from "process"; import process from "process";
import builtins from 'builtin-modules' import builtins from "builtin-modules";
const banner = const banner = `/*
`/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin if you want to view the source, please visit the github repository of this plugin
*/ */
`; `;
const prod = (process.argv[2] === 'production'); const prod = process.argv[2] === "production";
esbuild.build({ esbuild
banner: { .build({
js: banner, banner: {
}, js: banner,
entryPoints: ['main.ts'], },
bundle: true, entryPoints: ["main.ts"],
external: [ bundle: true,
'obsidian', external: [
'electron', "obsidian",
'@codemirror/autocomplete', "electron",
'@codemirror/closebrackets', "@codemirror/autocomplete",
'@codemirror/collab', "@codemirror/closebrackets",
'@codemirror/commands', "@codemirror/collab",
'@codemirror/comment', "@codemirror/commands",
'@codemirror/fold', "@codemirror/comment",
'@codemirror/gutter', "@codemirror/fold",
'@codemirror/highlight', "@codemirror/gutter",
'@codemirror/history', "@codemirror/highlight",
'@codemirror/language', "@codemirror/history",
'@codemirror/lint', "@codemirror/language",
'@codemirror/matchbrackets', "@codemirror/lint",
'@codemirror/panel', "@codemirror/matchbrackets",
'@codemirror/rangeset', "@codemirror/panel",
'@codemirror/rectangular-selection', "@codemirror/rangeset",
'@codemirror/search', "@codemirror/rectangular-selection",
'@codemirror/state', "@codemirror/search",
'@codemirror/stream-parser', "@codemirror/state",
'@codemirror/text', "@codemirror/stream-parser",
'@codemirror/tooltip', "@codemirror/text",
'@codemirror/view', "@codemirror/tooltip",
...builtins], "@codemirror/view",
format: 'cjs', ...builtins,
watch: !prod, ],
target: 'es2016', format: "cjs",
logLevel: "info", watch: !prod,
sourcemap: prod ? false : 'inline', target: "es2016",
treeShaking: true, logLevel: "info",
outfile: 'main.js', sourcemap: prod ? false : "inline",
}).catch(() => process.exit(1)); treeShaking: true,
outfile: "main.js",
})
.catch(() => process.exit(1));

View File

@ -1,20 +1,21 @@
import { App, MarkdownView, Plugin, PluginSettingTab, Setting } from "obsidian"; import {
import { encryptMarkdown } from "src/encryption"; EventRef,
MarkdownView,
Menu,
Plugin,
TAbstractFile,
TFile,
} from "obsidian";
import { NoteSharingService } from "src/NoteSharingService"; import { NoteSharingService } from "src/NoteSharingService";
import { DEFAULT_SETTINGS, PluginSettings } from "src/obsidian/PluginSettings";
import SettingsTab from "src/obsidian/SettingsTab";
// Remember to rename these classes and interfaces! // Remember to rename these classes and interfaces!
interface MyPluginSettings { export default class NoteSharingPlugin extends Plugin {
serverUrl: string; private settings: PluginSettings;
} private noteSharingService: NoteSharingService;
private eventRef: EventRef;
const DEFAULT_SETTINGS: MyPluginSettings = {
serverUrl: "http://localhost:8080",
};
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
noteSharingService: NoteSharingService;
async onload() { async onload() {
await this.loadSettings(); await this.loadSettings();
@ -22,25 +23,28 @@ export default class MyPlugin extends Plugin {
this.settings.serverUrl this.settings.serverUrl
); );
// This adds a settings tab so the user can configure various aspects of the plugin // Init settings tab
this.addSettingTab(new SettingsTab(this.app, this)); this.addSettingTab(new SettingsTab(this.app, this));
// This adds a complex command that can check whether the current state of the app allows execution of the command // Add note sharing command
this.addCommand({ this.addCommand({
id: "obsidian-note-sharing-share-note", id: "obsidian-note-sharing-share-note",
name: "Create share link", name: "Create share link",
checkCallback: (checking: boolean) => { checkCallback: (checking: boolean) => {
// Conditions to check // Only works on Markdown views
const markdownView = const activeView =
this.app.workspace.getActiveViewOfType(MarkdownView); this.app.workspace.getActiveViewOfType(MarkdownView);
if (markdownView) { if (!activeView) return false;
if (!checking) { if (checking) return true;
this.noteSharingService.shareNote(markdownView); this.noteSharingService.shareNote(activeView.getViewData());
}
return true;
}
}, },
}); });
this.eventRef = this.app.workspace.on(
"file-menu",
(menu, file, source) => this.onMenuOpenCallback(menu, file, source)
);
this.registerEvent(this.eventRef);
} }
onunload() {} onunload() {}
@ -57,34 +61,19 @@ export default class MyPlugin extends Plugin {
await this.saveData(this.settings); await this.saveData(this.settings);
this.noteSharingService.serverUrl = this.settings.serverUrl; this.noteSharingService.serverUrl = this.settings.serverUrl;
} }
}
class SettingsTab extends PluginSettingTab { // https://github.dev/platers/obsidian-linter/blob/c30ceb17dcf2c003ca97862d94cbb0fd47b83d52/src/main.ts#L139-L149
plugin: MyPlugin; onMenuOpenCallback(menu: Menu, file: TAbstractFile, source: string) {
if (file instanceof TFile && file.extension === "md") {
constructor(app: App, plugin: MyPlugin) { menu.addItem((item) => {
super(app, plugin); item.setIcon("paper-plane-glyph");
this.plugin = plugin; item.setTitle("Share note");
} item.onClick(async (evt) => {
this.noteSharingService.shareNote(
display(): void { await this.app.vault.read(file)
const { containerEl } = this; );
});
containerEl.empty(); });
}
containerEl.createEl("h2", { text: "Obsidian Note Sharing" });
new Setting(containerEl)
.setName("Server URL")
.setDesc("Server URL hosting the encrypted notes.")
.addText((text) =>
text
.setPlaceholder("enter URL")
.setValue(this.plugin.settings.serverUrl)
.onChange(async (value) => {
this.plugin.settings.serverUrl = value;
await this.plugin.saveSettings();
})
);
} }
} }

View File

@ -1,4 +1,4 @@
import { MarkdownView, requestUrl } from "obsidian"; import { requestUrl } from "obsidian";
import { encryptMarkdown } from "./encryption"; import { encryptMarkdown } from "./encryption";
export class NoteSharingService { export class NoteSharingService {
@ -12,8 +12,8 @@ export class NoteSharingService {
* @param mdView Markdown file to share. * @param mdView Markdown file to share.
* @returns link to shared note with attached decryption key. * @returns link to shared note with attached decryption key.
*/ */
public async shareNote(mdView: MarkdownView): Promise<string> { public async shareNote(mdText: string): Promise<string> {
const cryptData = encryptMarkdown(mdView); const cryptData = encryptMarkdown(mdText);
let url = await this.postNote(cryptData.ciphertext, cryptData.hmac); let url = await this.postNote(cryptData.ciphertext, cryptData.hmac);
url += `#${cryptData.key}`; url += `#${cryptData.key}`;
console.log(`Note shared: ${url}`); console.log(`Note shared: ${url}`);

View File

@ -1,4 +1,4 @@
import { MarkdownView, moment } from "obsidian"; import { moment } from "obsidian";
import { generateKey, encryptString } from "./crypto"; import { generateKey, encryptString } from "./crypto";
export interface EncryptedMarkdown { export interface EncryptedMarkdown {
@ -7,8 +7,7 @@ export interface EncryptedMarkdown {
key: string; key: string;
} }
export function encryptMarkdown(mdView: MarkdownView): EncryptedMarkdown { export function encryptMarkdown(plaintext: string): EncryptedMarkdown {
const plaintext = mdView.getViewData();
const key = generateKey(moment.now() + plaintext); const key = generateKey(moment.now() + plaintext);
const { ciphertext, hmac } = encryptString(plaintext, key); const { ciphertext, hmac } = encryptString(plaintext, key);
return { ciphertext, hmac, key }; return { ciphertext, hmac, key };

View File

@ -0,0 +1,7 @@
export interface PluginSettings {
serverUrl: string;
}
export const DEFAULT_SETTINGS: PluginSettings = {
serverUrl: "http://localhost:8080",
};

View File

@ -0,0 +1,32 @@
import NoteSharingPlugin from "main";
import { App, PluginSettingTab, Setting } from "obsidian";
export default class SettingsTab extends PluginSettingTab {
plugin: NoteSharingPlugin;
constructor(app: App, plugin: NoteSharingPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", { text: "Obsidian Note Sharing" });
new Setting(containerEl)
.setName("Server URL")
.setDesc("Server URL hosting the encrypted notes.")
.addText((text) =>
text
.setPlaceholder("enter URL")
.setValue(this.plugin.settings.serverUrl)
.onChange(async (value) => {
this.plugin.settings.serverUrl = value;
await this.plugin.saveSettings();
})
);
}
}

View File

@ -1,23 +1,17 @@
{ {
"compilerOptions": { "compilerOptions": {
"baseUrl": ".", "baseUrl": ".",
"inlineSourceMap": true, "inlineSourceMap": true,
"inlineSources": true, "inlineSources": true,
"module": "ESNext", "module": "ESNext",
"target": "ES6", "target": "ES6",
"allowJs": true, "allowJs": true,
"noImplicitAny": true, "noImplicitAny": true,
"moduleResolution": "node", "moduleResolution": "node",
"importHelpers": true, "importHelpers": true,
"isolatedModules": true, "isolatedModules": true,
"lib": [ "outDir": "./build" /* Specify an output folder for all emitted files. */,
"DOM", "lib": ["DOM", "ES5", "ES6", "ES7"]
"ES5", },
"ES6", "include": ["**/*.ts"]
"ES7"
]
},
"include": [
"**/*.ts"
]
} }

Binary file not shown.

View File

@ -4,7 +4,6 @@
"lib": ["esnext"], "lib": ["esnext"],
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"module": "commonjs" /* Specify what module code is generated. */, "module": "commonjs" /* Specify what module code is generated. */,
"outDir": "./build" /* Specify an output folder for all emitted files. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */, "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
"strict": true /* Enable all strict type-checking options. */, "strict": true /* Enable all strict type-checking options. */,