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

Backlinks for footnotes #100

Closed
welpo opened this issue Aug 2, 2023 · 1 comment · Fixed by #101
Closed

Backlinks for footnotes #100

welpo opened this issue Aug 2, 2023 · 1 comment · Fixed by #101
Assignees
Labels
enhancement New feature or request

Comments

@welpo
Copy link
Owner

welpo commented Aug 2, 2023

Footnotes usually have a backlink to the footnote's origin so readers can easily return after reading the note (example).

Zola currently lacks this feature (see getzola/zola#1285 and pulldown-cmark/pulldown-cmark#142).

To add this functionality, I will implement a JavaScript solution that creates backlinks similar to this comment.

It will have the option to be enabled globally in config.toml as well as for individual posts.

@welpo welpo added the enhancement New feature or request label Aug 2, 2023
@welpo welpo self-assigned this Aug 2, 2023
@welpo welpo moved this to 🏗 In progress in tabi roadmap Aug 2, 2023
@welpo welpo closed this as completed in #101 Aug 2, 2023
@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in tabi roadmap Aug 2, 2023
@welpo
Copy link
Owner Author

welpo commented Aug 2, 2023

It's done!

The JavaScript code is designed to create backlinks for footnotes (the 538 byte minified version is the file that's actually loaded).

To enable it, set footnote_backlinks = true either in the [extra] section of either config.toml (globally enabled) or the individual post's front matter.

How it Works

assignReferenceIds()

This function is used to assign unique IDs to all footnote references within the document. It does this by:

  • Selecting all elements with the class .footnote-reference.
  • Iterating over each reference and assigning a unique ID to it. This ID is created by using the hash of the reference's first child, prefixing it with "ref:".

This ensures that each reference in the document has a unique and easily identifiable ID.

createFootnoteBacklinks()

This function takes care of creating backlinks for each footnote definition if a corresponding reference exists in the text. This is done by:

  • Selecting all elements with the class .footnote-definition.
  • Iterating over each footnote and generating a backlink ID by concatenating "ref:" with the footnote's ID.
  • Checking whether a corresponding reference with that ID exists in the document; it's possible for a footnote to not be referenced in the text.
  • If it exists, creating an anchor element (<a>) with an href pointing to the corresponding reference ID and appending a backlink symbol (↩) to the last child element of the footnote.
    This creates a backlink next to each footnote that the reader can click on to navigate back to the corresponding reference in the text.

initFootnotes()

This is the main function that brings everything together by calling both assignReferenceIds() and createFootnoteBacklinks(). It ensures that both functions are executed in order to set up the references and backlinks as described above.

Event Listener

Finally, the code waits for the window to load by attaching an event listener to the load event. Once the window is fully loaded, the initFootnotes function is executed, initializing the footnote handling mechanism.

Styling

I've added some very basic styling to the footnote-backlink class:

tabi/sass/parts/_misc.scss

Lines 141 to 144 in 5cdc18e

.footnote-backlink {
font-size: 0.8rem;
margin-left: 0.2rem;
}

Live Example

I've enabled the feature in the Markdown examples page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant