Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown footnotes enhancement #1285

Closed
digitalresistor opened this issue Jan 2, 2021 · 13 comments
Closed

Markdown footnotes enhancement #1285

digitalresistor opened this issue Jan 2, 2021 · 13 comments

Comments

@digitalresistor
Copy link
Contributor

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.

@Keats
Copy link
Collaborator

Keats commented Jan 2, 2021

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

@digitalresistor
Copy link
Contributor Author

Looks like this is something that others asked about here: pulldown-cmark/pulldown-cmark#142

Would you like me to leave this issue open?

@Keats
Copy link
Collaborator

Keats commented Jan 2, 2021

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

@shumvgolove
Copy link

Related #1070

@meithecatte
Copy link
Contributor

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>

wjwat added a commit to wjwat/lhtlhtp.com that referenced this issue Mar 26, 2022
* Added JavaScript written by @NieDzejkob from Zola issue
  getzola/zola/issues/1285
* Changed styling for footnotes
* Updated index template to include site.js
wantguns added a commit to wantguns/wantguns.dev that referenced this issue Apr 13, 2022
copypasta from github.com/getzola/zola/issues/1285

Signed-off-by: Gunwant Jain <mail@wantguns.dev>
LunNova added a commit to LunNova/lunar-zen that referenced this issue May 27, 2022
@blueglyph
Copy link

@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>

@si14
Copy link

si14 commented Sep 11, 2023

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

@DerDrodt
Copy link

This issue would be closed by #2326.

@Keats
Copy link
Collaborator

Keats commented May 3, 2024

Can people try #2480 ?

Keats pushed a commit that referenced this issue May 9, 2024
* Implemented bottom footnotes with backreferences

Fixes #1285

* Added bottom_footnotes option to configuration.md

* Renamed fix_github_style_footnotes()

* Added tests for convert_footnotes_to_github_style()

* Changed test to plain html instead of Vec<Event>

* Added integration test for footnotes

* Applied suggested changes
@izissise
Copy link

Works great for me, thanks!

@sorcio
Copy link

sorcio commented May 12, 2024

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?

@totikom
Copy link
Contributor

totikom commented May 12, 2024

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.

@sorcio
Copy link

sorcio commented May 13, 2024

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?

veluca93 pushed a commit to veluca93/zola that referenced this issue May 14, 2024
* Implemented bottom footnotes with backreferences

Fixes getzola#1285

* Added bottom_footnotes option to configuration.md

* Renamed fix_github_style_footnotes()

* Added tests for convert_footnotes_to_github_style()

* Changed test to plain html instead of Vec<Event>

* Added integration test for footnotes

* Applied suggested changes
@Keats Keats closed this as completed in 6a2b890 Jun 20, 2024
berdandy pushed a commit to berdandy/azola that referenced this issue Sep 17, 2024
* Implemented bottom footnotes with backreferences

Fixes getzola#1285

* Added bottom_footnotes option to configuration.md

* Renamed fix_github_style_footnotes()

* Added tests for convert_footnotes_to_github_style()

* Changed test to plain html instead of Vec<Event>

* Added integration test for footnotes

* Applied suggested changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants