This commit is contained in:
Jade Lovelace 2022-03-14 15:14:47 -07:00
parent c8a2913172
commit 5a1c98d8a4
8 changed files with 170 additions and 0 deletions

View file

@ -0,0 +1,36 @@
{%- import "macros/colocated_asset.html" as colocated_asset -%}
<!-- Load a file and dump it in a code block. -->
{%- macro file(path, code_lang=false, colocated=false,
hide=false, show_path_with_prefix=false) -%}
{%- set newline = "
" -%}
{%- set mypath = path -%}
{%- if show_path_with_prefix == false -%}
{%- set header = "" -%}
{%- else -%}
{%- set header = show_path_with_prefix ~ " " ~ path ~ newline -%}
{%- endif -%}
{%- if colocated == true -%}
{%- set path = colocated_asset::colocated_asset(path=path) | trim -%}
{%- endif -%}
{%- if code_lang == true -%}
{%- set code_lang = '' -%}
{%- endif -%}
{%- set data = load_data(path=path, format="plain") -%}
{%- set source = "```" ~ code_lang ~ newline ~ header ~ data ~ newline ~ "```" | safe -%}
{%- if hide == true -%}
<details>
<summary>
<code>{{ mypath }}</code>
</summary>
{%- endif -%}
{{ source | markdown(inline=true) | safe }}
{%- if hide == true -%}
</details>
{%- endif -%}
{%- endmacro file -%}

View file

@ -0,0 +1,10 @@
<!--
Returns the file path of the colocated asset.
When Zola uses `resize_image` it looks relative to the `content` folder.
This means you have to reference the full page asset colocation path.
-->
{%- macro colocated_asset(path) -%}
{%- set page_url_components = page.relative_path | default(value=section.relative_path) | split(pat='/') -%}
{%- set page_base = page_url_components | slice(end=page_url_components | length - 1) | join(sep='/') -%}
{{ page_base ~ '/' ~ path }}
{%- endmacro colocated_asset -%}

View file

@ -0,0 +1,9 @@
{%- import "macros/code.html" as code -%}
{{ code::file(
path=path,
code_lang=code_lang | default(value=''),
colocated=colocated | default(value=false),
hide=hide | default(value=false),
show_path_with_prefix=show_path_with_prefix | default(value=false)
) }}