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

Unique IDs for footnotes with the same names #483

Closed
pjpoole opened this Issue Jan 2, 2018 · 5 comments

Comments

Projects
None yet
2 participants
@pjpoole
Copy link

pjpoole commented Jan 2, 2018

This author highlighted a problem I am currently having: #56

His answer was different than mine. I prefer to use numbers, and even if I do use custom footnote names, I don't trust myself to never re-use a single name for multiple footnotes that may show up in the same view.

It looks like one solution would be to keep track of a global context and manually advance the starting footnote number as I render posts in a blog index page, but I don't see a way to query a Kramdown document to ask how many footnotes it sees. (i.e., something like:

total_footnotes = 0

documents.each do |document|
  rendered = Kramdown::Document.new(document, { footnote_nr: total_footnotes + 1 })
  total_footnotes += renderered.footnote_count
end

but an easier solution would be the option to add in some sort of nonce into each footnote reference so that the ids would be like fn:1-adf123.

@pjpoole

This comment has been minimized.

Copy link
Author

pjpoole commented Jan 2, 2018

A third option I can think of is to offer a switch for removing footnotes completely. It looks like someone already did this using hand-rolled nokogiri to remove footnotes from excerpted posts, which is similar to what I'm trying to do.

@gettalong gettalong self-assigned this Jan 5, 2018

@gettalong

This comment has been minimized.

Copy link
Owner

gettalong commented Jan 5, 2018

Thank you for pointing out this problem!

I can imagine a solution similar to what is done with the auto_id_prefix option, i.e. a new option called footnote_prefix that gets used on conversion. Would this solve your problem?

@pjpoole

This comment has been minimized.

Copy link
Author

pjpoole commented Jan 5, 2018

It certainly would!

@gettalong

This comment has been minimized.

Copy link
Owner

gettalong commented Jun 24, 2018

@pjpoole This will be implemented for the next release.

@gettalong

This comment has been minimized.

Copy link
Owner

gettalong commented Jan 20, 2019

@pjpoole This will be in the release coming out today. I.e. there is a new option footnote_prefix that allows you to avoid duplicate IDs.

Additionally, I have added the footnote count of the document to the options of the root element.

Together with the option footnote_nr it is now possible to output multiple documents together with consecutively number footnotes and unique IDs:

require 'kramdown/document'
total_footnotes = 0
documents = [
  "footnote[^a][^a]\n\n[^a]: My note\n[^b]: another (ignored) note",
  "footnote[^a][^a]\n\n[^a]: My note\n[^b]: another (ignored) note",
  "footnote[^a][^a]\n\n[^a]: My note\n[^b]: another (ignored) note",
]

documents.each do |document|
  document = Kramdown::Document.new(document, {footnote_nr: total_footnotes + 1, footnote_prefix: total_footnotes + 1})
  puts document.to_html
  total_footnotes += document.root.options[:footnote_count]
end

results in:

<p>footnote<sup id="fnref:1a"><a href="#fn:1a" class="footnote">1</a></sup><sup id="fnref:1a:1"><a href="#fn:1a" class="footnote">1</a></sup></p>

<div class="footnotes">
  <ol>
    <li id="fn:1a">
      <p>My note <a href="#fnref:1a" class="reversefootnote">&#8617;</a> <a href="#fnref:1a:1" class="reversefootnote">&#8617;<sup>2</sup></a></p>
    </li>
  </ol>
</div>
<p>footnote<sup id="fnref:2a"><a href="#fn:2a" class="footnote">2</a></sup><sup id="fnref:2a:1"><a href="#fn:2a" class="footnote">2</a></sup></p>

<div class="footnotes">
  <ol start="2">
    <li id="fn:2a">
      <p>My note <a href="#fnref:2a" class="reversefootnote">&#8617;</a> <a href="#fnref:2a:1" class="reversefootnote">&#8617;<sup>2</sup></a></p>
    </li>
  </ol>
</div>
<p>footnote<sup id="fnref:3a"><a href="#fn:3a" class="footnote">3</a></sup><sup id="fnref:3a:1"><a href="#fn:3a" class="footnote">3</a></sup></p>

<div class="footnotes">
  <ol start="3">
    <li id="fn:3a">
      <p>My note <a href="#fnref:3a" class="reversefootnote">&#8617;</a> <a href="#fnref:3a:1" class="reversefootnote">&#8617;<sup>2</sup></a></p>
    </li>
  </ol>
</div>

@gettalong gettalong closed this Jan 20, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment