gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
75.11k stars 7.48k forks source link

Add a way to identify a non-Page HTML etc. resource #12274

Open jmooring opened 6 months ago

jmooring commented 6 months ago

Reference: https://discourse.gohugo.io/t/shortcode-for-iframe-breaking-at-update-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:

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:

{{ ((.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 }}
bep commented 6 months ago
jmooring commented 6 months ago

Understood. Is there something cleaner than this to just dump the file (and return Permalink/RelPermalink)?

{{ ((.Resources.Get "a.html").Content | resources.FromString "/p1/a.html").RelPermalink }}

Or this?

{{ with .Resources.Get "a.html" }}
  {{ (.Content | resources.FromString .Path).RelPermalink }}
{{ end }}
bep commented 6 months ago

Is there something cleaner than this to just dump the file (and return Permalink/RelPermalink)?

... copy it into ... static?

jmooring commented 6 months ago

OP's use case needs to organize these with the page. I'm recommending second example above, or renaming the resource (e.g., a.txt instead of a.htm).

https://discourse.gohugo.io/t/shortcode-for-iframe-breaking-at-update-hugo-122-x-123-8/48904/9?u=jmooring

bep commented 6 months ago

I have reopened and relabeled this ... I don't know how to practically do this, but it would be a good idea if we had a way to say that a particular bundled (e.g. HTML) file should be handled as a regular simple resource with a mime type (as in: not a Page).

jmooring commented 6 months ago

Related to https://github.com/gohugoio/hugo/issues/9197.