Load HTML templates and SCSS styling

This commit is contained in:
Naman Sood 2021-02-15 02:31:18 -05:00
parent 8daaa1b330
commit d347551455
10 changed files with 581 additions and 22 deletions

61
page.go
View file

@ -5,33 +5,74 @@ import (
"fmt"
"io/ioutil"
"github.com/aymerick/raymond"
"github.com/mitchellh/mapstructure"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting"
meta "github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
)
type page struct {
slug string
// Metadata stores the data about a page that needs to be visible
// at the home page.
type Metadata struct {
Title string
Summary string
Date string // TODO: better representation? time.Time might cause timezone issues...
}
func (p *page) render() ([]byte, error) {
data, err := ioutil.ReadFile("posts/" + p.slug + ".md")
// Page stores the contents of a blog post.
type Page struct {
Slug string
Metadata Metadata
Contents string
}
func newPage(slug string) (*Page, error) {
data, err := ioutil.ReadFile("posts/" + slug + ".md")
if err != nil {
return nil, fmt.Errorf("Could not read from %s.md: %s", p.slug, err)
return nil, fmt.Errorf("could not read file: %s", err)
}
md := goldmark.New(
goldmark.WithExtensions(extension.Linkify),
goldmark.WithExtensions(
extension.Linkify,
extension.Strikethrough,
extension.Typographer,
meta.Meta,
highlighting.Highlighting,
),
goldmark.WithRendererOptions(
html.WithUnsafe(),
),
)
var converted bytes.Buffer
err = md.Convert(data, &converted)
ctx := parser.NewContext()
err = md.Convert(data, &converted, parser.WithContext(ctx))
if err != nil {
return nil, fmt.Errorf("Could not parse markdown from %s.md: %s", p.slug, err)
return nil, fmt.Errorf("could not parse markdown: %s", err)
}
mdMap, err := meta.TryGet(ctx)
if err != nil {
return nil, fmt.Errorf("could not parse metadata: %s", err)
}
var metadata Metadata
err = mapstructure.Decode(mdMap, &metadata)
if err != nil {
return nil, fmt.Errorf("could not destructure metadata: %s", err)
}
return converted.Bytes(), nil
page := &Page{
Slug: slug,
Metadata: metadata,
Contents: converted.String(),
}
return page, nil
}
func (p *Page) render(tpl *raymond.Template) (string, error) {
return tpl.Exec(p)
}