Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski committed Jul 31, 2023
1 parent 7e9b450 commit e844608
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
9 changes: 8 additions & 1 deletion myst_nb/core/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class NbReader:
"""The configuration for parsing markdown cells."""
read_fmt: dict | None = dc.field(default=None)
"""The type of the reader, if known."""
support_cell_ids: bool = False
"""Whether the format supports stable cell IDs"""


def standard_nb_read(text: str) -> nbf.NotebookNode:
Expand Down Expand Up @@ -75,7 +77,11 @@ def create_nb_reader(
if commonmark_only:
# Markdown cells should be read as Markdown only
md_config = dc.replace(md_config, commonmark_only=True)
return NbReader(partial(reader, **(reader_kwargs or {})), md_config) # type: ignore
return NbReader(
partial(reader, **(reader_kwargs or {})),
md_config,
support_cell_ids=suffix == ".ipynb",
) # type: ignore

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

# if we get here, we did not find a reader
Expand Down
2 changes: 1 addition & 1 deletion myst_nb/core/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def render_nb_cell_code(self: SelfType, token: SyntaxTreeNode) -> None:
)

cell_id = token.meta["id"]
if cell_id:
if cell_id and not cell_id.startswith("RANDOM_CELL_ID"):
self.document.note_implicit_target(cell_container, cell_container)
slug = "cell-id=" + cell_id
cell_container["slug"] = slug
Expand Down
6 changes: 6 additions & 0 deletions myst_nb/sphinx_.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ def parse(self, inputstring: str, document: nodes.document) -> None:
return super().parse(inputstring, document)
notebook = nb_reader.read(inputstring)

if not nb_reader.support_cell_ids:
# mark randomly generated cell IDs
for cell in notebook["cells"]:
if "id" in cell:
cell["id"] = "RANDOM_CELL_ID_" + cell["id"]

# update the global markdown config with the file-level config
warning = lambda wtype, msg: create_warning( # noqa: E731
document, msg, line=1, append_to=document, subtype=wtype
Expand Down

0 comments on commit e844608

Please sign in to comment.