zola icon indicating copy to clipboard operation
zola copied to clipboard

Markdown footnotes enhancement

Open digitalresistor opened this issue 4 years ago • 13 comments

Enhancement

Environment

Zola version: 0.12.2 compiled from git

Have footnotes link back to where they were footnoted

It would be great if there was a way to have a link-back to the location of the footnote. So if the site visitor is reading an article, they can click the footnote and then click the little return icon to go back to where the footnote was declared.

digitalresistor avatar Jan 02 '21 08:01 digitalresistor

If that's implemented, that should be a site-wide option in the config.toml

Keats avatar Jan 02 '21 08:01 Keats

Looks like this is something that others asked about here: https://github.com/raphlinus/pulldown-cmark/issues/142

Would you like me to leave this issue open?

digitalresistor avatar Jan 02 '21 08:01 digitalresistor

Let's leave it open for now and see if there is progress on the pulldown-cmark issue

Keats avatar Jan 02 '21 08:01 Keats

Related #1070

shumvgolove avatar Jan 03 '21 11:01 shumvgolove

In the meantime, I implemented this on my side with some JavaScript:

<script type="text/javascript">
    window.addEventListener('load', function() {
        for (const ref of document.getElementsByClassName('footnote-reference')) {
            const hash = ref.children[0].hash.substring(1);
            const refhash = 'ref:' + hash;
            ref.id = refhash;
        }

        for (const footnote of document.getElementsByClassName('footnote-definition')) {
            const hash = footnote.id;
            const refhash = 'ref:' + hash;
            const backlink = document.createElement('a');
            backlink.href = '#' + refhash;
            backlink.className = 'footnote-backlink';
            backlink.innerText = '↩';
            const paras = footnote.children;
            const lastPara = paras[paras.length - 1];
            lastPara.appendChild(backlink);
        }
    });
</script>

meithecatte avatar Jun 29 '21 22:06 meithecatte

@meithecatte Thanks for the script, it's very helpful!

For information, I've replaced the line with the link back to the reference as below to avoid confusion when there are several references to the same footnote (the arrow only sends back to the 1st reference).

        backlink.href = 'javascript:if (window.location.href.endsWith("#' + hash + '")) history.back()';

That way, the arrow will send back to where the reader came from, and only if it comes from that reference. The drawback is that it's not possible to tell where the footnotes are referenced by scrolling down and using the added arrows, but I don't think people often do that.

Another solution would be to add several arrows but I find this confusing.

The whole script becomes (with an option in comment):

<script type="text/javascript">
    window.addEventListener('load', function () {
        for (const ref of document.getElementsByClassName('footnote-reference')) {
            const hash = ref.children[0].hash.substring(1);
            const refhash = 'ref:' + hash;
            ref.id = refhash;
        }
    
        for (const footnote of document.getElementsByClassName('footnote-definition')) {
            const hash = footnote.id;
            const refhash = 'ref:' + hash;
            const backlink = document.createElement('a');
            backlink.href = 'javascript:if (window.location.href.endsWith("#' + hash + '")) history.back()';
            // To get back by clicking any arrow, use this instead:
            // backlink.href = 'javascript:if (window.location.href.search("#") >= 0) history.back()';
            backlink.className = 'footnote-backlink';
            backlink.innerText = '↩';
            const paras = footnote.children;
            const lastPara = paras[paras.length - 1];
            lastPara.appendChild(backlink);
        }
    });
</script>

blueglyph avatar May 05 '23 14:05 blueglyph

I believe this is now unblocked after https://github.com/raphlinus/pulldown-cmark/pull/654 got merged, example: https://github.com/raphlinus/pulldown-cmark/blob/master/examples/footnote-rewrite.rs

si14 avatar Sep 11 '23 17:09 si14

This issue would be closed by #2326.

DerDrodt avatar Oct 11 '23 12:10 DerDrodt

Can people try https://github.com/getzola/zola/pull/2480 ?

Keats avatar May 03 '24 18:05 Keats

Works great for me, thanks!

izissise avatar May 12 '24 14:05 izissise

The PR is exactly what I was looking for, thank you!

What do you think of giving backref links a class, to help style them?

sorcio avatar May 12 '24 17:05 sorcio

What do you think of giving backref links a class, to help style them?

I've considered it, but it sounds like a separate big feature, that needs to be designed in the first place.

If this feature is desired, let's create a separate issue and discuss it.

totikom avatar May 12 '24 18:05 totikom

Oh, I didn't have any specific wish other than a named CSS class in the new rendered html. I worked around the need with a .footnotes-list li p:last-child a:last-child shenanigan which works fine for me because my content only references each note once, but does not work in the general case. Does this deserve a new issue?

Another less-than-ideal thing: the bottom_footnotes = true also variant adds square brackets around the number in footnote references, like GitHub does, but this is not implied by the name nor the documentation. Is this intentional?

sorcio avatar May 13 '24 11:05 sorcio