add a simpler templ render interface, support no highlighting

This commit is contained in:
facundoolano 2024-02-27 12:40:40 -03:00
parent 1461945787
commit a7866b7d2c
3 changed files with 27 additions and 17 deletions

View file

@ -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
}

View file

@ -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), "<p>Hello World!</p>")
}
@ -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 := `<h1>my new post</h1>
<h2>a blog post</h2>
@ -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 := `<div id="outline-container-headline-1" class="outline-1">
<h1 id="headline-1">
@ -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 := `<h1>My title</h1>
<h2>my Subtitle</h2>

View file

@ -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 ""
}