Skip to content

Be slightly more careful to take the first paragraph #2110

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

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 4 additions & 3 deletions lib/ex_doc/formatter/html/templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ defmodule ExDoc.Formatter.HTML.Templates do

def synopsis(doc) when is_binary(doc) do
doc =
case :binary.split(doc, "</p>") do
[left, _] -> String.trim_trailing(left, ":") <> "</p>"
[all] -> all
Regex.run(~r|<p>((?:(?!</p>).)*?):*</p>|, doc)
|> case do
nil -> doc
[_, first_paragraph_text] -> "<p>" <> first_paragraph_text <> "</p>"
end
Copy link
Member

@josevalim josevalim Apr 6, 2025

Choose a reason for hiding this comment

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

We are kind of assuming that it starts with <p>, so perhaps I would enforce that instead?

case String.trim_leading(doc) do
  "<p>" <> _ = doc ->
    doc =
      case :binary.split(doc, "</p>") do
        [left, _] -> String.trim_trailing(left, ":") <> "</p>"
        [all] -> all
      end

    Regex.replace(~r|(<[^>]*) id="[^"]*"([^>]*>)|, doc, ~S"\1\2", [])

  _ ->
    nil
end

Alternatively we capture the synopsis over the AST instead of the rendered document. This would also make it easier to remove links and what not.


# Remove any anchors found in synopsis.
Expand Down
3 changes: 3 additions & 0 deletions test/ex_doc/formatter/html/templates_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ defmodule ExDoc.Formatter.HTML.TemplatesTest do
assert Templates.synopsis("<p>::</p>") == "<p></p>"
assert Templates.synopsis("<p>Description:</p>") == "<p>Description</p>"
assert Templates.synopsis("<p>abcd</p>") == "<p>abcd</p>"
assert Templates.synopsis("<p>abc</p><p>def</p>") == "<p>abc</p>"
assert Templates.synopsis("<h2>title</h2><p>abc</p><p>def</p>") == "<p>abc</p>"
assert Templates.synopsis("<li><p>abc</p></li>") == "<p>abc</p>"
end

test "should not end have trailing periods or semicolons" do
Expand Down