Skip to content

Commit fe86c12

Browse files
committed
Update gist if was published before
1 parent 455b882 commit fe86c12

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

main.ts

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/gistPublishUrl: https.*\/(.*)/);
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(/publishUrl: .*/, `publishUrl: ${url}`)
92-
: `${frontmatter}\npublishUrl: ${url}`;
117+
const updatedFrontmatter = frontmatter.includes('gistPublishUrl')
118+
? frontmatter.replace(/gistPublishUrl: .*/, `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;

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "quick-share-note-to-gist",
33
"name": "Quick Share Note to gist",
4-
"version": "1.0.15",
4+
"version": "1.0.16",
55
"minAppVersion": "0.15.0",
66
"description": "Quick share notes to GitHub gist and its image by upload images to Imgur.",
77
"author": "Por Chainarong Tangsurakit",

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "quick-share-note-to-gist",
3-
"version": "1.0.15",
3+
"version": "1.0.16",
44
"description": "Quick share notes to GitHub gist and its image by upload images to Imgur (https://github.com/chaintng/quick-share-note-to-gist)",
55
"main": "main.js",
66
"scripts": {

0 commit comments

Comments
 (0)