Skip to content

Commit 894b2f1

Browse files
ioquatixst0012
andauthored
Image alt tag header formatting (#1320)
This allows us to parse the alt text from Markdown images, formatting a header text correctly (using the alt text). Without this PR, if a Markdown file has an image instead of a title, that image will be used in the table of contents, which is unreadable and undesirable. Used in <rack/rack#2313>. --------- Co-authored-by: Stan Lo <stan001212@gmail.com>
1 parent 0e5a763 commit 894b2f1

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

lib/rdoc/markdown.kpeg

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -992,9 +992,14 @@ Strike = &{ strike? }
992992
"~~"
993993
{ strike a.join }
994994

995-
# TODO alt text support
996-
Image = "!" ( ExplicitLink | ReferenceLink ):a
997-
{ "rdoc-image:#{a[/\[(.*)\]/, 1]}" }
995+
Image = "!" ExplicitLink:a
996+
{
997+
# Extract alt text and URL
998+
alt_text = a[/\{(.*?)\}/, 1] || ""
999+
url = a[/\[(.*?)\]/, 1] || ""
1000+
1001+
"rdoc-image:#{url}:#{alt_text}"
1002+
}
9981003

9991004
Link = ExplicitLink | ReferenceLink | AutoLink
10001005

lib/rdoc/markup/heading.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ def label context = nil
6666
# element.
6767

6868
def plain_html
69-
self.class.to_html.to_html(text.dup)
69+
text = self.text.dup
70+
71+
if matched = text.match(/rdoc-image:[^:]+:(.*)/)
72+
text = matched[1]
73+
end
74+
75+
self.class.to_html.to_html(text)
7076
end
7177

7278
def pretty_print q # :nodoc:

lib/rdoc/markup/to_html.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ def handle_RDOCLINK url # :nodoc:
9898

9999
gen_url CGI.escapeHTML(url), CGI.escapeHTML(text)
100100
when /^rdoc-image:/
101-
%[<img src=\"#{CGI.escapeHTML($')}\">]
101+
url, alt = $'.split(":", 2) # Split the string after "rdoc-image:" into url and alt
102+
if alt && !alt.empty?
103+
%[<img src="#{CGI.escapeHTML(url)}" alt="#{CGI.escapeHTML(alt)}">]
104+
else
105+
%[<img src="#{CGI.escapeHTML(url)}">]
106+
end
102107
when /\Ardoc-[a-z]+:/
103108
CGI.escapeHTML($')
104109
end

test/rdoc/test_rdoc_markdown.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ def test_parse_html_no_html
511511
def test_parse_image
512512
doc = parse "image ![alt text](path/to/image.jpg)"
513513

514-
expected = doc(para("image rdoc-image:path/to/image.jpg"))
514+
expected = doc(para("image rdoc-image:path/to/image.jpg:alt text"))
515515

516516
assert_equal expected, doc
517517
end
@@ -523,7 +523,7 @@ def test_parse_image_link
523523

524524
expected =
525525
doc(
526-
para('{rdoc-image:path/to/image.jpg}[http://example.com]'))
526+
para('{rdoc-image:path/to/image.jpg:alt text}[http://example.com]'))
527527

528528
assert_equal expected, doc
529529
end

test/rdoc/test_rdoc_markup_heading.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ def test_plain_html
2626
assert_equal 'Hello <strong>Friend</strong>!', @h.plain_html
2727
end
2828

29+
def test_plain_html_using_image_alt_as_text
30+
h = RDoc::Markup::Heading.new 1, 'rdoc-image:foo.png:Hello World'
31+
32+
assert_equal 'Hello World', h.plain_html
33+
end
2934
end

0 commit comments

Comments
 (0)