diff options
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/config.go | 20 | ||||
| -rw-r--r-- | backend/parser.go | 22 | ||||
| -rw-r--r-- | backend/templates/components.html | 2 | ||||
| -rw-r--r-- | backend/templates/data.html | 2 |
4 files changed, 37 insertions, 9 deletions
diff --git a/backend/config.go b/backend/config.go index 44b2033..e009b8a 100644 --- a/backend/config.go +++ b/backend/config.go @@ -5,6 +5,7 @@ import ( "log/slog" "os" + "git.anhgelus.world/anhgelus/small-web/markdown" "github.com/pelletier/go-toml/v2" ) @@ -22,6 +23,11 @@ type Logo struct { Favicon string `toml:"favicon"` } +type Replacer struct { + Symbol string `toml:"symbol"` + Replace string `tomle:"replace"` +} + type Config struct { Domain string `toml:"domain"` Name string `toml:"name"` @@ -37,6 +43,8 @@ type Config struct { Links []Link `toml:"links"` Logo Logo `toml:"logo"` + + Replacers []Replacer `toml:"replacers"` } func (c *Config) DefaultValues() { @@ -66,8 +74,11 @@ func (c *Config) DefaultValues() { c.RootFolder = "data" c.PublicFolder = "public" c.Quotes = []string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do."} + c.Replacers = []Replacer{{"~", " "}} } +var defaultMarkdownOption markdown.Option + func LoadConfig(path string) (*Config, bool) { b, err := os.ReadFile(path) var config Config @@ -97,5 +108,14 @@ func LoadConfig(path string) (*Config, bool) { slog.Error("unmarshalling config file", "error", err) return nil, false } + defaultMarkdownOption.ImageSource = getStatic + defaultMarkdownOption.Replaces = make(map[rune]string, len(config.Replacers)) + for _, r := range config.Replacers { + if len(r.Symbol) != 1 { + slog.Error("invalid symbol in config", "symbol", r.Symbol) + return nil, false + } + defaultMarkdownOption.Replaces[[]rune(r.Symbol)[0]] = r.Replace + } return &config, true } diff --git a/backend/parser.go b/backend/parser.go index 3625671..8dba673 100644 --- a/backend/parser.go +++ b/backend/parser.go @@ -3,6 +3,7 @@ package backend import ( "errors" "fmt" + "html" "html/template" "log/slog" "strings" @@ -14,7 +15,7 @@ import ( type EntryInfo struct { Title string `toml:"title"` - Description string `toml:"description"` + Description template.HTML `toml:"description"` Img image `toml:"image"` PubLocalDate toml.LocalDate `toml:"publication_date"` } @@ -38,30 +39,37 @@ func renderLink(content, href, url string) template.HTML { } func parse(b []byte, info *EntryInfo, d *data) (template.HTML, bool) { + opt := defaultMarkdownOption + opt.RenderLink = renderLinkFunc(d.URL) + var dd string + var err error splits := strings.SplitN(string(b), "---", 2) if len(splits) == 2 && info != nil { - err := toml.Unmarshal([]byte(splits[0]), info) + err = toml.Unmarshal([]byte(splits[0]), info) if err != nil { slog.Warn("parsing entry info", "error", err) } else { + info.Description, err = markdown.Parse(string(info.Description), &opt) dd = splits[1] } } else { dd = string(b) } - opt := new(markdown.Option) - opt.ImageSource = getStatic - opt.RenderLink = renderLinkFunc(d.URL) - content, err := markdown.Parse(dd, opt) + var errMd *markdown.ParseError errors.As(err, &errMd) + var content template.HTML + if errMd == nil { + content, err = markdown.Parse(dd, &opt) + errors.As(err, &errMd) + } if errMd != nil { slog.Error("parsing markdown") fmt.Println(errMd.Pretty()) return "", false } - d.PageDescription = info.Description + d.PageDescription = html.UnescapeString(string(info.Description)) d.title = info.Title d.Image = info.Img.Src return content, true diff --git a/backend/templates/components.html b/backend/templates/components.html index b4ffbe6..d9a653f 100644 --- a/backend/templates/components.html +++ b/backend/templates/components.html @@ -7,7 +7,7 @@ <a href="/{{ $uri }}/{{ .Slug }}"><img src="{{ static .Img.Src }}" alt="{{ .Img.Alt }}" /></a> <figcaption>{{ .Img.Legend }}</figcaption> </figure> - <p>{{ .Description }}</p> + {{ .Description }} </article> {{ end }} <div class="pagination"> diff --git a/backend/templates/data.html b/backend/templates/data.html index a384459..6fe15c4 100644 --- a/backend/templates/data.html +++ b/backend/templates/data.html @@ -1,7 +1,7 @@ {{define "body"}} <article id="content"> <h1>{{ .DataTitle }}</h1> - <p>{{ .Description }}</p> + {{ .Description }} <figure> <img src="{{ static .Img.Src }}" alt="{{ .Img.Alt }}" class="large" /> <figcaption>{{ .Img.Legend }}</figcaption> |
