Skip to content

Commit

Permalink
internal/web: allow markdown files to turn off templates
Browse files Browse the repository at this point in the history
If a markdown file has `template: false` in its metadata,
it will not be treated as a template.

Change-Id: I81599ea02b05ff7693f5f816293433df4988b64c
Reviewed-on: https://go-review.googlesource.com/c/website/+/537497
Run-TryBot: Jonathan Amsterdam <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
  • Loading branch information
jba committed Nov 1, 2023
1 parent e4467a2 commit fa86b70
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
28 changes: 18 additions & 10 deletions internal/web/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,24 +124,32 @@ func (site *Site) renderHTML(p Page, tmpl string, r *http.Request) ([]byte, erro

var buf bytes.Buffer
if _, ok := p["Content"]; !ok && data != "" {
// Load actual Markdown content (also a template).
tf := t.New(file)
if err := tmplfunc.Parse(tf, data); err != nil {
return nil, err
}
if err := tf.Execute(&buf, p); err != nil {
return nil, err
// Either the page explicitly requested templating, or it is markdown,
// which is treated as a template by default.
isTemplate, explicit := p["template"].(bool)
tdata := data
if !explicit || isTemplate {
// Load content as a template.
tf := t.New(file)
if err := tmplfunc.Parse(tf, data); err != nil {
return nil, err
}
if err := tf.Execute(&buf, p); err != nil {
return nil, err
}
tdata = buf.String()
buf.Reset()
}

if strings.HasSuffix(file, ".md") {
html, err := markdownToHTML(buf.String())
html, err := markdownToHTML(tdata)
if err != nil {
return nil, err
}
p["Content"] = html
} else {
p["Content"] = template.HTML(buf.String())
p["Content"] = template.HTML(tdata)
}
buf.Reset()
}

if err := t.Execute(&buf, p); err != nil {
Expand Down
8 changes: 6 additions & 2 deletions internal/web/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
// The key-value pair “layout: name” selects the page layout template with the given name.
// See the next section, “Page Rendering”, for details about layout and rendering.
//
// The key-value pair “template: bool” controls whether the page is treated as an HTML template
// (see the next section, “Page Rendering”). The default is false for HTML
// and true for markdown.
//
// In addition to these explicit key-value pairs, pages loaded from the file system
// have a few implicit key-value pairs added by the page loading process:
//
Expand Down Expand Up @@ -529,8 +533,8 @@ func (s *Site) serveHTML(w http.ResponseWriter, r *http.Request, p *pageFile) {
src = buf.String()
}

// Template is enabled always in Markdown.
// It can only be disabled for HTML files.
// If the file doesn't ask to be treated as a template and isn't Markdown,
// set the page's content to skip templating later, in Site.renderHTML.
isTemplate, _ := p.page["template"].(bool)
if !isTemplate && !isMarkdown {
p.page["Content"] = template.HTML(src)
Expand Down
9 changes: 5 additions & 4 deletions internal/web/site_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ func TestRedirectAndMetadata(t *testing.T) {

func TestMarkdown(t *testing.T) {
site := NewSite(fstest.MapFS{
"site.tmpl": {Data: []byte(`{{.Content}}`)},
"doc/test.md": {Data: []byte("**bold**")},
"doc/test2.md": {Data: []byte(`{{"*template*"}}`)},
"lib/godoc/site.html": {Data: []byte(`{{.Data}}`)},
"site.tmpl": {Data: []byte(`{{.Content}}`)},
"doc/test.md": {Data: []byte("**bold**")},
"doc/test2.md": {Data: []byte(`{{"*template*"}}`)},
"doc/test3.md": {Data: []byte("---\ntemplate: false\n---\n{{x}}")},
})

testServeBody(t, site, "/doc/test", "<strong>bold</strong>")
testServeBody(t, site, "/doc/test2", "<em>template</em>")
testServeBody(t, site, "/doc/test3", `{{x}}`)
}

func TestCode(t *testing.T) {
Expand Down

0 comments on commit fa86b70

Please sign in to comment.