Skip to content

Commit 8c2e435

Browse files
BryceStevenWilleygettalong
authored andcommitted
Add new option, footnote_link_text
`footnote_link_text` is a string that is directly inserted in front of the footnote number. It can also be a formatted string that the number is formatted into. For example, if the string is "[footnote %s]", the footnote link will be "[footnote 1]". It will determine it the option is a forat string by running it through `sprintf`; if the output is the same, then it's not a format string. Also adds tests to make sure of the new output.
1 parent cb6cad7 commit 8c2e435

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

lib/kramdown/converter/html.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,10 @@ def convert_footnote(el, _indent)
314314
@footnotes << [name, el.value, number, 0]
315315
@footnotes_by_name[name] = @footnotes.last
316316
end
317+
formatted_link_text = sprintf(@options[:footnote_link_text], number)
317318
"<sup id=\"fnref:#{name}#{repeat}\">" \
318319
"<a href=\"#fn:#{name}\" class=\"footnote\" rel=\"footnote\" role=\"doc-noteref\">" \
319-
"#{number}</a></sup>"
320+
"#{formatted_link_text}</a></sup>"
320321
end
321322

322323
def convert_raw(el, _indent)

lib/kramdown/options.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,24 @@ def self.simple_hash_validator(val, name)
596596
Used by: HTML
597597
EOF
598598

599+
define(:footnote_link_text, String, '%s', <<~EOF) do |val|
600+
The text used for the footnote number in a footnote link
601+
602+
This option can be used to add additional text to the footnote
603+
link. It should be a format string, and is passed the footnote
604+
number as the only argument to the format string.
605+
e.g. "[footnote %s]" would display as "[footnote 1]".
606+
607+
Default: '%s'
608+
Used by: HTML
609+
EOF
610+
if !val.include?('%s')
611+
raise Kramdown::Error, "option footnote_link_text needs to contain a '%s'"
612+
end
613+
val
614+
end
615+
616+
599617
define(:remove_line_breaks_for_cjk, Boolean, false, <<~EOF)
600618
Specifies whether line breaks should be removed between CJK characters
601619
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<p>This is a<sup id="fnref:ab"><a href="#fn:ab" class="footnote" rel="footnote" role="doc-noteref">[footnote 1]</a></sup> footnote<sup id="fnref:ab:1"><a href="#fn:ab" class="footnote" rel="footnote" role="doc-noteref">[footnote 1]</a></sup>. And another<sup id="fnref:bc"><a href="#fn:bc" class="footnote" rel="footnote" role="doc-noteref">[footnote 2]</a></sup>.</p>
2+
3+
<div class="footnotes" role="doc-endnotes">
4+
<ol>
5+
<li id="fn:ab" role="doc-endnote">
6+
<p>Some text. <a href="#fnref:ab" class="reversefootnote" role="doc-backlink">&#8617;</a> <a href="#fnref:ab:1" class="reversefootnote" role="doc-backlink">&#8617;<sup>2</sup></a></p>
7+
</li>
8+
<li id="fn:bc" role="doc-endnote">
9+
<p>Some other text. <a href="#fnref:bc" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
10+
</li>
11+
</ol>
12+
</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:footnote_link_text: "[footnote %s]"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This is a[^ab] footnote[^ab]. And another[^bc].
2+
3+
[^ab]: Some text.
4+
[^bc]: Some other text.

0 commit comments

Comments
 (0)