feat(FN-4411): complete Step 2 — add CodeMirror language resolver
Fusion-Task-Id: FN-4411 Fusion-Task-Lineage: d90785f2-b828-4fa0-b330-ede7f3c14349
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { resolveCodeMirrorLanguage } from "../codemirror-language";
|
||||
|
||||
describe("resolveCodeMirrorLanguage", () => {
|
||||
it.each(["a.js", "a.mjs", "a.cjs", "a.jsx"])("maps %s to javascript", (filePath) => {
|
||||
expect(resolveCodeMirrorLanguage(filePath)).not.toBeNull();
|
||||
});
|
||||
|
||||
it.each(["a.ts", "a.tsx"])("maps %s to typescript via javascript lang", (filePath) => {
|
||||
expect(resolveCodeMirrorLanguage(filePath)).not.toBeNull();
|
||||
});
|
||||
|
||||
it("maps css", () => {
|
||||
expect(resolveCodeMirrorLanguage("styles.css")).not.toBeNull();
|
||||
});
|
||||
|
||||
it("maps json", () => {
|
||||
expect(resolveCodeMirrorLanguage("data.json")).not.toBeNull();
|
||||
});
|
||||
|
||||
it.each(["README.md", "README.markdown", "README.mdx"])("maps %s to markdown", (filePath) => {
|
||||
expect(resolveCodeMirrorLanguage(filePath)).not.toBeNull();
|
||||
});
|
||||
|
||||
it("is case-insensitive", () => {
|
||||
expect(resolveCodeMirrorLanguage("COMPONENT.TSX")).not.toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for unknown extensions", () => {
|
||||
expect(resolveCodeMirrorLanguage("notes.txt")).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for missing path", () => {
|
||||
expect(resolveCodeMirrorLanguage(undefined)).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for empty path", () => {
|
||||
expect(resolveCodeMirrorLanguage("")).toBeNull();
|
||||
});
|
||||
});
|
||||
35
packages/dashboard/app/utils/codemirror-language.ts
Normal file
35
packages/dashboard/app/utils/codemirror-language.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { type Extension } from "@codemirror/state";
|
||||
import { css } from "@codemirror/lang-css";
|
||||
import { javascript } from "@codemirror/lang-javascript";
|
||||
import { json } from "@codemirror/lang-json";
|
||||
import { markdown } from "@codemirror/lang-markdown";
|
||||
|
||||
export function resolveCodeMirrorLanguage(filePath: string | undefined): Extension | null {
|
||||
if (!filePath) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const lowerPath = filePath.toLowerCase();
|
||||
|
||||
if (lowerPath.endsWith(".js") || lowerPath.endsWith(".mjs") || lowerPath.endsWith(".cjs") || lowerPath.endsWith(".jsx")) {
|
||||
return javascript({ jsx: true });
|
||||
}
|
||||
|
||||
if (lowerPath.endsWith(".ts") || lowerPath.endsWith(".tsx")) {
|
||||
return javascript({ jsx: true, typescript: true });
|
||||
}
|
||||
|
||||
if (lowerPath.endsWith(".css")) {
|
||||
return css();
|
||||
}
|
||||
|
||||
if (lowerPath.endsWith(".json")) {
|
||||
return json();
|
||||
}
|
||||
|
||||
if (lowerPath.endsWith(".md") || lowerPath.endsWith(".markdown") || lowerPath.endsWith(".mdx")) {
|
||||
return markdown();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user