mirror of
https://github.com/facundoolano/jorge.git
synced 2024-12-25 21:58:28 +01:00
add a simpler templ render interface, support no highlighting
This commit is contained in:
parent
1461945787
commit
a7866b7d2c
3 changed files with 27 additions and 17 deletions
|
@ -22,6 +22,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const FM_SEPARATOR = "---"
|
const FM_SEPARATOR = "---"
|
||||||
|
const NO_SYNTAX_HIGHLIGHTING = ""
|
||||||
|
|
||||||
type Engine = liquid.Engine
|
type Engine = liquid.Engine
|
||||||
|
|
||||||
|
@ -125,10 +126,18 @@ func (templ Template) IsPost() bool {
|
||||||
return ok
|
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.
|
// Renders the liquid template with the given context as bindings.
|
||||||
// If the template source is org or md, convert them to html after the
|
// If the template source is org or md, convert them to html after the
|
||||||
// liquid rendering.
|
// 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
|
// liquid rendering
|
||||||
content, err := templ.liquidTemplate.Render(context)
|
content, err := templ.liquidTemplate.Render(context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -142,7 +151,9 @@ func (templ Template) Render(context map[string]interface{}, hlTheme string) ([]
|
||||||
|
|
||||||
// make * -> h1, ** -> h2, etc
|
// make * -> h1, ** -> h2, etc
|
||||||
htmlWriter.TopLevelHLevel = 1
|
htmlWriter.TopLevelHLevel = 1
|
||||||
htmlWriter.HighlightCodeBlock = highlightCodeBlock(hlTheme)
|
if hlTheme != NO_SYNTAX_HIGHLIGHTING {
|
||||||
|
htmlWriter.HighlightCodeBlock = highlightCodeBlock(hlTheme)
|
||||||
|
}
|
||||||
|
|
||||||
contentStr, err := doc.Write(htmlWriter)
|
contentStr, err := doc.Write(htmlWriter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -152,11 +163,14 @@ func (templ Template) Render(context map[string]interface{}, hlTheme string) ([]
|
||||||
} else if templ.SrcExt() == ".md" {
|
} else if templ.SrcExt() == ".md" {
|
||||||
// markdown rendering
|
// markdown rendering
|
||||||
var buf bytes.Buffer
|
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),
|
gm_highlight.WithStyle(hlTheme),
|
||||||
),
|
)))
|
||||||
))
|
}
|
||||||
|
md := goldmark.New(options...)
|
||||||
if err := md.Convert(content, &buf); err != nil {
|
if err := md.Convert(content, &buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ tags: ["software", "web"]
|
||||||
assertEqual(t, templ.Metadata["tags"].([]interface{})[0], "software")
|
assertEqual(t, templ.Metadata["tags"].([]interface{})[0], "software")
|
||||||
assertEqual(t, templ.Metadata["tags"].([]interface{})[1], "web")
|
assertEqual(t, templ.Metadata["tags"].([]interface{})[1], "web")
|
||||||
|
|
||||||
content, err := templ.Render(nil, "github")
|
content, err := templ.Render()
|
||||||
assertEqual(t, err, nil)
|
assertEqual(t, err, nil)
|
||||||
assertEqual(t, string(content), "<p>Hello World!</p>")
|
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())
|
templ, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name())
|
||||||
assertEqual(t, err, nil)
|
assertEqual(t, err, nil)
|
||||||
ctx := map[string]interface{}{"page": templ.Metadata}
|
content, err := templ.Render()
|
||||||
content, err := templ.Render(ctx, "github")
|
|
||||||
assertEqual(t, err, nil)
|
assertEqual(t, err, nil)
|
||||||
expected := `<h1>my new post</h1>
|
expected := `<h1>my new post</h1>
|
||||||
<h2>a blog post</h2>
|
<h2>a blog post</h2>
|
||||||
|
@ -133,7 +132,7 @@ tags: ["software", "web"]
|
||||||
templ, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name())
|
templ, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name())
|
||||||
assertEqual(t, err, nil)
|
assertEqual(t, err, nil)
|
||||||
|
|
||||||
content, err := templ.Render(nil, "github")
|
content, err := templ.Render()
|
||||||
assertEqual(t, err, nil)
|
assertEqual(t, err, nil)
|
||||||
expected := `<div id="outline-container-headline-1" class="outline-1">
|
expected := `<div id="outline-container-headline-1" class="outline-1">
|
||||||
<h1 id="headline-1">
|
<h1 id="headline-1">
|
||||||
|
@ -175,7 +174,7 @@ tags: ["software", "web"]
|
||||||
templ, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name())
|
templ, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name())
|
||||||
assertEqual(t, err, nil)
|
assertEqual(t, err, nil)
|
||||||
|
|
||||||
content, err := templ.Render(nil, "github")
|
content, err := templ.Render()
|
||||||
assertEqual(t, err, nil)
|
assertEqual(t, err, nil)
|
||||||
expected := `<h1>My title</h1>
|
expected := `<h1>My title</h1>
|
||||||
<h2>my Subtitle</h2>
|
<h2>my Subtitle</h2>
|
||||||
|
|
|
@ -306,7 +306,7 @@ func (site *Site) render(templ *markup.Template) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx["page"] = templ.Metadata
|
ctx["page"] = templ.Metadata
|
||||||
content, err := templ.Render(ctx, site.Config.HighlightTheme)
|
content, err := templ.RenderWith(ctx, site.Config.HighlightTheme)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
if layout_templ, ok := site.layouts[layout.(string)]; ok {
|
||||||
ctx["layout"] = layout_templ.Metadata
|
ctx["layout"] = layout_templ.Metadata
|
||||||
ctx["content"] = content
|
ctx["content"] = content
|
||||||
content, err = layout_templ.Render(ctx, site.Config.HighlightTheme)
|
content, err = layout_templ.RenderWith(ctx, site.Config.HighlightTheme)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -372,10 +372,7 @@ func getExcerpt(templ *markup.Template) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := map[string]interface{}{
|
content, err := templ.Render()
|
||||||
"page": templ.Metadata,
|
|
||||||
}
|
|
||||||
content, err := templ.Render(ctx, "tango")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue