Skip to content

Commit 62c8d47

Browse files
authored
Merge pull request #24 from hackmdio/feature/code-snippet
Code snippet
2 parents fec463c + 1602f09 commit 62c8d47

21 files changed

+61
-17
lines changed
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.

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+14-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"theme": "dark"
1414
},
1515
"activationEvents": [
16+
"onCommand:HackMD.createCodeSnippet",
1617
"onView:mdTreeItems",
1718
"onCommand:HackMD.login",
1819
"onCommand:HackMD.logout",
@@ -69,7 +70,7 @@
6970
{
7071
"id": "mdTree",
7172
"title": "HackMD Notes",
72-
"icon": "src/icon/activity-bar.svg"
73+
"icon": "images/icon/activity-bar.svg"
7374
}
7475
]
7576
},
@@ -82,6 +83,10 @@
8283
]
8384
},
8485
"commands": [
86+
{
87+
"command": "HackMD.createCodeSnippet",
88+
"title": "HackMD: Create a code snippet"
89+
},
8590
{
8691
"command": "HackMD.login",
8792
"title": "Login",
@@ -97,26 +102,26 @@
97102
"title": "Refresh",
98103
"category": "HackMD",
99104
"icon": {
100-
"light": "src/icon/light/refresh-dark.svg",
101-
"dark": "src/icon/dark/refresh-light.svg"
105+
"light": "images/icon/light/refresh-dark.svg",
106+
"dark": "images/icon/dark/refresh-light.svg"
102107
}
103108
},
104109
{
105110
"command": "HackMD.showPreview",
106111
"title": "Open Preview",
107112
"category": "HackMD",
108113
"icon": {
109-
"light": "src/icon/light/view-dark.svg",
110-
"dark": "src/icon/dark/view-light.svg"
114+
"light": "images/icon/light/view-dark.svg",
115+
"dark": "images/icon/dark/view-light.svg"
111116
}
112117
},
113118
{
114119
"command": "HackMD.showPreviewAndEditor",
115120
"title": "Open Preview to the Side",
116121
"category": "HackMD",
117122
"icon": {
118-
"light": "src/icon/light/column-dark.svg",
119-
"dark": "src/icon/dark/column-light.svg"
123+
"light": "images/icon/light/column-dark.svg",
124+
"dark": "images/icon/dark/column-light.svg"
120125
}
121126
},
122127
{
@@ -129,8 +134,8 @@
129134
"title": "Open note on HackMD",
130135
"category": "HackMD",
131136
"icon": {
132-
"light": "src/icon/light/Browser-dark.png",
133-
"dark": "src/icon/dark/Browser-light.png"
137+
"light": "images/icon/light/Browser-dark.png",
138+
"dark": "images/icon/dark/Browser-light.png"
134139
}
135140
}
136141
],

src/api.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ vscode.workspace.onDidChangeConfiguration(async e => {
1414
}
1515
}
1616
});
17-
export { API, ExportType };
18-
1917

18+
export { API, ExportType };

src/commands/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as vscode from 'vscode';
2+
import { registerSnippetCommands } from './snippet';
23
import { registerUserCommands } from './user';
34
import { registerTreeViewCommands } from './treeView';
45
import { registerNoteCommands } from './note';
@@ -7,4 +8,5 @@ export function registerCommands(context: vscode.ExtensionContext) {
78
registerUserCommands(context);
89
registerTreeViewCommands(context);
910
registerNoteCommands(context);
11+
registerSnippetCommands(context);
1012
}

src/commands/snippet.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as vscode from 'vscode';
2+
import { API } from './../api';
3+
import { checkLogin } from './../utils';
4+
5+
export function registerSnippetCommands(context: vscode.ExtensionContext) {
6+
context.subscriptions.push(vscode.commands.registerCommand('HackMD.createCodeSnippet', async () => {
7+
if (!(await checkLogin())){
8+
vscode.window.showInformationMessage('Please login first.');
9+
return;
10+
}
11+
12+
const editor = vscode.window.activeTextEditor;
13+
if (editor.selection.isEmpty) {
14+
vscode.window.showInformationMessage('The code block is empty. Please select a range of text first.');
15+
return;
16+
}
17+
18+
const textRange = new vscode.Range(editor.selection.start.line, editor.selection.start.character, editor.selection.end.line, editor.selection.end.character);
19+
const text = vscode.window.activeTextEditor.document.getText(textRange);
20+
const filePath = vscode.workspace.asRelativePath(editor.document.uri.fsPath);
21+
const snippet =
22+
`---
23+
title: public/${filePath}
24+
---
25+
> \`${filePath}\`
26+
27+
\`\`\`${editor.document.languageId}=${editor.selection.start.line + 1}
28+
${text}
29+
\`\`\``;
30+
31+
const noteUrl = await API.newNote(snippet);
32+
const clicked = await vscode.window.showInformationMessage('New Snippet Established!', ...['Copy URL to clip board', 'Open in browser']);
33+
if (clicked === 'Copy URL to clip board') {
34+
vscode.env.clipboard.writeText(noteUrl);
35+
} else if (clicked === 'Open in browser') {
36+
vscode.env.openExternal(vscode.Uri.parse(noteUrl));
37+
}
38+
}));
39+
}

src/commands/treeView.ts

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export async function registerTreeViewCommands(context: vscode.ExtensionContext)
5555
const content = await API.exportString(noteNode.noteId, ExportType.MD);
5656
if (!checkNoteExist(content)) { return; }
5757

58-
console.log(noteNode.label);
5958
const uri = vscode.Uri.parse(`hackmd:${noteNode.label}.md#${noteNode.noteId}`);
6059
const doc = await vscode.workspace.openTextDocument(uri);
6160
await vscode.window.showTextDocument(doc, { preview: false });

src/extension.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as markdownitContainer from 'markdown-it-container';
66
import * as S from 'string';
77
import { initializeStorage } from './store/storage';
88
import * as Prism from 'prismjs';
9-
import { registerCommands } from './commands';
9+
import { registerCommands } from './commands/index';
1010

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

290290
// this method is called when your extension is deactivated
291-
export function deactivate() { }
291+
export function deactivate() { }

src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ export const getLoginCredential = (context: vscode.ExtensionContext) => {
4545
const email: string = context.globalState.get('email');
4646
const password: string = context.globalState.get('password');
4747
return { email, password };
48-
};
48+
};

0 commit comments

Comments
 (0)