CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

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