Skip to content

Image alt tag header formatting #1320

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

Merged
merged 5 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 8 additions & 3 deletions lib/rdoc/markdown.kpeg
Original file line number Diff line number Diff line change
Expand Up @@ -992,9 +992,14 @@ Strike = &{ strike? }
"~~"
{ strike a.join }

# TODO alt text support
Image = "!" ( ExplicitLink | ReferenceLink ):a
{ "rdoc-image:#{a[/\[(.*)\]/, 1]}" }
Image = "!" ExplicitLink:a
{
# Extract alt text and URL
alt_text = a[/\{(.*?)\}/, 1] || ""
url = a[/\[(.*?)\]/, 1] || ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a is a String that always match to these regexp because ExplicitLink:a is created by "{#{l}}[#{s}]".
Perhaps it could be more simple if ExplicitLink returns structured data

  ExplicitLink =  Label:l "(" @Sp Source:s Spnl Title @Sp ")"
-                 { "{#{l}}[#{s}]" } # returns string, need to parse this string again...
+                 { { label: l, source: s } } # can return hash

(need to change Link = ExplicitLink | ReferenceLink | AutoLink to return a string if ExplicitLink returns hash)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do this in a follow-up PR? I think it's out of scope for this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense 👍


"rdoc-image:#{url}:#{alt_text}"
}

Link = ExplicitLink | ReferenceLink | AutoLink

Expand Down
8 changes: 7 additions & 1 deletion lib/rdoc/markup/heading.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ def label context = nil
# element.

def plain_html
self.class.to_html.to_html(text.dup)
text = self.text.dup

if text.match?(/rdoc-image:[^:]+:(.*)/)
text = text.match(/rdoc-image:[^:]+:(.*)/)[1]
end

self.class.to_html.to_html(text)
end

def pretty_print q # :nodoc:
Expand Down
7 changes: 6 additions & 1 deletion lib/rdoc/markup/to_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ def handle_RDOCLINK url # :nodoc:

gen_url CGI.escapeHTML(url), CGI.escapeHTML(text)
when /^rdoc-image:/
%[<img src=\"#{CGI.escapeHTML($')}\">]
url, alt = $'.split(":", 2) # Split the string after "rdoc-image:" into url and alt
if alt
%[<img src="#{CGI.escapeHTML(url)}" alt="#{CGI.escapeHTML(alt)}">]
else
%[<img src="#{CGI.escapeHTML(url)}">]
end
when /\Ardoc-[a-z]+:/
CGI.escapeHTML($')
end
Expand Down
4 changes: 2 additions & 2 deletions test/rdoc/test_rdoc_markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def test_parse_html_no_html
def test_parse_image
doc = parse "image ![alt text](path/to/image.jpg)"

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

assert_equal expected, doc
end
Expand All @@ -523,7 +523,7 @@ def test_parse_image_link

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

assert_equal expected, doc
end
Expand Down
Loading