Skip to content

Commit 21dddfd

Browse files
committed
--markdown now shows the markdown that will be pushed to dev.to
1 parent 2b541e2 commit 21dddfd

File tree

2 files changed

+87
-29
lines changed

2 files changed

+87
-29
lines changed

‎README.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
## Notes
3+
4+
**Validation failed: Canonical url has already been taken** means that
5+
another article of yours exists with the same `canonical_url` field in its
6+
front matter; it often means that there is a duplicate article.
7+
8+
**Validation failed: Body markdown has already been taken** means that the
9+
same markdown body already existings in one of your articles on dev.to.
10+
Often means that there is a duplicate article.
11+
12+
**Validation failed: (<unknown>): could not find expected ':' while scanning a simple key at line 4 column 1**: you can use the command
13+
14+
```sh
15+
hudevto push --markdown ./content/2020/gh-actions-with-tf-private-repo/index.md
16+
```
17+
18+
to see what is being uploaded to dev.to. I often got this error when trying
19+
to do a multi-line description. I had to change from:
20+
21+
```yaml
22+
description: |
23+
We often talk about avoiding unecessary comments that needlessly paraphrase
24+
what the code does. In this article, I gathered some thoughts about why
25+
writing comments is as important as writing the code itself.
26+
```
27+
28+
to:
29+
30+
```yaml
31+
description: "We often talk about avoiding unecessary comments that needlessly paraphrase what the code does. In this article, I gathered some thoughts about why writing comments is as important as writing the code itself."
32+
```

‎main.go‎

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/gohugoio/hugo/deps"
2323
"github.com/gohugoio/hugo/hugofs"
2424
"github.com/gohugoio/hugo/hugolib"
25+
"github.com/gohugoio/hugo/resources/page"
2526
"github.com/sethgrid/gencurl"
2627
"github.com/spf13/jwalterweatherman"
2728
"github.com/spf13/viper"
@@ -30,9 +31,10 @@ import (
3031
)
3132

3233
var (
33-
rootDir = flag.String("root", "", "Root directory of the Hugo project.")
34-
apiKey = flag.String("apikey", os.Getenv("DEVTO_APIKEY"), "The API key for Dev.to.")
35-
debug = flag.Bool("debug", false, "Print debug information.")
34+
rootDir = flag.String("root", "", "Root directory of the Hugo project.")
35+
apiKey = flag.String("apikey", os.Getenv("DEVTO_APIKEY"), "The API key for Dev.to.")
36+
debug = flag.Bool("debug", false, "Print debug information.")
37+
showMarkdown = flag.Bool("markdown", false, "Show the markdown of the given article.")
3638
)
3739

3840
func main() {
@@ -45,7 +47,7 @@ func main() {
4547

4648
switch flag.Arg(0) {
4749
case "push":
48-
err := PushArticlesFromHugoToDevto(filepath.Clean(*rootDir), flag.Arg(1), *apiKey)
50+
err := PushArticlesFromHugoToDevto(filepath.Clean(*rootDir), flag.Arg(1), *showMarkdown, *apiKey)
4951
if err != nil {
5052
logutil.Errorf(err.Error())
5153
os.Exit(1)
@@ -62,7 +64,7 @@ func main() {
6264
}
6365

6466
// Updates all articles if pathToArticle is left empty.
65-
func PushArticlesFromHugoToDevto(rootDir, pathToArticle, apiKey string) error {
67+
func PushArticlesFromHugoToDevto(rootDir, pathToArticle string, showMarkdown bool, apiKey string) error {
6668
conf, err := loadHugoConfig(rootDir)
6769
if err != nil {
6870
return err
@@ -96,15 +98,11 @@ func PushArticlesFromHugoToDevto(rootDir, pathToArticle, apiKey string) error {
9698
return fmt.Errorf("devto client: %w", err)
9799
}
98100

99-
articles, err := client.Articles.ListMyUnpublishedArticles(context.Background(), nil)
101+
articles, err := listAllMyArticles(client)
100102
if err != nil {
101-
return fmt.Errorf("fetching unpublished articles: %s", err)
103+
return fmt.Errorf("listing all the user's articles: %w", err)
102104
}
103-
articlesPub, err := client.Articles.ListMyPublishedArticles(context.Background(), nil)
104-
if err != nil {
105-
return fmt.Errorf("fetching published articles: %s", err)
106-
}
107-
articles = append(articles, articlesPub...)
105+
108106
articlesIdMap := make(map[int]*devto.ListedArticle)
109107
articlesTitleMap := make(map[string]*devto.ListedArticle)
110108
for i := range articles {
@@ -113,11 +111,21 @@ func PushArticlesFromHugoToDevto(rootDir, pathToArticle, apiKey string) error {
113111
articlesTitleMap[art.Title] = art
114112
}
115113

114+
pages := sites.Pages()
116115
if pathToArticle != "" {
117-
sites.GetContentPage(pathToArticle)
116+
path := sites.AbsPathify(pathToArticle)
117+
if path == "" {
118+
return fmt.Errorf("%s not found", logutil.Gray(pathToArticle))
119+
}
120+
p := sites.GetContentPage(path)
121+
if p == nil {
122+
return fmt.Errorf("%s was found but does not seem to be a page", logutil.Gray(pathToArticle))
123+
}
124+
125+
pages = []page.Page{p}
118126
}
119127

120-
for _, page := range sites.Pages() {
128+
for _, page := range pages {
121129
if page.Kind() != "page" {
122130
continue
123131
}
@@ -240,6 +248,10 @@ func PushArticlesFromHugoToDevto(rootDir, pathToArticle, apiKey string) error {
240248

241249
content += convertHugoToLiquid(page.RawContent())
242250

251+
if showMarkdown {
252+
fmt.Print(content)
253+
}
254+
243255
Update:
244256
art, err := UpdateArticle(httpClient, id, Article{BodyMarkdown: content})
245257
switch {
@@ -273,6 +285,22 @@ func PushArticlesFromHugoToDevto(rootDir, pathToArticle, apiKey string) error {
273285
return nil
274286
}
275287

288+
// Returns all the user's unpublished articles and then the published
289+
// articles.
290+
func listAllMyArticles(client *devto.Client) ([]devto.ListedArticle, error) {
291+
// The max. number of items per page is 1000, see:
292+
// https://docs.forem.com/api/#tag/articles.
293+
articlesUnpublished, err := client.Articles.ListMyUnpublishedArticles(context.Background(), &devto.MyArticlesOptions{PerPage: 1000})
294+
if err != nil {
295+
return nil, fmt.Errorf("fetching unpublished articles: %s", err)
296+
}
297+
articlesPublished, err := client.Articles.ListMyPublishedArticles(context.Background(), &devto.MyArticlesOptions{PerPage: 1000})
298+
if err != nil {
299+
return nil, fmt.Errorf("fetching published articles: %s", err)
300+
}
301+
return append(articlesUnpublished, articlesPublished...), nil
302+
}
303+
276304
func PrintDevtoArticles(apiKey string) error {
277305
httpClient := http.DefaultClient
278306
httpClient.Transport = curlDebug(http.DefaultTransport, logutil.EnableDebug, apiKey)
@@ -283,28 +311,22 @@ func PrintDevtoArticles(apiKey string) error {
283311
return fmt.Errorf("devto client: %w", err)
284312
}
285313

286-
articles, err := client.Articles.ListMyUnpublishedArticles(context.Background(), &devto.MyArticlesOptions{})
314+
articles, err := listAllMyArticles(client)
287315
for _, article := range articles {
288-
fmt.Printf("%s %s %s\n",
289-
logutil.Gray(strconv.Itoa(int(article.ID))),
290-
logutil.Red(article.URL.String()+"/edit"),
291-
article.Title,
292-
)
293-
}
294-
if err != nil {
295-
return fmt.Errorf("listing all unpublished articles on dev.to: %w", err)
296-
}
297316

298-
articles, err = client.Articles.ListMyPublishedArticles(context.Background(), &devto.MyArticlesOptions{})
299-
for _, article := range articles {
300-
fmt.Printf("%s %s %s\n",
317+
publishedStr := logutil.Red("unpublished")
318+
if article.Published {
319+
publishedStr = logutil.Green("published")
320+
}
321+
fmt.Printf("%s: %s at %s (%s)\n",
301322
logutil.Gray(strconv.Itoa(int(article.ID))),
302-
logutil.Green(article.URL.String()),
323+
publishedStr,
324+
logutil.Yel(addEditSegment(article.URL.String(), article.Published)),
303325
article.Title,
304326
)
305327
}
306328
if err != nil {
307-
return fmt.Errorf("listing all published articles on dev.to: %w", err)
329+
return fmt.Errorf("listing user's articles on dev.to: %w", err)
308330
}
309331

310332
return nil
@@ -463,3 +485,7 @@ var hugoTag = regexp.MustCompile("{{< ([a-z]+) (.*) >}}")
463485
func convertHugoToLiquid(in string) string {
464486
return hugoTag.ReplaceAllString(in, "{% $1 $2 %}")
465487
}
488+
489+
// client.Articles.ListAllMyArticles was not actually listing all articles
490+
// and would only show the unpublished ones. Also, it would only show the
491+
// first 20.

0 commit comments

Comments
 (0)