diff --git a/CHANGES b/CHANGES
index b93bb05f0..10f5f5f26 100644
--- a/CHANGES
+++ b/CHANGES
@@ -19,6 +19,8 @@ Bugs fixed
----------
* #7839: autosummary: cannot handle umlauts in function names
+* #7866: autosummary: Failed to extract correct summary line when docstring
+ contains a hyperlink target
* #7715: LaTeX: ``numfig_secnum_depth > 1`` leads to wrong figure links
* #7846: html theme: XML-invalid files were generated
diff --git a/sphinx/ext/autosummary/__init__.py b/sphinx/ext/autosummary/__init__.py
index 7eaefd4b1..08f5bd3c4 100644
--- a/sphinx/ext/autosummary/__init__.py
+++ b/sphinx/ext/autosummary/__init__.py
@@ -497,6 +497,13 @@ def mangle_signature(sig: str, max_chars: int = 30) -> str:
def extract_summary(doc: List[str], document: Any) -> str:
"""Extract summary from docstring."""
+ def parse(doc: List[str], settings: Any) -> nodes.document:
+ state_machine = RSTStateMachine(state_classes, 'Body')
+ node = new_document('', settings)
+ node.reporter = NullReporter()
+ state_machine.run(doc, node)
+
+ return node
# Skip a blank lines at the top
while doc and not doc[0].strip():
@@ -514,11 +521,7 @@ def extract_summary(doc: List[str], document: Any) -> str:
return ''
# parse the docstring
- state_machine = RSTStateMachine(state_classes, 'Body')
- node = new_document('', document.settings)
- node.reporter = NullReporter()
- state_machine.run(doc, node)
-
+ node = parse(doc, document.settings)
if not isinstance(node[0], nodes.paragraph):
# document starts with non-paragraph: pick up the first line
summary = doc[0].strip()
@@ -532,7 +535,7 @@ def extract_summary(doc: List[str], document: Any) -> str:
while sentences:
summary += sentences.pop(0) + '.'
node[:] = []
- state_machine.run([summary], node)
+ node = parse(doc, document.settings)
if not node.traverse(nodes.system_message):
# considered as that splitting by period does not break inline markups
break
diff --git a/tests/test_ext_autosummary.py b/tests/test_ext_autosummary.py
index a65826141..f716feaae 100644
--- a/tests/test_ext_autosummary.py
+++ b/tests/test_ext_autosummary.py
@@ -108,6 +108,12 @@ def test_extract_summary(capsys):
'=========']
assert extract_summary(doc, document) == 'blah blah'
+ # hyperlink target
+ doc = ['Do `this `_ and that. '
+ 'blah blah blah.']
+ assert (extract_summary(doc, document) ==
+ 'Do `this `_ and that.')
+
_, err = capsys.readouterr()
assert err == ''