From b0c7b6a6902fb43c140f4a25651f83be6a149373 Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Wed, 7 Jun 2023 02:17:29 +0300 Subject: [PATCH] Working markdown in blog --- apps/blogviewer/blogfile.go | 7 +++++++ apps/blogviewer/blogviewer.go | 18 ++++++++++-------- go.mod | 1 + go.sum | 2 ++ libs/markdown.go | 13 +++++++++++-- routes/private.go | 27 ++++++++++++++++----------- 6 files changed, 47 insertions(+), 21 deletions(-) diff --git a/apps/blogviewer/blogfile.go b/apps/blogviewer/blogfile.go index 941b827..e6cb09e 100644 --- a/apps/blogviewer/blogfile.go +++ b/apps/blogviewer/blogfile.go @@ -1,5 +1,7 @@ package blogviewer +import "html/template" + type BlogFileData struct { Header string `bson:"header"` Blocks []*Block `bson:"blocks"` @@ -9,3 +11,8 @@ type Block struct { Type string `bson:"type"` Data []string `bson:"data"` } + +type RenderedBlock struct { + Type string `bson:"type"` + Data []template.HTML `bson:"data"` +} diff --git a/apps/blogviewer/blogviewer.go b/apps/blogviewer/blogviewer.go index 3b64243..2c7ab1f 100644 --- a/apps/blogviewer/blogviewer.go +++ b/apps/blogviewer/blogviewer.go @@ -229,25 +229,27 @@ func (b *BlogViewerApplication) Render(filePath string, appCtx appCtx.AppContext return nil, err } + newData := []RenderedBlock{} for _, block := range data.Blocks { - newData := []string{} + newBlock := RenderedBlock{} switch block.Type { case "image": - for _, image := range block.Data { - newData = append(newData, b.fs.RelativeToAbsolute(appCtx, image)) - } + // for _, image := range block.Data { + // newData = append(newData, b.fs.RelativeToAbsolute(appCtx, image)) + // } case "markdown": for _, data := range block.Data { - str := b.mLib.Render([]byte(data)) - newData = append(newData, str) + renderedMD := b.mLib.Render([]byte(data)) + newBlock.Data = append(newBlock.Data, renderedMD) } + newData = append(newData, newBlock) } - block.Data = newData + // block.Data = newData } return gin.H{ "header": data.Header, - "blocks": data.Blocks, + "blocks": newData, // "html": html, }, nil } diff --git a/go.mod b/go.mod index 604403e..9486a21 100644 --- a/go.mod +++ b/go.mod @@ -37,6 +37,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect + github.com/russross/blackfriday v1.6.0 github.com/russross/blackfriday/v2 v2.1.0 github.com/thinkerou/favicon v0.2.0 github.com/twitchyliquid64/golang-asm v0.15.1 // indirect diff --git a/go.sum b/go.sum index a3e7ef6..a8f7af2 100644 --- a/go.sum +++ b/go.sum @@ -91,6 +91,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= +github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww= +github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/libs/markdown.go b/libs/markdown.go index 2c2ca78..8054dc5 100644 --- a/libs/markdown.go +++ b/libs/markdown.go @@ -1,6 +1,8 @@ package libs import ( + "html/template" + "github.com/microcosm-cc/bluemonday" "github.com/russross/blackfriday/v2" ) @@ -8,10 +10,17 @@ import ( type MarkdownLib struct { } -func (ml *MarkdownLib) Render(s []byte) string { +func (ml *MarkdownLib) Render(s []byte) template.HTML { output := blackfriday.Run(s) _ = output html := bluemonday.UGCPolicy().SanitizeBytes(output) // println(string(html)) - return string(html) + kek := template.HTML(html) + return kek } + +// func (ml MarkdownLib) MarkDowner(args ...interface{}) template.HTML { +// s := blackfriday.MarkdownCommon([]byte(fmt.Sprintf("%s", args...))) + +// return template.HTML(s) +// } diff --git a/routes/private.go b/routes/private.go index ca08399..58eb34d 100644 --- a/routes/private.go +++ b/routes/private.go @@ -32,22 +32,27 @@ func PrivateRoutes(port string, webfs *webfilesystem.WebFileSystem, webde *wde.W router.MaxMultipartMemory = 8 << 20 // 8 MiB router.GET("/", func(ctx *gin.Context) { + // appString := AppString{ + // AppPath: "/Applications/Finder.app", + // Args: []string{"/home/user"}, + // } + appString := AppString{ - AppPath: "/Applications/Finder.app", - Args: []string{"/home/user"}, + AppPath: "/Applications/BlogViewer.app", + Args: []string{"/home/user/Blogs/blog1.blog"}, } - aboutMe := AppString{ - AppPath: "/Applications/AboutMe.app", - Args: []string{}, - } + // aboutMe := AppString{ + // AppPath: "/Applications/AboutMe.app", + // Args: []string{}, + // } - appString2 := AppString{ - AppPath: "/Applications/Finder.app", - Args: []string{"/home/user/", "--desktop", "desktop-layer"}, - } + // appString2 := AppString{ + // AppPath: "/Applications/Finder.app", + // Args: []string{"/home/user/", "--desktop", "desktop-layer"}, + // } - autostart := []AppString{appString2, appString, aboutMe} + autostart := []AppString{appString} ctx.HTML(http.StatusOK, "index.tmpl", gin.H{ "autostart": autostart, })