Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resources to top level sections without a _index.md file #11179

Closed
jmooring opened this issue Jun 27, 2023 · 5 comments
Closed

Add resources to top level sections without a _index.md file #11179

jmooring opened this issue Jun 27, 2023 · 5 comments

Comments

@jmooring
Copy link
Member

jmooring commented Jun 27, 2023

I'm not sure if this is a bug or an enhancement.

This content:

content/
├── books/
│   ├── book-1.md
│   ├── book-2.md
│   └── section.jpg
└── films/
    ├── film-1.md
    ├── film-2.md
    ├── _index.md
    └── section.jpg

With this code in layouts/_default/home.html:

{{ range .Sections.ByTitle }}
  <pre>
    .Title --> {{ .Title }}
    .IsSection --> {{ .IsSection }}
    .BundleType --> {{ .BundleType }}
    .Resources.Get "section.jpg" --> {{ .Resources.Get "section.jpg" }}
  </pre>
{{ end }}

Produces:

.Title --> Books
.IsSection --> true
.BundleType --> branch
.Resources.Get "section.jpg" --> 

.Title --> Films
.IsSection --> true
.BundleType --> branch
.Resources.Get "section.jpg" --> section.jpg

If the "books" section were a branch bundle, then "section.jpg" should be available as a page resource.

Two options:

  1. Make .BundleType return nothing for top level sections without _index.md files
  2. Treat top level sections without _index.md files as branch bundles.

Option 2 makes more sense to me, but I don't know what it would break.

Same results with v0.54.0 -- this isn't anything new.

@jmooring
Copy link
Member Author

Regarding Option 2 in my previous comment: it would improve link/image portability in conjunction with render hooks.

This content structure is common in Hugo and in other markdown environments (GitHub, Obsidian, etc.):

content/posts/  <--- no _index.md. It IS a section, and .BundleType currently (erroneously) returns "branch".
├── kitten.jpg
├── post-1.md
└── post-2.md

content/posts/post-1.md

![A kitten](kitten.jpg)

An image render hook that looks for a page resource won't find the image, and that's how it should be. But, if content/posts were a branch bundle (as .BundleType reports today), the render hook could fall back to the current section. A simplified example:

{{ with .Page.Resources.Get .Destination }}
  <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ else }}
  {{ with (and (ne .Page.BundleType "leaf") (.Page.CurrentSection.Resources.Get .Destination)) }}
     <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ else }}
    {{ errorf "Resource not found."
  {{ end }}
{{ end }}

This works fine today in directories with _index.md files. But the point is, for a top level section, it is common to not have an _index.md file. I understand that this won't work with directories further down the tree that lack _index.md files, and that's fine. The more common scenario relates to the top level section.

This is, to some extent, related to #4583.

Finally, I'm also fine just correcting .BundleType for the top level sections.

@bep bep added NotSure and removed NeedsTriage labels Jun 29, 2023
@bep bep added this to the v0.115.0 milestone Jun 29, 2023
@bep
Copy link
Member

bep commented Jun 29, 2023

Two options:

Or:

  1. Add a _index.md file.

So, this issue is certainly as designed, as

  • All first level folders with content in them are sections by definition (mostly to handle older Hugo sites)
  • All sections are by definitions branch bundles.
  • It's just a technical reason that we're doing the bundling when we're reading the files. And then, later, we create the missing sections for the top level folders (which then does not get the bundled resources).

@bep bep changed the title .Page.BundleType is branch for top level sections without _index.md files Add resources to top level sections without a _index.md file Jun 29, 2023
@bep
Copy link
Member

bep commented Jun 29, 2023

OK, thinking about this, I have fixed this in a branch that I had planned to get out the door soon.

@bep bep modified the milestones: v0.115.0, v0.116.0 Jun 30, 2023
@bep bep modified the milestones: v0.116.0, v0.117.0 Aug 1, 2023
@bep bep modified the milestones: v0.117.0, v0.118.0 Aug 30, 2023
@bep bep modified the milestones: v0.118.0, v0.119.0 Sep 15, 2023
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 5, 2023
@bep bep modified the milestones: v0.120.0, v0.121.0 Oct 31, 2023
@bep bep modified the milestones: v0.121.0, v0.122.0 Dec 6, 2023
@jmooring
Copy link
Member Author

Duplicate of #11439

@jmooring jmooring marked this as a duplicate of #11439 Jan 19, 2024
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants