From ce3f4e6e7d4600e3289a65b32ba217bfb3573fc5 Mon Sep 17 00:00:00 2001
From: Adam Wight
Date: Sun, 6 Apr 2025 20:06:12 +0200
Subject: [PATCH] Be slightly more careful to take the first paragraph
The previous algorithm was easily fooled into emitting unbalanced
tags, so the logic is tightened up with a more specific regex.
Overall, this probably isn't doing the right thing. Consider making
the synopsis more robust by using eg. Floki in production.
Closes #2108 , sort of...
---
lib/ex_doc/formatter/html/templates.ex | 7 ++++---
test/ex_doc/formatter/html/templates_test.exs | 3 +++
2 files changed, 7 insertions(+), 3 deletions(-)
diff --git a/lib/ex_doc/formatter/html/templates.ex b/lib/ex_doc/formatter/html/templates.ex
index 55b30a2ae..357d4623b 100644
--- a/lib/ex_doc/formatter/html/templates.ex
+++ b/lib/ex_doc/formatter/html/templates.ex
@@ -53,9 +53,10 @@ defmodule ExDoc.Formatter.HTML.Templates do
def synopsis(doc) when is_binary(doc) do
doc =
- case :binary.split(doc, "
") do
- [left, _] -> String.trim_trailing(left, ":") <> ""
- [all] -> all
+ Regex.run(~r|((?:(?!
).)*?):*|, doc)
+ |> case do
+ nil -> doc
+ [_, first_paragraph_text] -> "" <> first_paragraph_text <> "
"
end
# Remove any anchors found in synopsis.
diff --git a/test/ex_doc/formatter/html/templates_test.exs b/test/ex_doc/formatter/html/templates_test.exs
index ae4dbbba1..4ec59f0a5 100644
--- a/test/ex_doc/formatter/html/templates_test.exs
+++ b/test/ex_doc/formatter/html/templates_test.exs
@@ -169,6 +169,9 @@ defmodule ExDoc.Formatter.HTML.TemplatesTest do
assert Templates.synopsis("::
") == ""
assert Templates.synopsis("Description:
") == "Description
"
assert Templates.synopsis("abcd
") == "abcd
"
+ assert Templates.synopsis("abc
def
") == "abc
"
+ assert Templates.synopsis("title
abc
def
") == "abc
"
+ assert Templates.synopsis("abc
") == "abc
"
end
test "should not end have trailing periods or semicolons" do