Skip to content

Commit

Permalink
Add gistpad.dev integration + fix misc. bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
lostintangent authored Feb 16, 2025
1 parent 3adc691 commit 7d08d97
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 19 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## v0.6.1 (11/19/2023)
## v0.7.0 (02/10/25)

- Changed the default gist type to secret, by making the `+` icon in the tree create a new secret gist (as opposed to a public gist)
- Added a new `Copy GistPad URL` menu item to file nodes in the `Gists` tree
- Updated the `Copy GistPad URL` and `View gist in browser` menu items for gist nodes, so that they use `https://gistpad.dev` links for "note" type gists
- Fixed an error when refreshing the gists tree

## v0.6.0 (11/19/2023)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 20 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "GistPad",
"description": "Manage your code snippets and developer notes using GitHub Gists and repositories.",
"publisher": "vsls-contrib",
"version": "0.6.0",
"version": "0.7.0",
"extensionKind": [
"ui",
"workspace"
Expand Down Expand Up @@ -182,6 +182,10 @@
"command": "gistpad.copyFileContents",
"title": "Copy File Contents"
},
{
"command": "gistpad.copyGistPadFileUrl",
"title": "Copy GistPad URL"
},
{
"command": "gistpad.copyFileUrl",
"title": "Copy GitHub URL"
Expand Down Expand Up @@ -351,7 +355,7 @@
},
{
"command": "gistpad.openGistInBrowser",
"title": "View Gist in GitHub"
"title": "View Gist in Browser"
},
{
"command": "gistpad.openGistInBlocks",
Expand Down Expand Up @@ -666,6 +670,10 @@
"command": "gistpad.copyFileContents",
"when": "false"
},
{
"command": "gistpad.copyGistPadFileUrl",
"when": "false"
},
{
"command": "gistpad.copyFileUrl",
"when": "false"
Expand Down Expand Up @@ -1012,12 +1020,12 @@
"group": "base@3"
},
{
"command": "gistpad.copyGistUrl",
"command": "gistpad.copyGistPadUrl",
"when": "viewItem =~ /^(gists|starredGists|followedUser).gist/",
"group": "browse@1"
},
{
"command": "gistpad.copyGistPadUrl",
"command": "gistpad.copyGistUrl",
"when": "viewItem =~ /^(gists|starredGists|followedUser).gist/",
"group": "browse@2"
},
Expand Down Expand Up @@ -1102,19 +1110,24 @@
"group": "base@1"
},
{
"command": "gistpad.copyFileUrl",
"command": "gistpad.copyGistPadFileUrl",
"when": "viewItem =~ /^gistFile/",
"group": "base@1"
},
{
"command": "gistpad.copyFileContents",
"command": "gistpad.copyFileUrl",
"when": "viewItem =~ /^gistFile/",
"group": "base@2"
},
{
"command": "gistpad.copyFileContents",
"when": "viewItem =~ /^gistFile/",
"group": "copy@2"
},
{
"command": "gistpad.addFileToGist",
"when": "viewItem =~ /^gistFile/",
"group": "base@3"
"group": "copy@3"
},
{
"command": "gistpad.renameFile",
Expand Down
22 changes: 22 additions & 0 deletions src/commands/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { EXTENSION_NAME } from "../constants";
import { store } from "../store";
import { ensureAuthenticated } from "../store/auth";
import { GistFileNode, GistNode } from "../tree/nodes";
import { createGistPadOpenUrl, createGistPadWebUrl } from "../uriHandler";
import {
byteArrayToString,
decodeDirectoryName,
Expand Down Expand Up @@ -61,6 +62,27 @@ export function registerFileCommands(context: ExtensionContext) {
)
);

context.subscriptions.push(
commands.registerCommand(
`${EXTENSION_NAME}.copyGistPadFileUrl`,
async (nodeOrUri: GistFileNode | Uri) => {
let details: { gistId: string, file: string } | undefined;
if (nodeOrUri instanceof GistFileNode) {
details = { gistId: nodeOrUri.gistId, file: nodeOrUri.file.filename! };
} else {
const { gistId, file } = getGistDetailsFromUri(
encodeDirectoryUri(nodeOrUri)
);
details = { gistId, file };
}
const url = details.file.endsWith(".md")
? createGistPadWebUrl(details.gistId, details.file)
: createGistPadOpenUrl(details.gistId, details.file);
await env.clipboard.writeText(url);
}
)
);

context.subscriptions.push(
commands.registerCommand(
`${EXTENSION_NAME}.copyFileUrl`,
Expand Down
18 changes: 13 additions & 5 deletions src/commands/gist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
GistsNode,
StarredGistNode
} from "../tree/nodes";
import { createGistPadOpenUrl } from "../uriHandler";
import { createGistPadOpenUrl, createGistPadWebUrl } from "../uriHandler";
import {
byteArrayToString,
closeGistFiles,
Expand Down Expand Up @@ -301,7 +301,10 @@ export async function registerGistCommands(context: ExtensionContext) {
commands.registerCommand(
`${EXTENSION_NAME}.copyGistPadUrl`,
async (node: GistNode) => {
const url = createGistPadOpenUrl(node.gist.id);
const url = node.gist.type === "note"
? createGistPadWebUrl(node.gist.id)
: createGistPadOpenUrl(node.gist.id);

env.clipboard.writeText(url);
}
)
Expand Down Expand Up @@ -444,11 +447,11 @@ export async function registerGistCommands(context: ExtensionContext) {
);

context.subscriptions.push(
commands.registerCommand(`${EXTENSION_NAME}.newPublicGist`, newPublicGist)
commands.registerCommand(`${EXTENSION_NAME}.newPublicGist`, () => newPublicGist())
);

context.subscriptions.push(
commands.registerCommand(`${EXTENSION_NAME}.newSecretGist`, newSecretGist)
commands.registerCommand(`${EXTENSION_NAME}.newSecretGist`, () => newSecretGist())
);

context.subscriptions.push(
Expand Down Expand Up @@ -481,7 +484,12 @@ export async function registerGistCommands(context: ExtensionContext) {
commands.registerCommand(
`${EXTENSION_NAME}.openGistInBrowser`,
async (node: GistNode) => {
env.openExternal(Uri.parse(node.gist.html_url));
let url = node.gist.html_url;
if (node.gist.type === "note") {
url = createGistPadWebUrl(node.gist.id);
}

env.openExternal(Uri.parse(url));
}
)
);
Expand Down
1 change: 1 addition & 0 deletions src/tree/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ function getFileDisplayName(file: GistFile) {

return file.filename!;
}

export class GistFileNode extends TreeNode {
constructor(public gistId: string, public file: GistFile) {
super(getFileDisplayName(file));
Expand Down
22 changes: 18 additions & 4 deletions src/uriHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { openRepo } from "./repos/store/actions";
import { store } from "./store";
import { followUser, newScratchNote } from "./store/actions";
import { ensureAuthenticated as ensureAuthenticatedInternal } from "./store/auth";
import { openGist, withProgress } from "./utils";
import { decodeDirectoryName, fileNameToUri, openGist, openGistFile, withProgress } from "./utils";

const OPEN_PATH = "/open";
const GIST_PARAM = "gist";
const REPO_PARAM = "repo";
const FILE_PARAM = "file";

async function ensureAuthenticated() {
await when(() => store.isSignedIn, { timeout: 3000 });
Expand All @@ -33,10 +34,16 @@ async function handleFollowRequest(query: URLSearchParams) {
async function handleOpenRequest(query: URLSearchParams) {
const gistId = query.get(GIST_PARAM);
const repoName = query.get(REPO_PARAM);
const file = query.get(FILE_PARAM);
const openAsWorkspace = query.get("workspace") !== null;

if (gistId) {
openGist(gistId, !!openAsWorkspace);
if (file) {
const uri = fileNameToUri(gistId, decodeDirectoryName(file));
openGistFile(uri)
} else {
openGist(gistId, !!openAsWorkspace);
}
} else if (repoName) {
openRepo(repoName, true);
}
Expand Down Expand Up @@ -84,8 +91,15 @@ async function handleTodayRequest() {
});
}

export function createGistPadOpenUrl(gistId: string) {
return `vscode://${EXTENSION_ID}${OPEN_PATH}?${GIST_PARAM}=${gistId}`;
export function createGistPadOpenUrl(gistId: string, file?: string) {
const fileParam = file ? `&${FILE_PARAM}=${file}` : "";
return `vscode://${EXTENSION_ID}${OPEN_PATH}?${GIST_PARAM}=${gistId}${fileParam}`;
}

export function createGistPadWebUrl(gistId: string, file: string = "README.md", preview: boolean = true) {
const path = file && file !== "README.md" ? `/${file}` : "";
const query = preview ? `?view=preview` : "";
return `https://gistpad.dev/#${gistId}${path}${query}`;
}

class GistPadPUriHandler implements vscode.UriHandler {
Expand Down

0 comments on commit 7d08d97

Please sign in to comment.