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 pageresize_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:
- Add
page.relative_dirthat would point to post directory (similar topage.relative_path) - Add
dirname,basename,file_pathfilters 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
- imageprog: Also test usage with a date-prefixed page #788 — committed to vojtechkral/zola by vojtechkral 3 years ago
Zola
nextversion gets acolocated_pathpointing 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:
That’s what I ended up doing:
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 assetspagedirectory incontentdirectory treeBoth 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 withYYYY-MM-DDdate prefix.I’m not sure it should be in this issue or if it warrants separate issue though.