Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/editor-tmp.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { filename_extension_notilde, path_split } from "@cocalc/util/misc";6import { file_associations } from "./file-associations";7import { icon as file_icon } from "./file-editors";89// Given a text file (defined by content), try to guess10// what the extension should be.11function guess_file_extension_type(content: string): string {12content = $.trim(content);13const i = content.indexOf("\n");14const first_line = content.slice(0, i).toLowerCase();15if (first_line.slice(0, 2) === "#!") {16// A script. What kind?17if (first_line.indexOf("python") !== -1) {18return "py";19}20if (first_line.indexOf("bash") !== -1 || first_line.indexOf("sh") !== -1) {21return "sh";22}23if (first_line.indexOf("node") !== -1) {24return "js";25}26}27if (first_line.indexOf("html") !== -1) {28return "html";29}30if (first_line.indexOf("/*") !== -1 || first_line.indexOf("//") !== -1) {31// kind of a stretch32return "c++";33}34return "";35}3637export function file_options(filename: string, content?: string) {38let x;39let ext = filename_extension_notilde(filename).toLowerCase();40if (ext == "" && content != null) {41// no recognized extension, but have contents42ext = guess_file_extension_type(content);43}44if (ext == "") {45x = file_associations[`noext-${path_split(filename).tail.toLowerCase()}`];46} else {47x = file_associations[ext];48}49if (x == null) {50x = file_associations[""];51// Don't use the icon for this fallback, to give the icon selection below a chance to work;52// we do this so new react editors work. All this code will go away someday.53delete x.icon;54}55if (x.icon == null) {56const icon = file_icon(ext);57if (icon != null) {58x.icon = icon;59} else {60x.icon = UNKNOWN_FILE_TYPE_ICON;61}62}63return x;64}6566export const UNKNOWN_FILE_TYPE_ICON = "question-circle"676869