@@ -17,35 +17,93 @@ export async function registerTreeViewCommands(context: vscode.ExtensionContext)
17
17
context . subscriptions . push ( vscode . commands . registerCommand ( 'clickTreeItem' , async ( label , noteId ) => {
18
18
if ( label && noteId ) {
19
19
const content = await API . exportString ( noteId , ExportType . MD ) ;
20
- if ( content ) {
21
- const uri = vscode . Uri . parse ( `hackmd:${ label } .md#${ noteId } ` ) ;
22
- const doc = await vscode . workspace . openTextDocument ( uri ) ;
23
- await vscode . window . showTextDocument ( doc , { preview : false } ) ;
24
- }
20
+ if ( ! checkNoteExist ( content ) ) { return ; }
21
+
22
+ const uri = vscode . Uri . parse ( `hackmd:${ label } .md#${ noteId } ` ) ;
23
+ const doc = await vscode . workspace . openTextDocument ( uri ) ;
24
+ await vscode . window . showTextDocument ( doc , { preview : false } ) ;
25
+
25
26
}
26
27
} ) ) ;
27
28
28
- context . subscriptions . push ( vscode . commands . registerCommand ( 'note .showPreview' , async ( noteNode : NoteTreeNode ) => {
29
- if ( noteNode . label && noteNode . noteId ) {
29
+ context . subscriptions . push ( vscode . commands . registerCommand ( 'HackMD .showPreview' , async ( noteNode : NoteTreeNode ) => {
30
+ if ( noteNode ) {
30
31
const content = await API . exportString ( noteNode . noteId , ExportType . MD ) ;
31
- if ( content ) {
32
- const uri = vscode . Uri . parse ( `hackmd:${ noteNode . label } .md#${ noteNode . noteId } ` ) ;
33
- vscode . commands . executeCommand ( 'markdown.showPreview' , uri ) ;
34
- }
32
+ if ( ! checkNoteExist ( content ) ) { return ; }
33
+
34
+ const uri = vscode . Uri . parse ( `hackmd:${ noteNode . label } .md#${ noteNode . noteId } ` ) ;
35
+ vscode . commands . executeCommand ( 'markdown.showPreview' , uri ) ;
36
+ } else {
37
+ const editor = vscode . window . activeTextEditor ;
38
+ if ( ! checkEditorExist ( editor ) ) { return ; }
39
+
40
+ const noteId = editor . document . uri . fragment ;
41
+ if ( ! checkNoteIdExist ( noteId ) ) { return ; }
42
+
43
+ const content = await API . exportString ( noteId , ExportType . MD ) ;
44
+ if ( ! checkNoteExist ( content ) ) { return ; }
45
+
46
+ const fileName = editor . document . fileName . split ( '.' ) [ 0 ] ;
47
+ const uri = vscode . Uri . parse ( `hackmd:${ fileName } .md#${ noteId } ` ) ;
48
+ vscode . commands . executeCommand ( 'markdown.showPreview' , uri ) ;
35
49
}
36
50
} ) ) ;
37
51
38
- context . subscriptions . push ( vscode . commands . registerCommand ( 'note .showPreviewAndEditor' , async ( noteNode : NoteTreeNode ) => {
39
- if ( noteNode . label && noteNode . noteId ) {
52
+ context . subscriptions . push ( vscode . commands . registerCommand ( 'HackMD .showPreviewAndEditor' , async ( noteNode : NoteTreeNode ) => {
53
+ if ( noteNode ) {
40
54
const content = await API . exportString ( noteNode . noteId , ExportType . MD ) ;
41
- if ( content ) {
42
- const uri = vscode . Uri . parse ( `hackmd:${ noteNode . label } .md#${ noteNode . noteId } ` ) ;
43
- const doc = await vscode . workspace . openTextDocument ( uri ) ;
44
- await vscode . window . showTextDocument ( doc , { preview : false } ) ;
45
- vscode . commands . executeCommand ( 'markdown.showPreviewToSide' , uri ) ;
46
- }
55
+ if ( ! checkNoteExist ( content ) ) { return ; }
56
+
57
+ const uri = vscode . Uri . parse ( `hackmd:${ noteNode . label } .md#${ noteNode . noteId } ` ) ;
58
+ const doc = await vscode . workspace . openTextDocument ( uri ) ;
59
+ await vscode . window . showTextDocument ( doc , { preview : false } ) ;
60
+ vscode . commands . executeCommand ( 'markdown.showPreviewToSide' , uri ) ;
61
+
62
+ } else {
63
+ const editor = vscode . window . activeTextEditor ;
64
+ if ( ! checkEditorExist ( editor ) ) { return ; }
65
+
66
+ const noteId = editor . document . uri . fragment ;
67
+ if ( ! checkNoteIdExist ( noteId ) ) { return ; }
68
+
69
+ const content = await API . exportString ( noteId , ExportType . MD ) ;
70
+ if ( ! checkNoteExist ( content ) ) { return ; }
71
+
72
+ const fileName = editor . document . fileName . split ( '.' ) [ 0 ] ;
73
+ const uri = vscode . Uri . parse ( `hackmd:${ fileName } .md#${ noteId } ` ) ;
74
+ const doc = await vscode . workspace . openTextDocument ( uri ) ;
75
+ await vscode . window . showTextDocument ( doc , { preview : false } ) ;
76
+ vscode . commands . executeCommand ( 'markdown.showPreviewToSide' , uri ) ;
47
77
}
48
78
} ) ) ;
49
79
50
80
context . subscriptions . push ( vscode . workspace . registerTextDocumentContentProvider ( 'hackmd' , new MdTextDocumentContentProvider ( ) ) ) ;
51
- }
81
+ }
82
+
83
+ const checkEditorExist = ( editor ) => {
84
+ if ( editor ) {
85
+ return true ;
86
+ } else {
87
+ vscode . window . showInformationMessage ( 'Current window is not a text editor. Please open one first.' ) ;
88
+ return false ;
89
+ }
90
+ } ;
91
+
92
+ const checkNoteIdExist = ( noteId ) => {
93
+ if ( noteId ) {
94
+ return true ;
95
+ } else {
96
+ vscode . window . showInformationMessage ( "Please open a note first" ) ;
97
+ return false ;
98
+ }
99
+ } ;
100
+
101
+ const checkNoteExist = ( content ) => {
102
+ if ( content ) {
103
+ return true ;
104
+ } else {
105
+ vscode . window . showInformationMessage ( "Can't find the note from HackMD. Make sure it's still exist." ) ;
106
+ return false ;
107
+ }
108
+ } ;
109
+
0 commit comments