-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from magitools/feature/github
Feature/GitHub
- Loading branch information
Showing
7 changed files
with
117 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { RegisterPlatform, type IBasePlatform, type IPlatformSetting } from './base'; | ||
|
||
@RegisterPlatform | ||
export class GithubPlatform implements IBasePlatform<GithubPlatform> { | ||
settings: Record<string, string> = {}; | ||
frontmatter: Record<string, any> = {}; | ||
|
||
async publish(content: string) { | ||
const { github_token, github_repo, github_user, github_folder } = this.settings; | ||
if (!github_repo || !github_token || !github_user) { | ||
throw new Error('Required Settings not found'); | ||
} | ||
try { | ||
const title = this.frontmatter['title'].replaceAll(' ', '-').toLowerCase(); | ||
const commitMessage = this.settings['github_commit'].replace(/%([^%]+)%/g, (match, key) => { | ||
return key in this.frontmatter ? this.frontmatter[key] : match; | ||
}); | ||
const res = await fetch( | ||
`https://api.github.com/repos/${github_user}/${github_repo}/contents/${ | ||
github_folder ? github_folder + '/' : '' | ||
}${title}.md`, | ||
{ | ||
method: 'put', | ||
body: JSON.stringify({ | ||
content: btoa(content), | ||
message: commitMessage || 'published with magiedit' | ||
}), | ||
headers: { | ||
Authorization: `Bearer ${github_token}`, | ||
Accept: 'application/vnd.github+json', | ||
'Content-Type': 'application/json', | ||
'X-GitHub-Api-Version': '2022-11-28' | ||
} | ||
} | ||
); | ||
if (!res.ok) { | ||
throw new Error('request failed'); | ||
} | ||
} catch (error) { | ||
throw new Error(`${error}`); | ||
} | ||
} | ||
setSettings(settings: Record<string, any>): GithubPlatform { | ||
this.settings = settings; | ||
return this; | ||
} | ||
getRequiredSettings(): IPlatformSetting[] { | ||
return [ | ||
{ | ||
label: { htmlFor: 'github_token', value: 'Github Token' }, | ||
type: 'input', | ||
name: 'github_token', | ||
settings: { | ||
type: 'text', | ||
required: true | ||
} | ||
}, | ||
{ | ||
label: { htmlFor: 'github_user', value: 'Github User or Organization' }, | ||
type: 'input', | ||
name: 'github_user', | ||
settings: { | ||
type: 'text', | ||
required: true | ||
} | ||
}, | ||
{ | ||
label: { htmlFor: 'github_repo', value: 'Github Repository' }, | ||
type: 'input', | ||
name: 'github_repo', | ||
settings: { | ||
type: 'text', | ||
required: true | ||
} | ||
}, | ||
{ | ||
label: { | ||
htmlFor: 'github_folder', | ||
value: 'Github Folder Path (relative to the root of the repository)' | ||
}, | ||
type: 'input', | ||
name: 'github_folder', | ||
settings: { | ||
type: 'text' | ||
} | ||
}, | ||
{ | ||
label: { | ||
htmlFor: 'github_commit', | ||
value: | ||
'Commit Message for file creation: you can use your frontmatter variables by writing %variable_name% (i.e. %title%)' | ||
}, | ||
type: 'input', | ||
name: 'github_commit', | ||
settings: { | ||
type: 'text' | ||
} | ||
} | ||
]; | ||
} | ||
setFrontmatter(data: Record<string, any>): GithubPlatform { | ||
this.frontmatter = data; | ||
return this; | ||
} | ||
getPlatformName(): string { | ||
return 'Github'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { DevPlatform } from './dev'; | ||
import { HashnodePlatform } from './hashnode'; | ||
import { GithubPlatform } from './github'; | ||
|
||
export { DevPlatform, HashnodePlatform }; | ||
export { DevPlatform, HashnodePlatform, GithubPlatform }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters