Initial commit - read and serve markdown and static files

This commit is contained in:
Naman Sood 2021-02-06 16:27:00 -05:00
commit 8daaa1b330
8 changed files with 180 additions and 0 deletions

37
page.go Normal file
View file

@ -0,0 +1,37 @@
package main
import (
"bytes"
"fmt"
"io/ioutil"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer/html"
)
type page struct {
slug string
}
func (p *page) render() ([]byte, error) {
data, err := ioutil.ReadFile("posts/" + p.slug + ".md")
if err != nil {
return nil, fmt.Errorf("Could not read from %s.md: %s", p.slug, err)
}
md := goldmark.New(
goldmark.WithExtensions(extension.Linkify),
goldmark.WithRendererOptions(
html.WithUnsafe(),
),
)
var converted bytes.Buffer
err = md.Convert(data, &converted)
if err != nil {
return nil, fmt.Errorf("Could not parse markdown from %s.md: %s", p.slug, err)
}
return converted.Bytes(), nil
}