Timoses
December 20, 2024, 4:05pm
1
Hi there,
haven’t been able to find a solution to this issue in the forum.
I follow the Quick Start guide (Quick start | Hugo ) to create an example of my issue.
hugo new site quickstart
cd quickstart
git init
git submodule add https://github.com/theNewDynamic/gohugo-theme-ananke.git themes/ananke
echo "theme = 'ananke'" >> hugo.toml
hugo server
Then added:
content/index.md:
---
{{% resref test.md %}}
content/test.md:
my test
layouts/shortcodes/resref.html:
{{ .Page }}
{{ $resource := .Page.Page.Resources.GetMatch ($.Get 0) }}
{{ with $resource }}
ResourceType: {{ .ResourceType }}
Params: {{ .Params }}
Title: {{ .Title }}
Content: {{ .Content }}
Permalink: "{{ .Permalink }}"
RelPermalink: "{{ .RelPermalink }}"
{{ end }}
Result is:
ResourceType: page
Params: map[draft:false iscjklanguage:false]
Title:
Content: <p>my test</p>
Permalink: ""
RelPermalink: ""
Why are Permalink and RelPermalink empty?
Problem 1
mv content/index.md content/_index.md
https://gohugo.io/troubleshooting/faq/#what-is-the-difference-between-an-indexmd-file-and-an-_indexmd-file
Problem 2
Branch bundles (which includes the home page) cannot have page resources where the ResourceType
is page
.
https://gohugo.io/content-management/page-bundles/#comparison
Problem 3
.Page.Page.Resources
That’s not necessary. .Page.Resources
is sufficient.
Question
What are you trying to do?
Timoses
December 20, 2024, 4:30pm
3
Ok, so my actual case is not in the root directory of content. So I have a leaf bundle and want to reference other pages.
I use leaf bundles because I want to discern between navigation levels (branch bundles for section navigation and leaf bundles for extra content).
Doing mv content/index.md content/_index.md
transforms the leaf bundle into a branch bundle which I don’t really want.
Reducing the double .Page.Page
to just .Page
also does not work.
Do page resources just never have a Permalink?
The home page is, by definition, a branch bundle, and must be represented by an _index.md
file. You cannot change the home page into a leaf bundle.
Please share your actual use case.
Timoses
December 21, 2024, 10:33am
5
Thanks for the input.
I moved index.md
and test.md
into content/test/
directory. It doesn’t change much about the case however.
Now under http://localhost:1313/test/
the result is:
ResourceType: page
Params: map[draft:false iscjklanguage:false]
Title:
Content: <p>my test</p>
Permalink: ""
RelPermalink: ""
Permalink is empty. And I don’t understand why.
To me this looks like a bug. If it’s not a bug, I’d expect an exception to be documented here: Permalink | Hugo
With the exception of the index
file, all of the files in a leaf bundle are page resources . Each page resource has a ResourceType
.
content/
├── leaf-bundle-1/
│ ├── a.jpg <-- page resource with ResourceType = image
│ ├── b.md <-- page resource with ResourceType = page
│ └── index.md
├── leaf-bundle-2/
│ ├── c.jpg <-- page resource with ResourceType = image
│ └── index.md
└── _index.md
Hugo supports six content formats . Page resources with any of these media types have a ResourceType
of page
. Page resources with a ResourceType
of page
are not published, and their Permalink
and RelPermalink
methods return an empty string.
We can do a better job of documenting this behavior, and the terminology isn’t great either:
https://github.com/gohugoio/hugoDocs/issues/2428
The use case for page resources with a ResouceType
of page
is to augment content. For example, create Markdown snippets that you can insert with a shortcode:
layouts/shortcodes/include-page-resource.html
{{ with .Get 0 }}
{{ with $.Page.Resources.Get . }}
{{- .RenderShortcodes }}
{{ else }}
{{ errorf "The %q shortcode was unable to find %q. See %s" $.Name . $.Position }}
{{ end }}
{{ else }}
{{ errorf "The %q shortcode requires a positional parameter indicating the relative path of the page resource to include. See %s" .Name .Position }}
{{ end }}
Then call it from content/leaf-bundle-1/index.md using the {{% %}}
notation:
{{% include-page-resource "b.md"}}
In the shortcode above, note that the third line begins with {{-
. Without this whitespace removal the result is rendered as an indented code block .
1 Like
irkode
December 21, 2024, 2:06pm
7
also telated to this one:
opened 08:16PM - 18 Mar 24 UTC
Enhancement
Reference: <https://discourse.gohugo.io/t/shortcode-for-iframe-breaking-at-updat… e-hugo-122-x-123-8/48904>
Not sure if this is an enhancement or a bug.
The test below obviously passes with v0.122.0 because we blindly copied _content_ page resources (`.html`, .`htm`, `.adoc`, `.pdc`, `.org`, `.rst`, `.md`) when publishing the site.
By design we are no longer copying _content_ page resources, but I think site and theme authors should be able to publish them with `Permalink`, `RelPermalink`, and `Publish`.
Failing test:
```go
func TestFoo(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
baseURL = 'https://example.org/'
disableKinds = ['home','rss','section','sitemap','taxonomy','term']
-- content/p1/index.md --
---
title: p1
---
-- content/p1/a.html --
<p>a</p>
-- content/p1/b.html --
<p>b</p>
-- content/p1/c.html --
<p>c</p>
-- layouts/_default/single.html --
|{{ (.Resources.Get "a.html").RelPermalink -}}
|{{ (.Resources.Get "b.html").Permalink -}}
|{{ (.Resources.Get "c.html").Publish }}
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/p1/index.html", "|/p1/a.html|https://example.org/p1/b.html|")
b.AssertFileContent("public/p1/a.html", "<p>a</p>")
b.AssertFileContent("public/p1/b.html", "<p>b</p>")
b.AssertFileContent("public/p1/c.html", "<p>c</p>")
}
```
The workaround is a bit ugly:
```text
{{ ((.Resources.Get "a.html").Content | resources.FromString "/p1/a.html").RelPermalink }}
{{ ((.Resources.Get "b.html").Content | resources.FromString "/p1/b.html").Permalink }}
{{ ((.Resources.Get "c.html").Content | resources.FromString "/p1/c.html").Publish }}
```
he relpermalink doc mentioned by the op seems to be faulty (outdated)
Once you have captured a resource, use any of the applicable Resource
methods to return a value or perform an action.
Timoses
December 21, 2024, 4:09pm
8
Thanks, this helps a lot. Indeed, documenting this behaviour would be great.
In my case, I’ll opt for HTML anchors towards the specific page resource within the leaf bundle’s index.md
. Thanks!
Unless I am missing something, this isn’t going to help you. Page resources with a ResourceType
of page
are not published. That means they are not copied to the public directory when you build your site.
1 Like