getzola / zola

A fast static site generator in a single binary with everything built-in. https://www.getzola.org
https://www.getzola.org
MIT License
13.39k stars 937 forks source link

Undocumented or Faulty: linking colocated assets on a slug page through template #2598

Open Yamavu opened 1 month ago

Yamavu commented 1 month ago

Documentation issue

Summary

Listing all colocated assets of a page in a template page.html . The content index.md uses a slug

(tested on versions 0.18.0 and 0.19.1)

Files linked in markdown are unaffected.

file setup:

_index.md
page/
  image1.jpg
  image2.jpg
  index.jpg

From the documentation:

<div>
{% for asset in page.assets -%}
  {%- if asset is matching("[.](jpg|png)$") -%}
    {% set image = resize_image(path=asset, width=240, height=180) %}
    <a href="{{ get_url(path=asset) }}" target="_blank">
      <img src="{{ image.url }}" />
    </a>
  {%- endif %}
{%- endfor %}
</div>

If the page also uses a custom slug variable, {{ get_url(path=asset) }} doesn't provide this correctly,

Zola will only display the right output when the link is written like:

{{page.permalink | safe}}{{asset | split(pat='/') | last  | safe }}

Maybe I overlooked something, but linking all co-located assets seems to be quite essential for building almost any kind of art or photo gallery.

Proposed solution

Either current documentation should be expanded or get_url() should include this functionality.

Raymi306 commented 1 month ago

I just ran into this as well.

Yamavu commented 1 month ago

as far as I understood, _get_url ( path, lang="en", trailingslash=false, cachebust=false ) is also used internally which is why is simple and optimized.

the function currently supposed to only work with MD files, not the page assets (even though the documentation suggests that).

The function has a fallback option to also link to the static directory, and happens to work with un-sluggified content nodes too. It feels pretty alpha to me

Keats commented 4 weeks ago

Basically it's for 2 things:

  1. if it starts with @/, resolve the internal link
  2. otherwise just make a permalink with what we've been given without checking anything

To handle 2 for assets with renamed slugs, it would require detecting it's a colocated asset, load the related page and generate the permalink from there (what you need to load exactly in the GetUrl fn is TBD, you don't want to add the whole site). If someone wants to do a PR for that I would take it.