Automatically Detect Monaco Syntax Highlighting (#20)

This change passes the file name to monaco, so it can use its own
detection to determine highlighting of supported files. It also resolves
some of the mimetypes with more common use cases for a terminal.
This commit is contained in:
Sylvie Crowe 2024-06-06 14:52:01 -07:00 committed by GitHub
parent b3b99402b8
commit ba7d2cf061
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 20 deletions

View File

@ -58,11 +58,13 @@ function defaultEditorOptions(): MonacoTypes.editor.IEditorOptions {
} }
interface CodeEditProps { interface CodeEditProps {
readonly: boolean; readonly?: boolean;
text: string; text: string;
language?: string;
filename: string;
} }
export function CodeEdit({ readonly, text }: CodeEditProps) { export function CodeEdit({ readonly = false, text, language, filename }: CodeEditProps) {
const divRef = React.useRef<HTMLDivElement>(null); const divRef = React.useRef<HTMLDivElement>(null);
const monacoRef = React.useRef<MonacoTypes.editor.IStandaloneCodeEditor | null>(null); const monacoRef = React.useRef<MonacoTypes.editor.IStandaloneCodeEditor | null>(null);
const theme = "wave-theme-dark"; const theme = "wave-theme-dark";
@ -81,7 +83,7 @@ export function CodeEdit({ readonly, text }: CodeEditProps) {
function handleEditorMount(editor: MonacoTypes.editor.IStandaloneCodeEditor) { function handleEditorMount(editor: MonacoTypes.editor.IStandaloneCodeEditor) {
monacoRef.current = editor; monacoRef.current = editor;
const monacoModel = editor.getModel(); const monacoModel = editor.getModel();
monaco.editor.setModelLanguage(monacoModel, "text/markdown"); //monaco.editor.setModelLanguage(monacoModel, "text/markdown");
} }
function handleEditorChange(newText: string, ev: MonacoTypes.editor.IModelContentChangedEvent) { function handleEditorChange(newText: string, ev: MonacoTypes.editor.IModelContentChangedEvent) {
@ -97,11 +99,12 @@ export function CodeEdit({ readonly, text }: CodeEditProps) {
<Editor <Editor
theme={theme} theme={theme}
height={divDims.height} height={divDims.height}
defaultLanguage={"text/markdown"}
value={text} value={text}
onMount={handleEditorMount} onMount={handleEditorMount}
options={editorOpts} options={editorOpts}
onChange={handleEditorChange} onChange={handleEditorChange}
path={filename}
language={language}
/> />
) : null} ) : null}
</div> </div>
@ -111,12 +114,14 @@ export function CodeEdit({ readonly, text }: CodeEditProps) {
interface CodeEditViewProps { interface CodeEditViewProps {
readonly?: boolean; readonly?: boolean;
text: string; text: string;
language?: string;
filename?: string;
} }
export function CodeEditView({ readonly = false, text }: CodeEditViewProps) { export function CodeEditView({ readonly, text, language, filename }: CodeEditViewProps) {
return ( return (
<div className="view-codeedit"> <div className="view-codeedit">
<CodeEdit readonly={readonly} text={text} /> <CodeEdit readonly={readonly} text={text} language={language} filename={filename} />
</div> </div>
); );
} }

View File

@ -118,8 +118,6 @@ function PreviewView({ blockId }: { blockId: string }) {
} }
) )
); );
let name = jotai.useAtomValue(fileNameAtom);
console.log("file: ", name);
const statFileAtom = useBlockAtom(blockId, "preview:statfile", () => const statFileAtom = useBlockAtom(blockId, "preview:statfile", () =>
jotai.atom<Promise<FileInfo>>(async (get) => { jotai.atom<Promise<FileInfo>>(async (get) => {
const fileName = get(fileNameAtom); const fileName = get(fileNameAtom);
@ -156,6 +154,7 @@ function PreviewView({ blockId }: { blockId: string }) {
if (mimeType == null) { if (mimeType == null) {
mimeType = ""; mimeType = "";
} }
let fileName = jotai.useAtomValue(fileNameAtom);
const fileInfo = jotai.useAtomValue(statFileAtom); const fileInfo = jotai.useAtomValue(statFileAtom);
const fileContent = jotai.useAtomValue(fileContentAtom); const fileContent = jotai.useAtomValue(fileContentAtom);
@ -174,17 +173,12 @@ function PreviewView({ blockId }: { blockId: string }) {
specializedView = <CenteredDiv>File Too Large to Preview</CenteredDiv>; specializedView = <CenteredDiv>File Too Large to Preview</CenteredDiv>;
} else if (mimeType === "text/markdown") { } else if (mimeType === "text/markdown") {
specializedView = <MarkdownPreview contentAtom={fileContentAtom} />; specializedView = <MarkdownPreview contentAtom={fileContentAtom} />;
} else if (mimeType.startsWith("text/")) {
specializedView = (
<div className="view-preview view-preview-text">
<pre>{fileContent}</pre>
</div>
);
} else if ( } else if (
mimeType.startsWith("application") && mimeType.startsWith("text/") ||
(mimeType.includes("json") || mimeType.includes("yaml") || mimeType.includes("toml")) (mimeType.startsWith("application/") &&
(mimeType.includes("json") || mimeType.includes("yaml") || mimeType.includes("toml")))
) { ) {
specializedView = <CodeEditView readonly={true} text={fileContent} />; specializedView = specializedView = <CodeEditView readonly={true} text={fileContent} filename={fileName} />;
} else if (mimeType === "directory") { } else if (mimeType === "directory") {
specializedView = <DirectoryPreview contentAtom={fileContentAtom} fileNameAtom={fileNameAtom} />; specializedView = <DirectoryPreview contentAtom={fileContentAtom} fileNameAtom={fileNameAtom} />;
} else { } else {

View File

@ -155,7 +155,6 @@ var StaticMimeTypeMap = map[string]string{
".rl": "application/resource-lists+xml", ".rl": "application/resource-lists+xml",
".rld": "application/resource-lists-diff+xml", ".rld": "application/resource-lists-diff+xml",
".rfcxml": "application/rfc+xml", ".rfcxml": "application/rfc+xml",
".rs": "application/rls-services+xml",
".rapd": "application/route-apd+xml", ".rapd": "application/route-apd+xml",
".sls": "application/route-s-tsid+xml", ".sls": "application/route-s-tsid+xml",
".rusd": "application/route-usd+xml", ".rusd": "application/route-usd+xml",
@ -1094,9 +1093,12 @@ var StaticMimeTypeMap = map[string]string{
".csvs": "text/csv-schema", ".csvs": "text/csv-schema",
".soa": "text/dns", ".soa": "text/dns",
".gff3": "text/gff3", ".gff3": "text/gff3",
".go": "text/golang",
".html": "text/html", ".html": "text/html",
".es": "text/javascript", ".js": "text/javascript",
".cnd": "text/jcr-cnd", ".cnd": "text/jcr-cnd",
".jsx": "text/jsx",
".less": "text/less",
".md": "text/markdown", ".md": "text/markdown",
".miz": "text/mizar", ".miz": "text/mizar",
".n3": "text/n3", ".n3": "text/n3",
@ -1104,6 +1106,9 @@ var StaticMimeTypeMap = map[string]string{
".provn": "text/provenance-notation", ".provn": "text/provenance-notation",
".rst": "text/prs.fallenstein.rst", ".rst": "text/prs.fallenstein.rst",
".tag": "text/prs.lines.tag", ".tag": "text/prs.lines.tag",
".rs": "text/rust",
".sass": "text/scss",
".scss": "text/scss",
".sgml": "text/SGML", ".sgml": "text/SGML",
".shaclc": "text/shaclc", ".shaclc": "text/shaclc",
".shex": "text/shex", ".shex": "text/shex",
@ -1112,6 +1117,7 @@ var StaticMimeTypeMap = map[string]string{
".tm": "text/texmacs", ".tm": "text/texmacs",
".t": "text/troff", ".t": "text/troff",
".ttl": "text/turtle", ".ttl": "text/turtle",
".ts": "text/typescript",
".uris": "text/uri-list", ".uris": "text/uri-list",
".vcf": "text/vcard", ".vcf": "text/vcard",
".a": "text/vnd.a", ".a": "text/vnd.a",
@ -1136,7 +1142,6 @@ var StaticMimeTypeMap = map[string]string{
".mc2": "text/vnd.senx.warpscript", ".mc2": "text/vnd.senx.warpscript",
".sos": "text/vnd.sosi", ".sos": "text/vnd.sosi",
".jad": "text/vnd.sun.j2me.app-descriptor", ".jad": "text/vnd.sun.j2me.app-descriptor",
".ts": "text/vnd.trolltech.linguist",
".si": "text/vnd.wap.si", ".si": "text/vnd.wap.si",
".sl": "text/vnd.wap.sl", ".sl": "text/vnd.wap.sl",
".wml": "text/vnd.wap.wml", ".wml": "text/vnd.wap.wml",