-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
177 lines (139 loc) · 4.85 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import "github.com/mgutz/ansi"
import "github.com/briandowns/spinner"
import "fmt"
import "os"
import "bufio"
import "io/ioutil"
import "time"
import "github.com/Medium/medium-sdk-go"
import "github.com/google/go-github/github"
import "golang.org/x/oauth2"
import "context"
func main() {
logger := NewPrettyLogger()
config, err := LoadConfig()
if err != nil {
logger.Error(fmt.Sprintf("Failed to load config file: %s", err))
}
if len(os.Args) == 1 {
printUsage()
os.Exit(1)
}
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
var configChanged bool
if config.MediumAccessToken == "" {
mediumAccessToken := promptForMediumAccessToken()
config.MediumAccessToken = mediumAccessToken
configChanged = true
}
mediumClient := medium.NewClientWithAccessToken(config.MediumAccessToken)
if config.MediumUserId == "" {
logger.Info("Fetching Medium user")
s.Start()
user, err := mediumClient.GetUser("")
s.Stop()
if err != nil {
if err != nil {
logger.Error(fmt.Sprintf("Failed to load Medium user: %s", err))
}
}
config.MediumUserId = user.ID
configChanged = true
}
if config.GitHubAccessToken == "" {
if githubAccessToken := promptForGitHubAccessToken(); githubAccessToken != "" {
config.GitHubAccessToken = githubAccessToken
configChanged = true
}
}
if configChanged {
configPath, _ := ConfigPath()
logger.Info(fmt.Sprintf("Saving config file: %s", configPath))
if err := SaveConfig(config); err != nil {
logger.Error(fmt.Sprintf("Failed to save config file: %s", err))
}
}
markdownPath := os.Args[1]
if _, err := os.Stat(markdownPath); os.IsNotExist(err) {
logger.Error(fmt.Sprintf("File does not exist: %s", markdownPath))
}
markdownContent, err := ioutil.ReadFile(markdownPath)
if err != nil {
logger.Error(fmt.Sprintf("Failed to read %s: %s", markdownPath, err))
}
ctx := context.Background()
githubClient := github.NewClient(oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.GitHubAccessToken},
)))
metadata, markdownContent, err := ExtractMarkdownMetadata(markdownContent)
if err != nil {
logger.Error(fmt.Sprintf("Failed to extract metadata from Markdown file: %s", err))
}
postHtml := string(MarkdownToHTML(markdownContent)[:])
htmlFormatter, _ := NewHtmlFormatter(markdownPath, postHtml, logger, ctx)
if err := htmlFormatter.ReplaceCodeBlocks(githubClient); err != nil {
logger.Error(fmt.Sprintf("Error replacing code blocks with gists: %s", err))
}
if err := htmlFormatter.ReplaceImages(mediumClient); err != nil {
logger.Error(fmt.Sprintf("Error replacing images: %s", err))
}
formattedHtml, err := htmlFormatter.Html()
if err != nil {
logger.Error(fmt.Sprintf("Error formatting HTML: %s", err))
}
logger.Info("Creating Medium post")
s.Start()
post, err := mediumClient.CreatePost(medium.CreatePostOptions{
UserID: config.MediumUserId,
Content: formattedHtml,
ContentFormat: medium.ContentFormatHTML,
PublishStatus: medium.PublishStatusDraft,
Tags: metadata.Tags,
})
s.Stop()
if err != nil {
logger.Error(fmt.Sprintf("Failed to create Medium post: %s", err))
}
fmt.Println("")
logger.Success(fmt.Sprintf("Successfully created Medium post.\n You can find it here: %s%s%s", ansi.ColorCode("white+u"), post.URL, ansi.ColorCode("reset")))
}
func printUsage() {
fmt.Println("")
fmt.Println("Usage: md2markdown MARKDOWN_FILE_PATH")
fmt.Println("")
}
func promptForGitHubAccessToken() string {
fmt.Println("")
fmt.Println("If you wish to syntax highlight code snippets, you will need to provide a GitHub access token.")
fmt.Println("This token will only be used to create private gists which will be embedded within your post.")
fmt.Println("")
fmt.Printf("You can generate a personal access token here: %shttps://github.com/settings/tokens%s\n", ansi.ColorCode("white+u"), ansi.ColorCode("reset"))
fmt.Println("")
fmt.Println("Enter personal access token or hit enter to skip:")
fmt.Println("")
fmt.Printf("%s>%s ", ansi.ColorCode("white+b"), ansi.ColorCode("reset"))
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
githubAccessToken := scanner.Text()
fmt.Println("")
return githubAccessToken
}
func promptForMediumAccessToken() string {
fmt.Println("")
fmt.Println("In order to create the post on Medium, you will need to provide a Medium access token.")
fmt.Println("")
fmt.Printf("You can generate an integration token here: %shttps://medium.com/me/settings%s\n", ansi.ColorCode("white+u"), ansi.ColorCode("reset"))
fmt.Println("")
fmt.Println("Enter integration token:")
fmt.Println("")
fmt.Printf("%s>%s ", ansi.ColorCode("white+b"), ansi.ColorCode("reset"))
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
mediumAccessToken := scanner.Text()
if mediumAccessToken == "" {
return promptForMediumAccessToken()
}
fmt.Println("")
return mediumAccessToken
}