Skip to content

Build your personal knowledge base with Trilium Notes

fix(import): resolve java, json and sql code blocks to the right language

Elian Doran8aca78154309
diff --git a/packages/ckeditor5/src/plugins/paste_from_vscode.ts b/packages/ckeditor5/src/plugins/paste_from_vscode.ts
index a13712a0b8..a53d0ca31d 100644
--- a/packages/ckeditor5/src/plugins/paste_from_vscode.ts
+++ b/packages/ckeditor5/src/plugins/paste_from_vscode.ts
@@ -5,29 +5,6 @@ import type { ViewDocumentClipboardInputEvent } from "ckeditor5";
/** The clipboard type VS Code writes next to the copied text, holding the language of the editor. */
export const VSCODE_EDITOR_DATA = "vscode-editor-data";
-/**
- * VS Code language IDs that `getMimeTypeFromMarkdownName()` does not resolve, or resolves to a
- * variant (`java` to JSP, `json` to JSON-LD, `sql` to MariaDB).
- */
-const VSCODE_LANGUAGE_MIMES: Record<string, string> = {
- bat: "application/x-bat",
- dockercompose: "text/x-yaml",
- java: "text/x-java",
- javascriptreact: "text/jsx",
- json: "application/json",
- jsonc: "application/json",
- "objective-c": "text/x-objectivec",
- perl: "text/x-perl",
- php: "text/x-php",
- shellscript: "text/x-sh",
- sql: "text/x-sql",
- swift: "text/x-swift",
- terraform: "text/x-hcl",
- toml: "text/x-toml",
- typescriptreact: "text/typescript-jsx",
- vb: "text/x-vb"
-};
-
/**
* Pastes code copied from VS Code as code: a single line as inline code, several lines as a code
* block in the language of the VS Code editor.
@@ -97,7 +74,7 @@ export function vsCodePasteHtml(dataTransfer: { getData(type: string): string })
* language ID, or `null` if Trilium has no MIME type for it.
*/
export function codeBlockLanguage(mode: string): string | null {
- const mime = VSCODE_LANGUAGE_MIMES[mode] ?? getMimeTypeFromMarkdownName(mode)?.mime;
+ const mime = getMimeTypeFromMarkdownName(mode)?.mime;
return mime ? normalizeMimeTypeForCKEditor(mime) : null;
}
diff --git a/packages/commons/src/lib/mime_type.spec.ts b/packages/commons/src/lib/mime_type.spec.ts
index 8b5f5ac691..0632c2b6d6 100644
--- a/packages/commons/src/lib/mime_type.spec.ts
+++ b/packages/commons/src/lib/mime_type.spec.ts
@@ -33,6 +33,28 @@ describe("getMimeTypeFromMarkdownName", () => {
expect(result?.mime).toBe("text/javascript");
});
+ it("prefers the entry that lists the tag as an alias over earlier variants", () => {
+ expect(getMimeTypeFromMarkdownName("java")?.mime).toBe("text/x-java");
+ expect(getMimeTypeFromMarkdownName("json")?.mime).toBe("application/json");
+ expect(getMimeTypeFromMarkdownName("sql")?.mime).toBe("text/x-sql");
+ });
+
+ it("resolves the alternative names used by Markdown fences, PrismJS and VS Code", () => {
+ expect(getMimeTypeFromMarkdownName("bash")?.mime).toBe("text/x-sh");
+ expect(getMimeTypeFromMarkdownName("shellscript")?.mime).toBe("text/x-sh");
+ expect(getMimeTypeFromMarkdownName("ts")?.mime).toBe("application/typescript");
+ expect(getMimeTypeFromMarkdownName("typescriptreact")?.mime).toBe("text/typescript-jsx");
+ expect(getMimeTypeFromMarkdownName("jsonc")?.mime).toBe("application/json");
+ expect(getMimeTypeFromMarkdownName("yml")?.mime).toBe("text/x-yaml");
+ expect(getMimeTypeFromMarkdownName("php")?.mime).toBe("text/x-php");
+ expect(getMimeTypeFromMarkdownName("clike")?.mime).toBe("text/x-csrc");
+ });
+
+ it("lists each alias on one entry only", () => {
+ const aliases = MIME_TYPES_DICT.flatMap((mimeType) => mimeType.aliases ?? []);
+ expect(new Set(aliases).size).toBe(aliases.length);
+ });
+
it("returns undefined for an unknown language tag", () => {
expect(getMimeTypeFromMarkdownName("definitely-not-a-language")).toBeUndefined();
});
diff --git a/packages/commons/src/lib/mime_type.ts b/packages/commons/src/lib/mime_type.ts
index f13776d3e5..30788e78fb 100644
--- a/packages/commons/src/lib/mime_type.ts
+++ b/packages/commons/src/lib/mime_type.ts
@@ -16,6 +16,12 @@ export interface MimeTypeDefinition {
mime: string;
/** The name of the language/mime type as defined by highlight.js (or one of the aliases), in order to be used for syntax highlighting such as inside code blocks. */
mdLanguageCode?: string;
+ /**
+ * Other language names that resolve to this entry in `getMimeTypeFromMarkdownName()`: Markdown
+ * fence names, PrismJS and VS Code language IDs. An alias takes precedence over another entry's
+ * `mdLanguageCode`.
+ */
+ aliases?: string[];
icon?: string;
}
@@ -50,9 +56,9 @@ const MIME_TYPES_DICT_RAW = [
{ title: "ASN.1", mime: "text/x-ttcn-asn" },
{ title: "ASP.NET", mime: "application/x-aspx" },
{ title: "Asterisk", mime: "text/x-asterisk" },
- { title: "Batch file (DOS)", mime: "application/x-bat", mdLanguageCode: "dos", icon: "bx bx-terminal" },
+ { title: "Batch file (DOS)", mime: "application/x-bat", mdLanguageCode: "dos", aliases: ["bat", "batch"], icon: "bx bx-terminal" },
{ title: "Brainfuck", mime: "text/x-brainfuck", mdLanguageCode: "brainfuck" },
- { title: "C", mime: "text/x-csrc", mdLanguageCode: "c", default: true },
+ { title: "C", mime: "text/x-csrc", mdLanguageCode: "c", aliases: ["clike"], default: true },
{ title: "C#", mime: "text/x-csharp", mdLanguageCode: "csharp", default: true },
{ title: "C++", mime: "text/x-c++src", mdLanguageCode: "cpp", default: true, icon: "bx bxl-c-plus-plus" },
{ title: "Clojure", mime: "text/x-clojure", mdLanguageCode: "clojure" },
@@ -104,14 +110,14 @@ const MIME_TYPES_DICT_RAW = [
{ title: "HXML", mime: "text/x-hxml" },
{ title: "IDL", mime: "text/x-idl" },
{ title: "Java Server Pages", mime: "application/x-jsp", mdLanguageCode: "java", icon: "bx bxl-java" },
- { title: "Java", mime: "text/x-java", mdLanguageCode: "java", default: true, icon: "bx bxl-java" },
- { title: "JavaScript", mime: "text/javascript", mdLanguageCode: "javascript", default: true, icon: "bx bxl-javascript" },
+ { title: "Java", mime: "text/x-java", mdLanguageCode: "java", aliases: ["java"], default: true, icon: "bx bxl-java" },
+ { title: "JavaScript", mime: "text/javascript", mdLanguageCode: "javascript", aliases: ["js"], default: true, icon: "bx bxl-javascript" },
{ title: "JavaScript (Trilium backend)", mime: "application/javascript;env=backend", mdLanguageCode: "javascript", default: true, icon: "bx bxl-javascript" },
{ title: "JavaScript (Trilium frontend)", mime: "application/javascript;env=frontend", mdLanguageCode: "javascript", default: true, icon: "bx bxl-javascript" },
{ title: "Jinja2", mime: "text/jinja2" },
{ title: "JSON-LD", mime: "application/ld+json", mdLanguageCode: "json", icon: "bx bxs-file-json" },
- { title: "JSON", mime: "application/json", mdLanguageCode: "json", default: true, icon: "bx bxs-file-json" },
- { title: "JSX", mime: "text/jsx", mdLanguageCode: "jsx", default: true },
+ { title: "JSON", mime: "application/json", mdLanguageCode: "json", aliases: ["json", "jsonc"], default: true, icon: "bx bxs-file-json" },
+ { title: "JSX", mime: "text/jsx", mdLanguageCode: "jsx", aliases: ["javascriptreact"], default: true },
{ title: "Julia", mime: "text/x-julia", mdLanguageCode: "julia" },
{ title: "Kotlin", mime: "text/x-kotlin", mdLanguageCode: "kotlin", default: true },
{ title: "KDL", mime: "application/vnd.kdl", mdLanguageCode: "kdl" },
@@ -136,15 +142,15 @@ const MIME_TYPES_DICT_RAW = [
{ title: "Nix", mime: "text/x-nix", mdLanguageCode: "nix" },
{ title: "NSIS", mime: "text/x-nsis", mdLanguageCode: "nsis" },
{ title: "NTriples", mime: "application/n-triples" },
- { title: "Objective-C", mime: "text/x-objectivec", mdLanguageCode: "objectivec" },
+ { title: "Objective-C", mime: "text/x-objectivec", mdLanguageCode: "objectivec", aliases: ["objective-c", "objc"] },
{ title: "OCaml", mime: "text/x-ocaml", mdLanguageCode: "ocaml" },
{ title: "Octave", mime: "text/x-octave" },
{ title: "Oz", mime: "text/x-oz" },
{ title: "Pascal", mime: "text/x-pascal", mdLanguageCode: "delphi" },
{ title: "PEG.js", mime: "text/x-pegjs" },
- { title: "Perl", mime: "text/x-perl", default: true },
+ { title: "Perl", mime: "text/x-perl", aliases: ["perl"], default: true },
{ title: "PGP", mime: "application/pgp" },
- { title: "PHP", mime: "text/x-php", default: true, icon: "bx bxl-php" },
+ { title: "PHP", mime: "text/x-php", aliases: ["php"], default: true, icon: "bx bxl-php" },
{ title: "Pig", mime: "text/x-pig" },
{ title: "PLSQL", mime: "text/x-plsql", mdLanguageCode: "sql" },
{ title: "PostgreSQL", mime: "text/x-pgsql", mdLanguageCode: "pgsql", icon: "bx bxl-postgresql" },
@@ -153,7 +159,7 @@ const MIME_TYPES_DICT_RAW = [
{ title: "ProtoBuf", mime: "text/x-protobuf", mdLanguageCode: "protobuf" },
{ title: "Pug", mime: "text/x-pug" },
{ title: "Puppet", mime: "text/x-puppet", mdLanguageCode: "puppet" },
- { title: "Python", mime: "text/x-python", mdLanguageCode: "python", default: true, icon: "bx bxl-python" },
+ { title: "Python", mime: "text/x-python", mdLanguageCode: "python", aliases: ["py"], default: true, icon: "bx bxl-python" },
{ title: "Q", mime: "text/x-q", mdLanguageCode: "q" },
{ title: "R", mime: "text/x-rsrc", mdLanguageCode: "r" },
{ title: "reStructuredText", mime: "text/x-rst" },
@@ -166,7 +172,7 @@ const MIME_TYPES_DICT_RAW = [
{ title: "Scala", mime: "text/x-scala" },
{ title: "Scheme", mime: "text/x-scheme" },
{ title: "SCSS", mime: "text/x-scss", mdLanguageCode: "scss" },
- { title: "Shell (bash)", mime: "text/x-sh", mdLanguageCode: "sh", default: true, icon: "bx bx-terminal" },
+ { title: "Shell (bash)", mime: "text/x-sh", mdLanguageCode: "sh", aliases: ["bash", "shell", "shellscript", "zsh"], default: true, icon: "bx bx-terminal" },
{ title: "Sieve", mime: "application/sieve" },
{ title: "Slim", mime: "text/x-slim" },
{ title: "Smalltalk", mime: "text/x-stsrc", mdLanguageCode: "smalltalk" },
@@ -176,20 +182,20 @@ const MIME_TYPES_DICT_RAW = [
{ title: "Soy", mime: "text/x-soy" },
{ title: "SPARQL", mime: "application/sparql-query" },
{ title: "Spreadsheet", mime: "text/x-spreadsheet" },
- { title: "SQL", mime: "text/x-sql", mdLanguageCode: "sql", default: true, icon: "bx bx-data" },
+ { title: "SQL", mime: "text/x-sql", mdLanguageCode: "sql", aliases: ["sql"], default: true, icon: "bx bx-data" },
{ title: "SQLite (Trilium)", mime: "text/x-sqlite;schema=trilium", mdLanguageCode: "sql", default: true, icon: "bx bx-data" },
{ title: "SQLite", mime: "text/x-sqlite", mdLanguageCode: "sql", icon: "bx bx-data" },
{ title: "Squirrel", mime: "text/x-squirrel" },
{ title: "sTeX", mime: "text/x-stex" },
{ title: "Stylus", mime: "text/x-styl", mdLanguageCode: "stylus" },
- { title: "Swift", mime: "text/x-swift", default: true },
+ { title: "Swift", mime: "text/x-swift", aliases: ["swift"], default: true },
{ title: "SystemVerilog", mime: "text/x-systemverilog" },
{ title: "Tcl", mime: "text/x-tcl", mdLanguageCode: "tcl" },
- { title: "Terraform (HCL)", mime: "text/x-hcl", mdLanguageCode: "terraform" },
+ { title: "Terraform (HCL)", mime: "text/x-hcl", mdLanguageCode: "terraform", aliases: ["hcl"] },
{ title: "Textile", mime: "text/x-textile" },
{ title: "TiddlyWiki ", mime: "text/x-tiddlywiki" },
{ title: "Tiki wiki", mime: "text/tiki" },
- { title: "TOML", mime: "text/x-toml", mdLanguageCode: "ini", icon: "bx bx-bracket" },
+ { title: "TOML", mime: "text/x-toml", mdLanguageCode: "ini", aliases: ["toml"], icon: "bx bx-bracket" },
{ title: "Tornado", mime: "text/x-tornado" },
{ title: "Trilium Log", mime: "text/x-trilium-log", icon: "bx bx-list-ul" },
{ title: "troff", mime: "text/troff" },
@@ -197,9 +203,9 @@ const MIME_TYPES_DICT_RAW = [
{ title: "TTCN", mime: "text/x-ttcn" },
{ title: "Turtle", mime: "text/turtle" },
{ title: "Twig", mime: "text/x-twig", mdLanguageCode: "twig" },
- { title: "TypeScript-JSX", mime: "text/typescript-jsx" },
- { title: "TypeScript", mime: "application/typescript", mdLanguageCode: "typescript", icon: "bx bxl-typescript" },
- { title: "VB.NET", mime: "text/x-vb", mdLanguageCode: "vbnet" },
+ { title: "TypeScript-JSX", mime: "text/typescript-jsx", aliases: ["tsx", "typescriptreact"] },
+ { title: "TypeScript", mime: "application/typescript", mdLanguageCode: "typescript", aliases: ["ts"], icon: "bx bxl-typescript" },
+ { title: "VB.NET", mime: "text/x-vb", mdLanguageCode: "vbnet", aliases: ["vb"] },
{ title: "VBScript", mime: "text/vbscript", mdLanguageCode: "vbscript" },
{ title: "Velocity", mime: "text/velocity" },
{ title: "Verilog", mime: "text/x-verilog", mdLanguageCode: "verilog" },
@@ -210,7 +216,7 @@ const MIME_TYPES_DICT_RAW = [
{ title: "XQuery", mime: "application/xquery", mdLanguageCode: "xquery" },
{ title: "xu", mime: "text/x-xu" },
{ title: "Yacas", mime: "text/x-yacas" },
- { title: "YAML", mime: "text/x-yaml", mdLanguageCode: "yaml", default: true },
+ { title: "YAML", mime: "text/x-yaml", mdLanguageCode: "yaml", aliases: ["yml", "dockercompose"], default: true },
{ title: "Z80", mime: "text/x-z80" }
] as const satisfies readonly MimeTypeDefinition[];
export const MIME_TYPES_DICT = Object.freeze(MIME_TYPES_DICT_RAW as readonly MimeTypeDefinition[]);
@@ -223,7 +229,7 @@ export type SupportedMimeTypes = typeof MIME_TYPES_DICT_RAW[number]["mime"] | Me
/**
* Given a Markdown language tag (e.g. `css`), it returns a corresponding {@link MimeTypeDefinition} if found.
*
- * If there are multiple {@link MimeTypeDefinition}s for the language tag, then only the first one is retrieved. For example for `javascript`, the plain "JavaScript" (`text/javascript`) mime type is returned rather than the Trilium frontend/backend script variants.
+ * An entry that lists the tag in its `aliases` wins. Otherwise, if there are multiple {@link MimeTypeDefinition}s for the language tag, then only the first one is retrieved. For example for `javascript`, the plain "JavaScript" (`text/javascript`) mime type is returned rather than the Trilium frontend/backend script variants.
*
* @param mdLanguageCode a language tag.
* @returns the corresponding {@link MimeTypeDefinition} if found, or `undefined` otherwise.
@@ -231,6 +237,11 @@ export type SupportedMimeTypes = typeof MIME_TYPES_DICT_RAW[number]["mime"] | Me
export function getMimeTypeFromMarkdownName(mdLanguageCode: string) {
if (!byMarkdownNameMappings) {
byMarkdownNameMappings = {};
+ for (const mimeType of MIME_TYPES_DICT) {
+ for (const alias of mimeType.aliases ?? []) {
+ byMarkdownNameMappings[alias] = mimeType;
+ }
+ }
for (const mimeType of MIME_TYPES_DICT) {
if (mimeType.mdLanguageCode && !byMarkdownNameMappings[mimeType.mdLanguageCode]) {
byMarkdownNameMappings[mimeType.mdLanguageCode] = mimeType;
diff --git a/packages/trilium-core/src/services/import/anytype/content.ts b/packages/trilium-core/src/services/import/anytype/content.ts
index 8835d65cd6..ffc80dff53 100644
--- a/packages/trilium-core/src/services/import/anytype/content.ts
+++ b/packages/trilium-core/src/services/import/anytype/content.ts
@@ -474,13 +474,6 @@ export function renderBookmark(bookmark: NonNullable<AnytypeBlock["bookmark"]>):
// #endregion
// #region Code blocks
-// PrismJS language ids Anytype uses that don't line up with a Trilium markdown language code. Most ids
-// (javascript, python, go, rust, …) match directly; only the mismatches need listing here. `clike` is
-// PrismJS's generic C-family base, mapped to plain C as the closest concrete language.
-const LANGUAGE_ALIASES: Record<string, string> = {
- clike: "c"
-};
-
/**
* Renders an Anytype `Code`-style block as a Trilium/CKEditor code block. Anytype tags the block with a
* PrismJS language id in `fields.lang`; we map that to a Trilium MIME and emit it as the CKEditor
@@ -495,7 +488,7 @@ export function renderCodeBlock(text: string, lang: string | undefined): string
/** The CKEditor code-block language class value for an Anytype language id, or auto-detect when unknown. */
function codeLanguage(lang: string | undefined): string {
if (lang) {
- const mimeDefinition = getMimeTypeFromMarkdownName(LANGUAGE_ALIASES[lang] ?? lang);
+ const mimeDefinition = getMimeTypeFromMarkdownName(lang);
if (mimeDefinition) {
return normalizeMimeTypeForCKEditor(mimeDefinition.mime);
}