Hello.
I am trying to get some custom footnote code going. Basically, I want to add the index and a back button at the end of the text automatically (I also have a footnote reference shortcode to “cite” in the text).
I got it to work, but the code seems like an ugly hack and I was wondering if anyone had ideas on how to make it simpler.
Here is what I came up with:
{%- set idx_str = idx | as_str -%}
{%- set orig_txt = body | markdown -%}
{%- set orig_txt_len = orig_txt | length -%}
{%- set first_pass_txt = orig_txt | reverse | truncate( length = orig_txt_len - 3, end="" ) | reverse -%}
{%- set first_pass_txt_len = first_pass_txt | length -%}
{%- set txt = first_pass_txt | truncate( length = first_pass_txt_len - 5, end="" ) -%}
{{- '<p id="ftnt' ~ idx_str ~ '"><sup>[' ~ idx_str ~ "]</sup> " ~ txt ~ ' <a href="#" onclick="history.go(-1)">↩︎</a></p>' | safe }}
where I do the following:
- get the markdown
- measure its length
- reverse it, truncate to remove the
<p>and reverse it back - measure the length of the resulting text
- truncate it to remove the closing
</p>
This works and allows me to put the whole thing back in a single <p> element with the proper id.
I have not found another way to get the markdown without the surrounding <p></p> pair. The documentation says to put it in a <div>, but I can’t have a <div> in a <p> and I want to insert the id inside the <p>. Maybe I am thinking about this all wrong. Anyhow, here is an example of the result I am looking for.
<p id="ftnt1"><sup>[1]</sup> I usually target C++11 still. The code can be made slightly simpler and shorter with later versions of C++. <a href="#" onclick="history.go(-1)">↩︎</a></p>
This is what I would manually do before.
Thanks for any advice.