importer + markdown

This commit is contained in:
Niko Abeler 2023-07-13 20:39:24 +02:00
parent 9c30ff7877
commit 7c20b472f6
9 changed files with 118 additions and 17 deletions

View File

@ -17,6 +17,10 @@ func (s *BinaryService) Create(name string, file []byte) (*model.BinaryFile, err
return s.repo.Create(name, file, nil) return s.repo.Create(name, file, nil)
} }
func (s *BinaryService) CreateEntryFile(name string, file []byte, entry model.Entry) (*model.BinaryFile, error) {
return s.repo.Create(name, file, entry)
}
func (s *BinaryService) FindById(id string) (*model.BinaryFile, error) { func (s *BinaryService) FindById(id string) (*model.BinaryFile, error) {
return s.repo.FindById(id) return s.repo.FindById(id)
} }

View File

@ -2,9 +2,11 @@ package main
import ( import (
"fmt" "fmt"
"os"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/importer" "owl-blogs/importer"
"owl-blogs/infra" "owl-blogs/infra"
"path"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -32,30 +34,81 @@ var importCmd = &cobra.Command{
} }
for _, post := range posts { for _, post := range posts {
existing, _ := app.EntryService.FindById(post.Id)
if existing != nil {
continue
}
fmt.Println(post.Meta.Type) fmt.Println(post.Meta.Type)
// import assets
mediaDir := path.Join(userPath, post.MediaDir())
println(mediaDir)
files := importer.ListDir(mediaDir)
for _, file := range files {
// mock entry to pass to binary service
entry := &model.Article{}
entry.SetID(post.Id)
fileData, err := os.ReadFile(path.Join(mediaDir, file))
if err != nil {
panic(err)
}
app.BinaryService.CreateEntryFile(file, fileData, entry)
}
switch post.Meta.Type { switch post.Meta.Type {
case "article": case "article":
article := model.Article{} article := model.Article{}
article.SetID(post.Id) article.SetID(post.Id)
article.SetMetaData(model.ArticleMetaData{ article.SetPublishedAt(&post.Meta.Date)
article.SetMetaData(&model.ArticleMetaData{
Title: post.Meta.Title, Title: post.Meta.Title,
Content: post.Content, Content: post.Content,
}) })
article.SetPublishedAt(&post.Meta.Date)
app.EntryService.Create(&article) app.EntryService.Create(&article)
case "bookmark": case "bookmark":
case "reply": case "reply":
case "photo": case "photo":
photo := model.Image{}
photo.SetID(post.Id)
photo.SetPublishedAt(&post.Meta.Date)
photo.SetMetaData(&model.ImageMetaData{
Title: post.Meta.Title,
Content: post.Content,
ImageId: post.Meta.PhotoPath,
})
app.EntryService.Create(&photo)
case "note": case "note":
note := model.Note{}
note.SetID(post.Id)
note.SetPublishedAt(&post.Meta.Date)
note.SetMetaData(&model.NoteMetaData{
Content: post.Content,
})
app.EntryService.Create(&note)
case "recipe": case "recipe":
recipe := model.Recipe{}
recipe.SetID(post.Id)
recipe.SetPublishedAt(&post.Meta.Date)
recipe.SetMetaData(&model.RecipeMetaData{
Title: post.Meta.Title,
Yield: post.Meta.Recipe.Yield,
Duration: post.Meta.Recipe.Duration,
Ingredients: post.Meta.Recipe.Ingredients,
Content: post.Content,
})
app.EntryService.Create(&recipe)
case "page": case "page":
page := model.Page{}
page.SetID(post.Id)
page.SetPublishedAt(&post.Meta.Date)
page.SetMetaData(&model.PageMetaData{
Title: post.Meta.Title,
Content: post.Content,
})
app.EntryService.Create(&page)
default: default:
panic("Unknown type") panic("Unknown type")
} }

View File

@ -12,11 +12,12 @@ type Image struct {
type ImageMetaData struct { type ImageMetaData struct {
ImageId string `owl:"inputType=file"` ImageId string `owl:"inputType=file"`
Title string `owl:"inputType=text"`
Content string `owl:"inputType=text widget=textarea"` Content string `owl:"inputType=text widget=textarea"`
} }
func (e *Image) Title() string { func (e *Image) Title() string {
return "" return e.meta.Title
} }
func (e *Image) Content() EntryContent { func (e *Image) Content() EntryContent {

1
go.mod
View File

@ -27,6 +27,7 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.47.0 // indirect github.com/valyala/fasthttp v1.47.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect
github.com/yuin/goldmark v1.5.4 // indirect
golang.org/x/crypto v0.11.0 // indirect golang.org/x/crypto v0.11.0 // indirect
golang.org/x/sys v0.10.0 // indirect golang.org/x/sys v0.10.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect

2
go.sum
View File

@ -62,6 +62,8 @@ github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVS
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.5.4 h1:2uY/xC0roWy8IBEGLgB1ywIoEJFGmRrX21YQcvGZzjU=
github.com/yuin/goldmark v1.5.4/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=

View File

@ -43,6 +43,10 @@ type Post struct {
Content string Content string
} }
func (post *Post) MediaDir() string {
return path.Join("public", post.Id, "media")
}
func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error { func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error {
type T struct { type T struct {
Type string `yaml:"type"` Type string `yaml:"type"`
@ -155,7 +159,7 @@ func LoadMeta(data []byte) (PostMeta, error) {
} }
func AllUserPosts(userPath string) ([]Post, error) { func AllUserPosts(userPath string) ([]Post, error) {
postFiles := listDir(path.Join(userPath, "public")) postFiles := ListDir(path.Join(userPath, "public"))
posts := make([]Post, 0) posts := make([]Post, 0)
for _, id := range postFiles { for _, id := range postFiles {
// if is a directory and has index.md, add to posts // if is a directory and has index.md, add to posts
@ -182,7 +186,7 @@ func AllUserPosts(userPath string) ([]Post, error) {
return posts, nil return posts, nil
} }
func listDir(path string) []string { func ListDir(path string) []string {
dir, _ := os.Open(path) dir, _ := os.Open(path)
defer dir.Close() defer dir.Close()
files, _ := dir.Readdirnames(-1) files, _ := dir.Readdirnames(-1)

View File

@ -5,19 +5,33 @@ import (
"embed" "embed"
"io" "io"
"text/template" "text/template"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
) )
//go:embed templates //go:embed templates
var templates embed.FS var templates embed.FS
var funcMap = template.FuncMap{
"markdown": func(text string) string {
html, err := RenderMarkdown(text)
if err != nil {
return ">>>could not render markdown<<<"
}
return html
},
}
func CreateTemplateWithBase(templateName string) (*template.Template, error) { func CreateTemplateWithBase(templateName string) (*template.Template, error) {
return template.ParseFS( return template.New(templateName).Funcs(funcMap).ParseFS(
templates, templates,
"templates/base.tmpl", "templates/base.tmpl",
"templates/"+templateName+".tmpl", "templates/"+templateName+".tmpl",
) )
} }
func RenderTemplateWithBase(w io.Writer, templateName string, data interface{}) error { func RenderTemplateWithBase(w io.Writer, templateName string, data interface{}) error {
@ -35,11 +49,9 @@ func RenderTemplateWithBase(w io.Writer, templateName string, data interface{})
} }
func RenderTemplateToString(templateName string, data interface{}) (string, error) { func RenderTemplateToString(templateName string, data interface{}) (string, error) {
tmplStr, _ := templates.ReadFile("templates/" + templateName + ".tmpl")
t, err := template.ParseFS( t, err := template.New("templates/" + templateName + ".tmpl").Funcs(funcMap).Parse(string(tmplStr))
templates,
"templates/"+templateName+".tmpl",
)
if err != nil { if err != nil {
return "", err return "", err
@ -50,3 +62,21 @@ func RenderTemplateToString(templateName string, data interface{}) (string, erro
err = t.Execute(&output, data) err = t.Execute(&output, data)
return output.String(), err return output.String(), err
} }
func RenderMarkdown(mdText string) (string, error) {
markdown := goldmark.New(
goldmark.WithRendererOptions(
html.WithUnsafe(),
),
goldmark.WithExtensions(
// meta.Meta,
extension.GFM,
),
)
var buf bytes.Buffer
context := parser.NewContext()
err := markdown.Convert([]byte(mdText), &buf, parser.WithContext(context))
return buf.String(), err
}

View File

@ -1 +1 @@
{{.MetaData.Content}} {{.MetaData.Content | markdown }}

View File

@ -3,6 +3,7 @@ package web
import ( import (
"owl-blogs/app" "owl-blogs/app"
"owl-blogs/render" "owl-blogs/render"
"sort"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -19,6 +20,11 @@ func (h *IndexHandler) Handle(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
entries, err := h.entrySvc.FindAll() entries, err := h.entrySvc.FindAll()
// sort entries by date descending
sort.Slice(entries, func(i, j int) bool {
return entries[i].PublishedAt().After(*entries[j].PublishedAt())
})
if err != nil { if err != nil {
return err return err
} }