Skip to content

Commit ccd22f6

Browse files
Handle notebook collision
1 parent 54d6cfb commit ccd22f6

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

jupyterlite_sphinx/jupyterlite_sphinx.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,19 @@ def _should_convert_notebook(self, source_path: Path, target_path: Path) -> bool
321321

322322
return source_path.stat().st_mtime > target_path.stat().st_mtime
323323

324+
def _get_target_name(self, source_path: Path, notebooks_dir: Path) -> str:
325+
"""Get target filename. Here, we aim to handle potential collisions with
326+
existing notebooks."""
327+
base_target = f"{source_path.stem}.ipynb"
328+
converted_target = f"{source_path.stem}.converted.ipynb"
329+
330+
# For MyST-flavoured files, check if a similarly-named IPyNB exists
331+
# If it does, we will append ".converted.ipynb" to the target name.
332+
if source_path.suffix.lower() == '.md':
333+
if (notebooks_dir / base_target).exists():
334+
return converted_target
335+
return base_target
336+
324337
def run(self):
325338
width = self.options.pop("width", "100%")
326339
height = self.options.pop("height", "1000px")
@@ -349,12 +362,13 @@ def run(self):
349362
notebooks_dir = Path(self.env.app.srcdir) / CONTENT_DIR
350363
os.makedirs(notebooks_dir, exist_ok=True)
351364

352-
# For MyST Markdown notebooks, we create a unique target filename to avoid
353-
# collisions with other IPyNB files that may have the same name.
354-
if notebook_path.suffix.lower() == ".md":
355-
target_name = f"{notebook_path.stem}.ipynb"
356-
target_path = notebooks_dir / target_name
365+
target_name = self._get_target_name(notebook_path, notebooks_dir)
366+
target_path = notebooks_dir / target_name
357367

368+
# For MyST Markdown notebooks, we create a unique target filename
369+
# via _get_target_name() to avoid collisions with other IPyNB files
370+
# that may have the same name.
371+
if notebook_path.suffix.lower() == ".md":
358372
if self._should_convert_notebook(notebook_path, target_path):
359373
nb = jupytext.read(str(notebook_path))
360374
with open(target_path, "w", encoding="utf-8") as f:

0 commit comments

Comments
 (0)