mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
Merge pull request #662 from mholt/md-include-fix
markdown: Included files in Markdown templates have access to document vars
This commit is contained in:
commit
4f5fe2de24
4 changed files with 43 additions and 29 deletions
|
@ -20,35 +20,12 @@ import (
|
|||
type Context struct {
|
||||
Root http.FileSystem
|
||||
Req *http.Request
|
||||
// This is used to access information about the URL.
|
||||
URL *url.URL
|
||||
URL *url.URL
|
||||
}
|
||||
|
||||
// Include returns the contents of filename relative to the site root
|
||||
// Include returns the contents of filename relative to the site root.
|
||||
func (c Context) Include(filename string) (string, error) {
|
||||
file, err := c.Root.Open(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
tpl, err := template.New(filename).Parse(string(body))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = tpl.Execute(&buf, c)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return buf.String(), nil
|
||||
return ContextInclude(filename, c, c.Root)
|
||||
}
|
||||
|
||||
// Now returns the current timestamp in the specified format.
|
||||
|
@ -212,3 +189,34 @@ func (c Context) Markdown(filename string) (string, error) {
|
|||
|
||||
return string(markdown), nil
|
||||
}
|
||||
|
||||
// ContextInclude opens filename using fs and executes a template with the context ctx.
|
||||
// This does the same thing that Context.Include() does, but with the ability to provide
|
||||
// your own context so that the included files can have access to additional fields your
|
||||
// type may provide. You can embed Context in your type, then override its Include method
|
||||
// to call this function with ctx being the instance of your type, and fs being Context.Root.
|
||||
func ContextInclude(filename string, ctx interface{}, fs http.FileSystem) (string, error) {
|
||||
file, err := fs.Open(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
tpl, err := template.New(filename).Parse(string(body))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = tpl.Execute(&buf, ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
|
|
@ -107,7 +107,7 @@ func TestMarkdown(t *testing.T) {
|
|||
<title>Markdown test 1</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Header</h1>
|
||||
<h1>Header for: Markdown test 1</h1>
|
||||
|
||||
Welcome to A Caddy website!
|
||||
<h2>Welcome on the blog</h2>
|
||||
|
@ -208,7 +208,7 @@ DocFlags.var_bool true`
|
|||
<title>first_post</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Header</h1>
|
||||
<h1>Header for: first_post</h1>
|
||||
|
||||
Welcome to title!
|
||||
<h1>Test h1</h1>
|
||||
|
|
|
@ -28,6 +28,12 @@ type Data struct {
|
|||
Links []PageLink
|
||||
}
|
||||
|
||||
// Include "overrides" the embedded middleware.Context's Include()
|
||||
// method so that included files have access to d's fields.
|
||||
func (d Data) Include(filename string) (string, error) {
|
||||
return middleware.ContextInclude(filename, d, d.Root)
|
||||
}
|
||||
|
||||
// Process processes the contents of a page in b. It parses the metadata
|
||||
// (if any) and uses the template (if found).
|
||||
func (md Markdown) Process(c *Config, requestPath string, b []byte, ctx middleware.Context) ([]byte, error) {
|
||||
|
|
2
middleware/markdown/testdata/header.html
vendored
2
middleware/markdown/testdata/header.html
vendored
|
@ -1 +1 @@
|
|||
<h1>Header</h1>
|
||||
<h1>Header for: {{.Doc.title}}</h1>
|
Loading…
Reference in a new issue