@@ -323,6 +323,19 @@ def _should_convert_notebook(self, source_path: Path, target_path: Path) -> bool
323
323
324
324
return source_path .stat ().st_mtime > target_path .stat ().st_mtime
325
325
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
+
326
339
def run (self ):
327
340
width = self .options .pop ("width" , "100%" )
328
341
height = self .options .pop ("height" , "1000px" )
@@ -351,12 +364,13 @@ def run(self):
351
364
notebooks_dir = Path (self .env .app .srcdir ) / CONTENT_DIR
352
365
os .makedirs (notebooks_dir , exist_ok = True )
353
366
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
359
369
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" :
360
374
if self ._should_convert_notebook (notebook_path , target_path ):
361
375
nb = jupytext .read (str (notebook_path ))
362
376
with open (target_path , "w" , encoding = "utf-8" ) as f :
0 commit comments