Skip to content

Commit 93d2f8d

Browse files
committed
refactor: change totally articles uris
1 parent 2159857 commit 93d2f8d

6 files changed

Lines changed: 103 additions & 51 deletions

File tree

articles/How I deploy Nix + Docker.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: How I deploy Nix + Docker
33
date: 2024-06-09
4+
uri: deploy-nix-docker
45
---
56

67
Hello everyone :)

articles/Install NixOS on Contabo server.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: How to install NixOS on a Contabo Server
33
date: 2023-08-02
4+
uri: install-nixos-contabo-vps
45
---
56

67
In this guide, we'll see how to setup NixOS on a Ubuntu 22.04 Contabo VPS.

articles/Server block Ubuntu + Nginx + systemd.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: How to setup a server block with Nginx + systemd on Ubuntu with HTTPS
33
date: 2023-05-02
4+
uri: server-block-nginx-systemd-ubuntu-https
45
---
56

67
In this guide, we are going to use the keyword for you to replace. There will be `REPOSITORY ` and `DOMAIN_NAME` to replace.

pkg/routes/blog/article.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package blog
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io/fs"
7+
"net/url"
8+
"strings"
9+
"time"
10+
11+
chroma "github.com/alecthomas/chroma/v2/formatters/html"
12+
"github.com/charmbracelet/log"
13+
"github.com/yuin/goldmark"
14+
highlighting "github.com/yuin/goldmark-highlighting/v2"
15+
meta "github.com/yuin/goldmark-meta"
16+
mdParser "github.com/yuin/goldmark/parser"
17+
)
18+
19+
type article struct {
20+
Title string
21+
Date time.Time
22+
Content []byte
23+
Uri string
24+
}
25+
26+
// TODO: Add metadata validation
27+
28+
func NewArticle(file fs.File) (*article, error) {
29+
30+
var buf bytes.Buffer
31+
context, err := fileToMarkdown(file, &buf)
32+
if err != nil {
33+
return nil, fmt.Errorf("Could not convert to markdown: %s", err)
34+
}
35+
36+
metadata := meta.Get(context)
37+
38+
date, err := time.Parse("2006-01-02", metadata["date"].(string))
39+
if err != nil {
40+
return nil, fmt.Errorf("Could not parse as date : %s", err)
41+
}
42+
43+
uri, err := makeArticleUri(date, metadata)
44+
if err != nil {
45+
return nil, fmt.Errorf("Could make article uri: %s", err)
46+
}
47+
48+
return &article{
49+
Title: metadata["title"].(string),
50+
Date: date,
51+
Content: buf.Bytes(),
52+
Uri: uri,
53+
}, nil
54+
}
55+
56+
func fileToMarkdown(file fs.File, buf *bytes.Buffer) (mdParser.Context, error) {
57+
info, err := file.Stat()
58+
if err != nil {
59+
return nil, fmt.Errorf("Error getting file info: %s", err)
60+
}
61+
log.Info("Opening new article", "name", info.Name)
62+
63+
content := make([]byte, info.Size())
64+
if _, err = file.Read(content); err != nil {
65+
return nil, fmt.Errorf("Could not read file: %s", err)
66+
}
67+
68+
markdown := goldmark.New(
69+
goldmark.WithExtensions(
70+
highlighting.NewHighlighting(
71+
highlighting.WithStyle("catppuccin-mocha"),
72+
highlighting.WithFormatOptions(chroma.WithLineNumbers(true)),
73+
),
74+
meta.Meta,
75+
),
76+
)
77+
context := mdParser.NewContext()
78+
if err = markdown.Convert(content, buf, mdParser.WithContext(context)); err != nil {
79+
return nil, fmt.Errorf("Could not converting file to markdown: %s", err)
80+
}
81+
return context, nil
82+
}
83+
84+
func makeArticleUri(date time.Time, metadata map[string]any) (string, error) {
85+
86+
metaUri := metadata["uri"].(string)
87+
88+
if metaUri == "" {
89+
metaTitle := metadata["title"].(string)
90+
if metaTitle == "" {
91+
return "", fmt.Errorf("Either one of 'uri' or 'title' needs to be set")
92+
}
93+
metaUri = url.QueryEscape(strings.ReplaceAll(strings.ToLower(metaTitle), " ", "-"))
94+
}
95+
96+
return fmt.Sprintf("%s/%s", date.Format("2006/01"), metaUri), nil
97+
}

pkg/routes/blog/blog.go

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,28 @@
11
package blog
22

33
import (
4-
"bytes"
54
"errors"
65
"io/fs"
76
"net/http"
87
"net/url"
98
"sort"
109
"strings"
11-
"time"
1210

1311
"app/articles"
1412
"app/pkg/app"
1513

1614
"github.com/a-h/templ"
17-
chroma "github.com/alecthomas/chroma/v2/formatters/html"
1815
"github.com/charmbracelet/log"
19-
"github.com/yuin/goldmark"
20-
highlighting "github.com/yuin/goldmark-highlighting/v2"
21-
meta "github.com/yuin/goldmark-meta"
22-
"github.com/yuin/goldmark/parser"
2316
)
2417

2518
var Articles = make(map[string]article)
2619

2720
func init() {
2821
for _, a := range getArticles(articles.Articles) {
29-
Articles[a.Title] = a
22+
Articles[a.Uri] = a
3023
}
3124
}
3225

33-
type article struct {
34-
Title string
35-
Date time.Time
36-
Content []byte
37-
}
38-
3926
func Handler(serv *app.Server, w http.ResponseWriter, r *http.Request) (templ.Component, error) {
4027
article_name, err := url.PathUnescape(r.PathValue("article"))
4128
if err != nil || article_name == "" {
@@ -82,45 +69,10 @@ func getArticles(filesystem fs.ReadDirFS) []article {
8269
log.Warn("Error opening file:", "reason", err)
8370
continue
8471
}
85-
a, err := parseMarkdownArticle(file)
72+
a, err := NewArticle(file)
8673
if err == nil || a != nil {
8774
result = append(result, *a)
8875
}
8976
}
9077
return result
9178
}
92-
93-
func parseMarkdownArticle(file fs.File) (*article, error) {
94-
info, err := file.Stat()
95-
if err != nil {
96-
log.Warn("Error getting file info:", "reason", err)
97-
return nil, err
98-
}
99-
content := make([]byte, info.Size())
100-
if _, err = file.Read(content); err != nil {
101-
return nil, err
102-
}
103-
104-
markdown := goldmark.New(
105-
goldmark.WithExtensions(
106-
highlighting.NewHighlighting(
107-
highlighting.WithStyle("catppuccin-mocha"),
108-
highlighting.WithFormatOptions(chroma.WithLineNumbers(true)),
109-
),
110-
meta.Meta,
111-
),
112-
)
113-
var buf bytes.Buffer
114-
context := parser.NewContext()
115-
if err = markdown.Convert(content, &buf, parser.WithContext(context)); err != nil {
116-
return nil, err
117-
}
118-
119-
metaData := meta.Get(context)
120-
date, err := time.Parse("2006-01-02", metaData["date"].(string))
121-
return &article{
122-
Title: metaData["title"].(string),
123-
Date: date,
124-
Content: buf.Bytes(),
125-
}, nil
126-
}

pkg/routes/blog/blog.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ templ blog(articles []article) {
77
for _, article := range articles {
88
<li>
99
<span class="mr-2">{ article.Date.Format("2006-01-02") }</span>
10-
<a href={ templ.SafeURL("/blog/" + article.Title) }>{ article.Title }</a>
10+
<a href={ templ.SafeURL("/blog/" + article.Uri) }>{ article.Title }</a>
1111
</li>
1212
}
1313
</ul>

0 commit comments

Comments
 (0)