Skip to content

Commit d7749b0

Browse files
Handle notebook collision
1 parent 52acb3b commit d7749b0

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

jupyterlite_sphinx/jupyterlite_sphinx.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,19 @@ def _should_convert_notebook(self, source_path: Path, target_path: Path) -> bool
323323

324324
return source_path.stat().st_mtime > target_path.stat().st_mtime
325325

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

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

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

0 commit comments

Comments
 (0)