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

For footnotes linked via an id, use id when generating the hash #50

Open
LeaVerou opened this issue Jan 10, 2024 · 8 comments
Open

For footnotes linked via an id, use id when generating the hash #50

LeaVerou opened this issue Jan 10, 2024 · 8 comments

Comments

@LeaVerou
Copy link

Thanks for making this wonderful plugin!

I’ve been using this for a while, and this is the only place where I keep feeling there is room for improvement.
The syntax allows for specifying footnotes either with an id, or with a number:

Here is a footnote reference,[^1] and another.[^longnote]

[^1]: Here is the footnote.

[^longnote]: Here's one with multiple blocks.

However, when markdown is generated, links to footnotes still look like #fn1. This has the usual ordered reference problem: if more footnotes are added above a given footnote, its link changes. If it didn't work that way, using ids could have been a nice way to ensure that Cool URIs don’t change. I understand this may not always be desirable, but it could be a sensible default with an option to override it if the current behavior is desired.

@rlidwka
Copy link
Member

rlidwka commented Jan 10, 2024

This has the usual ordered reference problem: if more footnotes are added above a given footnote, its link changes.

Can you show a markdown example where that link changes?

@LeaVerou
Copy link
Author

This has the usual ordered reference problem: if more footnotes are added above a given footnote, its link changes.

Can you show a markdown example where that link changes?

I think I may not have explained this clearly enough. I was referring to the link changing as the content is edited (e.g. adding a paragraph with a footnote above another paragraph with a footnote). This is not something that can be "shown" as an example.

@beep
Copy link

beep commented Jul 16, 2024

Just want to add a belated +1 to this issue, as I just noticed that my footnote ids were getting rewritten by the plugin. If it’d be possible to preserve the ids as written, that’d be remarkable.

@geoffkendall
Copy link

Plus one for that! :-) I have two different markdown sections on a single web page, and the problem at the moment is that footnotes in both sections have the same id's (e.g. #fn1), and so when the user clicks on the link in the second (lower down) section, it jumps the browser back up to the first. Ideally I need to be able to set the id's manually, or provide a prefix for each markdown section, so that they use different anchors. Other than this the plugin is AWESOME, thank you!

@puzrin
Copy link
Member

puzrin commented Sep 27, 2024

Just want to add a belated +1 to this issue, as I just noticed that my footnote ids were getting rewritten by the plugin. If it’d be possible to preserve the ids as written, that’d be remarkable.

See this:

function render_footnote_anchor_name (tokens, idx, options, env/*, slf */) {
const n = Number(tokens[idx].meta.id + 1).toString()
let prefix = ''
if (typeof env.docId === 'string') prefix = `-${env.docId}-`
return prefix + n
}

You can customize anchor name generation to use ref content, for example. It will stay stable until ref text unchanged.

We probably used the most simple approach by default because the look of stable links is opinion-based and "not nice."

Maybe we should add a note with an example to the readme for this case.

@puzrin
Copy link
Member

puzrin commented Sep 27, 2024

I have two different markdown sections on a single web page, and the problem at the moment is that footnotes in both sections have the same id's (e.g. #fn1), and so when the user clicks on the link in the second (lower down) section, it jumps the browser back up to the first. Ideally I need to be able to set the id's manually, or provide a prefix for each markdown section, so that they use different anchors.

I think your case is not related to this issue. If you have multiple documents on the page, references should have unique prefixes for each document to avoid collisions.

See this line

if (typeof env.docId === 'string') prefix = `-${env.docId}-`

Add some docId to env (second render param), and it will be added to your anchors. Of course, you must guarantee docId uniqueness for each doc.

@geoffkendall
Copy link

I think your case is not related to this issue. If you have multiple documents on the page, references should have unique
prefixes for each document to avoid collisions.

That's awesome, thank you @puzrin! Works like a dream :-)

@beep
Copy link

beep commented Sep 28, 2024

You can customize anchor name generation to use ref content, for example. It will stay stable until ref text unchanged.

That’s amazing — thank you so much for the help, @puzrin! tokens[idx].meta.label seems to be exactly what I needed.

Maybe we should add a note with an example to the readme for this case.

That would get an enthusiastic 👍🏻 from me, personally.

Thank you again.

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

No branches or pull requests

5 participants