Skip to content

Commit

Permalink
Simplify footer render
Browse files Browse the repository at this point in the history
  • Loading branch information
fishy committed Jun 14, 2023
1 parent 7562be1 commit 5e91ffa
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 19 deletions.
12 changes: 3 additions & 9 deletions app/lib/htmltemplate/engine.go
Expand Up @@ -50,10 +50,7 @@ func (te *Engine) partialTemplate(w http.ResponseWriter, r *http.Request, mainTe
if err != nil {
return http.StatusInternalServerError, err
}
t, err = te.sanitizedContent(t, "footer", footer)
if err != nil {
return http.StatusInternalServerError, err
}
vars["footerHTML"] = te.RenderMarkdown(footer)

// Output the status code.
w.WriteHeader(statusCode)
Expand Down Expand Up @@ -87,13 +84,10 @@ func (te *Engine) Post(w http.ResponseWriter, r *http.Request, mainTemplate stri
if err != nil {
return http.StatusInternalServerError, err
}
t, err = te.sanitizedContent(t, "footer", footer)
if err != nil {
return http.StatusInternalServerError, err
}
vars["footerHTML"] = te.RenderMarkdown(footer)

// Parse the content.
t, err = te.sanitizedContent(t, "content", post.Content)
t, err = te.sanitizedContent(t, post.Content)
if err != nil {
return http.StatusInternalServerError, err
}
Expand Down
14 changes: 7 additions & 7 deletions app/lib/htmltemplate/sanitize.go
Expand Up @@ -9,25 +9,25 @@ import (
blackfriday "github.com/russross/blackfriday/v2"
)

// SanitizedHTML returns a sanitized content html.
func (te *Engine) SanitizedHTML(content string) []byte {
// RenderMarkdown renders markdown to html
func (te *Engine) RenderMarkdown(markdown string) template.HTML {
// Ensure unit line endings are used when pulling out of JSON.
markdownWithUnixLineEndings := strings.Replace(content, "\r\n", "\n", -1)
markdownWithUnixLineEndings := strings.ReplaceAll(markdown, "\r\n", "\n")
htmlCode := blackfriday.Run([]byte(markdownWithUnixLineEndings))

// Sanitize by removing HTML if true.
if !te.allowUnsafeHTML {
htmlCode = bluemonday.UGCPolicy().SanitizeBytes(htmlCode)
}
return htmlCode
return template.HTML(htmlCode)
}

// sanitizedContent returns a sanitized content block or an error is one occurs.
func (te *Engine) sanitizedContent(t *template.Template, name, markdown string) (*template.Template, error) {
htmlCode := te.SanitizedHTML(markdown)
func (te *Engine) sanitizedContent(t *template.Template, markdown string) (*template.Template, error) {
htmlCode := te.RenderMarkdown(markdown)

// Change delimiters temporarily so code samples can use Go blocks.
safeContent := fmt.Sprintf(`[{[{define "%s"}]}]%s[{[{end}]}]`, name, htmlCode)
safeContent := fmt.Sprintf(`[{[{define "content"}]}]%s[{[{end}]}]`, htmlCode)
t = t.Delims("[{[{", "}]}]")
var err error
t, err = t.Parse(safeContent)
Expand Down
2 changes: 1 addition & 1 deletion app/route/xml.go
Expand Up @@ -171,7 +171,7 @@ func (c *XMLUtil) rss(w http.ResponseWriter, r *http.Request) (status int, err e
}

for _, v := range posts {
html := c.Render.SanitizedHTML(v.Post.Content)
html := c.Render.RenderMarkdown(v.Post.Content)
m.Items = append(m.Items, Item{
Title: v.Title,
Link: site.SiteURL() + "/" + v.FullURL(),
Expand Down
2 changes: 1 addition & 1 deletion html/base.tmpl
Expand Up @@ -71,7 +71,7 @@
{{end}}
</main>
<footer>
{{template "footer" .}}
{{.footerHTML}}
</footer>

{{if GoogleAnalyticsID}}
Expand Down
2 changes: 1 addition & 1 deletion html/dashboard.tmpl
Expand Up @@ -25,7 +25,7 @@
</content>
</main>
<footer>
{{template "footer" .}}
{{.footerHTML}}
</footer>

{{if EnableStackEdit}}
Expand Down

0 comments on commit 5e91ffa

Please sign in to comment.