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

Add new option, footnote_link_title #825

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions lib/kramdown/converter/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,18 @@ def convert_footnote(el, _indent)
@footnotes_by_name[name] = @footnotes.last
end
formatted_link_text = sprintf(@options[:footnote_link_text], number)
formatted_link_title = sprintf(@options[:footnote_link_title], number)

link_attr = {
href: "#fn:#{name}",
class: "footnote",
rel: "footnote",
role: "doc-noteref"
}
link_attr['title'] = formatted_link_title unless formatted_link_title.empty?

"<sup id=\"fnref:#{name}#{repeat}\">" \
"<a href=\"#fn:#{name}\" class=\"footnote\" rel=\"footnote\" role=\"doc-noteref\">" \
"<a#{html_attributes(link_attr)}>" \
"#{formatted_link_text}</a></sup>"
end

Expand Down Expand Up @@ -490,7 +500,7 @@ def obfuscate(text)
result
end

FOOTNOTE_BACKLINK_FMT = "%s<a href=\"#fnref:%s\" class=\"reversefootnote\" role=\"doc-backlink\">%s</a>"
FOOTNOTE_BACKLINK_FMT = "%s<a href=\"#fnref:%s\" class=\"reversefootnote\" role=\"doc-backlink\"%s>%s</a>"

# Return an HTML ordered list with the footnote content for the used footnotes.
def footnote_content
Expand Down Expand Up @@ -520,10 +530,20 @@ def footnote_content

unless @options[:footnote_backlink].empty?
nbsp = entity_to_str(ENTITY_NBSP)
value = sprintf(FOOTNOTE_BACKLINK_FMT, (insert_space ? nbsp : ''), name, backlink_text)
number = @footnotes_by_name[name][2]
backlink_title_attr = @options[:footnote_backlink_title].empty? ? '' : " title=\"#{escape_html(sprintf(@options[:footnote_backlink_title], number))}\""
value = sprintf(FOOTNOTE_BACKLINK_FMT,
(insert_space ? nbsp : ''),
name,
backlink_title_attr,
backlink_text)
para.children << Element.new(:raw, value)
(1..repeat).each do |index|
value = sprintf(FOOTNOTE_BACKLINK_FMT, nbsp, "#{name}:#{index}",
backlink_title_attr = @options[:footnote_backlink_title].empty? ? '' : " title=\"#{escape_html(sprintf(@options[:footnote_backlink_title], number))}\""
value = sprintf(FOOTNOTE_BACKLINK_FMT,
nbsp,
"#{name}:#{index}",
backlink_title_attr,
"#{backlink_text}<sup>#{index + 1}</sup>")
para.children << Element.new(:raw, value)
end
Expand Down
32 changes: 32 additions & 0 deletions lib/kramdown/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,38 @@ def self.simple_hash_validator(val, name)
val
end

define(:footnote_link_title, String, '', <<~EOF) do |val|
The tooltip on the footnote link

This option can be used to add the title attribute on footnote
links. It should be a format string, and is passed the footnote
number as the only argument to the format string.
e.g. "Jump to footnote %s" would display as "Jump to footnote 1".

Tooltips must be plain text. Any special HTML characters will be escaped.

Default: ''
Used by: HTML
EOF
if !val.empty? && !val.include?('%s')
raise Kramdown::Error, "option footnote_link_title needs to contain a '%s'"
end
val
end

define(:footnote_backlink_title, String, '', <<~EOF)
The tooltip on the footnote backlink

This option can be used to change the title attribute on footnote
backlinks. If set to a format string, the footnote number is available as
first and only argument to the format string.
e.g. "Jump back to [%s] in the text" would display as "Jump back to [1] in the text".

Tooltips must be plain text. Any special HTML characters will be escaped.

Default: ''
Used by: HTML
EOF

define(:remove_line_breaks_for_cjk, Boolean, false, <<~EOF)
Specifies whether line breaks should be removed between CJK characters
Expand Down
1 change: 1 addition & 0 deletions test/test_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ def tidy_output(out)
'test/testcases/block/09_html/standalone_image_in_div.html', # bc of standalone image
'test/testcases/block/09_html/processing_instruction.html', # bc of PI
'test/testcases/block/04_header/with_header_links.html', # bc of header_links option
'test/testcases/span/04_footnote/footnote_link_title.html', # bc of attribute ordering
].compact
Dir[File.dirname(__FILE__) + '/testcases/**/*.html'].each do |html_file|
next if EXCLUDE_HTML_KD_FILES.any? {|f| html_file =~ /#{f}$/ }
Expand Down
12 changes: 12 additions & 0 deletions test/testcases/span/04_footnote/footnote_link_title.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<p>This is a footnote. <sup id="fnref:ab"><a href="#fn:ab" class="footnote" rel="footnote" role="doc-noteref" title="Jump to footnote 1">[1]</a></sup> And another. <sup id="fnref:bc"><a href="#fn:bc" class="footnote" rel="footnote" role="doc-noteref" title="Jump to footnote 2">[2]</a></sup></p>

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

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