Skip to content

Commit

Permalink
Add new command for quickly sharing note (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ole Eskild Steensen committed Mar 17, 2023
1 parent 6d25086 commit a1d6c33
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 57 deletions.
142 changes: 87 additions & 55 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Notice, Plugin, PluginSettingTab, ButtonComponent, addIcon, Modal } from 'obsidian';
import { App, Notice, Plugin, PluginSettingTab, ButtonComponent, addIcon, Modal, Events } from 'obsidian';
import Publisher from 'src/Publisher';
import DigitalGardenSettings from 'src/DigitalGardenSettings';
import DigitalGardenSiteManager from 'src/DigitalGardenSiteManager';
Expand Down Expand Up @@ -58,7 +58,7 @@ export default class DigitalGarden extends Plugin {
publishModal: PublishModal;

async onload() {
this.appVersion = this.manifest.version;
this.appVersion = this.manifest.version;

console.log("Initializing DigitalGarden plugin v" + this.appVersion);
await this.loadSettings();
Expand All @@ -68,7 +68,7 @@ export default class DigitalGarden extends Plugin {
await this.addCommands();

addIcon('digital-garden-icon', seedling);
if(this.settings.showRibbonIcon){
if (this.settings.showRibbonIcon) {
this.addRibbonIcon("digital-garden-icon", "Digital Garden Publication Center", async () => {
this.openPublishModal();
});
Expand All @@ -90,36 +90,37 @@ export default class DigitalGarden extends Plugin {
}

async addCommands() {

this.addCommand({
id: 'publish-note',
name: 'Publish Single Note',
id: 'quick-publish-and-share-note',
name: 'Quick Publish And Share Note',
callback: async () => {
try {
const { vault, workspace, metadataCache } = this.app;

const currentFile = workspace.getActiveFile();
if (!currentFile) {
new Notice("No file is open/active. Please open a file and try again.")
return;
}
if (currentFile.extension !== 'md') {
new Notice("The current file is not a markdown file. Please open a markdown file and try again.")
return;
new Notice("Adding publish flag to note and publishing it.")
await this.addPublishFlag();
const activeFile = this.app.workspace.getActiveFile();
let event = this.app.metadataCache.on('changed', async (file, data, cache) => {
console.log(`File ${file.path} changed with type ${data}.`)
if (file.path === activeFile.path) {
const successfullyPublished = await this.publishSingleNote();
if(successfullyPublished){
await this.copyGardenUrlToClipboard();
}
this.app.metadataCache.offref(event);
}
});

new Notice("Publishing note...");
const publisher = new Publisher(vault, metadataCache, this.settings);
const publishSuccessful = await publisher.publish(currentFile);
// Remove the event listener after 5 seconds in case the file is not changed.
setTimeout(() => {
this.app.metadataCache.offref(event);
}, 5000);

if (publishSuccessful) {
new Notice(`Successfully published note to your garden.`);
}
}
});

} catch (e) {
console.error(e)
new Notice("Unable to publish note, something went wrong.")
}
},
this.addCommand({
id: 'publish-note',
name: 'Publish Single Note',
callback: this.publishSingleNote
});

this.addCommand({
Expand Down Expand Up @@ -179,7 +180,7 @@ export default class DigitalGarden extends Plugin {
if (filesToDelete.length > 0) {
new Notice(`Successfully deleted ${filesToDelete.length - errorDeleteFiles} notes from your garden.`);
}
if(imagesToDelete.length > 0) {
if (imagesToDelete.length > 0) {
new Notice(`Successfully deleted ${imagesToDelete.length - errorDeleteImage} images from your garden.`);
}

Expand All @@ -194,46 +195,77 @@ export default class DigitalGarden extends Plugin {
this.addCommand({
id: 'copy-garden-url',
name: 'Copy Garden URL',
callback: async () => {
try {
const { metadataCache, workspace } = this.app;
const currentFile = workspace.getActiveFile();
if (!currentFile) {
new Notice("No file is open/active. Please open a file and try again.")
return;
}

const siteManager = new DigitalGardenSiteManager(metadataCache, this.settings);
const fullUrl = siteManager.getNoteUrl(currentFile);

await navigator.clipboard.writeText(fullUrl);
new Notice(`Note URL copied to clipboard`);
} catch (e) {
console.log(e)
new Notice("Unable to copy note URL to clipboard, something went wrong.")
}
}
callback: this.copyGardenUrlToClipboard
});

this.addCommand({
id: 'dg-open-publish-modal',
name: 'Open Publication Center',
callback: async () => {
this.openPublishModal();
}
callback: this.openPublishModal
});

this.addCommand({
id: 'dg-mark-note-for-publish',
name: 'Add publish flag',
callback: async () => {
const engine = new ObsidianFrontMatterEngine(this.app.vault, this.app.metadataCache, this.app.workspace.getActiveFile());
engine.set("dg-publish", true).apply();
}
callback: this.addPublishFlag
});

}

async copyGardenUrlToClipboard() {
try {
const { metadataCache, workspace } = this.app;
const currentFile = workspace.getActiveFile();
if (!currentFile) {
new Notice("No file is open/active. Please open a file and try again.")
return;
}

const siteManager = new DigitalGardenSiteManager(metadataCache, this.settings);
const fullUrl = siteManager.getNoteUrl(currentFile);

await navigator.clipboard.writeText(fullUrl);
new Notice(`Note URL copied to clipboard`);
} catch (e) {
console.log(e)
new Notice("Unable to copy note URL to clipboard, something went wrong.")
}
}

async publishSingleNote() {
try {
const { vault, workspace, metadataCache } = this.app;

const currentFile = workspace.getActiveFile();
if (!currentFile) {
new Notice("No file is open/active. Please open a file and try again.")
return;
}
if (currentFile.extension !== 'md') {
new Notice("The current file is not a markdown file. Please open a markdown file and try again.")
return;
}

new Notice("Publishing note...");
const publisher = new Publisher(vault, metadataCache, this.settings);
const publishSuccessful = await publisher.publish(currentFile);

if (publishSuccessful) {
new Notice(`Successfully published note to your garden.`);
}
return publishSuccessful;

} catch (e) {
console.error(e)
new Notice("Unable to publish note, something went wrong.")
return false;
}
}
async addPublishFlag() {
const engine = new ObsidianFrontMatterEngine(this.app.vault, this.app.metadataCache, this.app.workspace.getActiveFile());
engine.set("dg-publish", true).apply();
}

openPublishModal() {
if (!this.publishModal) {
const siteManager = new DigitalGardenSiteManager(this.app.metadataCache, this.settings);
Expand All @@ -254,7 +286,7 @@ class DigitalGardenSettingTab extends PluginSettingTab {
super(app, plugin);
this.plugin = plugin;

if(!this.plugin.settings.noteSettingsIsInitialized) {
if (!this.plugin.settings.noteSettingsIsInitialized) {
const siteManager = new DigitalGardenSiteManager(this.app.metadataCache, this.plugin.settings);
siteManager.updateEnv();
this.plugin.settings.noteSettingsIsInitialized = true;
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "digitalgarden",
"name": "Digital Garden",
"version": "2.39.0",
"version": "2.40.0",
"minAppVersion": "0.12.0",
"description": "Publish your notes to the web for others to enjoy. For free.",
"author": "Ole Eskild Steensen",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@typescript-eslint/parser": "^5.2.0",
"builtin-modules": "^3.2.0",
"esbuild": "0.13.12",
"obsidian": "^0.13.30",
"obsidian": "^1.1.1",
"tslib": "2.3.1",
"typescript": "4.4.4"
},
Expand Down
1 change: 1 addition & 0 deletions src/Publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { excaliDrawBundle, excalidraw } from "./constants";
import { getAPI } from "obsidian-dataview";
import slugify from "@sindresorhus/slugify";
import LZString from "lz-string";
import ObsidianFrontMatterEngine from './ObsidianFrontMatterEngine';

export interface MarkedForPublishing {
notes: TFile[],
Expand Down
1 change: 1 addition & 0 deletions versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"2.40.0": "0.12.0",
"2.39.0": "0.12.0",
"2.38.0": "0.12.0",
"2.37.0": "0.12.0",
Expand Down

0 comments on commit a1d6c33

Please sign in to comment.