Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
import {
type Language, LanguageDescription, LanguageSupport, StreamLanguage, type StreamParser
} from "@codemirror/language";
import type { Extension } from "@codemirror/state";
import { SupportedMimeTypes } from "@triliumnext/commons";
async function buildJavaScript() {
// Syntax highlighting only. Code notes are stored snippets, not linted; the
// executed script notes (env=backend / env=frontend) instead get full
// diagnostics and completion from the TypeScript language service, wired in
// separately via ./type_completion.
const { javascript } = await import('@codemirror/lang-javascript');
return [ javascript() ];
}
async function buildMermaid() {
const { mermaid, foldByIndent } = (await import("./languages/mermaid/index.js"));
return [ mermaid(), foldByIndent() ];
}
/**
* Markdown with highlighted fenced code blocks. `@codemirror/language-data` has no Mermaid entry,
* so `mermaid` blocks get the grammar Mermaid notes use, loaded on the first such block.
*/
async function buildMarkdown() {
const { markdown, markdownLanguage } = await import("@codemirror/lang-markdown");
const { languages } = await import("@codemirror/language-data");
const mermaidDescription = LanguageDescription.of({
name: "Mermaid",
alias: [ "mmd" ],
extensions: [ "mmd", "mermaid" ],
load: async () => (await import("./languages/mermaid/index.js")).mermaid()
});
return markdown({
base: markdownLanguage,
codeLanguages: [ mermaidDescription, ...languages ]
});
}
async function buildJson() {
// `jsonParseLinter` validates with the real JSON.parse, so it reports precise,
// correctly-positioned syntax errors (unlike a tolerant Lezer grammar). The
// gutter marker mirrors the TypeScript script linter and the Mermaid linter.
const { json, jsonParseLinter } = await import('@codemirror/lang-json');
const { linter, lintGutter } = await import('@codemirror/lint');
return [ json(), linter(jsonParseLinter()), lintGutter() ];
}
/** The backend log, with the statements of slow queries highlighted as SQLite (Trilium) notes. */
async function buildTriliumLog() {
const [ { triliumLogHighlighter }, sqlLanguage ] = await Promise.all([
import("./extensions/trilium_log_highlighter.js"),
loadLanguage("text/x-sqlite;schema=trilium")
]);
return [ triliumLogHighlighter(sqlLanguage) ];
}
const byMimeType: Record<SupportedMimeTypes, (() => Promise<StreamParser<unknown> | LanguageSupport | Extension[]>) | null> = {
"text/plain": null,
"application/dart": async () => (await import('@codemirror/legacy-modes/mode/clike')).dart,
"application/edn": async () => (await import('@codemirror/legacy-modes/mode/clojure')).clojure,
"application/javascript;env=backend": async () => buildJavaScript(),
"application/javascript;env=frontend": async () => buildJavaScript(),
"application/json": async () => buildJson(),
"application/ld+json": async () => (await import('@codemirror/legacy-modes/mode/javascript')).jsonld,
"application/mbox": async () => (await import('@codemirror/legacy-modes/mode/mbox')).mbox,
"application/n-triples": async () => (await import('@codemirror/legacy-modes/mode/ntriples')).ntriples,
"application/pgp": async () => (await import('@codemirror/legacy-modes/mode/asciiarmor')).asciiArmor,
"application/sieve": async () => (await import('@codemirror/legacy-modes/mode/sieve')).sieve,
"application/sparql-query": async () => (await import('@codemirror/legacy-modes/mode/sparql')).sparql,
"application/typescript": async () => (await import('@codemirror/lang-javascript')).javascript({ typescript: true }),
"application/vnd.kdl": null,
"application/x-aspx": null,
"application/x-bat": async () => (await import("./languages/batch.js")).batch,
"application/x-cypher-query": async () => (await import('@codemirror/legacy-modes/mode/cypher')).cypher,
"application/x-ejs": null,
"application/x-erb": null,
"application/x-jsp": null,
"application/x-powershell": async () => (await import('@codemirror/legacy-modes/mode/powershell')).powerShell,
"application/xml-dtd": async () => (await import('@codemirror/legacy-modes/mode/xml')).xml,
"application/xquery": async () => (await import('@codemirror/legacy-modes/mode/xquery')).xQuery,
"message/http": async () => (await import('@codemirror/legacy-modes/mode/http')).http,
"text/apl": async () => (await import('@codemirror/legacy-modes/mode/apl')).apl,
"text/coffeescript": async () => (await import('@codemirror/legacy-modes/mode/coffeescript')).coffeeScript,
"text/css": async () => (await import('@codemirror/lang-css')).css(),
"text/html": async () => (await import('@codemirror/lang-html')).html(),
"text/javascript": async () => buildJavaScript(),
"text/jinja2": async () => (await import('@codemirror/legacy-modes/mode/jinja2')).jinja2,
"text/jsx": async () => (await import('@codemirror/lang-javascript')).javascript({ jsx: true }),
"text/mirc": async () => (await import('@codemirror/legacy-modes/mode/mirc')).mirc,
"text/tiki": async () => (await import('@codemirror/legacy-modes/mode/tiki')).tiki,
"text/troff": async () => (await import('@codemirror/legacy-modes/mode/troff')).troff,
"text/turtle": async () => (await import('@codemirror/legacy-modes/mode/turtle')).turtle,
"text/typescript-jsx": async () => (await import('@codemirror/lang-javascript')).javascript({ typescript: true, jsx: true }),
"text/vbscript": async () => (await import('@codemirror/legacy-modes/mode/vbscript')).vbScript,
"text/velocity": async () => (await import('@codemirror/legacy-modes/mode/velocity')).velocity,
"text/vnd.mermaid": async () => buildMermaid(),
"text/mermaid": async () => buildMermaid(),
"text/x-asm-mips": null,
"text/x-abap": async () => (await import('./languages/abap.js')).abapMode,
"text/x-asterisk": async () => (await import('@codemirror/legacy-modes/mode/asterisk')).asterisk,
"text/x-brainfuck": async () => (await import('@codemirror/legacy-modes/mode/brainfuck')).brainfuck,
"text/x-c++src": async () => (await import('@codemirror/legacy-modes/mode/clike')).cpp,
"text/x-cassandra": async () => (await import('@codemirror/legacy-modes/mode/sql')).cassandra,
"text/x-clojure": async () => (await import('@codemirror/legacy-modes/mode/clojure')).clojure,
"text/x-clojurescript": async () => (await import('@codemirror/legacy-modes/mode/clojure')).clojure,
"text/x-cmake": async () => (await import('@codemirror/legacy-modes/mode/cmake')).cmake,
"text/x-cobol": async () => (await import('@codemirror/legacy-modes/mode/cobol')).cobol,
"text/x-common-lisp": async () => (await import('@codemirror/legacy-modes/mode/commonlisp')).commonLisp,
"text/x-crystal": async () => (await import('@codemirror/legacy-modes/mode/crystal')).crystal,
"text/x-csharp": async () => (await import('@codemirror/legacy-modes/mode/clike')).csharp,
"text/x-csrc": async () => (await import('@codemirror/legacy-modes/mode/clike')).c,
"text/x-cython": async () => (await import('@codemirror/legacy-modes/mode/python')).cython,
"text/x-d": async () => (await import('@codemirror/legacy-modes/mode/d')).d,
"text/x-diff": async () => (await import('@codemirror/legacy-modes/mode/diff')).diff,
"text/x-django": null,
"text/x-dockerfile": async () => (await import('@codemirror/legacy-modes/mode/dockerfile')).dockerFile,
"text/x-dylan": async () => (await import('@codemirror/legacy-modes/mode/dylan')).dylan,
"text/x-ebnf": async () => (await import('@codemirror/legacy-modes/mode/ebnf')).ebnf,
"text/x-ecl": async () => (await import('@codemirror/legacy-modes/mode/ecl')).ecl,
"text/x-eiffel": async () => (await import('@codemirror/legacy-modes/mode/eiffel')).eiffel,
"text/x-elixir": async () => (await import('codemirror-lang-elixir')).elixir(),
"text/x-elm": async () => (await import('@codemirror/legacy-modes/mode/elm')).elm,
"text/x-erlang": async () => (await import('@codemirror/legacy-modes/mode/erlang')).erlang,
"text/x-esper": async () => (await import('@codemirror/legacy-modes/mode/sql')).esper,
"text/x-factor": async () => (await import('@codemirror/legacy-modes/mode/factor')).factor,
"text/x-fcl": async () => (await import('@codemirror/legacy-modes/mode/fcl')).fcl,
"text/x-feature": async () => (await import('@codemirror/legacy-modes/mode/gherkin')).gherkin,
"text/x-forth": async () => (await import('@codemirror/legacy-modes/mode/forth')).forth,
"text/x-fortran": async () => (await import('@codemirror/legacy-modes/mode/fortran')).fortran,
"text/x-fsharp": async () => (await import('@codemirror/legacy-modes/mode/mllike')).fSharp,
"text/x-gas": async () => (await import('@codemirror/legacy-modes/mode/gas')).gas,
"text/x-gdscript": async () => (await import('./languages/gdscript.js')).gdscript,
"text/x-gfm": buildMarkdown,
"text/x-go": async () => (await import('@codemirror/legacy-modes/mode/go')).go,
"text/x-groovy": async () => (await import('@codemirror/legacy-modes/mode/groovy')).groovy,
"text/x-gss": async () => (await import('@codemirror/legacy-modes/mode/css')).gss,
"text/x-haml": null,
"text/x-haskell": async () => (await import('@codemirror/legacy-modes/mode/haskell')).haskell,
"text/x-haxe": async () => (await import('@codemirror/legacy-modes/mode/haxe')).haxe,
"text/x-hcl": async () => (await import('codemirror-lang-hcl')).hcl(),
"text/x-hxml": async () => (await import('@codemirror/legacy-modes/mode/haxe')).hxml,
"text/x-idl": async () => (await import('@codemirror/legacy-modes/mode/idl')).idl,
"text/x-java": async () => (await import('@codemirror/legacy-modes/mode/clike')).java,
"text/x-julia": async () => (await import('@codemirror/legacy-modes/mode/julia')).julia,
"text/x-pegjs": async () => (await import('@codemirror/legacy-modes/mode/pegjs')).pegjs,
"text/x-kotlin": async () => (await import('@codemirror/legacy-modes/mode/clike')).kotlin,
"text/x-latex": async () => (await import('@codemirror/legacy-modes/mode/stex')).stex,
"text/x-less": async () => (await import('@codemirror/legacy-modes/mode/css')).less,
"text/x-literate-haskell": async () => (await import('@codemirror/legacy-modes/mode/haskell')).haskell,
"text/x-livescript": async () => (await import('@codemirror/legacy-modes/mode/livescript')).liveScript,
"text/x-lua": async () => (await import('@codemirror/legacy-modes/mode/lua')).lua,
"text/x-mariadb": async () => (await import('@codemirror/legacy-modes/mode/sql')).mariaDB,
"text/x-markdown": buildMarkdown,
"text/x-mathematica": async () => (await import('@codemirror/legacy-modes/mode/mathematica')).mathematica,
"text/x-modelica": async () => (await import('@codemirror/legacy-modes/mode/modelica')).modelica,
"text/x-mscgen": async () => (await import('@codemirror/legacy-modes/mode/mscgen')).mscgen,
"text/x-msgenny": async () => (await import('@codemirror/legacy-modes/mode/mscgen')).msgenny,
"text/x-mssql": async () => (await import('@codemirror/legacy-modes/mode/sql')).msSQL,
"text/x-mumps": async () => (await import('@codemirror/legacy-modes/mode/mumps')).mumps,
"text/x-mysql": async () => (await import('@codemirror/legacy-modes/mode/sql')).mySQL,
"text/x-nix": async () => (await import('@replit/codemirror-lang-nix')).nix(),
"text/x-nim": async () => (await import('codemirror-lang-nim')).nim(),
"text/x-nginx-conf": async () => (await import('@codemirror/legacy-modes/mode/nginx')).nginx,
"text/x-nsis": async () => (await import('@codemirror/legacy-modes/mode/nsis')).nsis,
"text/x-objectivec": async () => (await import('@codemirror/legacy-modes/mode/clike')).objectiveC,
"text/x-ocaml": async () => (await import('@codemirror/legacy-modes/mode/mllike')).oCaml,
"text/x-octave": async () => (await import('@codemirror/legacy-modes/mode/octave')).octave,
"text/x-oz": async () => (await import('@codemirror/legacy-modes/mode/oz')).oz,
"text/x-pascal": async () => (await import('@codemirror/legacy-modes/mode/pascal')).pascal,
"text/x-perl": async () => (await import('@codemirror/legacy-modes/mode/perl')).perl,
"text/x-pgsql": async () => (await import('@codemirror/legacy-modes/mode/sql')).pgSQL,
"text/x-php": async () => ((await import('@codemirror/lang-php')).php()),
"text/x-pig": async () => (await import('@codemirror/legacy-modes/mode/pig')).pig,
"text/x-plsql": async () => (await import('@codemirror/legacy-modes/mode/sql')).plSQL,
"text/x-properties": async () => (await import('@codemirror/legacy-modes/mode/properties')).properties,
"text/x-protobuf": async () => (await import('@codemirror/legacy-modes/mode/protobuf')).protobuf,
"text/x-pug": async () => (await import('@codemirror/legacy-modes/mode/pug')).pug,
"text/x-puppet": async () => (await import('@codemirror/legacy-modes/mode/puppet')).puppet,
"text/x-python": async () => (await import('@codemirror/legacy-modes/mode/python')).python,
"text/x-q": async () => (await import('@codemirror/legacy-modes/mode/q')).q,
"text/x-rpm-changes": async () => (await import('@codemirror/legacy-modes/mode/rpm')).rpmChanges,
"text/x-rpm-spec": async () => (await import('@codemirror/legacy-modes/mode/rpm')).rpmSpec,
"text/x-rsrc": async () => (await import('@codemirror/legacy-modes/mode/r')).r,
"text/x-rst": null,
"text/x-ruby": async () => (await import('@codemirror/legacy-modes/mode/ruby')).ruby,
"text/x-rustsrc": async () => (await import('@codemirror/legacy-modes/mode/rust')).rust,
"text/x-sas": async () => (await import('@codemirror/legacy-modes/mode/sas')).sas,
"text/x-sass": async () => (await import('@codemirror/legacy-modes/mode/sass')).sass,
"text/x-scala": async () => (await import('@codemirror/legacy-modes/mode/clike')).scala,
"text/x-scheme": async () => (await import('@codemirror/legacy-modes/mode/scheme')).scheme,
"text/x-scss": async () => (await import('@codemirror/legacy-modes/mode/css')).sCSS,
"text/x-sh": async () => (await import('@codemirror/legacy-modes/mode/shell')).shell,
"text/x-slim": null,
"text/x-smarty": async () => ((await import('@ssddanbrown/codemirror-lang-smarty')).smarty),
"text/x-sml": async () => (await import('@codemirror/legacy-modes/mode/mllike')).sml,
"text/x-solr": async () => (await import('@codemirror/legacy-modes/mode/solr')).solr,
"text/x-soy": null,
"text/x-spreadsheet": async () => (await import('@codemirror/legacy-modes/mode/spreadsheet')).spreadsheet,
"text/x-sql": async () => (await import('@codemirror/legacy-modes/mode/sql')).mySQL,
"text/x-sqlite;schema=trilium": async () => (await import('@codemirror/legacy-modes/mode/sql')).sqlite,
"text/x-sqlite": async () => (await import('@codemirror/legacy-modes/mode/sql')).sqlite,
"text/x-squirrel": async () => (await import('@codemirror/legacy-modes/mode/clike')).squirrel,
"text/x-stex": async () => (await import('@codemirror/legacy-modes/mode/stex')).stex,
"text/x-stsrc": async () => (await import('@codemirror/legacy-modes/mode/smalltalk')).smalltalk,
"text/x-styl": async () => (await import('@codemirror/legacy-modes/mode/stylus')).stylus,
"text/x-swift": async () => (await import('@codemirror/legacy-modes/mode/swift')).swift,
"text/x-systemverilog": async () => (await import('@codemirror/legacy-modes/mode/verilog')).verilog,
"text/x-tcl": async () => (await import('@codemirror/legacy-modes/mode/tcl')).tcl,
"text/x-textile": async () => (await import('@codemirror/legacy-modes/mode/textile')).textile,
"text/x-tiddlywiki": async () => (await import('@codemirror/legacy-modes/mode/tiddlywiki')).tiddlyWiki,
"text/x-toml": async () => (await import('@codemirror/legacy-modes/mode/toml')).toml,
"text/x-tornado": null,
"text/x-trilium-log": buildTriliumLog,
"text/x-trilium-search": async () => [ (await import('./extensions/trilium_search_highlighter.js')).triliumSearchHighlighter ],
"text/x-ttcn-asn": async () => (await import('@codemirror/legacy-modes/mode/asn1')).asn1({ multiLineStrings: true }),
"text/x-ttcn-cfg": async () => (await import('@codemirror/legacy-modes/mode/ttcn-cfg')).ttcnCfg,
"text/x-ttcn": async () => (await import('@codemirror/legacy-modes/mode/ttcn')).ttcn,
"text/x-twig": async () => ((await import('@ssddanbrown/codemirror-lang-twig')).twig()),
"text/x-vb": async () => (await import('@codemirror/legacy-modes/mode/vb')).vb,
"text/x-verilog": async () => (await import('@codemirror/legacy-modes/mode/verilog')).verilog,
"text/x-vhdl": async () => (await import('@codemirror/legacy-modes/mode/vhdl')).vhdl,
"text/x-vue": async () => ((await import('@codemirror/lang-vue')).vue()),
"text/x-webidl": async () => (await import('@codemirror/legacy-modes/mode/webidl')).webIDL,
"text/x-xu": async () => (await import('@codemirror/legacy-modes/mode/mscgen')).xu,
"text/x-yacas": async () => (await import('@codemirror/legacy-modes/mode/yacas')).yacas,
"text/x-yaml": async () => (await import('@codemirror/legacy-modes/mode/yaml')).yaml,
"text/x-z80": async () => (await import('@codemirror/legacy-modes/mode/z80')).z80,
"text/xml": async () => (await import('@codemirror/lang-xml')).xml()
}
/**
* Maps MIME types that aren't first-class code-note languages onto an equivalent
* {@link byMimeType} language for syntax highlighting. For example, SVG image notes
* (`image/svg+xml`) are XML under the hood, so their source view highlights as XML.
*/
export const MIME_ALIASES: Record<string, SupportedMimeTypes> = {
"image/svg+xml": "text/xml"
};
/** Loads the language that highlights `mime` in a code note, or `null` if it has none. */
export async function loadLanguage(mime: string): Promise<Language | null> {
const load = byMimeType[(MIME_ALIASES[mime] ?? mime) as SupportedMimeTypes];
if (!load) return null;
const syntax = await load();
if ("token" in syntax) return StreamLanguage.define(syntax);
const extensions = Array.isArray(syntax) ? syntax : [ syntax ];
return extensions
.find((extension): extension is LanguageSupport => extension instanceof LanguageSupport)
?.language ?? null;
}
export default byMimeType;