owl-blogs/post.go

89 lines
1.7 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"
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-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
}
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, 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
}
2022-08-06 17:38:13 +00:00
func (post Post) Aliases() []string {
_, metaData := post.MarkdownData()
if metaData["aliases"] != nil {
alias_data := metaData["aliases"].([]interface{})
aliases := make([]string, 0)
for _, alias := range alias_data {
aliases = append(aliases, alias.(string))
}
return aliases
}
return []string{}
}