Skip to content

Commit

Permalink
mataroa: use relative links in Markdown and rewrite them before publi…
Browse files Browse the repository at this point in the history
…shing
  • Loading branch information
thiagokokada committed Jul 30, 2024
1 parent 28eb1df commit 4e3f254
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.21'

- name: Update
run: make
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
If everything works as expected this blog post should appear in [in my
blog](https://kokada.capivaras.dev/) without I ever touching the
[capivaras.dev](https://capivavas.dev) website. I rewrote my [previous Python
script](https://kokada.capivaras.dev/blog/using-github-as-a-bad-blog-platform/)
to Go
script](/2024-07-26/02-using-github-as-a-bad-blog-platform.md) to Go
([permalink](https://github.com/thiagokokada/blog/blob/3c39e0f7cd58b1af885f69871490b05bf6fc7d99/blog.go))
since my attempt to generate proper description to the RSS feed resulted in
slow startup times (not because of Python, but because of my usage of
Expand Down
11 changes: 5 additions & 6 deletions 2024-07-29/02-go-a-reasonable-good-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ library](https://github.com/thiagokokada/hyprland-go/). I am also writing more
[scripts](https://github.com/thiagokokada/nix-configs/blob/8c559527ed12e1d4f57a3fc5c72630b956f4c290/home-manager/desktop/wayland/hyprland/hyprtabs/hyprtabs.go)
in the language, where I would prefer to use Bash or Python before. Heck, even
this blog is automatically published with a [Go
script](https://kokada.capivaras.dev/blog/quick-bits-why-you-should-automate-everything/),
that used to be a [Python
one](https://kokada.capivaras.dev/blog/using-github-as-a-bad-blog-platform/)
before. I can say that nowadays it is another language in my toolbox, and while
it is still a love and hate relationship, recently it is more about love and
less about hate.
script](/2024-07-29/01-quick-bits-why-you-should-automate-everything.md), that
used to be a [Python
one](/2024-07-26/02-using-github-as-a-bad-blog-platform.md) before. I can say
that nowadays it is another language in my toolbox, and while it is still a
love and hate relationship, recently it is more about love and less about hate.

The points that I love about Go is probably obvious for some, but still
interesting to talk about anyway. The fact that the language generates static
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module github.com/thiagokokada/blog

go 1.20
go 1.21

require (
github.com/elliotchance/orderedmap/v2 v2.2.0
github.com/gorilla/feeds v1.2.0
github.com/gosimple/slug v1.14.0
github.com/teekennedy/goldmark-markdown v0.3.0
github.com/yuin/goldmark v1.7.4
)

Expand Down
13 changes: 12 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elliotchance/orderedmap/v2 v2.2.0 h1:7/2iwO98kYT4XkOjA9mBEIwvi4KpGB4cyHeOFOnj4Vk=
github.com/elliotchance/orderedmap/v2 v2.2.0/go.mod h1:85lZyVbpGaGvHvnKa7Qhx7zncAdBIBq6u56Hb1PRU5Q=
github.com/gorilla/feeds v1.2.0 h1:O6pBiXJ5JHhPvqy53NsjKOThq+dNFm8+DFrxBEdzSCc=
Expand All @@ -8,10 +9,20 @@ github.com/gosimple/slug v1.14.0/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818J
github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rhysd/go-fakeio v1.0.0 h1:+TjiKCOs32dONY7DaoVz/VPOdvRkPfBkEyUDIpM8FQY=
github.com/rhysd/go-fakeio v1.0.0/go.mod h1:joYxF906trVwp2JLrE4jlN7A0z6wrz8O6o1UjarbFzE=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/teekennedy/goldmark-markdown v0.3.0 h1:ik9/biVGCwGWFg8dQ3KVm2pQ/wiiG0whYiUcz9xH0W8=
github.com/teekennedy/goldmark-markdown v0.3.0/go.mod h1:kMhDz8La77A9UHvJGsxejd0QUflN9sS+QXCqnhmxmNo=
github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg=
github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
74 changes: 74 additions & 0 deletions link_rewriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// Version 2, December 2004
//
// Copyright (C) 2024 Thiago Kenji Okada <thiagokokada@gmail.com>
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
//
// 0. You just DO WHAT THE FUCK YOU WANT TO.

import (
"log"
"net/url"
"strings"

"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)

// linkRewriter is the main struct for your extension
type linkRewriter struct {
posts posts
}

// NewLinkRewriter returns a new instance of LinkRewriter
func NewLinkRewriter(posts posts) *linkRewriter {
return &linkRewriter{posts}
}

// Extend will be called by Goldmark to add your extension
func (e *linkRewriter) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(parser.WithASTTransformers(util.Prioritized(e, 0)))
}

// Transform is the method that modifies the AST
func (e *linkRewriter) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
if link, ok := n.(*ast.Link); ok {
e.rewriteLink(link)
}
return ast.WalkContinue, nil
})
}

// rewriteLink modifies the link URL
func (e *linkRewriter) rewriteLink(l *ast.Link) {
link := string(l.Destination)

if strings.HasPrefix(link, ".") {
log.Printf("[WARN]: relative link reference found: %s\n", link)
}

if strings.HasPrefix(link, "/") {
post, ok := e.posts.Get(link[1:])
if ok {
dest := must1(url.JoinPath(mataroaBlogUrl, post.slug))
l.Destination = []byte(dest)
} else {
log.Printf("[WARN]: did not find reference to link: %s\n", link)
}
}
}
29 changes: 21 additions & 8 deletions mataroa.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ import (
"net/url"
"os"
"time"

markdown "github.com/teekennedy/goldmark-markdown"
"github.com/yuin/goldmark"
)

const mataroaApiUrl = "https://capivaras.dev/api/"
const mataroaBaseUrl = "https://kokada.capivaras.dev"
const mataroaApiUrl = mataroaBaseUrl + "/api/"
const mataroaBlogUrl = mataroaBaseUrl + "/blog/"

var mataroaToken = os.Getenv("MATAROA_TOKEN")

Expand Down Expand Up @@ -77,34 +82,42 @@ func mustGetMataroaPost(post post) (p mataroaResponse, r *http.Response) {
return mustMataroaReq("GET", []string{"posts", post.slug}, nil)
}

func mustPatchMataroaPost(post post) (p mataroaResponse, r *http.Response) {
func mustPatchMataroaPost(md goldmark.Markdown, post post) (p mataroaResponse, r *http.Response) {
buf := bytes.Buffer{}
must(md.Convert([]byte(post.contents), &buf))
reqBody := must1(json.Marshal(mataroaPatchRequest{
Title: post.title,
Body: string(post.contents),
Body: buf.String(),
Slug: post.slug,
PublishedAt: post.date.Format(time.DateOnly),
}))
return mustMataroaReq("PATCH", []string{"posts", post.slug}, reqBody)
}

func mustPostMataroaPost(post post) (p mataroaResponse, r *http.Response) {
func mustPostMataroaPost(md goldmark.Markdown, post post) (p mataroaResponse, r *http.Response) {
buf := bytes.Buffer{}
must(md.Convert([]byte(post.contents), &buf))
reqBody := must1(json.Marshal(mataroaPostRequest{
Title: post.title,
Body: string(post.contents),
Body: buf.String(),
PublishedAt: post.date.Format(time.DateOnly),
}))
return mustMataroaReq("POST", []string{"posts"}, reqBody)
}

func publishToMataroa(posts posts) {
for el := posts.Back(); el != nil; el = el.Prev() {
md := goldmark.New(
goldmark.WithRenderer(markdown.NewRenderer()),
goldmark.WithExtensions(NewLinkRewriter(posts)),
)
for el := posts.Front(); el != nil; el = el.Next() {
post := el.Value
p, resp := mustGetMataroaPost(post)
if p.Ok {
p, resp = mustPatchMataroaPost(post)
p, resp = mustPatchMataroaPost(md, post)
log.Printf("[UPDATED] (code=%d): %+v\n", resp.StatusCode, p)
} else if resp.StatusCode == 404 {
p, resp = mustPostMataroaPost(post)
p, resp = mustPostMataroaPost(md, post)
log.Printf("[NEW] (code=%d): %+v\n", resp.StatusCode, p)
} else {
log.Printf("[ERROR] %s: %+v\n", post.slug, resp)
Expand Down
4 changes: 2 additions & 2 deletions rss.xml

Large diffs are not rendered by default.

0 comments on commit 4e3f254

Please sign in to comment.