Skip to content

Commit

Permalink
feat: add articles date
Browse files Browse the repository at this point in the history
  • Loading branch information
drawbu committed Jun 12, 2024
1 parent 377a108 commit d7728f9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
1 change: 1 addition & 0 deletions pkg/routes/blog/article.templ
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package blog
templ articleShow(article article) {
<div class="flex flex-col gap-4">
<h1>{ article.Title }</h1>
<span class="text-gray-500">Written the { article.Date.Format("2 January 2006") } by drawbu</span>
<a href="/blog" class="text-blue-500">Back to blog</a>
<div id="article" class="flex flex-col gap-4">
@templ.Raw(string(article.Content))
Expand Down
39 changes: 24 additions & 15 deletions pkg/routes/blog/blog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,61 @@ import (
"net/http"
"net/url"
"os"
"sort"
"strings"
"time"

"app/pkg/app"

chroma "github.com/alecthomas/chroma/v2/formatters/html"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting/v2"
"github.com/yuin/goldmark-meta"
meta "github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/parser"
)

func Handler(articlesDir string) *handler {
articles := make(map[string]article)
for _, a := range getArticles(articlesDir) {
articles[a.Title] = a
}
return &handler{
articles: getArticles(articlesDir),
articles: articles,
}
}

type handler struct {
articles []article
articles map[string]article
}

type article struct {
Title string
Date string
Date time.Time
Content []byte
}

func (h *handler) Render(serv *app.Server, w http.ResponseWriter, r *http.Request) error {
article_name, err := url.PathUnescape(r.PathValue("article"))
if err != nil || article_name == "" {
return serv.Template(blog(h.articles)).Render(context.Background(), w)
return serv.Template(blog(getSortedArticles(h.articles))).Render(context.Background(), w)
}

a, err := findArticle(h.articles, article_name)
if err != nil {
return err
a, ok := h.articles[article_name]
if !ok {
return errors.New("Article not found")
}
return serv.Template(articleShow(*a)).Render(context.Background(), w)
return serv.Template(articleShow(a)).Render(context.Background(), w)
}

func findArticle(articles []article, title string) (*article, error) {
func getSortedArticles(articles map[string]article) []article {
result := make([]article, 0, len(articles))
for _, a := range articles {
if a.Title == title {
return &a, nil
}
result = append(result, a)
}
return nil, errors.New("Article not found")
sort.SliceStable(result, func(i, j int) bool {
return result[i].Date.After(result[j].Date)
})
return result
}

func getArticles(path string) []article {
Expand Down Expand Up @@ -104,9 +112,10 @@ func parseMarkdownArticle(path string) (*article, error) {
}

metaData := meta.Get(context)
date, err := time.Parse("2006-01-02", metaData["date"].(string))
return &article{
Title: metaData["title"].(string),
Date: metaData["date"].(string),
Date: date,
Content: buf.Bytes(),
}, nil
}
1 change: 1 addition & 0 deletions pkg/routes/blog/blog.templ
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ templ blog(articles []article) {
<ul>
for _, article := range articles {
<li>
<span>{ article.Date.Format("2006-01-02") }</span>
<a href={ "/blog/" + templ.URL(article.Title) }>{ article.Title }</a>
</li>
}
Expand Down

0 comments on commit d7728f9

Please sign in to comment.