(filename: string, content?: string)
| 86 | * For .py files, content is required to distinguish between Marimo and Percent formats. |
| 87 | */ |
| 88 | export function detectFormat(filename: string, content?: string): NotebookFormat { |
| 89 | const lowercaseFilename = filename.toLowerCase() |
| 90 | |
| 91 | if (lowercaseFilename.endsWith('.ipynb')) return 'jupyter' |
| 92 | if (lowercaseFilename.endsWith('.deepnote')) return 'deepnote' |
| 93 | if (lowercaseFilename.endsWith('.qmd')) return 'quarto' |
| 94 | |
| 95 | if (lowercaseFilename.endsWith('.py')) { |
| 96 | if (!content) { |
| 97 | throw new UnsupportedFormatError('Content is required to detect format for .py files', { filename }) |
| 98 | } |
| 99 | if (isMarimoContent(content)) return 'marimo' |
| 100 | if (isPercentContent(content)) return 'percent' |
| 101 | throw new UnsupportedFormatError( |
| 102 | 'Unsupported Python file format. File must be percent format (# %%) or Marimo (@app.cell).', |
| 103 | { filename } |
| 104 | ) |
| 105 | } |
| 106 | |
| 107 | throw new UnsupportedFormatError(`Unsupported file format: ${filename}`, { filename }) |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Non-throwing variant of {@link detectFormat}. |
no test coverage detected