Skip to content

Commit

Permalink
fixup! Allow marking pages and parts of pages as sensitive
Browse files Browse the repository at this point in the history
Use text/template to hide sensitive content
  • Loading branch information
foodelevator committed Apr 2, 2024
1 parent 4d34dde commit f40755b
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pages/pages.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package pages

import (
"bytes"
"os"
"path/filepath"
"strings"
"text/template"

"github.com/datasektionen/taitan/anchor"

Expand Down Expand Up @@ -178,18 +180,26 @@ func parseDirs(isReception bool, root string, dirs []string) (pages map[string]*

// toHTML reads a markdown file and returns a HTML string.
func toHTML(isReception bool, filename string) (string, error) {
buf, err := os.ReadFile(filename)
rawMarkdown, err := os.ReadFile(filename)
if err != nil {
return "", err
}
t, err := template.New("").Parse(string(rawMarkdown))
if err != nil {
return "", err
}
var filteredMarkdown bytes.Buffer
if err := t.Execute(&filteredMarkdown, map[string]any{"reception": isReception}); err != nil {
return "", err
}
// Use standard HTML rendering.
renderer := blackfriday.HtmlRenderer(blackfriday.HTML_USE_XHTML, "", "")
// Parse markdown where all id's are created from the values inside
// the element tag.
buf = blackfriday.MarkdownOptions(buf, renderer, blackfriday.Options{
html := blackfriday.MarkdownOptions(filteredMarkdown.Bytes(), renderer, blackfriday.Options{
Extensions: blackfriday.EXTENSION_AUTO_HEADER_IDS | blackfriday.EXTENSION_TABLES | blackfriday.EXTENSION_FENCED_CODE,
})
return string(buf), nil
return string(html), nil
}

// parseDir creates a response for a directory.
Expand Down

0 comments on commit f40755b

Please sign in to comment.