diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2022-12-03 13:38:05 -0500 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2022-12-03 13:39:47 -0500 |
| commit | 10c60b3c9ba2c17419534cf4089328a66568e4f1 (patch) | |
| tree | 33e6ecedfc3dec7d96488878291179c8a593e779 /page/page.go | |
| parent | d6f60ce24e123ee83b73f6c9dbe8c4b9af5c629e (diff) | |
| download | go-website-10c60b3c9ba2c17419534cf4089328a66568e4f1.tar.gz go-website-10c60b3c9ba2c17419534cf4089328a66568e4f1.tar.xz | |
Add a couple handlers to serve up JSON and markdown
Diffstat (limited to 'page/page.go')
| -rw-r--r-- | page/page.go | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/page/page.go b/page/page.go index d34a1f7..29b35bc 100644 --- a/page/page.go +++ b/page/page.go @@ -55,7 +55,10 @@ type Page struct { Date *PageTime Published bool Vars map[string]interface{} - markdown []byte + // Keys of vars to be included when RenderJson is called, all vars are + // omitted if empty. + JsonVars []string + markdown []byte } // Global is meant to be supplied by external users of this package to populate @@ -232,6 +235,13 @@ func (p *Page) Read() error { // markdown file, then runs it through the markdown parser. Typically // this is called in the base template. func (p *Page) RenderBody() (string, error) { + s, err := p.GetMarkdown() + return string(blackfriday.Run([]byte(s))), err +} + +// GetMarkdown renders and executes a template from the body of the +// markdown file, then simply returns the unrendered markdown. +func (p *Page) GetMarkdown() (string, error) { buf := &bytes.Buffer{} t, err := template.New("Body").Funcs(Funcs).Parse(string(p.markdown)) @@ -245,7 +255,7 @@ func (p *Page) RenderBody() (string, error) { return "", fmt.Errorf("template execute; %w", err) } - return string(blackfriday.Run(buf.Bytes())), nil + return buf.String(), nil } func (p Page) String() string { |
