Skip to content

Commit

Permalink
Added commad for copying note URL to clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Ole Eskild Steensen committed Mar 1, 2022
1 parent ca72557 commit 0374ce8
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 37 deletions.
66 changes: 60 additions & 6 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { App, Notice, Plugin, PluginSettingTab, Setting, getLinkpath, Editor, MarkdownView } from 'obsidian';
import { Octokit } from "@octokit/core";
import { Base64 } from "js-base64";
import fm from 'front-matter';
import slugify from '@sindresorhus/slugify';

interface DigitalGardenSettings {
githubToken: string;
githubRepo: string;
githubUserName: string;
gardenBaseUrl: string;
}

const DEFAULT_SETTINGS: DigitalGardenSettings = {
githubRepo: '',
githubToken: '',
githubUserName: ''
githubUserName: '',
gardenBaseUrl: ''
}

export default class DigitalGarden extends Plugin {
Expand Down Expand Up @@ -43,9 +48,9 @@ export default class DigitalGarden extends Plugin {
name: 'Publish Note',
callback: async () => {
try {
const { vault } = this.app;
const currentFile = this.app.workspace.getActiveFile();
if(!currentFile){
const { vault, workspace } = this.app;
const currentFile = workspace.getActiveFile();
if (!currentFile) {
new Notice("No file is open/active. Please open a file and try again.")
return;
}
Expand All @@ -60,11 +65,46 @@ export default class DigitalGarden extends Plugin {
new Notice("Unable to publish note, something went wrong.")
}
},
});

this.addCommand({
id: 'copy-note-url',
name: 'Copy Note URL',
callback: async () => {
try {
const { vault, 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 baseUrl = this.settings.gardenBaseUrl ?
`https://${this.extractBaseUrl(this.settings.gardenBaseUrl)}`
: `https://${this.settings.githubRepo}.netlify.app`;

let urlPath = `/notes/${slugify(currentFile.basename)}`;
const content = await vault.cachedRead(currentFile);
const fmData = fm(content);
if (fmData.attributes.permalink) {
urlPath = `/${fmData.attributes.permalink}`;
}

const fullUrl = `${baseUrl}${urlPath}`;
await navigator.clipboard.writeText(fullUrl);
new Notice(`Copied note URL to clipboard: ${fullUrl}`);
} catch (e) {
new Notice("Unable to copy note URL to clipboard, something went wrong.")
}
}
});

}

extractBaseUrl(url: string) {
return url && url.replace("https://", "").replace("http://", "").replace(/\/$/, '')
}


async uploadText(title: string, content: string) {
if (!this.settings.githubRepo) {
Expand All @@ -83,7 +123,7 @@ export default class DigitalGarden extends Plugin {

const octokit = new Octokit({ auth: this.settings.githubToken });


const base64Content = Base64.encode(content);
const path = `src/site/notes/${title}`

Expand Down Expand Up @@ -127,7 +167,7 @@ export default class DigitalGarden extends Plugin {
const tranclusionFileName = transclusionMatch.substring(transclusionMatch.indexOf('[') + 2, transclusionMatch.indexOf(']'));
const tranclusionFilePath = getLinkpath(tranclusionFileName);
const linkedFile = this.app.metadataCache.getFirstLinkpathDest(tranclusionFilePath, filePath);
if(["md", "txt"].indexOf(linkedFile.extension) == -1){
if (["md", "txt"].indexOf(linkedFile.extension) == -1) {
continue;
}
let fileText = await this.app.vault.cachedRead(linkedFile);
Expand Down Expand Up @@ -227,6 +267,20 @@ class DigitalGardenSettingTab extends PluginSettingTab {
this.plugin.settings.githubToken = value;
await this.plugin.saveSettings();
}));

new Setting(containerEl)
.setName('Base URL')
.setDesc(`
This is used for the "Copy Note URL" command and is optional.
If you leave it blank, the plugin will try to guess it from the repo name.
`)
.addText(text => text
.setPlaceholder('my-digital-garden.netlify.app')
.setValue(this.plugin.settings.gardenBaseUrl)
.onChange(async (value) => {
this.plugin.settings.gardenBaseUrl = value;
await this.plugin.saveSettings();
}));
}
}

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": "1.1.0",
"version": "1.2.0",
"minAppVersion": "0.12.0",
"description": "Publish your notes to a digital garden for others to enjoy.",
"author": "Ole Eskild Steensen",
Expand Down
56 changes: 29 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
{
"name": "obsidian-garden",
"version": "1.0.0",
"description": "A plugin used for publishing notes to personal site",
"main": "main.js",
"scripts": {
"dev": "node esbuild.config.mjs",
"dev1": "parcel main.ts",
"build": "node esbuild.config.mjs production"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "^5.2.0",
"@typescript-eslint/parser": "^5.2.0",
"builtin-modules": "^3.2.0",
"esbuild": "0.13.12",
"obsidian": "^0.12.17",
"tslib": "2.3.1",
"typescript": "4.4.4"
},
"dependencies": {
"@octokit/core": "^3.5.1",
"js-base64": "^3.7.2"
}
}
"name": "obsidian-garden",
"version": "1.0.0",
"description": "A plugin used for publishing notes to personal site",
"main": "main.js",
"scripts": {
"dev": "node esbuild.config.mjs",
"dev1": "parcel main.ts",
"build": "node esbuild.config.mjs production"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "^5.2.0",
"@typescript-eslint/parser": "^5.2.0",
"builtin-modules": "^3.2.0",
"esbuild": "0.13.12",
"obsidian": "^0.12.17",
"tslib": "2.3.1",
"typescript": "4.4.4"
},
"dependencies": {
"@octokit/core": "^3.5.1",
"front-matter": "^4.0.2",
"js-base64": "^3.7.2",
"@sindresorhus/slugify": "^1.1.0"
}
}
7 changes: 4 additions & 3 deletions versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"1.0.1": "0.9.12",
"1.0.0": "0.9.7"
}
"1.2.0": "0.12.0",
"1.1.0": "0.12.0",
"1.0.0": "0.12.0"
}

0 comments on commit 0374ce8

Please sign in to comment.