feat(mime): support KDL (closes #7848)

This commit is contained in:
Elian Doran 2025-11-24 18:06:24 +02:00
parent 9a2979e577
commit 95169bbc84
No known key found for this signature in database
4 changed files with 102 additions and 0 deletions

View File

@ -37,6 +37,7 @@ const byMimeType: Record<string, (() => Promise<StreamParser<unknown> | Language
"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,

View File

@ -104,6 +104,7 @@ export const MIME_TYPES_DICT: readonly MimeTypeDefinition[] = Object.freeze([
{ title: "JSX", mime: "text/jsx", mdLanguageCode: "javascript" },
{ 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" },
{ title: "LaTeX", mime: "text/x-latex", mdLanguageCode: "latex" },
{ title: "LESS", mime: "text/x-less", mdLanguageCode: "less" },
{ title: "LiveScript", mime: "text/x-livescript", mdLanguageCode: "livescript" },

View File

@ -0,0 +1,99 @@
// Source: https://github.com/Devasta/highlightjs-kdl/blob/master/src/languages/kdl.js
import type { HLJSApi, Language } from "highlight.js";
/*
Language: KDL
Description: A cuddly document language.
Author: Daniel Murphy <daniel@devasta.ie>
Website: https://kdl.dev
Category: config
*/
export default function hljsDefineKDL(hljs: HLJSApi): Language {
const ESCAPES = {
scope: 'char.escape',
variants: [
{ begin: /\\n/ },
{ begin: /\\r/ },
{ begin: /\\t/ },
{ begin: /\\"/ },
{ begin: /\\\\/ },
{ begin: /\\b/ },
{ begin: /\\f/ }
]
};
const LITERALS = [
"true",
"false",
"null"
];
const STRINGS = {
scope: 'string',
variants: [
{
begin: /r(#)+"/,
end: /"(#)+/
},
{
begin: /"/,
end: /"/
}
],
contains: [
ESCAPES
]
};
const COMMENTS = {
scope: 'comment',
variants: [
hljs.C_BLOCK_COMMENT_MODE,
hljs.C_LINE_COMMENT_MODE,
{
begin: /\/-/,
end: /\n/
}
]
};
const NUMBERS = {
scope: 'number',
variants: [
{
begin: /([+-])?0b[_01]*/,
},
{
begin: /([+-])?0o[_0-7]*/,
},
{
begin: /([+-])?0x[_0-9A-Fa-f]*/,
},
{
begin: hljs.C_NUMBER_RE
}
]
};
const TYPEANNOTATIONS = {
scope: 'type',
begin: /\(/,
end: /\)/
};
return {
name: 'KDL',
aliases: [ 'kdl' ],
contains: [
STRINGS,
COMMENTS,
NUMBERS,
TYPEANNOTATIONS
],
keywords: {
literal: LITERALS
}
};
}

View File

@ -17,6 +17,7 @@ export const byMimeType: MimeRecord = {
"application/sieve": null,
"application/sparql-query": null,
"application/typescript": () => import("highlight.js/lib/languages/typescript"),
"application/vnd.kdl": () => import("./languages/kdl.js"),
"application/x-aspx": null,
"application/x-bat": () => import("highlight.js/lib/languages/dos"),
"application/x-cypher-query": () => import("highlightjs-cypher"),