Skip to content

Commit

Permalink
Add new option, footnote_link_text
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
BryceStevenWilley authored and gettalong committed Nov 16, 2024
1 parent cb6cad7 commit 8c2e435
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/kramdown/converter/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,10 @@ def convert_footnote(el, _indent)
@footnotes << [name, el.value, number, 0]
@footnotes_by_name[name] = @footnotes.last
end
formatted_link_text = sprintf(@options[:footnote_link_text], number)
"<sup id=\"fnref:#{name}#{repeat}\">" \
"<a href=\"#fn:#{name}\" class=\"footnote\" rel=\"footnote\" role=\"doc-noteref\">" \
"#{number}</a></sup>"
"#{formatted_link_text}</a></sup>"
end

def convert_raw(el, _indent)
Expand Down
18 changes: 18 additions & 0 deletions lib/kramdown/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,24 @@ def self.simple_hash_validator(val, name)
Used by: HTML
EOF

define(:footnote_link_text, String, '%s', <<~EOF) do |val|
The text used for the footnote number in a footnote link
This option can be used to add additional text to the footnote
link. It should be a format string, and is passed the footnote
number as the only argument to the format string.
e.g. "[footnote %s]" would display as "[footnote 1]".
Default: '%s'
Used by: HTML
EOF
if !val.include?('%s')
raise Kramdown::Error, "option footnote_link_text needs to contain a '%s'"
end
val
end


define(:remove_line_breaks_for_cjk, Boolean, false, <<~EOF)
Specifies whether line breaks should be removed between CJK characters
Expand Down
12 changes: 12 additions & 0 deletions test/testcases/span/04_footnote/footnote_link_text.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<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>

<div class="footnotes" role="doc-endnotes">
<ol>
<li id="fn:ab" role="doc-endnote">
<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>
</li>
<li id="fn:bc" role="doc-endnote">
<p>Some other text. <a href="#fnref:bc" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
</li>
</ol>
</div>
1 change: 1 addition & 0 deletions test/testcases/span/04_footnote/footnote_link_text.options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:footnote_link_text: "[footnote %s]"
4 changes: 4 additions & 0 deletions test/testcases/span/04_footnote/footnote_link_text.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is a[^ab] footnote[^ab]. And another[^bc].

[^ab]: Some text.
[^bc]: Some other text.

0 comments on commit 8c2e435

Please sign in to comment.