Skip to content

Build your personal knowledge base with Trilium Notes

refactor(commons): deduplicate mime_type_definitions

Elian Doran1a4d1bc791b5
diff --git a/apps/client/src/services/mime_type_definitions.ts b/apps/client/src/services/mime_type_definitions.ts
deleted file mode 100644
index 5ea70d6b8e..0000000000
--- a/apps/client/src/services/mime_type_definitions.ts
+++ /dev/null
@@ -1,203 +0,0 @@
-// TODO: deduplicate with /src/services/import/mime_type_definitions.ts
-
-import type { MimeTypeDefinition } from "@triliumnext/commons";
-
-/**
- * A pseudo-MIME type which is used in the editor to automatically determine the language used in code blocks via heuristics.
- */
-export const MIME_TYPE_AUTO = "text-x-trilium-auto";
-
-
-/**
- * For highlight.js-supported languages, see https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md.
- */
-
-export const MIME_TYPES_DICT: readonly MimeTypeDefinition[] = Object.freeze([
- { title: "Plain text", mime: "text/plain", default: true },
-
- // Keep sorted alphabetically.
- { title: "APL", mime: "text/apl" },
- { 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", codeMirrorSource: "libraries/codemirror/batch.js" },
- { title: "Brainfuck", mime: "text/x-brainfuck" },
- { title: "C", mime: "text/x-csrc", default: true },
- { title: "C#", mime: "text/x-csharp", default: true },
- { title: "C++", mime: "text/x-c++src", default: true },
- { title: "Clojure", mime: "text/x-clojure" },
- { title: "ClojureScript", mime: "text/x-clojurescript" },
- { title: "Closure Stylesheets (GSS)", mime: "text/x-gss" },
- { title: "CMake", mime: "text/x-cmake" },
- { title: "Cobol", mime: "text/x-cobol" },
- { title: "CoffeeScript", mime: "text/coffeescript" },
- { title: "Common Lisp", mime: "text/x-common-lisp" },
- { title: "CQL", mime: "text/x-cassandra" },
- { title: "Crystal", mime: "text/x-crystal" },
- { title: "CSS", mime: "text/css", default: true },
- { title: "Cypher", mime: "application/x-cypher-query" },
- { title: "Cython", mime: "text/x-cython" },
- { title: "D", mime: "text/x-d" },
- { title: "Dart", mime: "application/dart" },
- { title: "diff", mime: "text/x-diff" },
- { title: "Django", mime: "text/x-django" },
- { title: "Dockerfile", mime: "text/x-dockerfile" },
- { title: "DTD", mime: "application/xml-dtd" },
- { title: "Dylan", mime: "text/x-dylan" },
- { title: "EBNF", mime: "text/x-ebnf" },
- { title: "ECL", mime: "text/x-ecl" },
- { title: "edn", mime: "application/edn" },
- { title: "Eiffel", mime: "text/x-eiffel" },
- { title: "Elm", mime: "text/x-elm" },
- { title: "Embedded Javascript", mime: "application/x-ejs" },
- { title: "Embedded Ruby", mime: "application/x-erb" },
- { title: "Erlang", mime: "text/x-erlang" },
- { title: "Esper", mime: "text/x-esper" },
- { title: "F#", mime: "text/x-fsharp" },
- { title: "Factor", mime: "text/x-factor" },
- { title: "FCL", mime: "text/x-fcl" },
- { title: "Forth", mime: "text/x-forth" },
- { title: "Fortran", mime: "text/x-fortran" },
- { title: "Gas", mime: "text/x-gas" },
- { title: "GDScript (Godot)", mime: "text/x-gdscript" },
- { title: "Gherkin", mime: "text/x-feature" },
- { title: "GitHub Flavored Markdown", mime: "text/x-gfm" },
- { title: "Go", mime: "text/x-go", default: true },
- { title: "Groovy", mime: "text/x-groovy", default: true },
- { title: "HAML", mime: "text/x-haml" },
- { title: "Haskell (Literate)", mime: "text/x-literate-haskell" },
- { title: "Haskell", mime: "text/x-haskell", default: true },
- { title: "Haxe", mime: "text/x-haxe" },
- { title: "HTML", mime: "text/html", default: true },
- { title: "HTTP", mime: "message/http", default: true },
- { title: "HXML", mime: "text/x-hxml" },
- { title: "IDL", mime: "text/x-idl" },
- { title: "Java Server Pages", mime: "application/x-jsp" },
- { title: "Java", mime: "text/x-java", default: true },
- { title: "Jinja2", mime: "text/jinja2" },
- { title: "JS backend", mime: "application/javascript;env=backend", default: true },
- { title: "JS frontend", mime: "application/javascript;env=frontend", default: true },
- { title: "JSON-LD", mime: "application/ld+json"},
- { title: "JSON", mime: "application/json" },
- { title: "JSX", mime: "text/jsx" },
- { title: "Julia", mime: "text/x-julia" },
- { title: "Kotlin", mime: "text/x-kotlin", default: true },
- { title: "LaTeX", mime: "text/x-latex" },
- { title: "LESS", mime: "text/x-less" },
- { title: "LiveScript", mime: "text/x-livescript" },
- { title: "Lua", mime: "text/x-lua" },
- { title: "MariaDB SQL", mime: "text/x-mariadb" },
- { title: "Markdown", mime: "text/x-markdown", default: true },
- { title: "Mathematica", mime: "text/x-mathematica" },
- { title: "mbox", mime: "application/mbox" },
- { title: "MIPS Assembler", mime: "text/x-asm-mips" },
- { title: "mIRC", mime: "text/mirc" },
- { title: "Modelica", mime: "text/x-modelica" },
- { title: "MS SQL", mime: "text/x-mssql" },
- { title: "mscgen", mime: "text/x-mscgen" },
- { title: "msgenny", mime: "text/x-msgenny" },
- { title: "MUMPS", mime: "text/x-mumps" },
- { title: "MySQL", mime: "text/x-mysql" },
- { title: "Nix", mime: "text/x-nix" },
- { title: "Nginx", mime: "text/x-nginx-conf" },
- { title: "NSIS", mime: "text/x-nsis" },
- { title: "NTriples", mime: "application/n-triples" },
- { title: "Objective-C", mime: "text/x-objectivec" },
- { title: "OCaml", mime: "text/x-ocaml" },
- { title: "Octave", mime: "text/x-octave" },
- { title: "Oz", mime: "text/x-oz" },
- { title: "Pascal", mime: "text/x-pascal" },
- { title: "PEG.js", mime: "null" },
- { title: "Perl", mime: "text/x-perl", default: true },
- { title: "PGP", mime: "application/pgp" },
- { title: "PHP", mime: "text/x-php", default: true },
- { title: "Pig", mime: "text/x-pig" },
- { title: "PLSQL", mime: "text/x-plsql" },
- { title: "PostgreSQL", mime: "text/x-pgsql" },
- { title: "PowerShell", mime: "application/x-powershell" },
- { title: "Properties files", mime: "text/x-properties" },
- { title: "ProtoBuf", mime: "text/x-protobuf" },
- { title: "Pug", mime: "text/x-pug" },
- { title: "Puppet", mime: "text/x-puppet" },
- { title: "Python", mime: "text/x-python", default: true },
- { title: "Q", mime: "text/x-q" },
- { title: "R", mime: "text/x-rsrc" },
- { title: "reStructuredText", mime: "text/x-rst" },
- { title: "RPM Changes", mime: "text/x-rpm-changes" },
- { title: "RPM Spec", mime: "text/x-rpm-spec" },
- { title: "Ruby", mime: "text/x-ruby", default: true },
- { title: "Rust", mime: "text/x-rustsrc" },
- { title: "SAS", mime: "text/x-sas" },
- { title: "Sass", mime: "text/x-sass" },
- { title: "Scala", mime: "text/x-scala" },
- { title: "Scheme", mime: "text/x-scheme" },
- { title: "SCSS", mime: "text/x-scss" },
- { title: "Shell (bash)", mime: "text/x-sh", default: true },
- { title: "Sieve", mime: "application/sieve" },
- { title: "Slim", mime: "text/x-slim" },
- { title: "Smalltalk", mime: "text/x-stsrc" },
- { title: "Smarty", mime: "text/x-smarty" },
- { title: "SML", mime: "text/x-sml" },
- { title: "Solr", mime: "text/x-solr" },
- { title: "Soy", mime: "text/x-soy" },
- { title: "SPARQL", mime: "application/sparql-query" },
- { title: "Spreadsheet", mime: "text/x-spreadsheet" },
- { title: "SQL", mime: "text/x-sql", default: true },
- { title: "SQLite (Trilium)", mime: "text/x-sqlite;schema=trilium", default: true },
- { title: "SQLite", mime: "text/x-sqlite" },
- { title: "Squirrel", mime: "text/x-squirrel" },
- { title: "sTeX", mime: "text/x-stex" },
- { title: "Stylus", mime: "text/x-styl" },
- { title: "Swift", mime: "text/x-swift", default: true },
- { title: "SystemVerilog", mime: "text/x-systemverilog" },
- { title: "Tcl", mime: "text/x-tcl" },
- { title: "Terraform (HCL)", mime: "text/x-hcl", codeMirrorSource: "libraries/codemirror/hcl.js" },
- { title: "Textile", mime: "text/x-textile" },
- { title: "TiddlyWiki ", mime: "text/x-tiddlywiki" },
- { title: "Tiki wiki", mime: "text/tiki" },
- { title: "TOML", mime: "text/x-toml" },
- { title: "Tornado", mime: "text/x-tornado" },
- { title: "troff", mime: "text/troff" },
- { title: "TTCN_CFG", mime: "text/x-ttcn-cfg" },
- { title: "TTCN", mime: "text/x-ttcn" },
- { title: "Turtle", mime: "text/turtle" },
- { title: "Twig", mime: "text/x-twig" },
- { title: "TypeScript-JSX", mime: "text/typescript-jsx" },
- { title: "TypeScript", mime: "application/typescript" },
- { title: "VB.NET", mime: "text/x-vb" },
- { title: "VBScript", mime: "text/vbscript" },
- { title: "Velocity", mime: "text/velocity" },
- { title: "Verilog", mime: "text/x-verilog" },
- { title: "VHDL", mime: "text/x-vhdl" },
- { title: "Vue.js Component", mime: "text/x-vue" },
- { title: "Web IDL", mime: "text/x-webidl" },
- { title: "XML", mime: "text/xml", default: true },
- { title: "XQuery", mime: "application/xquery" },
- { title: "xu", mime: "text/x-xu" },
- { title: "Yacas", mime: "text/x-yacas" },
- { title: "YAML", mime: "text/x-yaml", default: true },
- { title: "Z80", mime: "text/x-z80" }
-]);
-
-let byHighlightJsNameMappings: Record<string, MimeTypeDefinition> | null = null;
-
-/**
- * Given a Highlight.js 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 "JS frontend" mime type is returned.
- *
- * @param highlightJsName a language tag.
- * @returns the corresponding {@link MimeTypeDefinition} if found, or `undefined` otherwise.
- */
-export function getMimeTypeFromHighlightJs(highlightJsName: string) {
- if (!byHighlightJsNameMappings) {
- byHighlightJsNameMappings = {};
- for (const mimeType of MIME_TYPES_DICT) {
- if (mimeType.highlightJs && !byHighlightJsNameMappings[mimeType.highlightJs]) {
- byHighlightJsNameMappings[mimeType.highlightJs] = mimeType;
- }
- }
- }
-
- return byHighlightJsNameMappings[highlightJsName];
-}
diff --git a/apps/client/src/services/mime_types.ts b/apps/client/src/services/mime_types.ts
index e009cb50f8..4b821e4a45 100644
--- a/apps/client/src/services/mime_types.ts
+++ b/apps/client/src/services/mime_types.ts
@@ -1,5 +1,4 @@
-import { normalizeMimeTypeForCKEditor, type MimeType } from "@triliumnext/commons";
-import { MIME_TYPE_AUTO, MIME_TYPES_DICT } from "./mime_type_definitions.js";
+import { normalizeMimeTypeForCKEditor, type MimeType, MIME_TYPE_AUTO, MIME_TYPES_DICT } from "@triliumnext/commons";
import options from "./options.js";
let mimeTypes: MimeType[] | null = null;
diff --git a/apps/client/src/widgets/type_widgets/ckeditor/config.ts b/apps/client/src/widgets/type_widgets/ckeditor/config.ts
index dc1a8b77a8..2f1244f395 100644
--- a/apps/client/src/widgets/type_widgets/ckeditor/config.ts
+++ b/apps/client/src/widgets/type_widgets/ckeditor/config.ts
@@ -1,5 +1,5 @@
import { ALLOWED_PROTOCOLS } from "../../../services/link.js";
-import { MIME_TYPE_AUTO } from "../../../services/mime_type_definitions.js";
+import { MIME_TYPE_AUTO } from "@triliumnext/commons";
import { getHighlightJsNameForMime } from "../../../services/mime_types.js";
import options from "../../../services/options.js";
import { ensureMimeTypesForHighlighting, isSyntaxHighlightEnabled } from "../../../services/syntax_highlight.js";
diff --git a/apps/server/src/services/import/markdown.ts b/apps/server/src/services/import/markdown.ts
index 3df4249a11..6996ed42de 100644
--- a/apps/server/src/services/import/markdown.ts
+++ b/apps/server/src/services/import/markdown.ts
@@ -1,6 +1,12 @@
"use strict";
import { parse, Renderer, type Tokens } from "marked";
+import htmlSanitizer from "../html_sanitizer.js";
+import importUtils from "./utils.js";
+import { getMimeTypeFromHighlightJs, MIME_TYPE_AUTO } from "@triliumnext/commons";
+import { ADMONITION_TYPE_MAPPINGS } from "../export/markdown.js";
+import utils from "../utils.js";
+import { normalizeMimeTypeForCKEditor } from "@triliumnext/commons";
/**
* Keep renderer code up to date with https://github.com/markedjs/marked/blob/master/src/Renderer.ts.
@@ -123,15 +129,6 @@ class CustomMarkdownRenderer extends Renderer {
}
-const renderer = new CustomMarkdownRenderer({ async: false });
-
-import htmlSanitizer from "../html_sanitizer.js";
-import importUtils from "./utils.js";
-import { getMimeTypeFromHighlightJs, MIME_TYPE_AUTO } from "./mime_type_definitions.js";
-import { ADMONITION_TYPE_MAPPINGS } from "../export/markdown.js";
-import utils from "../utils.js";
-import { normalizeMimeTypeForCKEditor } from "@triliumnext/commons";
-
function renderToHtml(content: string, title: string) {
// Double-escape slashes in math expression because they are otherwise consumed by the parser somewhere.
content = content.replaceAll("\\$", "\\\\$");
@@ -168,6 +165,8 @@ function getNormalizedMimeFromMarkdownLanguage(language: string | undefined) {
return MIME_TYPE_AUTO;
}
+const renderer = new CustomMarkdownRenderer({ async: false });
+
export default {
renderToHtml
};
diff --git a/apps/server/src/services/import/mime_type_definitions.ts b/apps/server/src/services/import/mime_type_definitions.ts
deleted file mode 100644
index 7d5b5df491..0000000000
--- a/apps/server/src/services/import/mime_type_definitions.ts
+++ /dev/null
@@ -1,212 +0,0 @@
-// TODO: deduplicate with /src/public/app/services/mime_type_definitions.ts
-
-/**
- * A pseudo-MIME type which is used in the editor to automatically determine the language used in code blocks via heuristics.
- */
-export const MIME_TYPE_AUTO = "text-x-trilium-auto";
-
-export interface MimeTypeDefinition {
- default?: boolean;
- title: string;
- 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. */
- highlightJs?: string;
- /** If specified, will load the corresponding highlight.js file from the `libraries/highlightjs/${id}.js` instead of `node_modules/@highlightjs/cdn-assets/languages/${id}.min.js`. */
- highlightJsSource?: "libraries";
- /** If specified, will load the corresponding highlight file from the given path instead of `node_modules`. */
- codeMirrorSource?: string;
-}
-
-/**
- * For highlight.js-supported languages, see https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md.
- */
-
-export const MIME_TYPES_DICT: readonly MimeTypeDefinition[] = Object.freeze([
- { title: "Plain text", mime: "text/plain", highlightJs: "plaintext", default: true },
-
- // Keep sorted alphabetically.
- { title: "APL", mime: "text/apl" },
- { 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", highlightJs: "dos", codeMirrorSource: "libraries/codemirror/batch.js" },
- { title: "Brainfuck", mime: "text/x-brainfuck", highlightJs: "brainfuck" },
- { title: "C", mime: "text/x-csrc", highlightJs: "c", default: true },
- { title: "C#", mime: "text/x-csharp", highlightJs: "csharp", default: true },
- { title: "C++", mime: "text/x-c++src", highlightJs: "cpp", default: true },
- { title: "Clojure", mime: "text/x-clojure", highlightJs: "clojure" },
- { title: "ClojureScript", mime: "text/x-clojurescript" },
- { title: "Closure Stylesheets (GSS)", mime: "text/x-gss" },
- { title: "CMake", mime: "text/x-cmake", highlightJs: "cmake" },
- { title: "Cobol", mime: "text/x-cobol" },
- { title: "CoffeeScript", mime: "text/coffeescript", highlightJs: "coffeescript" },
- { title: "Common Lisp", mime: "text/x-common-lisp", highlightJs: "lisp" },
- { title: "CQL", mime: "text/x-cassandra" },
- { title: "Crystal", mime: "text/x-crystal", highlightJs: "crystal" },
- { title: "CSS", mime: "text/css", highlightJs: "css", default: true },
- { title: "Cypher", mime: "application/x-cypher-query" },
- { title: "Cython", mime: "text/x-cython" },
- { title: "D", mime: "text/x-d", highlightJs: "d" },
- { title: "Dart", mime: "application/dart", highlightJs: "dart" },
- { title: "diff", mime: "text/x-diff", highlightJs: "diff" },
- { title: "Django", mime: "text/x-django", highlightJs: "django" },
- { title: "Dockerfile", mime: "text/x-dockerfile", highlightJs: "dockerfile" },
- { title: "DTD", mime: "application/xml-dtd" },
- { title: "Dylan", mime: "text/x-dylan" },
- { title: "EBNF", mime: "text/x-ebnf", highlightJs: "ebnf" },
- { title: "ECL", mime: "text/x-ecl" },
- { title: "edn", mime: "application/edn" },
- { title: "Eiffel", mime: "text/x-eiffel" },
- { title: "Elm", mime: "text/x-elm", highlightJs: "elm" },
- { title: "Embedded Javascript", mime: "application/x-ejs" },
- { title: "Embedded Ruby", mime: "application/x-erb", highlightJs: "erb" },
- { title: "Erlang", mime: "text/x-erlang", highlightJs: "erlang" },
- { title: "Esper", mime: "text/x-esper" },
- { title: "F#", mime: "text/x-fsharp", highlightJs: "fsharp" },
- { title: "Factor", mime: "text/x-factor" },
- { title: "FCL", mime: "text/x-fcl" },
- { title: "Forth", mime: "text/x-forth" },
- { title: "Fortran", mime: "text/x-fortran", highlightJs: "fortran" },
- { title: "Gas", mime: "text/x-gas" },
- { title: "GDScript (Godot)", mime: "text/x-gdscript" },
- { title: "Gherkin", mime: "text/x-feature", highlightJs: "gherkin" },
- { title: "GitHub Flavored Markdown", mime: "text/x-gfm", highlightJs: "markdown" },
- { title: "Go", mime: "text/x-go", highlightJs: "go", default: true },
- { title: "Groovy", mime: "text/x-groovy", highlightJs: "groovy", default: true },
- { title: "HAML", mime: "text/x-haml", highlightJs: "haml" },
- { title: "Haskell (Literate)", mime: "text/x-literate-haskell" },
- { title: "Haskell", mime: "text/x-haskell", highlightJs: "haskell", default: true },
- { title: "Haxe", mime: "text/x-haxe", highlightJs: "haxe" },
- { title: "HTML", mime: "text/html", highlightJs: "xml", default: true },
- { title: "HTTP", mime: "message/http", highlightJs: "http", default: true },
- { title: "HXML", mime: "text/x-hxml" },
- { title: "IDL", mime: "text/x-idl" },
- { title: "Java Server Pages", mime: "application/x-jsp", highlightJs: "java" },
- { title: "Java", mime: "text/x-java", highlightJs: "java", default: true },
- { title: "Jinja2", mime: "text/jinja2" },
- { title: "JS backend", mime: "application/javascript;env=backend", highlightJs: "javascript", default: true },
- { title: "JS frontend", mime: "application/javascript;env=frontend", highlightJs: "javascript", default: true },
- { title: "JSON-LD", mime: "application/ld+json", highlightJs: "json" },
- { title: "JSON", mime: "application/json", highlightJs: "json", default: true },
- { title: "JSX", mime: "text/jsx", highlightJs: "javascript" },
- { title: "Julia", mime: "text/x-julia", highlightJs: "julia" },
- { title: "Kotlin", mime: "text/x-kotlin", highlightJs: "kotlin", default: true },
- { title: "LaTeX", mime: "text/x-latex", highlightJs: "latex" },
- { title: "LESS", mime: "text/x-less", highlightJs: "less" },
- { title: "LiveScript", mime: "text/x-livescript", highlightJs: "livescript" },
- { title: "Lua", mime: "text/x-lua", highlightJs: "lua" },
- { title: "MariaDB SQL", mime: "text/x-mariadb", highlightJs: "sql" },
- { title: "Markdown", mime: "text/x-markdown", highlightJs: "markdown", default: true },
- { title: "Mathematica", mime: "text/x-mathematica", highlightJs: "mathematica" },
- { title: "mbox", mime: "application/mbox" },
- { title: "MIPS Assembler", mime: "text/x-asm-mips", highlightJs: "mips" },
- { title: "mIRC", mime: "text/mirc" },
- { title: "Modelica", mime: "text/x-modelica" },
- { title: "MS SQL", mime: "text/x-mssql", highlightJs: "sql" },
- { title: "mscgen", mime: "text/x-mscgen" },
- { title: "msgenny", mime: "text/x-msgenny" },
- { title: "MUMPS", mime: "text/x-mumps" },
- { title: "MySQL", mime: "text/x-mysql", highlightJs: "sql" },
- { title: "Nix", mime: "text/x-nix", highlightJs: "nix" },
- { title: "Nginx", mime: "text/x-nginx-conf", highlightJs: "nginx" },
- { title: "NSIS", mime: "text/x-nsis", highlightJs: "nsis" },
- { title: "NTriples", mime: "application/n-triples" },
- { title: "Objective-C", mime: "text/x-objectivec", highlightJs: "objectivec" },
- { title: "OCaml", mime: "text/x-ocaml", highlightJs: "ocaml" },
- { title: "Octave", mime: "text/x-octave" },
- { title: "Oz", mime: "text/x-oz" },
- { title: "Pascal", mime: "text/x-pascal", highlightJs: "delphi" },
- { title: "PEG.js", mime: "null" },
- { title: "Perl", mime: "text/x-perl", default: true },
- { title: "PGP", mime: "application/pgp" },
- { title: "PHP", mime: "text/x-php", default: true },
- { title: "Pig", mime: "text/x-pig" },
- { title: "PLSQL", mime: "text/x-plsql", highlightJs: "sql" },
- { title: "PostgreSQL", mime: "text/x-pgsql", highlightJs: "pgsql" },
- { title: "PowerShell", mime: "application/x-powershell", highlightJs: "powershell" },
- { title: "Properties files", mime: "text/x-properties", highlightJs: "properties" },
- { title: "ProtoBuf", mime: "text/x-protobuf", highlightJs: "protobuf" },
- { title: "Pug", mime: "text/x-pug" },
- { title: "Puppet", mime: "text/x-puppet", highlightJs: "puppet" },
- { title: "Python", mime: "text/x-python", highlightJs: "python", default: true },
- { title: "Q", mime: "text/x-q", highlightJs: "q" },
- { title: "R", mime: "text/x-rsrc", highlightJs: "r" },
- { title: "reStructuredText", mime: "text/x-rst" },
- { title: "RPM Changes", mime: "text/x-rpm-changes" },
- { title: "RPM Spec", mime: "text/x-rpm-spec" },
- { title: "Ruby", mime: "text/x-ruby", highlightJs: "ruby", default: true },
- { title: "Rust", mime: "text/x-rustsrc", highlightJs: "rust" },
- { title: "SAS", mime: "text/x-sas", highlightJs: "sas" },
- { title: "Sass", mime: "text/x-sass" },
- { title: "Scala", mime: "text/x-scala" },
- { title: "Scheme", mime: "text/x-scheme" },
- { title: "SCSS", mime: "text/x-scss", highlightJs: "scss" },
- { title: "Shell (bash)", mime: "text/x-sh", highlightJs: "bash", default: true },
- { title: "Sieve", mime: "application/sieve" },
- { title: "Slim", mime: "text/x-slim" },
- { title: "Smalltalk", mime: "text/x-stsrc", highlightJs: "smalltalk" },
- { title: "Smarty", mime: "text/x-smarty" },
- { title: "SML", mime: "text/x-sml", highlightJs: "sml" },
- { title: "Solr", mime: "text/x-solr" },
- { title: "Soy", mime: "text/x-soy" },
- { title: "SPARQL", mime: "application/sparql-query" },
- { title: "Spreadsheet", mime: "text/x-spreadsheet" },
- { title: "SQL", mime: "text/x-sql", highlightJs: "sql", default: true },
- { title: "SQLite (Trilium)", mime: "text/x-sqlite;schema=trilium", highlightJs: "sql", default: true },
- { title: "SQLite", mime: "text/x-sqlite", highlightJs: "sql" },
- { title: "Squirrel", mime: "text/x-squirrel" },
- { title: "sTeX", mime: "text/x-stex" },
- { title: "Stylus", mime: "text/x-styl", highlightJs: "stylus" },
- { title: "Swift", mime: "text/x-swift", default: true },
- { title: "SystemVerilog", mime: "text/x-systemverilog" },
- { title: "Tcl", mime: "text/x-tcl", highlightJs: "tcl" },
- { title: "Terraform (HCL)", mime: "text/x-hcl", highlightJs: "terraform", highlightJsSource: "libraries", codeMirrorSource: "libraries/codemirror/hcl.js" },
- { title: "Textile", mime: "text/x-textile" },
- { title: "TiddlyWiki ", mime: "text/x-tiddlywiki" },
- { title: "Tiki wiki", mime: "text/tiki" },
- { title: "TOML", mime: "text/x-toml", highlightJs: "ini" },
- { title: "Tornado", mime: "text/x-tornado" },
- { title: "troff", mime: "text/troff" },
- { title: "TTCN_CFG", mime: "text/x-ttcn-cfg" },
- { title: "TTCN", mime: "text/x-ttcn" },
- { title: "Turtle", mime: "text/turtle" },
- { title: "Twig", mime: "text/x-twig", highlightJs: "twig" },
- { title: "TypeScript-JSX", mime: "text/typescript-jsx" },
- { title: "TypeScript", mime: "application/typescript", highlightJs: "typescript" },
- { title: "VB.NET", mime: "text/x-vb", highlightJs: "vbnet" },
- { title: "VBScript", mime: "text/vbscript", highlightJs: "vbscript" },
- { title: "Velocity", mime: "text/velocity" },
- { title: "Verilog", mime: "text/x-verilog", highlightJs: "verilog" },
- { title: "VHDL", mime: "text/x-vhdl", highlightJs: "vhdl" },
- { title: "Vue.js Component", mime: "text/x-vue" },
- { title: "Web IDL", mime: "text/x-webidl" },
- { title: "XML", mime: "text/xml", highlightJs: "xml", default: true },
- { title: "XQuery", mime: "application/xquery", highlightJs: "xquery" },
- { title: "xu", mime: "text/x-xu" },
- { title: "Yacas", mime: "text/x-yacas" },
- { title: "YAML", mime: "text/x-yaml", highlightJs: "yaml", default: true },
- { title: "Z80", mime: "text/x-z80" }
-]);
-
-let byHighlightJsNameMappings: Record<string, MimeTypeDefinition> | null = null;
-
-/**
- * Given a Highlight.js 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 "JS frontend" mime type is returned.
- *
- * @param highlightJsName a language tag.
- * @returns the corresponding {@link MimeTypeDefinition} if found, or `undefined` otherwise.
- */
-export function getMimeTypeFromHighlightJs(highlightJsName: string) {
- if (!byHighlightJsNameMappings) {
- byHighlightJsNameMappings = {};
- for (const mimeType of MIME_TYPES_DICT) {
- if (mimeType.highlightJs && !byHighlightJsNameMappings[mimeType.highlightJs]) {
- byHighlightJsNameMappings[mimeType.highlightJs] = mimeType;
- }
- }
- }
-
- return byHighlightJsNameMappings[highlightJsName];
-}
diff --git a/packages/commons/src/lib/mime_type.ts b/packages/commons/src/lib/mime_type.ts
index a53a1f7ac6..edd007a21a 100644
--- a/packages/commons/src/lib/mime_type.ts
+++ b/packages/commons/src/lib/mime_type.ts
@@ -1,3 +1,8 @@
+/**
+ * A pseudo-MIME type which is used in the editor to automatically determine the language used in code blocks via heuristics.
+ */
+export const MIME_TYPE_AUTO = "text-x-trilium-auto";
+
export interface MimeTypeDefinition {
default?: boolean;
title: string;
@@ -27,3 +32,197 @@ export interface MimeType extends MimeTypeDefinition {
export function normalizeMimeTypeForCKEditor(mimeType: string) {
return mimeType.toLowerCase().replace(/[\W_]+/g, "-");
}
+
+/**
+ * For highlight.js-supported languages, see https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md.
+ */
+
+export const MIME_TYPES_DICT: readonly MimeTypeDefinition[] = Object.freeze([
+ { title: "Plain text", mime: "text/plain", highlightJs: "plaintext", default: true },
+
+ // Keep sorted alphabetically.
+ { title: "APL", mime: "text/apl" },
+ { 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", highlightJs: "dos", codeMirrorSource: "libraries/codemirror/batch.js" },
+ { title: "Brainfuck", mime: "text/x-brainfuck", highlightJs: "brainfuck" },
+ { title: "C", mime: "text/x-csrc", highlightJs: "c", default: true },
+ { title: "C#", mime: "text/x-csharp", highlightJs: "csharp", default: true },
+ { title: "C++", mime: "text/x-c++src", highlightJs: "cpp", default: true },
+ { title: "Clojure", mime: "text/x-clojure", highlightJs: "clojure" },
+ { title: "ClojureScript", mime: "text/x-clojurescript" },
+ { title: "Closure Stylesheets (GSS)", mime: "text/x-gss" },
+ { title: "CMake", mime: "text/x-cmake", highlightJs: "cmake" },
+ { title: "Cobol", mime: "text/x-cobol" },
+ { title: "CoffeeScript", mime: "text/coffeescript", highlightJs: "coffeescript" },
+ { title: "Common Lisp", mime: "text/x-common-lisp", highlightJs: "lisp" },
+ { title: "CQL", mime: "text/x-cassandra" },
+ { title: "Crystal", mime: "text/x-crystal", highlightJs: "crystal" },
+ { title: "CSS", mime: "text/css", highlightJs: "css", default: true },
+ { title: "Cypher", mime: "application/x-cypher-query" },
+ { title: "Cython", mime: "text/x-cython" },
+ { title: "D", mime: "text/x-d", highlightJs: "d" },
+ { title: "Dart", mime: "application/dart", highlightJs: "dart" },
+ { title: "diff", mime: "text/x-diff", highlightJs: "diff" },
+ { title: "Django", mime: "text/x-django", highlightJs: "django" },
+ { title: "Dockerfile", mime: "text/x-dockerfile", highlightJs: "dockerfile" },
+ { title: "DTD", mime: "application/xml-dtd" },
+ { title: "Dylan", mime: "text/x-dylan" },
+ { title: "EBNF", mime: "text/x-ebnf", highlightJs: "ebnf" },
+ { title: "ECL", mime: "text/x-ecl" },
+ { title: "edn", mime: "application/edn" },
+ { title: "Eiffel", mime: "text/x-eiffel" },
+ { title: "Elm", mime: "text/x-elm", highlightJs: "elm" },
+ { title: "Embedded Javascript", mime: "application/x-ejs" },
+ { title: "Embedded Ruby", mime: "application/x-erb", highlightJs: "erb" },
+ { title: "Erlang", mime: "text/x-erlang", highlightJs: "erlang" },
+ { title: "Esper", mime: "text/x-esper" },
+ { title: "F#", mime: "text/x-fsharp", highlightJs: "fsharp" },
+ { title: "Factor", mime: "text/x-factor" },
+ { title: "FCL", mime: "text/x-fcl" },
+ { title: "Forth", mime: "text/x-forth" },
+ { title: "Fortran", mime: "text/x-fortran", highlightJs: "fortran" },
+ { title: "Gas", mime: "text/x-gas" },
+ { title: "GDScript (Godot)", mime: "text/x-gdscript" },
+ { title: "Gherkin", mime: "text/x-feature", highlightJs: "gherkin" },
+ { title: "GitHub Flavored Markdown", mime: "text/x-gfm", highlightJs: "markdown" },
+ { title: "Go", mime: "text/x-go", highlightJs: "go", default: true },
+ { title: "Groovy", mime: "text/x-groovy", highlightJs: "groovy", default: true },
+ { title: "HAML", mime: "text/x-haml", highlightJs: "haml" },
+ { title: "Haskell (Literate)", mime: "text/x-literate-haskell" },
+ { title: "Haskell", mime: "text/x-haskell", highlightJs: "haskell", default: true },
+ { title: "Haxe", mime: "text/x-haxe", highlightJs: "haxe" },
+ { title: "HTML", mime: "text/html", highlightJs: "xml", default: true },
+ { title: "HTTP", mime: "message/http", highlightJs: "http", default: true },
+ { title: "HXML", mime: "text/x-hxml" },
+ { title: "IDL", mime: "text/x-idl" },
+ { title: "Java Server Pages", mime: "application/x-jsp", highlightJs: "java" },
+ { title: "Java", mime: "text/x-java", highlightJs: "java", default: true },
+ { title: "Jinja2", mime: "text/jinja2" },
+ { title: "JS backend", mime: "application/javascript;env=backend", highlightJs: "javascript", default: true },
+ { title: "JS frontend", mime: "application/javascript;env=frontend", highlightJs: "javascript", default: true },
+ { title: "JSON-LD", mime: "application/ld+json", highlightJs: "json" },
+ { title: "JSON", mime: "application/json", highlightJs: "json", default: true },
+ { title: "JSX", mime: "text/jsx", highlightJs: "javascript" },
+ { title: "Julia", mime: "text/x-julia", highlightJs: "julia" },
+ { title: "Kotlin", mime: "text/x-kotlin", highlightJs: "kotlin", default: true },
+ { title: "LaTeX", mime: "text/x-latex", highlightJs: "latex" },
+ { title: "LESS", mime: "text/x-less", highlightJs: "less" },
+ { title: "LiveScript", mime: "text/x-livescript", highlightJs: "livescript" },
+ { title: "Lua", mime: "text/x-lua", highlightJs: "lua" },
+ { title: "MariaDB SQL", mime: "text/x-mariadb", highlightJs: "sql" },
+ { title: "Markdown", mime: "text/x-markdown", highlightJs: "markdown", default: true },
+ { title: "Mathematica", mime: "text/x-mathematica", highlightJs: "mathematica" },
+ { title: "mbox", mime: "application/mbox" },
+ { title: "MIPS Assembler", mime: "text/x-asm-mips", highlightJs: "mips" },
+ { title: "mIRC", mime: "text/mirc" },
+ { title: "Modelica", mime: "text/x-modelica" },
+ { title: "MS SQL", mime: "text/x-mssql", highlightJs: "sql" },
+ { title: "mscgen", mime: "text/x-mscgen" },
+ { title: "msgenny", mime: "text/x-msgenny" },
+ { title: "MUMPS", mime: "text/x-mumps" },
+ { title: "MySQL", mime: "text/x-mysql", highlightJs: "sql" },
+ { title: "Nix", mime: "text/x-nix", highlightJs: "nix" },
+ { title: "Nginx", mime: "text/x-nginx-conf", highlightJs: "nginx" },
+ { title: "NSIS", mime: "text/x-nsis", highlightJs: "nsis" },
+ { title: "NTriples", mime: "application/n-triples" },
+ { title: "Objective-C", mime: "text/x-objectivec", highlightJs: "objectivec" },
+ { title: "OCaml", mime: "text/x-ocaml", highlightJs: "ocaml" },
+ { title: "Octave", mime: "text/x-octave" },
+ { title: "Oz", mime: "text/x-oz" },
+ { title: "Pascal", mime: "text/x-pascal", highlightJs: "delphi" },
+ { title: "PEG.js", mime: "null" },
+ { title: "Perl", mime: "text/x-perl", default: true },
+ { title: "PGP", mime: "application/pgp" },
+ { title: "PHP", mime: "text/x-php", default: true },
+ { title: "Pig", mime: "text/x-pig" },
+ { title: "PLSQL", mime: "text/x-plsql", highlightJs: "sql" },
+ { title: "PostgreSQL", mime: "text/x-pgsql", highlightJs: "pgsql" },
+ { title: "PowerShell", mime: "application/x-powershell", highlightJs: "powershell" },
+ { title: "Properties files", mime: "text/x-properties", highlightJs: "properties" },
+ { title: "ProtoBuf", mime: "text/x-protobuf", highlightJs: "protobuf" },
+ { title: "Pug", mime: "text/x-pug" },
+ { title: "Puppet", mime: "text/x-puppet", highlightJs: "puppet" },
+ { title: "Python", mime: "text/x-python", highlightJs: "python", default: true },
+ { title: "Q", mime: "text/x-q", highlightJs: "q" },
+ { title: "R", mime: "text/x-rsrc", highlightJs: "r" },
+ { title: "reStructuredText", mime: "text/x-rst" },
+ { title: "RPM Changes", mime: "text/x-rpm-changes" },
+ { title: "RPM Spec", mime: "text/x-rpm-spec" },
+ { title: "Ruby", mime: "text/x-ruby", highlightJs: "ruby", default: true },
+ { title: "Rust", mime: "text/x-rustsrc", highlightJs: "rust" },
+ { title: "SAS", mime: "text/x-sas", highlightJs: "sas" },
+ { title: "Sass", mime: "text/x-sass" },
+ { title: "Scala", mime: "text/x-scala" },
+ { title: "Scheme", mime: "text/x-scheme" },
+ { title: "SCSS", mime: "text/x-scss", highlightJs: "scss" },
+ { title: "Shell (bash)", mime: "text/x-sh", highlightJs: "bash", default: true },
+ { title: "Sieve", mime: "application/sieve" },
+ { title: "Slim", mime: "text/x-slim" },
+ { title: "Smalltalk", mime: "text/x-stsrc", highlightJs: "smalltalk" },
+ { title: "Smarty", mime: "text/x-smarty" },
+ { title: "SML", mime: "text/x-sml", highlightJs: "sml" },
+ { title: "Solr", mime: "text/x-solr" },
+ { title: "Soy", mime: "text/x-soy" },
+ { title: "SPARQL", mime: "application/sparql-query" },
+ { title: "Spreadsheet", mime: "text/x-spreadsheet" },
+ { title: "SQL", mime: "text/x-sql", highlightJs: "sql", default: true },
+ { title: "SQLite (Trilium)", mime: "text/x-sqlite;schema=trilium", highlightJs: "sql", default: true },
+ { title: "SQLite", mime: "text/x-sqlite", highlightJs: "sql" },
+ { title: "Squirrel", mime: "text/x-squirrel" },
+ { title: "sTeX", mime: "text/x-stex" },
+ { title: "Stylus", mime: "text/x-styl", highlightJs: "stylus" },
+ { title: "Swift", mime: "text/x-swift", default: true },
+ { title: "SystemVerilog", mime: "text/x-systemverilog" },
+ { title: "Tcl", mime: "text/x-tcl", highlightJs: "tcl" },
+ { title: "Terraform (HCL)", mime: "text/x-hcl", highlightJs: "terraform", highlightJsSource: "libraries", codeMirrorSource: "libraries/codemirror/hcl.js" },
+ { title: "Textile", mime: "text/x-textile" },
+ { title: "TiddlyWiki ", mime: "text/x-tiddlywiki" },
+ { title: "Tiki wiki", mime: "text/tiki" },
+ { title: "TOML", mime: "text/x-toml", highlightJs: "ini" },
+ { title: "Tornado", mime: "text/x-tornado" },
+ { title: "troff", mime: "text/troff" },
+ { title: "TTCN_CFG", mime: "text/x-ttcn-cfg" },
+ { title: "TTCN", mime: "text/x-ttcn" },
+ { title: "Turtle", mime: "text/turtle" },
+ { title: "Twig", mime: "text/x-twig", highlightJs: "twig" },
+ { title: "TypeScript-JSX", mime: "text/typescript-jsx" },
+ { title: "TypeScript", mime: "application/typescript", highlightJs: "typescript" },
+ { title: "VB.NET", mime: "text/x-vb", highlightJs: "vbnet" },
+ { title: "VBScript", mime: "text/vbscript", highlightJs: "vbscript" },
+ { title: "Velocity", mime: "text/velocity" },
+ { title: "Verilog", mime: "text/x-verilog", highlightJs: "verilog" },
+ { title: "VHDL", mime: "text/x-vhdl", highlightJs: "vhdl" },
+ { title: "Vue.js Component", mime: "text/x-vue" },
+ { title: "Web IDL", mime: "text/x-webidl" },
+ { title: "XML", mime: "text/xml", highlightJs: "xml", default: true },
+ { title: "XQuery", mime: "application/xquery", highlightJs: "xquery" },
+ { title: "xu", mime: "text/x-xu" },
+ { title: "Yacas", mime: "text/x-yacas" },
+ { title: "YAML", mime: "text/x-yaml", highlightJs: "yaml", default: true },
+ { title: "Z80", mime: "text/x-z80" }
+]);
+
+let byHighlightJsNameMappings: Record<string, MimeTypeDefinition> | null = null;
+
+/**
+ * Given a Highlight.js 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 "JS frontend" mime type is returned.
+ *
+ * @param highlightJsName a language tag.
+ * @returns the corresponding {@link MimeTypeDefinition} if found, or `undefined` otherwise.
+ */
+export function getMimeTypeFromHighlightJs(highlightJsName: string) {
+ if (!byHighlightJsNameMappings) {
+ byHighlightJsNameMappings = {};
+ for (const mimeType of MIME_TYPES_DICT) {
+ if (mimeType.highlightJs && !byHighlightJsNameMappings[mimeType.highlightJs]) {
+ byHighlightJsNameMappings[mimeType.highlightJs] = mimeType;
+ }
+ }
+ }
+
+ return byHighlightJsNameMappings[highlightJsName];
+}