Skip to content

Commit

Permalink
mataroa: update slug in Mataroa if they differ from generated one
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Aug 15, 2024
1 parent 200a733 commit c86d53d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
5 changes: 0 additions & 5 deletions blog.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,7 @@ func getAndValidateSlug(mdFilename, title string) (string, error) {
}

func getSlug(s string) string {
// Mataroa slug function removes any "." from the slug
slug.CustomSub = map[string]string{
".": "",
}
return slug.Make(s)

}

func grabPosts() posts {
Expand Down
22 changes: 16 additions & 6 deletions mataroa.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ func mustMataroaReq(method string, url string, body []byte) (p mataroaResponse,
return p, resp
}

func mustGetMataroaPost(post post) (p mataroaResponse, r *http.Response) {
return mustMataroaReq("GET", mustMataroaUrl("posts", post.slug), nil)
func mustGetMataroaPost(slug string) (p mataroaResponse, r *http.Response) {
return mustMataroaReq("GET", mustMataroaUrl("posts", slug), nil)
}

func mustPatchMataroaPost(post post) (p mataroaResponse, r *http.Response) {
func mustPatchMataroaPost(slug string, post post) (p mataroaResponse, r *http.Response) {
buf := bytes.Buffer{}
must(md.Convert([]byte(post.contents), &buf))
reqBody := must1(json.Marshal(mataroaPatchRequest{
Expand All @@ -100,7 +100,7 @@ func mustPatchMataroaPost(post post) (p mataroaResponse, r *http.Response) {
Slug: post.slug,
PublishedAt: post.date.Format(time.DateOnly),
}))
return mustMataroaReq("PATCH", mustMataroaUrl("posts", post.slug), reqBody)
return mustMataroaReq("PATCH", mustMataroaUrl("posts", slug), reqBody)
}

func mustPostMataroaPost(post post) (p mataroaResponse, r *http.Response) {
Expand Down Expand Up @@ -128,13 +128,23 @@ func publishToMataroa(posts posts) {
)
for el := posts.Front(); el != nil; el = el.Next() {
post := el.Value
p, resp := mustGetMataroaPost(post)
p, resp := mustGetMataroaPost(post.slug)
if p.Ok {
p, resp = mustPatchMataroaPost(post)
p, resp = mustPatchMataroaPost(post.slug, post)
log.Printf("[UPDATED] (code=%d): %+v\n", resp.StatusCode, p)
} else if resp.StatusCode == 404 {
p, resp = mustPostMataroaPost(post)
log.Printf("[NEW] (code=%d): %+v\n", resp.StatusCode, p)

if p.Ok && p.Slug != post.slug {
log.Printf(
"[INFO] Updating slug since they're different, Mataroa slug: %s, generated one: %s",
p.Slug,
post.slug,
)
p, resp = mustPatchMataroaPost(p.Slug, post)
log.Printf("[UPDATED] (code=%d): %+v\n", resp.StatusCode, p)
}
} else {
log.Printf("[ERROR] %s: %+v\n", post.slug, resp)
}
Expand Down

0 comments on commit c86d53d

Please sign in to comment.