owl-blogs/post.go

113 lines
2.2 KiB
Go
Raw Normal View History

2022-08-03 14:55:48 +00:00
package owl
2022-07-21 17:02:37 +00:00
import (
2022-07-21 17:44:07 +00:00
"bytes"
2022-07-21 17:02:37 +00:00
"io/ioutil"
"path"
2022-07-21 17:44:07 +00:00
"github.com/yuin/goldmark"
2022-07-27 19:26:37 +00:00
"github.com/yuin/goldmark/extension"
2022-07-21 17:44:07 +00:00
"github.com/yuin/goldmark/parser"
2022-08-21 09:31:48 +00:00
"github.com/yuin/goldmark/renderer/html"
"gopkg.in/yaml.v2"
2022-07-21 17:02:37 +00:00
)
type Post struct {
2022-08-05 20:04:03 +00:00
user *User
2022-07-21 17:44:07 +00:00
id string
title string
2022-07-21 17:02:37 +00:00
}
type PostMeta struct {
Title string `yaml:"title"`
Aliases []string `yaml:"aliases"`
Date string `yaml:"date"`
2022-08-20 20:35:51 +00:00
Draft bool `yaml:"draft"`
}
2022-08-03 17:41:13 +00:00
func (post Post) Id() string {
return post.id
}
2022-07-21 17:02:37 +00:00
func (post Post) Dir() string {
return path.Join(post.user.Dir(), "public", post.id)
}
2022-08-01 17:50:29 +00:00
func (post Post) MediaDir() string {
return path.Join(post.Dir(), "media")
}
2022-07-24 13:34:52 +00:00
func (post Post) UrlPath() string {
2022-08-03 17:41:13 +00:00
return post.user.UrlPath() + "posts/" + post.id + "/"
}
2022-08-13 16:47:27 +00:00
func (post Post) FullUrl() string {
return post.user.FullUrl() + "posts/" + post.id + "/"
}
2022-08-03 17:41:13 +00:00
func (post Post) UrlMediaPath(filename string) string {
return post.UrlPath() + "media/" + filename
2022-07-23 15:19:47 +00:00
}
2022-07-21 17:44:07 +00:00
func (post Post) Title() string {
return post.title
}
2022-07-21 17:02:37 +00:00
func (post Post) ContentFile() string {
return path.Join(post.Dir(), "index.md")
}
func (post Post) Content() []byte {
// read file
data, _ := ioutil.ReadFile(post.ContentFile())
return data
}
2022-07-21 17:44:07 +00:00
func (post Post) MarkdownData() (bytes.Buffer, PostMeta) {
2022-07-21 17:44:07 +00:00
data := post.Content()
// get yaml metadata block
meta := PostMeta{}
trimmedData := bytes.TrimSpace(data)
// check first line is ---
if string(trimmedData[0:4]) == "---\n" {
trimmedData = trimmedData[4:]
// find --- end
end := bytes.Index(trimmedData, []byte("\n---\n"))
if end != -1 {
metaData := trimmedData[:end]
yaml.Unmarshal(metaData, &meta)
data = trimmedData[end+5:]
}
}
2022-08-21 09:31:48 +00:00
options := goldmark.WithRendererOptions()
if post.user.repo.AllowRawHtml() {
options = goldmark.WithRendererOptions(
html.WithUnsafe(),
)
}
2022-07-21 17:44:07 +00:00
markdown := goldmark.New(
2022-08-21 09:31:48 +00:00
options,
2022-07-21 17:44:07 +00:00
goldmark.WithExtensions(
// meta.Meta,
2022-07-27 19:26:37 +00:00
extension.GFM,
2022-07-21 17:44:07 +00:00
),
)
var buf bytes.Buffer
context := parser.NewContext()
if err := markdown.Convert(data, &buf, parser.WithContext(context)); err != nil {
panic(err)
}
// metaData := meta.Get(context)
2022-07-21 17:44:07 +00:00
return buf, meta
2022-07-21 17:44:07 +00:00
}
2022-08-06 17:38:13 +00:00
func (post Post) Aliases() []string {
_, metaData := post.MarkdownData()
return metaData.Aliases
2022-08-06 17:38:13 +00:00
}