@@ -37,6 +37,8 @@ export default class QuickShareNotePlugin extends Plugin {
37
37
let fileContent = await this . app . vault . read ( activeFile ) ;
38
38
let updatedContent = await this . uploadImagesAndReplaceLinks ( fileContent ) ;
39
39
40
+ const gistIdMatch = updatedContent . match ( / g i s t P u b l i s h U r l : h t t p s .* \/ ( .* ) / ) ;
41
+
40
42
if ( ! this . settings . showFrontmatter ) {
41
43
updatedContent = updatedContent . replace ( / ^ - - - \n [ \s \S ] * ?\n - - - \n / , '' ) ; // Remove frontmatter if setting is false
42
44
}
@@ -48,7 +50,20 @@ export default class QuickShareNotePlugin extends Plugin {
48
50
const header = `# ${ fileNameWithoutSuffix } \n\n` ;
49
51
const contentToPublish = frontmatter + header + contentWithoutFrontmatter ;
50
52
51
- const response = await requestUrl ( {
53
+ const response = ( gistIdMatch ) ?
54
+ await this . updateGist ( gistIdMatch [ 1 ] , activeFile . name , contentToPublish )
55
+ : await this . createNewGist ( activeFile . name , contentToPublish ) ;
56
+
57
+ const gistUrl = response . json . html_url ;
58
+ this . copyLinkToClipboard ( gistUrl ) ;
59
+ await this . updateFrontmatter ( activeFile , gistUrl ) ;
60
+ notice . hide ( ) ;
61
+ new Notice ( 'Note published to GitHub gist' ) ;
62
+ }
63
+
64
+
65
+ async createNewGist ( activeFileName : string , contentToPublish : string ) {
66
+ return await requestUrl ( {
52
67
url : 'https://api.github.com/gists' ,
53
68
method : 'POST' ,
54
69
headers : {
@@ -57,19 +72,31 @@ export default class QuickShareNotePlugin extends Plugin {
57
72
} ,
58
73
body : JSON . stringify ( {
59
74
files : {
60
- [ activeFile . name ] : {
75
+ [ activeFileName ] : {
61
76
content : contentToPublish ,
62
77
} ,
63
78
} ,
64
79
public : false , // Set to false for secret gist
65
80
} ) ,
66
81
} ) ;
82
+ }
67
83
68
- const gistUrl = response . json . html_url ;
69
- this . copyLinkToClipboard ( gistUrl ) ;
70
- await this . updateFrontmatter ( activeFile , gistUrl ) ;
71
- notice . hide ( ) ;
72
- new Notice ( 'Note published to GitHub gist' ) ;
84
+ async updateGist ( gistId : string , activeFileName : string , contentToPublish : string ) {
85
+ return await requestUrl ( {
86
+ url : `https://api.github.com/gists/${ gistId } ` ,
87
+ method : 'PATCH' ,
88
+ headers : {
89
+ Authorization : `token ${ this . settings . githubToken } ` ,
90
+ 'Content-Type' : 'application/json' ,
91
+ } ,
92
+ body : JSON . stringify ( {
93
+ files : {
94
+ [ activeFileName ] : {
95
+ content : contentToPublish ,
96
+ } ,
97
+ } ,
98
+ } ) ,
99
+ } ) ;
73
100
}
74
101
75
102
async addHeaderToContent ( file : TFile ) {
@@ -87,12 +114,12 @@ export default class QuickShareNotePlugin extends Plugin {
87
114
88
115
if ( match ) {
89
116
const frontmatter = match [ 1 ] ;
90
- const updatedFrontmatter = frontmatter . includes ( 'publishUrl ' )
91
- ? frontmatter . replace ( / p u b l i s h U r l : .* / , `publishUrl : ${ url } ` )
92
- : `${ frontmatter } \npublishUrl : ${ url } ` ;
117
+ const updatedFrontmatter = frontmatter . includes ( 'gistPublishUrl ' )
118
+ ? frontmatter . replace ( / g i s t P u b l i s h U r l : .* / , `gistPublishUrl : ${ url } ` )
119
+ : `${ frontmatter } \ngistPublishUrl : ${ url } ` ;
93
120
newContent = fileContent . replace ( frontmatterRegex , `---\n${ updatedFrontmatter } \n---` ) ;
94
121
} else {
95
- newContent = `---\npublishUrl : ${ url } \n---\n${ fileContent } ` ;
122
+ newContent = `---\ngistPublishUrl : ${ url } \n---\n${ fileContent } ` ;
96
123
}
97
124
98
125
await this . app . vault . modify ( file , newContent ) ;
@@ -104,7 +131,6 @@ export default class QuickShareNotePlugin extends Plugin {
104
131
const notePath = activeFile ? activeFile . path . replace ( / [ ^ / ] + $ / , '' ) : '' ;
105
132
let match ;
106
133
while ( ( match = imageRegex . exec ( content ) ) !== null ) {
107
- // const imagePath = `${notePath}/attachments/${match[1]}`;
108
134
const attachFile = this . app . metadataCache . getFirstLinkpathDest ( match [ 1 ] , notePath ) ;
109
135
if ( attachFile == null ) {
110
136
continue ;
0 commit comments