Include Date in RSS Feed #3 + Parse metadata as struct
This commit is contained in:
parent
e93808ac88
commit
bed274cbf6
42
post.go
42
post.go
|
@ -6,9 +6,9 @@ import (
|
|||
"path"
|
||||
|
||||
"github.com/yuin/goldmark"
|
||||
meta "github.com/yuin/goldmark-meta"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
"github.com/yuin/goldmark/parser"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Post struct {
|
||||
|
@ -17,6 +17,12 @@ type Post struct {
|
|||
title string
|
||||
}
|
||||
|
||||
type PostMeta struct {
|
||||
Title string `yaml:"title"`
|
||||
Aliases []string `yaml:"aliases"`
|
||||
Date string `yaml:"date"`
|
||||
}
|
||||
|
||||
func (post Post) Id() string {
|
||||
return post.id
|
||||
}
|
||||
|
@ -55,11 +61,27 @@ func (post Post) Content() []byte {
|
|||
return data
|
||||
}
|
||||
|
||||
func (post Post) MarkdownData() (bytes.Buffer, map[string]interface{}) {
|
||||
func (post Post) MarkdownData() (bytes.Buffer, PostMeta) {
|
||||
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:]
|
||||
}
|
||||
}
|
||||
|
||||
markdown := goldmark.New(
|
||||
goldmark.WithExtensions(
|
||||
meta.Meta,
|
||||
// meta.Meta,
|
||||
extension.GFM,
|
||||
),
|
||||
)
|
||||
|
@ -68,21 +90,13 @@ func (post Post) MarkdownData() (bytes.Buffer, map[string]interface{}) {
|
|||
if err := markdown.Convert(data, &buf, parser.WithContext(context)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
metaData := meta.Get(context)
|
||||
// metaData := meta.Get(context)
|
||||
|
||||
return buf, metaData
|
||||
return buf, meta
|
||||
|
||||
}
|
||||
|
||||
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{}
|
||||
return metaData.Aliases
|
||||
}
|
||||
|
|
6
rss.go
6
rss.go
|
@ -43,15 +43,11 @@ func RenderRSSFeed(user User) (string, error) {
|
|||
for _, postId := range posts {
|
||||
post, _ := user.GetPost(postId)
|
||||
_, meta := post.MarkdownData()
|
||||
date, ok := meta["date"]
|
||||
if !ok {
|
||||
date = ""
|
||||
}
|
||||
rss.Channel.Items = append(rss.Channel.Items, RSSItem{
|
||||
Guid: postId,
|
||||
Title: post.Title(),
|
||||
Link: post.FullUrl(),
|
||||
PubDate: date.(string),
|
||||
PubDate: meta.Date,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
3
user.go
3
user.go
|
@ -73,7 +73,7 @@ func (user User) GetPost(id string) (Post, error) {
|
|||
|
||||
post := Post{user: &user, id: id}
|
||||
_, metaData := post.MarkdownData()
|
||||
title := metaData["title"]
|
||||
title := metaData.Title
|
||||
post.title = fmt.Sprint(title)
|
||||
|
||||
return post, nil
|
||||
|
@ -100,6 +100,7 @@ func (user User) CreateNewPost(title string) (Post, error) {
|
|||
initial_content := ""
|
||||
initial_content += "---\n"
|
||||
initial_content += "title: " + title + "\n"
|
||||
initial_content += "date: " + time.Now().UTC().Format(time.RFC3339) + "\n"
|
||||
initial_content += "---\n"
|
||||
initial_content += "\n"
|
||||
initial_content += "Write your post here.\n"
|
||||
|
|
12
user_test.go
12
user_test.go
|
@ -35,6 +35,18 @@ func TestCreateNewPostCreatesMediaDir(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCreateNewPostAddsDateToMetaBlock(t *testing.T) {
|
||||
user := getTestUser()
|
||||
// Create a new post
|
||||
user.CreateNewPost("testpost")
|
||||
posts, _ := user.Posts()
|
||||
post, _ := user.GetPost(posts[0])
|
||||
_, meta := post.MarkdownData()
|
||||
if meta.Date == "" {
|
||||
t.Error("Found no date. Got: " + meta.Date)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateNewPostMultipleCalls(t *testing.T) {
|
||||
// Create a new user
|
||||
repo, _ := owl.CreateRepository(testRepoName())
|
||||
|
|
Loading…
Reference in New Issue