1
0

Part of GetArticle() for loading an article from a file was moved out to init() func.

This commit is contained in:
Alexander Andreev 2023-09-24 20:00:17 +04:00
parent 72b09218f8
commit 48e7a86312
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -2,6 +2,7 @@ package web
import (
"embed"
"log"
"sort"
"strings"
"time"
@ -47,24 +48,11 @@ func GetArticleMetadata() (meta []ArticleMetadata) {
return
}
func GetArticle(name string) (*article.Article, error) {
func GetArticle(name string) *article.Article {
if artcl, ok := articles[name]; ok {
return artcl, nil
return artcl
}
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
}
articles[name], err = article.New(markdown.ToHTML(md, nil, nil), meta)
return articles[name], err
return nil
}
func init() {
@ -72,7 +60,22 @@ func init() {
for _, entry := range entries {
if strings.HasSuffix(entry.Name(), articleFileExtension) {
GetArticle(strings.TrimSuffix(entry.Name(), articleFileExtension))
name := strings.TrimSuffix(entry.Name(), articleFileExtension)
meta, err := articlesFS.ReadFile("articles/" + entry.Name())
if err != nil {
log.Fatalln("an article \"", name, "\" cannot be read:", err)
}
md, err := articlesFS.ReadFile("articles/" + name + articleMetaExtension)
if err != nil {
log.Fatalln("a metadata for an article \"", name, "\" cannot be read:", err)
}
articles[name], err = article.New(markdown.ToHTML(md, nil, nil), meta)
if err != nil {
log.Fatalln("an article \"", name, "\" cannot be parsed:", err)
}
}
}
}