|
2 | 2 | from abc import ABC, abstractmethod |
3 | 3 | import os |
4 | 4 | from typing import List, Optional |
| 5 | +from unittest import mock |
5 | 6 |
|
6 | 7 | from importlib_metadata import entry_points |
7 | 8 | from docutils import nodes |
|
19 | 20 | from jupyter_sphinx.utils import sphinx_abs_dir |
20 | 21 |
|
21 | 22 | from myst_parser.main import default_parser, MdParserConfig |
| 23 | +from myst_parser.docutils_renderer import make_document |
22 | 24 |
|
23 | 25 | from .nodes import CellOutputBundleNode |
24 | 26 |
|
@@ -118,7 +120,15 @@ def run(self): |
118 | 120 | if "candidates" in node: |
119 | 121 | continue |
120 | 122 | col = ImageCollector() |
121 | | - col.process_doc(self.app, node) |
| 123 | + |
| 124 | + # use the node docname, where possible, to deal with single document builds |
| 125 | + docname = ( |
| 126 | + self.app.env.path2doc(node.source) |
| 127 | + if node.source |
| 128 | + else self.app.env.docname |
| 129 | + ) |
| 130 | + with mock.patch.dict(self.app.env.temp_data, {"docname": docname}): |
| 131 | + col.process_doc(self.app, node) |
122 | 132 |
|
123 | 133 |
|
124 | 134 | class CellOutputRendererBase(ABC): |
@@ -227,9 +237,25 @@ def parse_markdown( |
227 | 237 | ) -> List[nodes.Node]: |
228 | 238 | """Parse text as CommonMark, in a new document.""" |
229 | 239 | parser = default_parser(MdParserConfig(commonmark_only=True)) |
230 | | - parent = parent or nodes.container() |
| 240 | + |
| 241 | + # setup parent node |
| 242 | + if parent is None: |
| 243 | + parent = nodes.container() |
| 244 | + self.add_source_and_line(parent) |
231 | 245 | parser.options["current_node"] = parent |
232 | | - parser.render(text) |
| 246 | + |
| 247 | + # setup containing document |
| 248 | + new_doc = make_document(self.node.source) |
| 249 | + new_doc.settings = self.document.settings |
| 250 | + new_doc.reporter = self.document.reporter |
| 251 | + parser.options["document"] = new_doc |
| 252 | + |
| 253 | + # use the node docname, where possible, to deal with single document builds |
| 254 | + with mock.patch.dict( |
| 255 | + self.env.temp_data, {"docname": self.env.path2doc(self.node.source)} |
| 256 | + ): |
| 257 | + parser.render(text) |
| 258 | + |
233 | 259 | # TODO is there any transforms we should retroactively carry out? |
234 | 260 | return parent.children |
235 | 261 |
|
|
0 commit comments