Skip to content

Commit

Permalink
blog: also rewrite RSS links
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Jul 30, 2024
1 parent 4e3f254 commit 61beb09
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
8 changes: 5 additions & 3 deletions blog.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/yuin/goldmark"
)

const rssBaseUrl = "https://github.com/thiagokokada/blog/blob/main"
const blogBaseUrl = "https://github.com/thiagokokada/blog/blob/main"
const readmeTemplate = `# Blog
Mirror of my blog in https://kokada.capivaras.dev/.
Expand Down Expand Up @@ -149,12 +149,14 @@ func genRss(posts posts) string {
Title: "kokada's blog",
Description: "# dd if=/dev/urandom of=/dev/brain0",
}
md := goldmark.New(goldmark.WithExtensions(NewLinkRewriter(blogBaseUrl, nil)))

var items []*feeds.Item
for el := posts.Back(); el != nil; el = el.Prev() {
post := el.Value
link := must1(url.JoinPath(rssBaseUrl, post.file))
link := must1(url.JoinPath(blogBaseUrl, post.file))
var buf bytes.Buffer
must(goldmark.Convert(post.contents, &buf))
must(md.Convert(post.contents, &buf))
items = append(items, &feeds.Item{
Title: post.title,
Link: &feeds.Link{Href: link},
Expand Down
25 changes: 17 additions & 8 deletions link_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import (

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

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

// Extend will be called by Goldmark to add your extension
Expand Down Expand Up @@ -63,12 +64,20 @@ func (e *linkRewriter) rewriteLink(l *ast.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)
if e.posts != nil {
// If posts are not nil, it means we will grab the slug
// from posts
post, ok := e.posts.Get(link[1:])
if ok {
dest := must1(url.JoinPath(e.prefixUrl, post.slug))
l.Destination = []byte(dest)
} else {
log.Printf("[WARN]: did not find reference to link: %s\n", link)
}
} else {
log.Printf("[WARN]: did not find reference to link: %s\n", link)
// Else we will just append the prefixUrl to the link
dest := must1(url.JoinPath(e.prefixUrl, link))
l.Destination = []byte(dest)
}
}
}
2 changes: 1 addition & 1 deletion mataroa.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func mustPostMataroaPost(md goldmark.Markdown, post post) (p mataroaResponse, r
func publishToMataroa(posts posts) {
md := goldmark.New(
goldmark.WithRenderer(markdown.NewRenderer()),
goldmark.WithExtensions(NewLinkRewriter(posts)),
goldmark.WithExtensions(NewLinkRewriter(mataroaBlogUrl, posts)),
)
for el := posts.Front(); el != nil; el = el.Next() {
post := el.Value
Expand Down
Loading

0 comments on commit 61beb09

Please sign in to comment.