Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/attachment embeds: autodetect syntax highlight language by extension

Adorian Doran48ae4d1e009e
diff --git a/apps/client/src/services/content_renderer.spec.ts b/apps/client/src/services/content_renderer.spec.ts
index 6c99c2ab35..5cee53ad32 100644
--- a/apps/client/src/services/content_renderer.spec.ts
+++ b/apps/client/src/services/content_renderer.spec.ts
@@ -343,13 +343,26 @@ describe("getRenderedContent editable code", () => {
};
}
- it("renders an attached code file as code", async () => {
- const att = buildAttachment({ role: "file", mime: "text/x-python" });
+ it("renders an attached code file as code, highlighted by the language of its name", async () => {
+ const att = buildAttachment({ role: "file", mime: "text/x-python", title: "a.py" });
att.getBlob = (async () => ({ content: "print(1)" })) as any;
const { type, $renderedContent } = await getRenderedContent(att);
expect(type).toBe("code");
expect($renderedContent.find("pre > code").text()).toBe("print(1)");
+
+ const generic = [
+ buildAttachment({ role: "file", mime: "application/x-javascript", title: "app.js" }),
+ buildAttachment({ role: "file", mime: "text/plain", title: "types.ts" }),
+ buildAttachment({ role: "file", mime: "text/plain", title: "notes.txt" })
+ ];
+ for (const attachment of generic) {
+ attachment.getBlob = (async () => ({ content: "" })) as any;
+ await getRenderedContent(attachment);
+ }
+ expect(applySingleBlockSyntaxHighlight.mock.calls.map(([ , mime ]) => mime)).toEqual([
+ "text-x-python", "text-javascript", "application-typescript", "text-plain"
+ ]);
});
it("mounts CodeEmbed for an interactive code note, saved by `noteEditor`", async () => {
@@ -370,6 +383,7 @@ describe("getRenderedContent editable code", () => {
const props = codeEmbedComponent.mock.calls[0][0];
expect(props.entity).toBe(note);
expect(props.content).toBe("unsaved");
+ expect(props.mime).toBe("text/javascript");
expect(props.preview.textContent).toBe("unsaved");
const getContent = () => "edited";
@@ -393,6 +407,7 @@ describe("getRenderedContent editable code", () => {
const props = codeEmbedComponent.mock.calls[0][0];
expect(props.entity).toBe(att);
expect(props.content).toBe("{}");
+ expect(props.mime).toBe("application/json");
props.editor.scheduleSave(() => "[]");
props.editor.release();
expect(attachmentEditor.getUnsavedContent).toHaveBeenCalledWith(att.attachmentId);
diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.ts
index 3746ee7c28..5652a7f9a7 100644
--- a/apps/client/src/services/content_renderer.ts
+++ b/apps/client/src/services/content_renderer.ts
@@ -3,6 +3,7 @@ import "./content_renderer.css";
import {
attachmentIcon,
CANVAS_ATTACHMENT_MIME,
+ getMimeTypeFromFileName,
isAcceptedImageMime,
isImageAttachmentRole,
isOfficeMimeType,
@@ -307,7 +308,8 @@ async function renderCode(
) {
const editor = options.interactive ? getContentEditor(entity, options) : undefined;
const content = editor?.getUnsavedContent() ?? (await entity.getBlob())?.content ?? "";
- const preview = await renderCodePreview(content, entity.mime);
+ const mime = getCodeMime(entity);
+ const preview = await renderCodePreview(content, mime);
if (!editor) {
$renderedContent.append(preview);
@@ -318,7 +320,10 @@ async function renderCode(
const $container = $('<div class="code-embed">');
const container = $container.get(0);
if (container) {
- await mountInteractiveWidget(h(CodeEmbed, { entity, editor, content, preview }), container);
+ await mountInteractiveWidget(
+ h(CodeEmbed, { entity, editor, content, mime, preview }),
+ container
+ );
}
$renderedContent.append($container);
}
@@ -340,6 +345,16 @@ export async function renderCodePreview(content: string, mime: string) {
return $pre[0];
}
+/**
+ * The MIME type that highlights the code of `entity`. An attachment keeps the type the browser
+ * uploaded it with, often a generic one such as `text/plain`, so its file name decides first.
+ */
+function getCodeMime(entity: FNote | FAttachment) {
+ return entity instanceof FAttachment
+ ? getMimeTypeFromFileName(entity.title) ?? entity.mime
+ : entity.mime;
+}
+
/** Saves the changes that rendered content makes to one note or attachment. */
export interface ContentEditor {
/** Whether the rendered content can change the note or attachment. */
diff --git a/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsx b/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsx
index f287acc37b..32a74a8f99 100644
--- a/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsx
@@ -2,17 +2,22 @@ import { render } from "preact";
import { act } from "preact/test-utils";
import { afterEach, describe, expect, it, vi } from "vitest";
+import FAttachment from "../../../entities/fattachment";
import type FNote from "../../../entities/fnote";
import type { ContentEditor } from "../../../services/content_renderer";
+import froca from "../../../services/froca";
import options from "../../../services/options";
import { buildNote } from "../../../test/easy-froca";
import { ParentComponent } from "../../react/react_utils";
import { getContentEmbedTools } from "../text/content_embed_tools";
import CodeEmbed from "./CodeEmbed";
-vi.mock("../../../services/content_renderer", () => ({
- renderCodePreview: async (content: string) => buildPreview(content)
+const renderCodePreview = vi.hoisted(() => vi.fn(async (content: string, _mime: string) => {
+ const pre = document.createElement("pre");
+ pre.textContent = content;
+ return pre;
}));
+vi.mock("../../../services/content_renderer", () => ({ renderCodePreview }));
// `CodeEditor` reads the theme as a string.
options.set("codeNoteTheme", "");
@@ -80,6 +85,35 @@ describe("CodeEmbed", () => {
expect(figure.querySelector(".cm-editor")).toBeNull();
});
+ it("highlights an attachment with the MIME type it is given", async () => {
+ const attachment = new FAttachment(froca, {
+ attachmentId: "script1",
+ ownerId: "owner",
+ role: "file",
+ mime: "text/plain",
+ title: "script.py",
+ dateModified: "",
+ utcDateModified: "",
+ utcDateScheduledForErasureSince: "",
+ contentLength: 0
+ } as never);
+ const { figure } = await mount(attachment, buildEditor(), {
+ content: "a",
+ mime: "text/x-python",
+ isEditable: true
+ });
+ const view = await findView(figure);
+ act(() => view.dispatch({ changes: { from: 1, insert: "b" } }));
+
+ await act(async () => {
+ delete figure.dataset.editable;
+ await Promise.resolve();
+ });
+ await vi.waitFor(() => {
+ expect(renderCodePreview).toHaveBeenLastCalledWith("ab", "text/x-python");
+ });
+ });
+
it("loads the note as saved elsewhere, unless it has unsaved changes", async () => {
const { figure } = await mount(buildCodeNote("saved"), buildEditor(), {
content: "stale",
@@ -103,12 +137,17 @@ describe("CodeEmbed", () => {
interface MountOptions {
content?: string;
+ mime?: string;
preview?: HTMLElement;
isEditable?: boolean;
}
/** Renders `CodeEmbed` in the markup of an embed, as the host does. */
-async function mount(note: FNote, editor: ContentEditor, options: MountOptions = {}) {
+async function mount(
+ entity: FNote | FAttachment,
+ editor: ContentEditor,
+ options: MountOptions = {}
+) {
const content = options.content ?? "";
const figure = document.createElement("figure");
figure.className = "include-note";
@@ -125,9 +164,10 @@ async function mount(note: FNote, editor: ContentEditor, options: MountOptions =
render(
<ParentComponent.Provider value={parent}>
<CodeEmbed
- entity={note}
+ entity={entity}
editor={editor}
- content={options.content ?? (await note.getBlob())?.content ?? content}
+ content={options.content ?? (await entity.getBlob())?.content ?? content}
+ mime={options.mime ?? entity.mime}
preview={options.preview ?? buildPreview(content)}
/>
</ParentComponent.Provider>,
diff --git a/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx b/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx
index 0452600644..d5228c66c7 100644
--- a/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx
+++ b/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx
@@ -21,6 +21,8 @@ interface CodeEmbedProps {
editor: ContentEditor;
/** The content shown first. */
content: string;
+ /** The MIME type that highlights the content. A note follows its own `mime` instead. */
+ mime: string;
/** The highlighted `content`. */
preview: HTMLElement;
}
@@ -30,7 +32,7 @@ interface CodeEmbedProps {
* Editable toggle of its embed is on.
*/
export default function CodeEmbed({
- entity, editor, content: initialContent, preview
+ entity, editor, content: initialContent, mime: initialMime, preview
}: CodeEmbedProps) {
const rootRef = useRef<HTMLDivElement>(null);
const note = entity instanceof FNote ? entity : null;
@@ -39,12 +41,12 @@ export default function CodeEmbed({
const isEmbedEditable = useIsContentEmbedEditable(rootRef);
const isEditing = canEdit && isEmbedEditable;
const [ content, setContent ] = useState(initialContent);
- const mime = useNoteProperty(note, "mime") ?? entity.mime;
+ const mime = useNoteProperty(note, "mime") ?? initialMime;
useEditableFlag(rootRef, canEdit);
useChangesFromElsewhere(note, editor, setContent);
useFocusFromEmbedBox(rootRef, isEditing);
- const isInitial = content === initialContent && mime === entity.mime;
+ const isInitial = content === initialContent && mime === initialMime;
const previewView = (
<CodePreview
content={content}
diff --git a/packages/commons/src/lib/mime_type.spec.ts b/packages/commons/src/lib/mime_type.spec.ts
index 0632c2b6d6..9ca29bcd17 100644
--- a/packages/commons/src/lib/mime_type.spec.ts
+++ b/packages/commons/src/lib/mime_type.spec.ts
@@ -1,6 +1,9 @@
import { describe, expect, it } from "vitest";
-import { getMimeTypeFromMarkdownName, MIME_TYPE_AUTO, MIME_TYPES_DICT, normalizeMimeTypeForCKEditor } from "./mime_type.js";
+import {
+ getMimeTypeFromFileName, getMimeTypeFromMarkdownName, MIME_TYPE_AUTO, MIME_TYPES_DICT,
+ normalizeMimeTypeForCKEditor
+} from "./mime_type.js";
describe("normalizeMimeTypeForCKEditor", () => {
it("collapses non-word characters and underscores into single dashes", () => {
@@ -77,3 +80,21 @@ describe("exports", () => {
expect(Object.isFrozen(MIME_TYPES_DICT)).toBe(true);
});
});
+
+describe("getMimeTypeFromFileName", () => {
+ it("names the language of a file by its extension, in any case", () => {
+ expect([
+ "app.js", "app.MJS", "types.ts", "script.py", "main.rb", "lib.rs", "Main.kt",
+ "Program.cs", "header.h", "README.md", "page.htm", "build.ps1", "Dockerfile"
+ ].map(getMimeTypeFromFileName)).toEqual([
+ "text/javascript", "text/javascript", "application/typescript", "text/x-python",
+ "text/x-ruby", "text/x-rustsrc", "text/x-kotlin", "text/x-csharp", "text/x-csrc",
+ "text/x-gfm", "text/html", "application/x-powershell", "text/x-dockerfile"
+ ]);
+ });
+
+ it("has no type for a file without a known language", () => {
+ expect([ "notes.txt", "server.log", "archive", ".", "data.unknown" ]
+ .map(getMimeTypeFromFileName)).toEqual(Array(5).fill(undefined));
+ });
+});
diff --git a/packages/commons/src/lib/mime_type.ts b/packages/commons/src/lib/mime_type.ts
index 30788e78fb..1c1b28c1bf 100644
--- a/packages/commons/src/lib/mime_type.ts
+++ b/packages/commons/src/lib/mime_type.ts
@@ -251,3 +251,40 @@ export function getMimeTypeFromMarkdownName(mdLanguageCode: string) {
return byMarkdownNameMappings[mdLanguageCode];
}
+
+/**
+ * The code MIME type of a file, from its extension, such as `text/x-python` for `script.py`, or
+ * `undefined` for a file without a known language.
+ */
+export function getMimeTypeFromFileName(fileName: string) {
+ const lowerCaseName = fileName.toLowerCase();
+ const extension = lowerCaseName.includes(".")
+ ? lowerCaseName.slice(lowerCaseName.lastIndexOf(".") + 1)
+ : lowerCaseName;
+ return getMimeTypeFromMarkdownName(LANGUAGES_BY_EXTENSION[extension] ?? extension)?.mime;
+}
+
+/** The Markdown language names of the file extensions that are not language names themselves. */
+const LANGUAGES_BY_EXTENSION: Record<string, string> = {
+ cc: "cpp",
+ cjs: "javascript",
+ clj: "clojure",
+ cs: "csharp",
+ cxx: "cpp",
+ erl: "erlang",
+ ex: "elixir",
+ exs: "elixir",
+ h: "c",
+ hpp: "cpp",
+ hs: "haskell",
+ htm: "html",
+ kt: "kotlin",
+ kts: "kotlin",
+ m: "objectivec",
+ md: "markdown",
+ mjs: "javascript",
+ pl: "perl",
+ ps1: "powershell",
+ rb: "ruby",
+ rs: "rust"
+};