owl-blogs/post.go

64 lines
1.1 KiB
Go
Raw Normal View History

2022-07-21 17:02:37 +00:00
package kiss
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"
meta "github.com/yuin/goldmark-meta"
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-07-21 17:02:37 +00:00
)
type Post struct {
2022-07-21 17:44:07 +00:00
user User
id string
title string
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-01 19:06:48 +00:00
return post.user.Path() + "/posts/" + post.id
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, map[string]interface{}) {
data := post.Content()
markdown := goldmark.New(
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)
return buf, metaData
}