Skip to content

Commit edc6606

Browse files
committed
Fix tests
1 parent 7e9b450 commit edc6606

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

myst_nb/core/read.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class NbReader:
3131
"""The configuration for parsing markdown cells."""
3232
read_fmt: dict | None = dc.field(default=None)
3333
"""The type of the reader, if known."""
34+
support_cell_ids: bool = False
35+
"""Whether the format supports stable cell IDs"""
3436

3537

3638
def standard_nb_read(text: str) -> nbf.NotebookNode:
@@ -75,7 +77,11 @@ def create_nb_reader(
7577
if commonmark_only:
7678
# Markdown cells should be read as Markdown only
7779
md_config = dc.replace(md_config, commonmark_only=True)
78-
return NbReader(partial(reader, **(reader_kwargs or {})), md_config) # type: ignore
80+
return NbReader(
81+
partial(reader, **(reader_kwargs or {})), # type: ignore
82+
md_config,
83+
support_cell_ids=suffix == ".ipynb",
84+
)
7985

8086
# a Markdown file is a special case, since we only treat it as a notebook,
8187
# if it starts with certain "top-matter"
@@ -89,6 +95,7 @@ def create_nb_reader(
8995
),
9096
md_config,
9197
{"type": "plugin", "name": "myst_nb_md"},
98+
support_cell_ids=False,
9299
)
93100

94101
# if we get here, we did not find a reader

myst_nb/core/render.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def render_nb_cell_code(self: SelfType, token: SyntaxTreeNode) -> None:
182182
)
183183

184184
cell_id = token.meta["id"]
185-
if cell_id:
185+
if cell_id and not cell_id.startswith("RANDOM_CELL_ID"):
186186
self.document.note_implicit_target(cell_container, cell_container)
187187
slug = "cell-id=" + cell_id
188188
cell_container["slug"] = slug

myst_nb/sphinx_.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ def parse(self, inputstring: str, document: nodes.document) -> None:
8787
return super().parse(inputstring, document)
8888
notebook = nb_reader.read(inputstring)
8989

90+
if not nb_reader.support_cell_ids:
91+
# mark randomly generated cell IDs
92+
for cell in notebook["cells"]:
93+
if "id" in cell:
94+
cell["id"] = "RANDOM_CELL_ID_" + cell["id"]
95+
9096
# update the global markdown config with the file-level config
9197
warning = lambda wtype, msg: create_warning( # noqa: E731
9298
document, msg, line=1, append_to=document, subtype=wtype

0 commit comments

Comments
 (0)