Files
cosmos-explorer/src/Explorer/Notebook/NotebookUtil.ts
T
jawelton74 b7caca1cd6 Remove Phoenix & Notebooks - Phase 4: Remove GitHub integration (#2528)
Removes the GitHub notebook-repo integration that only existed to pin/browse notebook repositories. Deletes src/GitHub/, the GitHub controls/panes, GitHubUtils, JunoUtils, and the connectToGitHub webpack entry. Decouples NotebookManager, useNotebook, the resource tree, and Explorer from GitHub wiring. Trims JunoClient's GitHub-only methods while keeping the Schema and gallery methods. Removes GitHub config fields from ConfigContext and strips the dead github:// branches from NotebookUtil.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-29 13:21:30 -07:00

82 lines
2.3 KiB
TypeScript

import * as StringUtils from "../../Utils/StringUtils";
import { NotebookContentItem, NotebookContentItemType } from "./NotebookContentItem";
// Must match rx-jupyter' FileType
export type FileType = "directory" | "file" | "notebook";
// Utilities for notebooks
export class NotebookUtil {
/**
* It's a notebook file if the filename ends with .ipynb.
*/
public static isNotebookFile(notebookPath: string): boolean {
const fileName = NotebookUtil.getName(notebookPath);
return !!fileName && StringUtils.endsWith(fileName, ".ipynb");
}
/**
* Note: this does not connect the item to a parent in a tree.
* @param name
* @param path
*/
public static createNotebookContentItem(name: string, path: string, type: FileType): NotebookContentItem {
return {
name,
path,
type: NotebookUtil.getType(type),
timestamp: NotebookUtil.getCurrentTimestamp(),
};
}
/**
* Convert rx-jupyter type to our type
* @param type
*/
public static getType(type: FileType): NotebookContentItemType {
switch (type) {
case "directory":
return NotebookContentItemType.Directory;
case "notebook":
return NotebookContentItemType.Notebook;
case "file":
return NotebookContentItemType.File;
default:
throw new Error(`Unknown file type: ${type}`);
}
}
public static getCurrentTimestamp(): number {
return new Date().getTime();
}
public static getFilePath(path: string, fileName: string): string {
return `${path}/${fileName}`;
}
public static getParentPath(filepath: string): undefined | string {
const basename = NotebookUtil.getName(filepath);
if (basename) {
const parentPath = filepath.split(basename).shift();
if (parentPath) {
return parentPath.replace(/\/$/, ""); // no trailling slash
}
}
return undefined;
}
public static getName(path: string): undefined | string {
return path.split("/").pop();
}
public static replaceName(path: string, newName: string): string {
const contentName = path.split("/").pop();
if (!contentName) {
throw new Error(`Failed to extract name from path ${path}`);
}
const basePath = path.split(contentName).shift();
return `${basePath}${newName}`;
}
}