Code for articles fs moved out to a separate articles.go file.
Implemented an init func that preload articles. Implemented GetArticlesMetadata() that returns metadata for articles used on the Stuff page.
This commit is contained in:
parent
db41e25c84
commit
f18cc3c44e
83
web/articles.go
Normal file
83
web/articles.go
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
package web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"embed"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.arav.su/Arav/dwelling-home/pkg/article"
|
||||||
|
"github.com/gomarkdown/markdown"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
articleFileExtension = ".md"
|
||||||
|
articleMetaExtension = ".meta"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed articles
|
||||||
|
var articlesFS embed.FS
|
||||||
|
var articles map[string]article.Article = make(map[string]article.Article)
|
||||||
|
|
||||||
|
// ArticleMetadata holds date, title and URL of an article used in articles list
|
||||||
|
// on the Stuff page.
|
||||||
|
type ArticleMetadata struct {
|
||||||
|
ID int64
|
||||||
|
Date time.Time
|
||||||
|
Title string
|
||||||
|
URL string
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetArticlesMetadata returns a slice of metadata that is sorted by date
|
||||||
|
func GetArticlesMetadata() (meta []ArticleMetadata) {
|
||||||
|
for urlid, article := range articles {
|
||||||
|
|
||||||
|
meta = append(meta,
|
||||||
|
ArticleMetadata{
|
||||||
|
ID: article.ID,
|
||||||
|
Date: article.Date,
|
||||||
|
Title: article.Title,
|
||||||
|
URL: "stuff/article/" + urlid})
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(meta, func(i, j int) bool {
|
||||||
|
return meta[i].ID > meta[j].ID
|
||||||
|
})
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetArticle(name string) (*article.Article, error) {
|
||||||
|
if artcl, ok := articles[name]; ok {
|
||||||
|
return &artcl, nil
|
||||||
|
} else {
|
||||||
|
meta, err := articlesFS.ReadFile("articles/" + name + articleMetaExtension)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
md, err := articlesFS.ReadFile("articles/" + name + articleFileExtension)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
artcl := article.Article{
|
||||||
|
Body: markdown.ToHTML(md, nil, nil)}
|
||||||
|
|
||||||
|
artcl.ParseMetadata(meta)
|
||||||
|
|
||||||
|
articles[name] = artcl
|
||||||
|
|
||||||
|
return &artcl, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
entries, _ := articlesFS.ReadDir("articles")
|
||||||
|
|
||||||
|
for _, entry := range entries {
|
||||||
|
if strings.HasSuffix(entry.Name(), articleFileExtension) {
|
||||||
|
GetArticle(strings.TrimSuffix(entry.Name(), articleFileExtension))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
32
web/web.go
32
web/web.go
@ -4,9 +4,6 @@ import (
|
|||||||
"embed"
|
"embed"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.arav.su/Arav/dwelling-home/pkg/article"
|
|
||||||
"github.com/gomarkdown/markdown"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// To install a Jade compiler: go install github.com/Joker/jade/cmd/jade@latest
|
// To install a Jade compiler: go install github.com/Joker/jade/cmd/jade@latest
|
||||||
@ -30,32 +27,3 @@ func Assets() http.FileSystem {
|
|||||||
func AssetsGetFile(path string) ([]byte, error) {
|
func AssetsGetFile(path string) ([]byte, error) {
|
||||||
return assetsDir.ReadFile("assets/" + path)
|
return assetsDir.ReadFile("assets/" + path)
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:embed articles
|
|
||||||
var articlesFS embed.FS
|
|
||||||
var articles map[string]article.Article = make(map[string]article.Article)
|
|
||||||
|
|
||||||
func GetArticle(name string) (*article.Article, error) {
|
|
||||||
if artcl, ok := articles[name]; ok {
|
|
||||||
return &artcl, nil
|
|
||||||
} else {
|
|
||||||
meta, err := articlesFS.ReadFile("articles/" + name + ".meta")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
md, err := articlesFS.ReadFile("articles/" + name + ".md")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
artcl := article.Article{
|
|
||||||
Body: markdown.ToHTML(md, nil, nil)}
|
|
||||||
|
|
||||||
artcl.ParseMetadata(meta)
|
|
||||||
|
|
||||||
articles[name] = artcl
|
|
||||||
|
|
||||||
return &artcl, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user