include: adjust to Hugo's build-in link render hook #859

- resolve render hook destination with leading ./
- resolve not only resources but pages as well
This commit is contained in:
Sören Weber 2024-05-28 22:06:17 +02:00
parent db38203826
commit 5c2a221dad
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
3 changed files with 18 additions and 9 deletions

View file

@ -18,6 +18,12 @@ This document shows you what's new in the latest release and flags it with one o
--- ---
## 6.1.0.beta (XXXX-XX-XX) {#610}
- {{% badge style="note" title=" " %}}Change{{% /badge %}} The [`include` shortcode](shortcodes/include) is now able to resolve links to pages as well as resources or files in the file system (the old behavior).
---
## 6.0.0 (2024-04-27) {#600} ## 6.0.0 (2024-04-27) {#600}
- {{% badge style="warning" title=" " %}}Breaking{{% /badge %}} This release requires you to move your self-defined variant (`theme-*.css`) and chroma stylesheets (`chroma-*.css`) from `static/css` to `assets/css`. - {{% badge style="warning" title=" " %}}Breaking{{% /badge %}} This release requires you to move your self-defined variant (`theme-*.css`) and chroma stylesheets (`chroma-*.css`) from `static/css` to `assets/css`.

View file

@ -3,7 +3,7 @@ description = "Displays content from other files"
title = "Include" title = "Include"
+++ +++
The `include` shortcode includes other files from your project inside of the current page. The `include` shortcode includes other pages, resources or files from your project.
## Usage ## Usage
@ -42,7 +42,7 @@ The included files can even contain Markdown and will be taken into account when
| Name | Position | Default | Notes | | Name | Position | Default | Notes |
|----------------------|----------|------------------|-------------| |----------------------|----------|------------------|-------------|
| **file** | 1 | _<empty>_ | The path to the file to be included. Path resolution adheres to [Hugo's build-in `readFile` function](https://gohugo.io/functions/readfile/) | | **file** | 1 | _<empty>_ | The path to the page, resource or file to be included. Page and resource paths adhere to [Hugo's logical path](https://gohugo.io/methods/page/path/). If not found by logical path it falls back to [Hugo's build-in `readFile` function](https://gohugo.io/functions/readfile/) |
| **hidefirstheading** | 2 | `false` | When `true` and the included file contains headings, the first heading will be hidden. This comes in handy, eg. if you include otherwise standalone Markdown files. | | **hidefirstheading** | 2 | `false` | When `true` and the included file contains headings, the first heading will be hidden. This comes in handy, eg. if you include otherwise standalone Markdown files. |
## Examples ## Examples

View file

@ -3,21 +3,24 @@
{{- $page = .context }} {{- $page = .context }}
{{- warnf "%q: DEPRECATED parameter 'context' for shortcode 'include' found, use 'page' instead; see https://mcshelby.github.io/hugo-theme-relearn/basics/migration#5180" $page.File.Filename }} {{- warnf "%q: DEPRECATED parameter 'context' for shortcode 'include' found, use 'page' instead; see https://mcshelby.github.io/hugo-theme-relearn/basics/migration#5180" $page.File.Filename }}
{{- end }} {{- end }}
{{- $file := .file }} {{- $u := urls.Parse .file }}
{{- $path := strings.TrimPrefix "./" $u.Path }}
{{- $content := "" }} {{- $content := "" }}
{{- with or {{- with or
($page.Resources.Get $file) ($page.Page.GetPage $path)
(resources.Get $file) ($page.Page.GetPage (strings.TrimRight "/" $path))
($page.Page.Resources.Get $path)
(resources.Get $path)
}} }}
{{- $content = .Content }} {{- $content = .Content }}
{{- else }} {{- else }}
{{- if (fileExists $file) }} {{- if (fileExists .file) }}
{{- $content = $file | readFile }} {{- $content = .file | readFile }}
{{- else }} {{- else }}
{{- if eq $page.Site.Params.include.errorlevel "warning" }} {{- if eq $page.Site.Params.include.errorlevel "warning" }}
{{- warnf "%q: file '%s' is not a resource nor accessible via file system" $page.File.Filename $file }} {{- warnf "%q: included file '%s' is not a page, resource or accessible via file system" $page.File.Filename .file }}
{{- else if eq $page.Site.Params.include.errorlevel "error" }} {{- else if eq $page.Site.Params.include.errorlevel "error" }}
{{- errorf "%q: image '%s' is not a resource nor accessible via file system" $page.File.Filename $file }} {{- errorf "%q: included file '%s' is not a page, resource or accessible via file system" $page.File.Filename .file }}
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- end }} {{- end }}