Add expressive unified file icons for popular extension families

This commit is contained in:
SantaSpeen committed 2026-10-22 15:19:41 +03:00
1 parent d8bdf5f390
commit 6aca927851
6 files changed
+320 -26

No files matched your search

@@ -0,0 +1,54 @@
import { expect, it } from "vitest";
import { fileIcon } from "../file-icons";
it("distinguishes common formats by symbol family as well as extension", () => {
const examples: Record<string, string[]> = {
pdf: ["pdf"],
document: ["doc", "docx", "odt", "rtf"],
spreadsheet: ["xlsx", "ods", "csv"],
presentation: ["pptx", "odp", "key"],
image: ["jpg", "png", "heic", "nef"],
vector: ["svg", "eps"],
audio: ["mp3", "flac", "opus"],
video: ["mp4", "mkv", "avi"],
archive: ["zip", "rar", "7z", "gz"],
code: ["ts", "js", "go", "py", "html", "vue"],
config: ["json", "yaml", "toml"],
database: ["sql", "sqlite", "parquet"],
executable: ["exe", "msi", "deb", "rpm"],
disc: ["iso", "vhdx"],
medical: ["dcm", "dicom", "nii.gz"],
book: ["epub", "fb2"],
font: ["ttf", "woff2"],
model: ["stl", "glb", "blend"],
design: ["psd", "fig"],
certificate: ["pem", "crt"],
torrent: ["torrent"],
text: ["txt", "md", "log"],
};
for (const [kind, extensions] of Object.entries(examples))
for (const ext of extensions)
expect(fileIcon(`sample.${ext.toUpperCase()}`).kind).toBe(kind);
const icons = Object.values(examples).map((exts) => fileIcon(`a.${exts[0]}`));
expect(new Set(icons.map((icon) => icon.path)).size).toBe(icons.length);
});
it("recognises special filenames, bounds labels and prioritises directories", () => {
expect(fileIcon("README").kind).toBe("text");
expect(fileIcon("Dockerfile").kind).toBe("code");
expect(fileIcon(".env.production").kind).toBe("config");
expect(fileIcon(".gitignore").kind).toBe("config");
expect(fileIcon("C:\\folder\\a.dcm").label).toBe("DCM");
expect(fileIcon("folder.png", true)).toMatchObject({
kind: "folder",
label: "",
});
expect(fileIcon("a.unknown-extension")).toMatchObject({
kind: "file",
label: "FILE",
});
expect(fileIcon("a.<svg>").label).toBe("FILE");
expect(fileIcon("a.docx").label).toBe("DOCX");
expect(fileIcon("a.doc").label).toBe("DOC");
expect(fileIcon("constructor").kind).toBe("file");
expect(fileIcon("__proto__").kind).toBe("file");
});
+114
View File
@@ -0,0 +1,114 @@
// Shared by listing, public downloads, preview fallbacks and ZIP entries.
// Shapes and labels keep types distinguishable without relying on colour alone.
const symbols = {
file: "M10 12h9 M10 16h9 M10 20h6",
text: "M9 11h12 M9 15h12 M9 19h8",
pdf: "M10 20c4-3 6-8 4-9-2-1-1 8 4 8s-3-3-8 1z",
document: "M9 11l2 9 4-6 4 6 2-9",
spreadsheet: "M9 11h12v10H9z M9 16h12 M15 11v10",
presentation: "M9 10h12v9H9z M15 19v3 M12 22h6 M12 16l3-3 3 2",
image: "M9 10h12v11H9z m0 8 4-4 4 4 2-2 2 2 M18 13h.01",
vector: "m15 10 6 6-6 6-6-6z M15 13v6 M12 16h6",
audio:
"M14 18v-7l7-1v7 M14 18a2 2 0 1 1-4 0 2 2 0 0 1 4 0 M21 17a2 2 0 1 1-4 0 2 2 0 0 1 4 0",
video: "M9 10h12v11H9z m4 3 5 3-5 3z",
archive: "M14 10h2 M16 12h2 M14 14h2 M16 16h2 M14 18h4v4h-4z",
code: "m12 12-4 4 4 4 m6-8 4 4-4 4 M16 11l-2 10",
config: "M9 13h5 M18 13h3 M9 19h2 M15 19h6 M14 11h4v4h-4z M11 17h4v4h-4z",
database:
"M21 12c0 3-12 3-12 0s12-3 12 0v8c0 3-12 3-12 0v-8 M9 16c0 3 12 3 12 0",
executable: "m9 13 6-3 6 3-6 3z M9 13v6l6 3 6-3v-6 M15 16v6",
disc: "M22 16a7 7 0 1 1-14 0 7 7 0 0 1 14 0 M17 16a2 2 0 1 1-4 0 2 2 0 0 1 4 0 M11 12l1 1 M18 19l1 1",
medical: "M9 10h12v12H9z M10 16h2l2-3 2 6 2-3h2",
book: "M9 10h12v12H9z M12 10v12 M15 13h3 M15 16h3",
font: "m10 21 5-11 5 11 M12 17h6",
model: "m15 10 7 4v6l-7 4-7-4v-6z M8 14l7 4 7-4 M15 18v6 M15 10v8",
design: "m9 21 3-7 7-5 3 3-5 7z M12 14l5 5 M9 21l5-5",
certificate: "M20 14a5 5 0 1 1-10 0 5 5 0 0 1 10 0 M12 18l-1 5 4-2 4 2-1-5",
torrent: "M15 10v10 m-4-4 4 4 4-4 M9 20v3h12v-3",
folder:
"M3 9V6a2 2 0 0 1 2-2h7l3 3h12a2 2 0 0 1 2 2v17a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z M3 11h26",
} as const;
export type FileIconKind = keyof typeof symbols;
const extensions: Partial<Record<FileIconKind, string>> = {
pdf: "pdf",
document: "doc docx docm dot dotx odt rtf pages wpd",
spreadsheet: "xls xlsx xlsm xlsb ods numbers csv tsv",
presentation: "ppt pptx pptm pps ppsx odp key",
image:
"jpg jpeg jfif png gif webp avif heic heif bmp tif tiff ico icns raw dng cr2 cr3 nef arw raf orf rw2",
vector: "svg eps ai cdr",
audio:
"mp3 wav wave ogg oga opus flac aac m4a alac aiff aif wma mid midi ape amr ac3 m3u m3u8 pls",
video: "mp4 m4v webm mkv avi mov wmv flv mpg mpeg m2ts mts ts vob ogv 3gp",
archive:
"zip zipx rar 7z tar gz gzip tgz bz2 tbz2 xz txz zst zstd lz lzma cab arj cpio br",
code: "js mjs cjs jsx ts tsx py pyw go rs c cpp cc cxx h hpp cs java class jar kt kts swift rb php phtml pl pm lua r dart vue svelte astro html htm xhtml css scss sass less wasm ipynb sh bash zsh fish bat cmd ps1 vbs dockerfile makefile cmake gradle",
config:
"json jsonc json5 yaml yml toml ini conf cfg config env properties editorconfig gitignore gitattributes lock service reg desktop xml plist manifest",
database:
"sql db sqlite sqlite3 db3 mdb accdb bson parquet arrow feather h5 hdf5",
executable:
"exe msi msix appx app apk aab deb rpm appimage dmg pkg dll so dylib bin elf com scr",
disc: "iso img vhd vhdx vmdk qcow qcow2 vdi cue nrg",
medical: "dcm dicom ima nii nrrd mhd mha",
book: "epub mobi azw azw3 fb2 djvu djv cbz cbr",
font: "ttf otf woff woff2 eot fon",
model: "stl obj fbx gltf glb blend 3ds dae ply step stp iges igs dwg dxf",
design: "psd psb sketch fig xcf afdesign afphoto indd aep prproj drp",
certificate: "pem crt cer p12 pfx p7b key pub asc gpg sig",
torrent: "torrent",
text: "txt text log md markdown rst adoc nfo srt vtt ass sub tex bib",
};
const lookup = new Map<string, FileIconKind>();
for (const [kind, names] of Object.entries(extensions))
for (const name of names.split(" "))
if (!lookup.has(name)) lookup.set(name, kind as FileIconKind);
// Resolve ambiguous extensions toward the common workspace use.
lookup.set("ts", "code");
lookup.set("key", "presentation");
const named: Record<string, FileIconKind> = {
dockerfile: "code",
makefile: "code",
cmakelists: "code",
jenkinsfile: "code",
license: "text",
licence: "text",
readme: "text",
changelog: "text",
authors: "text",
copying: "text",
};
export function fileIcon(name: string, directory = false) {
const base = name.split(/[\\/]/).pop()?.toLowerCase() || "";
const extension = base.includes(".") ? base.split(".").pop() || "" : "";
let kind: FileIconKind = directory
? "folder"
: (Object.prototype.hasOwnProperty.call(named, base)
? named[base]
: undefined) ||
(base.startsWith(".env") && (base === ".env" || base.startsWith(".env."))
? "config"
: lookup.get(extension) || "file");
// Never truncate long/unknown extensions into a misleading recognised label.
if (!directory && base.endsWith(".nii.gz")) kind = "medical";
const fallbackLabels: Partial<Record<FileIconKind, string>> = {
text: "TXT",
code: "CODE",
config: "CONF",
document: "DOC",
spreadsheet: "TABLE",
presentation: "SLIDE",
image: "IMAGE",
archive: "ZIP",
medical: "DCM",
database: "DATA",
certificate: "CERT",
};
const label = directory
? ""
: /^[a-z0-9]{1,5}$/.test(extension)
? extension.toUpperCase()
: fallbackLabels[kind] || "FILE";
return { kind, label, path: symbols[kind] };
}