zola: Can't get directory path to collocated assets when using YYYY-MM-DD_ page prefix

Bug Report

Environment

Zola version: 0.8.0 Features used:

  • shortcode
  • collocated assets for page
  • YYYY-MM-DD_ prefix for page
  • resize_image() function

Setup

Directory structure like this:

content/section/2000-01-01_page/index.md
content/section/2000-01-01_page/image1.jpg
content/section/2000-01-01_page/image2.jpg

Attempt to create shortcode that would convert this (in index.md):

{{ render_image(path="image1.jpg", width=100, height=200) }}

Into this:

<div class="image">
<a href="/section/page/image1.jpg"><img src="static/processed_images/foo.jpg"></a>
</div>

Expected Behavior

I’ve expected that when I pass relative asset name to shortcode, I can use page from Tera context to compute both:

  • path to image source to pass into resize_image()
  • path to deployed image to pass into link

Current Behavior

The page.path points to path used in public directory (section/page/ - ie without prefix). It can’t be used to construct path for asset source location needed by resize_image().

The page.relative_path points section/2000-01-01_page/index.md file in correct - directory. But points to file not directory and can’t be used for computing source path for render_image()

Note: May be it could be use by sequence of split, remove last element, join, … operations, but I would not consider that very user friendly, especially for beginners 😃

Proposed solution

I can see two solutions:

  1. Add page.relative_dir that would point to post directory (similar to page.relative_path)
  2. Add dirname, basename, file_path filters to Tera to allow dissection and concatenation of paths

Personally, I find the second approach more desirable as it would benefit to Tera too and would be usable in wider range of applications, instead of fixing just one corner-case

Workaround

I’ve resorted into passing directory and file into shortcode separately:

{{ render_image(dir="section/2000-01-01_page", "file="image1.jpg", width=100, height=200) }}

And shortcode:

<div>
    {% set uri  = page.permalink ~ "/" ~ file %}
    {% set path = dir ~ "/" ~ file %}
    <a href="{{ uri }}"><img src="{{ resize_image(path=path, ....) }}" /></a>
</div>

I don’t like it much, because it forces me to hard-code page location in repository into index.md, which imho, negates some of the benefits of asset collocation - it will be harder to move pages around without extensive editing and breaking stuff.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 3
  • Comments: 19 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Zola next version gets a colocated_path pointing to the folder on disk when a page is colocated, no need to split/slice anymore.

Ran into this today, the cleanest possible workaround I find:

{% set path = page.relative_path | split(pat="/") | slice(end=-1) | join(sep="/") %}
{% set thumbnail = resize_image(path=path ~ "/" ~ src, width=size, op="fit_width", format="jpg") %}

Note: May be it could be use by sequence of split, remove last element, join, … operations, but I would not consider that very user friendly, especially for beginners 😃

That’s what I ended up doing:

{% if page %}
  {% set relative_path = page.relative_path | split(pat="/") -%}
{% else %}
  {% set relative_path = "./" ~ section.relative_path | split(pat="/") -%}
{% endif %}
{% set relative_path = relative_path | slice(end=relative_path | length - 1) | join(sep="/") -%}
{% set relative_path = relative_path ~ "/" ~ src -%}
{% set src = resize_image(path=relative_path, width=resize_w, quality=resize_q, op="fit_width") -%}

It works, but is certainly not an elegant solution.

Here is a site for issue reproduction. Writing that up together, I realized that I’ve actually run into 2 separate issues:

  • get_url() does not work for page assets
  • it is not possible to get path to page directory in content directory tree

Both are (hopefully) explained in the test site itself. If there are any questions or need for clarification, let me know and I will do my best.

issue_reproduction_site.zip

I’m afraid it is not open-source site. But I will create minimal reproduction site to attach to the issue. Hopefully I will some time to do that tomorrow.

I’ve played with zola bit more yesterday and attempted to use gallery rendering snippet. The <a href={{ get_url(asset) }}>...</a> generates invalid links when assets are coming from page with YYYY-MM-DD date prefix.

I’m not sure it should be in this issue or if it warrants separate issue though.