Skip to content
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
22 changes: 22 additions & 0 deletions lib/rdoc/generator/darkfish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,17 @@ def template_for(file, page = true, klass = ERB)
ParagraphExcerptRegexpOther = %r[\b\w[^./:]++\.]
# use \p/\P{letter} instead of \w/\W in Unicode
ParagraphExcerptRegexpUnicode = %r[\b\p{letter}[^./:]++\.]

# Link markup to replace with its text (or remove) before taking an excerpt
ExcerptMarkdownLinks = [
[/(?<![\w`])!\[[^\]]*\]\([^)]*\)/, ''], # ![alt](image)
[/(?<![\w`])\[([^\]]*)\]\([^)]*\)/, '\\1'], # [text](url)
]
ExcerptRDocLinks = [
[/\{([^}]*)\}\[[^\]]*\]/, '\\1'], # {text}[url]
[/(\S+)\[(?:https?|ftp|mailto|irc|www|link|rdoc-[a-z]+):[^\]]*\]/, '\\1'], # text[url]
]
ExcerptURL = %r[<?\b(?:https?|ftp)://[^\s>]*[^\s>.,;:!?)]>?]
# :startdoc:

# Returns an excerpt of the comment for usage in meta description tags
Expand All @@ -725,6 +736,8 @@ def excerpt(comment)
comment
end

text = strip_links(text, comment.respond_to?(:format) ? comment.format : nil)

# Match from a capital letter to the first period, discarding any links, so
# that we don't end up matching badges in the README
pattern = ParagraphExcerptRegexpUnicode
Expand All @@ -745,6 +758,15 @@ def excerpt(comment)
extracted_text[0...150].tr_s("\n", " ").squeeze(" ")
end

# Replaces links in +text+ written in +format+ markup with their text, and
# removes images and bare URLs, so that link targets don't end up in excerpts
def strip_links(text, format) # :nodoc:
links = format == 'markdown' ? ExcerptMarkdownLinks : ExcerptRDocLinks

text = links.inject(text) { |t, (regexp, replacement)| t.gsub(regexp, replacement) }
text.gsub(ExcerptURL, '')
end

def generate_ancestor_list(ancestors, klass)
return '' if ancestors.empty?

Expand Down
43 changes: 43 additions & 0 deletions test/rdoc/generator/darkfish_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,49 @@ def test_meta_tags_for_markdown_files
)
end

def test_meta_tags_for_markdown_links
top_level = @store.add_file("file.rb")
klass = top_level.add_class(RDoc::NormalClass, "Linked")
comment = RDoc::Comment.new(<<~MARKDOWN, top_level)
Foo.

[Example Link](https://example.com) and ![Badge](https://example.com/badge.svg)
MARKDOWN
comment.format = "markdown"
klass.add_comment(comment, top_level)

@g.generate

content = File.binread("Linked.html")
assert_include(content, '<meta name="description" content="class Linked: Foo.">')
end

def test_meta_tags_for_rdoc_links
top_level = @store.add_file("file.rb")
klass = top_level.add_class(RDoc::NormalClass, "Linked")
klass.add_comment(<<~RDOC, top_level)
See {the docs}[https://example.com/docs.html] or RDoc[https://ruby.github.io/rdoc/] for more.
RDOC

@g.generate

content = File.binread("Linked.html")
assert_include(content, '<meta name="description" content="class Linked: See the docs or RDoc for more.">')
end

def test_meta_tags_for_bare_urls
top_level = @store.add_file("file.rb")
klass = top_level.add_class(RDoc::NormalClass, "Linked")
klass.add_comment(<<~RDOC, top_level)
Details are at https://example.com/docs. Use it.
RDOC

@g.generate

content = File.binread("Linked.html")
assert_include(content, '<meta name="description" content="class Linked: Details are at . Use it.">')
end

def test_meta_tags_for_raw_pages
top_level = @store.add_file("MyPage", parser: RDoc::Parser::Simple)
comment = RDoc::Comment.new('this is a comment')
Expand Down
Loading