Skip to content

Commit

Permalink
feat: tags support
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkobrombin committed Feb 14, 2024
1 parent 96251ee commit 4dde427
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 37 deletions.
21 changes: 20 additions & 1 deletion core/handler_articles.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ func HandleArticles(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, fmt.Sprintf("/%s/articles/en", repoId), http.StatusFound)
}

articles := repo.ArticlesGrouped[lang]
tags := getTags(articles)
response := structs.ArticlesResponse{
Title: repo.Id,
SupportedLang: repo.Languages,
Articles: repo.ArticlesGrouped[lang],
Tags: tags,
Articles: articles,
}

w.Header().Set("Content-Type", "application/json")
Expand All @@ -59,3 +62,19 @@ func HandleArticles(w http.ResponseWriter, r *http.Request) {

w.Write(jsonData)
}

func getTags(articles []structs.Article) []string {
tags := make(map[string]bool)
for _, article := range articles {
for _, tag := range article.Tags {
tags[tag] = true
}
}

var tagsList []string
for tag := range tags {
tagsList = append(tagsList, tag)
}

return tagsList
}
17 changes: 11 additions & 6 deletions core/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,14 @@ func loadArticle(repo structs.Repo, path string) (structs.Article, error) {
return structs.Article{}, fmt.Errorf("invalid article format: %s", path)
}

header := parts[0]
rawHeader := parts[0]
body := parts[1]

title, description, publicationDate, authors := parseArticleHeader(header)
header, err := parseArticleHeader(rawHeader)
if err != nil {
return structs.Article{}, err
}

slug := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))

var lang string
Expand All @@ -328,10 +332,11 @@ func loadArticle(repo structs.Repo, path string) (structs.Article, error) {
}

article := structs.Article{
Title: title,
Description: description,
PublicationDate: publicationDate,
Authors: authors,
Title: header.Title,
Description: header.Description,
PublicationDate: header.PublicationDate,
Authors: header.Authors,
Tags: header.Tags,
Body: body,
Path: path,
Url: strings.TrimSuffix(path, filepath.Ext(path)),
Expand Down
40 changes: 10 additions & 30 deletions core/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,18 @@ package core
*/

import (
"strings"
"github.com/vanilla-os/Chronos/structs"
"gopkg.in/yaml.v3"
)

// parseArticleHeader parses the header of an article and extracts the details
// like title, description, publication date and authors.
func parseArticleHeader(header string) (string, string, string, []string) {
title := ""
description := ""
publicationDate := ""
authors := make([]string, 0)

lines := strings.Split(header, "\n")
for _, line := range lines {
parts := strings.SplitN(line, ":", 2)
if len(parts) != 2 {
continue
}

key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])

switch key {
case "Title":
title = value
case "Description":
description = value
case "PublicationDate":
publicationDate = value
case "Authors":
authors = strings.Split(value, ",")
}
// parseArticleHeader parses the YAML header of an article and returns a
// structs.ArticleHeader object.
func parseArticleHeader(header string) (structs.ArticleHeader, error) {
var articleHeader structs.ArticleHeader
err := yaml.Unmarshal([]byte(header), &articleHeader)
if err != nil {
return structs.ArticleHeader{}, err
}

return title, description, publicationDate, authors
return articleHeader, nil
}
9 changes: 9 additions & 0 deletions structs/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Article struct {
Description string
PublicationDate string
Authors []string
Tags []string
Body string
Language string
Path string
Expand All @@ -28,3 +29,11 @@ func (a *Article) ParseBody() {
parsedBody := blackfriday.Run([]byte(a.Body))
a.Body = string(parsedBody)
}

type ArticleHeader struct {
Title string `yaml:"Title"`
Description string `yaml:"Description"`
PublicationDate string `yaml:"PublicationDate"`
Authors []string `yaml:"Authors"`
Tags []string `yaml:"Tags"`
}
1 change: 1 addition & 0 deletions structs/articleResponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ package structs
type ArticlesResponse struct {
Title string `json:"title"`
SupportedLang []string `json:"SupportedLang"`
Tags []string `json:"tags"`
Articles []Article `json:"articles"`
}

0 comments on commit 4dde427

Please sign in to comment.