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
6 changes: 6 additions & 0 deletions myst_parser/mdit_to_docutils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,12 @@ def render_restructuredtext(self, token: SyntaxTreeNode) -> None:
if node["names"]:
self.document.note_explicit_target(node, node)
self.current_node.extend(newdoc.children)
# ``extend`` only re-parents the direct children, so point every nested node
# at the real document, not the temporary one (which shares the ``settings.env``
# that Sphinx strips from its own copy of the settings before pickling the doctree)
for node in newdoc.children:
for descendant in node.findall(nodes.Element):
descendant.document = self.document

def render_directive(
self,
Expand Down
17 changes: 17 additions & 0 deletions tests/test_docutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,23 @@ def test_field_list_body_source_line():
assert bodies[0].line # a real line, not 0/None


def test_eval_rst_nested_nodes_belong_to_document():
"""Nodes created inside an ``{eval-rst}`` block must point at the real document.

Regression: the block is parsed into a temporary document sharing the page's
settings, and only the direct children were re-parented when it was merged, so
deeper nodes kept ``.document`` set to the temporary document. Sphinx removes
``settings.env`` from its own copy of the settings before pickling a doctree, but
not from the temporary document's settings, so those nodes dragged the whole
``BuildEnvironment`` into the pickled doctree.
"""
source = "```{eval-rst}\n* item one\n\n * nested item\n\n paragraph\n```\n"
doctree = publish_doctree(source=source, parser=Parser())
elements = list(doctree.findall(nodes.Element))
assert any(isinstance(node, nodes.list_item) for node in elements)
assert [node.tagname for node in elements if node.document is not doctree] == []


def test_linkify_disabled_without_linkify_it_py(monkeypatch):
"""Enabling ``linkify`` without ``linkify-it-py`` warns and disables it.

Expand Down