diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2024-04-11 17:46:18 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2024-04-15 09:49:57 +0200 |
commit | df11327ba90179747be2b25574ac48c2f336b298 (patch) | |
tree | 70f7487cee6815109fc325ed0619130bb29834ca /markup/goldmark/codeblocks/render.go | |
parent | a18e2bcb9a2c556e03dc7e790b0343c83163877a (diff) |
Pass .RenderShortcodes' Page to render hooks as .PageInner
The main use case for this is to resolve links and resources (e.g. images) relative to the included `Page`.
A typical `include` would similar to this:
```handlebars
{{ with site.GetPage (.Get 0) }}
{{ .RenderShortcodes }}
{{ end }}
```
And when used in a Markdown file:
```markdown
{{% include "/posts/p1" %}}
```
Any render hook triggered while rendering `/posts/p1` will get `/posts/p1` when calling `.PageInner`.
Note that
* This is only relevant for shortcodes included with `{{%` that calls `.RenderShortcodes`.
* `.PageInner` is available in all render hooks that, before this commit, received `.Page`.
* `.PageInner` will fall back to the value of `.Page` if not relevant and will always have a value.
Fixes #12356
Diffstat (limited to 'markup/goldmark/codeblocks/render.go')
-rw-r--r-- | markup/goldmark/codeblocks/render.go | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/markup/goldmark/codeblocks/render.go b/markup/goldmark/codeblocks/render.go index 67053640d..5dfa8262f 100644 --- a/markup/goldmark/codeblocks/render.go +++ b/markup/goldmark/codeblocks/render.go @@ -108,6 +108,7 @@ func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.No } cbctx := &codeBlockContext{ page: ctx.DocumentContext().Document, + pageInner: r.getPageInner(ctx), lang: lang, code: s, ordinal: ordinal, @@ -132,7 +133,6 @@ func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.No w, cbctx, ) - if err != nil { return ast.WalkContinue, herrors.NewFileErrorFromPos(err, cbctx.createPos()) } @@ -140,11 +140,24 @@ func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.No return ast.WalkContinue, nil } +func (r *htmlRenderer) getPageInner(rctx *render.Context) any { + pid := rctx.PeekPid() + if pid > 0 { + if lookup := rctx.DocumentContext().DocumentLookup; lookup != nil { + if v := rctx.DocumentContext().DocumentLookup(pid); v != nil { + return v + } + } + } + return rctx.DocumentContext().Document +} + type codeBlockContext struct { - page any - lang string - code string - ordinal int + page any + pageInner any + lang string + code string + ordinal int // This is only used in error situations and is expensive to create, // to delay creation until needed. @@ -159,6 +172,10 @@ func (c *codeBlockContext) Page() any { return c.page } +func (c *codeBlockContext) PageInner() any { + return c.pageInner +} + func (c *codeBlockContext) Type() string { return c.lang } |