markdown-it-footnote icon indicating copy to clipboard operation
markdown-it-footnote copied to clipboard

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

Open LeaVerou opened this issue 2 years ago • 8 comments

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.

LeaVerou avatar Jan 10 '24 14:01 LeaVerou

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?

rlidwka avatar Jan 10 '24 14:01 rlidwka

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.

LeaVerou avatar Jan 10 '24 15:01 LeaVerou

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.

beep avatar Jul 16 '24 00:07 beep

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!

geoffkendall avatar Sep 26 '24 21:09 geoffkendall

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:

https://github.com/markdown-it/markdown-it-footnote/blob/fe6c169c72b9f4d6656b10aa449128456f5a990e/index.mjs#L8-L15

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 avatar Sep 27 '24 16:09 puzrin

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

https://github.com/markdown-it/markdown-it-footnote/blob/fe6c169c72b9f4d6656b10aa449128456f5a990e/index.mjs#L12

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.

puzrin avatar Sep 27 '24 16:09 puzrin

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 :-)

geoffkendall avatar Sep 28 '24 10:09 geoffkendall

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.

beep avatar Sep 28 '24 21:09 beep