Skip to content

Commit fa86b70

Browse files
committed
internal/web: allow markdown files to turn off templates
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]>
1 parent e4467a2 commit fa86b70

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

internal/web/render.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,24 +124,32 @@ func (site *Site) renderHTML(p Page, tmpl string, r *http.Request) ([]byte, erro
124124

125125
var buf bytes.Buffer
126126
if _, ok := p["Content"]; !ok && data != "" {
127-
// Load actual Markdown content (also a template).
128-
tf := t.New(file)
129-
if err := tmplfunc.Parse(tf, data); err != nil {
130-
return nil, err
131-
}
132-
if err := tf.Execute(&buf, p); err != nil {
133-
return nil, err
127+
// Either the page explicitly requested templating, or it is markdown,
128+
// which is treated as a template by default.
129+
isTemplate, explicit := p["template"].(bool)
130+
tdata := data
131+
if !explicit || isTemplate {
132+
// Load content as a template.
133+
tf := t.New(file)
134+
if err := tmplfunc.Parse(tf, data); err != nil {
135+
return nil, err
136+
}
137+
if err := tf.Execute(&buf, p); err != nil {
138+
return nil, err
139+
}
140+
tdata = buf.String()
141+
buf.Reset()
134142
}
143+
135144
if strings.HasSuffix(file, ".md") {
136-
html, err := markdownToHTML(buf.String())
145+
html, err := markdownToHTML(tdata)
137146
if err != nil {
138147
return nil, err
139148
}
140149
p["Content"] = html
141150
} else {
142-
p["Content"] = template.HTML(buf.String())
151+
p["Content"] = template.HTML(tdata)
143152
}
144-
buf.Reset()
145153
}
146154

147155
if err := t.Execute(&buf, p); err != nil {

internal/web/site.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
// The key-value pair “layout: name” selects the page layout template with the given name.
5252
// See the next section, “Page Rendering”, for details about layout and rendering.
5353
//
54+
// The key-value pair “template: bool” controls whether the page is treated as an HTML template
55+
// (see the next section, “Page Rendering”). The default is false for HTML
56+
// and true for markdown.
57+
//
5458
// In addition to these explicit key-value pairs, pages loaded from the file system
5559
// have a few implicit key-value pairs added by the page loading process:
5660
//
@@ -529,8 +533,8 @@ func (s *Site) serveHTML(w http.ResponseWriter, r *http.Request, p *pageFile) {
529533
src = buf.String()
530534
}
531535

532-
// Template is enabled always in Markdown.
533-
// It can only be disabled for HTML files.
536+
// If the file doesn't ask to be treated as a template and isn't Markdown,
537+
// set the page's content to skip templating later, in Site.renderHTML.
534538
isTemplate, _ := p.page["template"].(bool)
535539
if !isTemplate && !isMarkdown {
536540
p.page["Content"] = template.HTML(src)

internal/web/site_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ func TestRedirectAndMetadata(t *testing.T) {
5353

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

6262
testServeBody(t, site, "/doc/test", "<strong>bold</strong>")
6363
testServeBody(t, site, "/doc/test2", "<em>template</em>")
64+
testServeBody(t, site, "/doc/test3", `{{x}}`)
6465
}
6566

6667
func TestCode(t *testing.T) {

0 commit comments

Comments
 (0)