@@ -37,6 +37,8 @@ export default class QuickShareNotePlugin extends Plugin {
3737 let fileContent = await this . app . vault . read ( activeFile ) ;
3838 let updatedContent = await this . uploadImagesAndReplaceLinks ( fileContent ) ;
3939
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+
4042 if ( ! this . settings . showFrontmatter ) {
4143 updatedContent = updatedContent . replace ( / ^ - - - \n [ \s \S ] * ?\n - - - \n / , '' ) ; // Remove frontmatter if setting is false
4244 }
@@ -48,7 +50,20 @@ export default class QuickShareNotePlugin extends Plugin {
4850 const header = `# ${ fileNameWithoutSuffix } \n\n` ;
4951 const contentToPublish = frontmatter + header + contentWithoutFrontmatter ;
5052
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 ( {
5267 url : 'https://api.github.com/gists' ,
5368 method : 'POST' ,
5469 headers : {
@@ -57,19 +72,31 @@ export default class QuickShareNotePlugin extends Plugin {
5772 } ,
5873 body : JSON . stringify ( {
5974 files : {
60- [ activeFile . name ] : {
75+ [ activeFileName ] : {
6176 content : contentToPublish ,
6277 } ,
6378 } ,
6479 public : false , // Set to false for secret gist
6580 } ) ,
6681 } ) ;
82+ }
6783
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+ } ) ;
73100 }
74101
75102 async addHeaderToContent ( file : TFile ) {
@@ -87,12 +114,12 @@ export default class QuickShareNotePlugin extends Plugin {
87114
88115 if ( match ) {
89116 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 } ` ;
93120 newContent = fileContent . replace ( frontmatterRegex , `---\n${ updatedFrontmatter } \n---` ) ;
94121 } else {
95- newContent = `---\npublishUrl : ${ url } \n---\n${ fileContent } ` ;
122+ newContent = `---\ngistPublishUrl : ${ url } \n---\n${ fileContent } ` ;
96123 }
97124
98125 await this . app . vault . modify ( file , newContent ) ;
@@ -104,7 +131,6 @@ export default class QuickShareNotePlugin extends Plugin {
104131 const notePath = activeFile ? activeFile . path . replace ( / [ ^ / ] + $ / , '' ) : '' ;
105132 let match ;
106133 while ( ( match = imageRegex . exec ( content ) ) !== null ) {
107- // const imagePath = `${notePath}/attachments/${match[1]}`;
108134 const attachFile = this . app . metadataCache . getFirstLinkpathDest ( match [ 1 ] , notePath ) ;
109135 if ( attachFile == null ) {
110136 continue ;
0 commit comments