Skip to content

Commit

Permalink
Merge pull request #47 from timrogers/v1.1.0
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
timrogers committed Jul 21, 2022
2 parents 5bc5534 + 48d2ce7 commit 474967a
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 462 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v1.1.0 (July 21, 2022)

* Add new "Enable updating gists after creation" option, allowing users to control if gists can be updated after creation. This defaults to "on", but if it is turned off, the plugin adds no front matter to your notes! ✨
* Fix "Include front matter in gists" option (before it did the opposite of what was intended!)
* Rename commands from "Create public link on GitHub.com" and "Create private link on GitHub.com" to "Share as public gist on GitHub.com" and "Share as public gist on GitHub.com"
* Apply settings changed immediately, rather than waiting for the plugin to be reloaded

# v1.0.2 (July 11, 2022)

* Polyfill `Buffer` in non-Node.js environments for wider compatability
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ Once you've create a gist, if you make changes to your note (for example respond

3. Open "Settings" in Obsidian, then go to "Share as Gist" under "Plugin Options".

<img width="779" alt="Screenshot 2022-06-09 at 09 46 53" src="https://user-images.githubusercontent.com/116134/172805445-3d633311-6fdd-45be-8364-d37641550f62.png">
<img width="976" alt="Screenshot 2022-07-21 at 09 10 52" src="https://user-images.githubusercontent.com/116134/180163869-4a072203-00e6-4510-81e8-456dd71c5443.png">

4. Paste your access token into the "GitHub.com access token" box, then close "Settings".

5. To share a note, open the Command Palette and type "gist". You'll see commands for creating a public and private link. Pick the one you want and hit enter. Your gist will be created, and the URL for sharing will be added to your clipboard.

<img width="779" alt="Screenshot 2022-06-09 at 09 48 31" src="https://user-images.githubusercontent.com/116134/172805839-e0416c2a-6cc2-4a24-9b10-c01c701e2bfa.png">
<img width="770" alt="Screenshot 2022-07-21 at 09 12 16" src="https://user-images.githubusercontent.com/116134/180164154-02817121-e88a-419d-9528-9be58212ed9c.png">

6. Make a change to your note, and then follow step 5 again. You will be asked if you want to update the existing note or create a new one.

<img width="781" alt="Screenshot 2022-06-09 at 09 49 00" src="https://user-images.githubusercontent.com/116134/172805957-dbbe8223-8056-4685-aad4-4fd54338c85f.png">

By default, any YAML front matter will not be included in your gists. You can change that by toggling the "Include front matter in gists" setting.
To enable existing gists to be updated, by default, extra YAML front matter is added to your notes. You can turn this off by disabling the "Enable updating gists after creation" setting.

By default, any YAML front matter will not be included in your gists when they are shared. You can change that by toggling the "Include front matter in gists" setting.

## Securing your GitHub personal access token

Expand Down
67 changes: 51 additions & 16 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,40 @@ import {
} from './src/shared-gists';

interface ShareAsGistSettings {
enableUpdatingGistsAfterCreation: boolean;
includeFrontMatter: boolean;
}

const DEFAULT_SETTINGS: ShareAsGistSettings = {
includeFrontMatter: false,
enableUpdatingGistsAfterCreation: true,
};

interface ShareGistEditorCallbackParams {
isPublic: boolean;
app: App;
includeFrontMatter: boolean;
plugin: ShareAsGistPlugin;
}

const getLatestSettings = async (
plugin: ShareAsGistPlugin,
): Promise<ShareAsGistSettings> => {
await plugin.loadSettings();
return plugin.settings;
};

const stripFrontMatter = (content: string): string => matter(content).content;

const shareGistEditorCallback =
(opts: ShareGistEditorCallbackParams) =>
async (editor: Editor, view: MarkdownView) => {
const { isPublic, app, includeFrontMatter } = opts;
const { isPublic, app, plugin } = opts;

const accessToken = getAccessToken();

const { enableUpdatingGistsAfterCreation, includeFrontMatter } =
await getLatestSettings(plugin);

if (!accessToken) {
return new Notice(
'You need to add your GitHub personal access token in Settings.',
Expand All @@ -54,10 +66,10 @@ const shareGistEditorCallback =
);

const content = includeFrontMatter
? stripFrontMatter(rawContent)
: rawContent;
? rawContent
: stripFrontMatter(rawContent);

if (existingSharedGists.length) {
if (enableUpdatingGistsAfterCreation && existingSharedGists.length) {
new ShareAsGistSelectExistingGistModal(
app,
existingSharedGists,
Expand Down Expand Up @@ -103,11 +115,17 @@ const shareGistEditorCallback =
new Notice(
`Copied ${isPublic ? 'public' : 'private'} gist URL to clipboard`,
);
const updatedContent = upsertSharedGistForFile(
result.sharedGist,
content,
);
editor.setValue(updatedContent);

if (enableUpdatingGistsAfterCreation) {
const updatedContent = upsertSharedGistForFile(
result.sharedGist,
content,
);

editor.setValue(updatedContent);
} else {
editor.setValue(content);
}
} else {
new Notice(`GitHub API error: ${result.errorMessage}`);
}
Expand All @@ -119,23 +137,21 @@ export default class ShareAsGistPlugin extends Plugin {
async onload() {
await this.loadSettings();

const { includeFrontMatter } = this.settings;

this.addCommand({
id: 'share-as-public-dotcom-gist',
name: 'Create public link on GitHub.com',
name: 'Share as public gist on GitHub.com',
editorCallback: shareGistEditorCallback({
includeFrontMatter,
plugin: this,
app: this.app,
isPublic: true,
}),
});

this.addCommand({
id: 'share-as-private-dotcom-gist',
name: 'Create private link on GitHub.com',
name: 'Share as private gist on GitHub.com',
editorCallback: shareGistEditorCallback({
includeFrontMatter,
plugin: this,
app: this.app,
isPublic: false,
}),
Expand Down Expand Up @@ -217,6 +233,25 @@ class ShareAsGistSettingTab extends PluginSettingTab {
.onChange(setAccessToken),
);

new Setting(containerEl)
.setName('Enable updating gists after creation')
.setDesc(
'Whether gists should be updateable through this plugin after creation. ' +
'If this is turned on, when you create a gist, you will be able to choose ' +
'to update an existing gist (if one exists) or create a brand new one. ' +
'To make this possible, front matter will be added to your notes to track ' +
'gists that you have created. If this is turned off, a brand new gist will ' +
'always be created.',
)
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.enableUpdatingGistsAfterCreation)
.onChange(async (value) => {
this.plugin.settings.enableUpdatingGistsAfterCreation = value;
await this.plugin.saveSettings();
}),
);

new Setting(containerEl)
.setName('Include front matter in gists')
.setDesc(
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": "obsidian-share-as-gist",
"name": "Share as Gist",
"version": "1.0.2",
"version": "1.1.0",
"minAppVersion": "0.9.7",
"description": "Shares an Obsidian note as a GitHub.com gist",
"author": "Tim Rogers",
Expand Down

0 comments on commit 474967a

Please sign in to comment.