Getting markdown without paragraph


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)">&#8617;&#65038;</a></p>' | safe }}

where I do the following:

  1. get the markdown
  2. measure its length
  3. reverse it, truncate to remove the <p> and reverse it back
  4. measure the length of the resulting text
  5. 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)">&#8617;&#65038;</a></p>

This is what I would manually do before.
Thanks for any advice.

OK, I feel silly. There might be better ways yet, but looking more closely at the documentation of the Tera templates, I have found trim_start_matches and trim_end_matches which make the code much cleaner.

OK, now I really feel silly. Finally found the inline = true option/argument.

Sorry for the noise. You can probably even delete this discussion all together.

Hi , may I have the chance to clone your footnote shortcode and try it for my blog?

I’m very interested in it!


No problem. Here is the code. First, there is a file named ftnt_refs.html which contains this:

{%- set arr_len = idxs | length -%}
{%- set last_idx = arr_len - 1 -%}
{%- for i in range(end=arr_len) -%}
{%- set idx = idxs | nth( n=i ) -%}
<sup {%- if not no_back_ref %} id="ftntref:{{- idx -}}"{%- endif -%}><a href="#ftnt:{{- idx -}}">{{- idx -}}</a>{%- if i != last_idx -%}, {% endif -%}</sup>
{%- endfor -%}

Using it in your markdown will create a clickable superscript with each of the numbers passed in. The click will take the reader to the item with the id ftnt:i where i is the number passed in. In the markdown file, it is used like so:

some text with a footnote{{ ftnt_refs( idxs=[3]) }}

to create “some text with a footnote3”. Here is an example with more than one index.

some other text with a footnote{{ ftnt_refs( idxs=[1,2]) }}

which will create “some other text with a footnote1, 2”. This will create multiple clickable references. Note that it is not sophisticated enough to have cases with 1 to 3 or 4 to 7 and 9. Nothing fancy I am afraid.

Then, in another file named ftnt.html, the following content is found:

{%- set idx_str = idx | as_str -%}
<p id="ftnt:{{- idx_str -}}"><sup>[{{- idx_str -}}]</sup>&nbsp;{{- body | markdown( inline = true ) | safe -}}
{%- if not no_back_ref -%}
&nbsp;<a href="#ftntref:{{- idx_str -}}">&#8617;&#65038;</a>
{%- endif -%}

which can be used in the following way in the text:

{% ftnt( idx = 3 ) %}
The explanation of the footnote.
{% end %}

This will produce “[3] The explanation of the footnote. :leftwards_arrow_with_hook:︎” where it is used. The arrow will take the user back in the text to a reference ftntref:i where i is the number of the footnote if that reference was produced by the preceding macro.

Only a single number per footnote is supported (because otherwise, it is difficult to know where the back link should point). There is an optional parameter if you want to add a footnote with no reference in the text or if you do not want the back button (a bit weird, but I had that need in one of my posts). In this case, it is used like so:

{% ftnt( idx = 2, no_back_ref = true ) %}
The explanation of footnote number 2, with no back link.
{% end %}

which will generate the text “[2] The explanation of footnote number 2, with no back link.”. You can easily adapt the code in the shortcode files for your needs if you do not like the square brackets for instance.

Hope this helps.

1 Like

Thanks a lot !!!

1 Like

I’ve tried the codes after finishing my works today, it’s amazing, thank you for this!