(content: string, absolutePath: string)
| 71 | * @throws LoadRunnableFileError for unsupported extensions and parse/format failures |
| 72 | */ |
| 73 | export function parseRunnableFileContent(content: string, absolutePath: string): LoadedRunnableFile { |
| 74 | const ext = path.extname(absolutePath).toLowerCase() |
| 75 | |
| 76 | if (!isRunnableExtension(ext)) { |
| 77 | throw unsupportedExtensionError(ext) |
| 78 | } |
| 79 | |
| 80 | const filename = path.basename(absolutePath) |
| 81 | const projectName = path.basename(absolutePath, ext) |
| 82 | |
| 83 | if (ext === '.deepnote') { |
| 84 | let file: DeepnoteFile |
| 85 | try { |
| 86 | file = deserializeDeepnoteFile(content) |
| 87 | } catch (parseError) { |
| 88 | const message = parseError instanceof Error ? parseError.message : String(parseError) |
| 89 | throw new LoadRunnableFileError(`Failed to parse .deepnote file: ${absolutePath}\n\n` + `Parse error: ${message}`) |
| 90 | } |
| 91 | |
| 92 | return { |
| 93 | file, |
| 94 | originalPath: absolutePath, |
| 95 | format: 'deepnote', |
| 96 | wasConverted: false, |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (ext === '.ipynb') { |
| 101 | let notebook: JupyterNotebook |
| 102 | try { |
| 103 | notebook = JSON.parse(content) as JupyterNotebook |
| 104 | } catch (parseError) { |
| 105 | const message = parseError instanceof Error ? parseError.message : String(parseError) |
| 106 | throw new LoadRunnableFileError( |
| 107 | `Invalid Jupyter notebook: ${absolutePath}\n\n` + `The file is not valid JSON. Parse error: ${message}` |
| 108 | ) |
| 109 | } |
| 110 | |
| 111 | const file = convertJupyterNotebookToDeepnote({ filename, notebook }, { projectName }) |
| 112 | return { |
| 113 | file, |
| 114 | originalPath: absolutePath, |
| 115 | format: 'jupyter', |
| 116 | wasConverted: true, |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (ext === '.qmd') { |
| 121 | let document: ReturnType<typeof parseQuartoFormat> |
| 122 | try { |
| 123 | document = parseQuartoFormat(content) |
| 124 | } catch (parseError) { |
| 125 | const message = parseError instanceof Error ? parseError.message : String(parseError) |
| 126 | throw new LoadRunnableFileError( |
| 127 | `Failed to parse Quarto document: ${absolutePath}\n\n` + `Parse error: ${message}` |
| 128 | ) |
| 129 | } |
| 130 | const file = convertQuartoDocumentToDeepnote({ filename, document }, { projectName }) |
no test coverage detected