@@ -321,6 +321,19 @@ def _should_convert_notebook(self, source_path: Path, target_path: Path) -> bool
321
321
322
322
return source_path .stat ().st_mtime > target_path .stat ().st_mtime
323
323
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
+
324
337
def run (self ):
325
338
width = self .options .pop ("width" , "100%" )
326
339
height = self .options .pop ("height" , "1000px" )
@@ -349,12 +362,13 @@ def run(self):
349
362
notebooks_dir = Path (self .env .app .srcdir ) / CONTENT_DIR
350
363
os .makedirs (notebooks_dir , exist_ok = True )
351
364
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
357
367
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" :
358
372
if self ._should_convert_notebook (notebook_path , target_path ):
359
373
nb = jupytext .read (str (notebook_path ))
360
374
with open (target_path , "w" , encoding = "utf-8" ) as f :
0 commit comments