From a7866b7d2c4abe16afb603ae0406c7767a5d3031 Mon Sep 17 00:00:00 2001 From: facundoolano Date: Tue, 27 Feb 2024 12:40:40 -0300 Subject: [PATCH] add a simpler templ render interface, support no highlighting --- markup/templates.go | 26 ++++++++++++++++++++------ markup/templates_test.go | 9 ++++----- site/site.go | 9 +++------ 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/markup/templates.go b/markup/templates.go index cfabe15..3468b22 100644 --- a/markup/templates.go +++ b/markup/templates.go @@ -22,6 +22,7 @@ import ( ) const FM_SEPARATOR = "---" +const NO_SYNTAX_HIGHLIGHTING = "" type Engine = liquid.Engine @@ -125,10 +126,18 @@ func (templ Template) IsPost() bool { return ok } +// Renders the liquid template with default bindings. +func (templ Template) Render() ([]byte, error) { + ctx := map[string]interface{}{ + "page": templ.Metadata, + } + return templ.RenderWith(ctx, NO_SYNTAX_HIGHLIGHTING) +} + // Renders the liquid template with the given context as bindings. // If the template source is org or md, convert them to html after the // liquid rendering. -func (templ Template) Render(context map[string]interface{}, hlTheme string) ([]byte, error) { +func (templ Template) RenderWith(context map[string]interface{}, hlTheme string) ([]byte, error) { // liquid rendering content, err := templ.liquidTemplate.Render(context) if err != nil { @@ -142,7 +151,9 @@ func (templ Template) Render(context map[string]interface{}, hlTheme string) ([] // make * -> h1, ** -> h2, etc htmlWriter.TopLevelHLevel = 1 - htmlWriter.HighlightCodeBlock = highlightCodeBlock(hlTheme) + if hlTheme != NO_SYNTAX_HIGHLIGHTING { + htmlWriter.HighlightCodeBlock = highlightCodeBlock(hlTheme) + } contentStr, err := doc.Write(htmlWriter) if err != nil { @@ -152,11 +163,14 @@ func (templ Template) Render(context map[string]interface{}, hlTheme string) ([] } else if templ.SrcExt() == ".md" { // markdown rendering var buf bytes.Buffer - md := goldmark.New(goldmark.WithExtensions( - gm_highlight.NewHighlighting( + + options := make([]goldmark.Option, 0) + if hlTheme != NO_SYNTAX_HIGHLIGHTING { + options = append(options, goldmark.WithExtensions(gm_highlight.NewHighlighting( gm_highlight.WithStyle(hlTheme), - ), - )) + ))) + } + md := goldmark.New(options...) if err := md.Convert(content, &buf); err != nil { return nil, err } diff --git a/markup/templates_test.go b/markup/templates_test.go index b70d425..1da4c8f 100644 --- a/markup/templates_test.go +++ b/markup/templates_test.go @@ -26,7 +26,7 @@ tags: ["software", "web"] assertEqual(t, templ.Metadata["tags"].([]interface{})[0], "software") assertEqual(t, templ.Metadata["tags"].([]interface{})[1], "web") - content, err := templ.Render(nil, "github") + content, err := templ.Render() assertEqual(t, err, nil) assertEqual(t, string(content), "

Hello World!

") } @@ -102,8 +102,7 @@ tags: ["software", "web"] templ, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name()) assertEqual(t, err, nil) - ctx := map[string]interface{}{"page": templ.Metadata} - content, err := templ.Render(ctx, "github") + content, err := templ.Render() assertEqual(t, err, nil) expected := `

my new post

a blog post

@@ -133,7 +132,7 @@ tags: ["software", "web"] templ, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name()) assertEqual(t, err, nil) - content, err := templ.Render(nil, "github") + content, err := templ.Render() assertEqual(t, err, nil) expected := `

@@ -175,7 +174,7 @@ tags: ["software", "web"] templ, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name()) assertEqual(t, err, nil) - content, err := templ.Render(nil, "github") + content, err := templ.Render() assertEqual(t, err, nil) expected := `

My title

my Subtitle

diff --git a/site/site.go b/site/site.go index 9542037..3532944 100644 --- a/site/site.go +++ b/site/site.go @@ -306,7 +306,7 @@ func (site *Site) render(templ *markup.Template) ([]byte, error) { } ctx["page"] = templ.Metadata - content, err := templ.Render(ctx, site.Config.HighlightTheme) + content, err := templ.RenderWith(ctx, site.Config.HighlightTheme) if err != nil { return nil, err } @@ -317,7 +317,7 @@ func (site *Site) render(templ *markup.Template) ([]byte, error) { if layout_templ, ok := site.layouts[layout.(string)]; ok { ctx["layout"] = layout_templ.Metadata ctx["content"] = content - content, err = layout_templ.Render(ctx, site.Config.HighlightTheme) + content, err = layout_templ.RenderWith(ctx, site.Config.HighlightTheme) if err != nil { return nil, err } @@ -372,10 +372,7 @@ func getExcerpt(templ *markup.Template) string { return "" } - ctx := map[string]interface{}{ - "page": templ.Metadata, - } - content, err := templ.Render(ctx, "tango") + content, err := templ.Render() if err != nil { return "" }