Skip to content

Code snippet #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
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.

23 changes: 14 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"theme": "dark"
},
"activationEvents": [
"onCommand:HackMD.createCodeSnippet",
"onView:mdTreeItems",
"onCommand:HackMD.login",
"onCommand:HackMD.logout",
Expand Down Expand Up @@ -69,7 +70,7 @@
{
"id": "mdTree",
"title": "HackMD Notes",
"icon": "src/icon/activity-bar.svg"
"icon": "images/icon/activity-bar.svg"
}
]
},
Expand All @@ -82,6 +83,10 @@
]
},
"commands": [
{
"command": "HackMD.createCodeSnippet",
"title": "HackMD: Create a code snippet"
},
{
"command": "HackMD.login",
"title": "Login",
Expand All @@ -97,26 +102,26 @@
"title": "Refresh",
"category": "HackMD",
"icon": {
"light": "src/icon/light/refresh-dark.svg",
"dark": "src/icon/dark/refresh-light.svg"
"light": "images/icon/light/refresh-dark.svg",
"dark": "images/icon/dark/refresh-light.svg"
}
},
{
"command": "HackMD.showPreview",
"title": "Open Preview",
"category": "HackMD",
"icon": {
"light": "src/icon/light/view-dark.svg",
"dark": "src/icon/dark/view-light.svg"
"light": "images/icon/light/view-dark.svg",
"dark": "images/icon/dark/view-light.svg"
}
},
{
"command": "HackMD.showPreviewAndEditor",
"title": "Open Preview to the Side",
"category": "HackMD",
"icon": {
"light": "src/icon/light/column-dark.svg",
"dark": "src/icon/dark/column-light.svg"
"light": "images/icon/light/column-dark.svg",
"dark": "images/icon/dark/column-light.svg"
}
},
{
Expand All @@ -129,8 +134,8 @@
"title": "Open note on HackMD",
"category": "HackMD",
"icon": {
"light": "src/icon/light/Browser-dark.png",
"dark": "src/icon/dark/Browser-light.png"
"light": "images/icon/light/Browser-dark.png",
"dark": "images/icon/dark/Browser-light.png"
}
}
],
Expand Down
3 changes: 1 addition & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ vscode.workspace.onDidChangeConfiguration(async e => {
}
}
});
export { API, ExportType };


export { API, ExportType };
2 changes: 2 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as vscode from 'vscode';
import { registerSnippetCommands } from './snippet';
import { registerUserCommands } from './user';
import { registerTreeViewCommands } from './treeView';
import { registerNoteCommands } from './note';
Expand All @@ -7,4 +8,5 @@ export function registerCommands(context: vscode.ExtensionContext) {
registerUserCommands(context);
registerTreeViewCommands(context);
registerNoteCommands(context);
registerSnippetCommands(context);
}
39 changes: 39 additions & 0 deletions src/commands/snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as vscode from 'vscode';
import { API } from './../api';
import { checkLogin } from './../utils';

export function registerSnippetCommands(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('HackMD.createCodeSnippet', async () => {
if (!(await checkLogin())){
vscode.window.showInformationMessage('Please login first.');
return;
}

const editor = vscode.window.activeTextEditor;
if (editor.selection.isEmpty) {
vscode.window.showInformationMessage('The code block is empty. Please select a range of text first.');
return;
}

const textRange = new vscode.Range(editor.selection.start.line, editor.selection.start.character, editor.selection.end.line, editor.selection.end.character);
const text = vscode.window.activeTextEditor.document.getText(textRange);
const filePath = vscode.workspace.asRelativePath(editor.document.uri.fsPath);
const snippet =
`---
title: public/${filePath}
---
> \`${filePath}\`

\`\`\`${editor.document.languageId}=${editor.selection.start.line + 1}
${text}
\`\`\``;

const noteUrl = await API.newNote(snippet);
const clicked = await vscode.window.showInformationMessage('New Snippet Established!', ...['Copy URL to clip board', 'Open in browser']);
if (clicked === 'Copy URL to clip board') {
vscode.env.clipboard.writeText(noteUrl);
} else if (clicked === 'Open in browser') {
vscode.env.openExternal(vscode.Uri.parse(noteUrl));
}
}));
}
1 change: 0 additions & 1 deletion src/commands/treeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export async function registerTreeViewCommands(context: vscode.ExtensionContext)
const content = await API.exportString(noteNode.noteId, ExportType.MD);
if (!checkNoteExist(content)) { return; }

console.log(noteNode.label);
const uri = vscode.Uri.parse(`hackmd:${noteNode.label}.md#${noteNode.noteId}`);
const doc = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(doc, { preview: false });
Expand Down
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as markdownitContainer from 'markdown-it-container';
import * as S from 'string';
import { initializeStorage } from './store/storage';
import * as Prism from 'prismjs';
import { registerCommands } from './commands';
import { registerCommands } from './commands/index';

require('prismjs/components/prism-wiki');
require('prismjs/components/prism-haskell');
Expand Down Expand Up @@ -288,4 +288,4 @@ export async function activate(context: vscode.ExtensionContext) {
}

// this method is called when your extension is deactivated
export function deactivate() { }
export function deactivate() { }
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ export const getLoginCredential = (context: vscode.ExtensionContext) => {
const email: string = context.globalState.get('email');
const password: string = context.globalState.get('password');
return { email, password };
};
};