Add KDL syntax highlighting

This commit is contained in:
jlkiri 2020-12-27 21:42:09 +09:00
parent 7e4d8fefec
commit c41f4332c7
5 changed files with 314 additions and 3 deletions

View File

@ -1,5 +1,28 @@
const {
Worker,
receiveMessageOnPort,
MessageChannel,
} = require("worker_threads");
function wait_highlight(...args) {
const worker = new Worker("./highlight_worker.js");
const signal = new Int32Array(new SharedArrayBuffer(4));
signal[0] = 0;
try {
const subChannel = new MessageChannel();
worker.postMessage({ signal, port: subChannel.port1, args }, [
subChannel.port1
]);
Atomics.wait(signal, 0, 0);
return receiveMessageOnPort(subChannel.port2).message.result;
} finally {
worker.unref();
}
}
module.exports = (eleventyConfig) => {
eleventyConfig.addPassthroughCopy("src/CNAME");
eleventyConfig.addMarkdownHighlighter(wait_highlight);
return {
dir: {

33
highlight_worker.js Normal file
View File

@ -0,0 +1,33 @@
const { parentPort } = require("worker_threads");
const shiki = require("shiki");
const path = require("path");
async function shiki_highlight(code, lang) {
const highlighter = await
shiki
.getHighlighter({
theme: 'nord',
langs: [
{
id: "kdl",
scopeName: "source.kdl",
path: path.join(process.cwd(), "kdl.tmLanguage.json")
}
]
})
return highlighter.codeToHtml(code, lang)
}
parentPort.addListener("message", async ({ signal, port, args }) => {
// This is the async function that we want to run "synchronously"
const result = await shiki_highlight(...args);
// Post the result to the main thread before unlocking "signal"
port.postMessage({ result });
port.close();
// Change the value of signal[0] to 1
Atomics.store(signal, 0, 1);
// This will unlock the main thread when we notify it
Atomics.notify(signal, 0);
});

183
kdl.tmLanguage.json Normal file
View File

@ -0,0 +1,183 @@
{
"$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
"comment":"Some of these patterns are taken straight from rust-analyzer: https://github.com/rust-lang/vscode-rust/blob/master/rust-analyzer/editors/code/rust.tmGrammar.json. Some was also taken from https://github.com/arm32x/vscode-sdlang/blob/master/syntaxes/sdlang.tmLanguage.json",
"name": "KDL",
"patterns": [
{
"include": "#null"
},
{
"include": "#boolean"
},
{
"include": "#float_fraction"
},
{
"include": "#float_exp"
},
{
"include": "#decimal"
},
{
"include": "#hexadecimal"
},
{
"include": "#octal"
},
{
"include": "#binary"
},
{
"include": "#raw-strings"
},
{
"include": "#strings"
},
{
"include": "#block_comment"
},
{
"include": "#block_doc_comment"
},
{
"include": "#slashdash_block_comment"
},
{
"include": "#slashdash_comment"
},
{
"include": "#slashdash_node_comment"
},
{
"include": "#line_comment"
},
{
"include": "#attribute"
},
{
"include": "#node_name"
}
],
"repository": {
"float_fraction": {
"comment": "Floating point literal (fraction)",
"name": "constant.numeric.float.rust",
"match": "\\b([0-9\\-\\+]|\\-|\\+)[0-9_]*\\.[0-9][0-9_]*([eE][+-]?[0-9_]+)?\\b"
},
"float_exp": {
"comment": "Floating point literal (exponent)",
"name": "constant.numeric.float.rust",
"match": "\\b[0-9][0-9_]*(\\.[0-9][0-9_]*)?[eE][+-]?[0-9_]+\\b"
},
"decimal": {
"comment": "Integer literal (decimal)",
"name": "constant.numeric.integer.decimal.rust",
"match": "\\b[0-9\\-\\+][0-9_]*\\b"
},
"hexadecimal": {
"comment": "Integer literal (hexadecimal)",
"name": "constant.numeric.integer.hexadecimal.rust",
"match": "\\b0x[a-fA-F0-9_]+\\b"
},
"octal": {
"comment": "Integer literal (octal)",
"name": "constant.numeric.integer.octal.rust",
"match": "\\b0o[0-7_]+\\b"
},
"binary": {
"comment": "Integer literal (binary)",
"name": "constant.numeric.integer.binary.rust",
"match": "\\b0b[01_]+\\b"
},
"node_name": {
"name": "entity.name.tag",
"match": "(?![\\\\{\\}<>;\\[\\]\\=,])[\\w\\-_~!@#\\$%^&*+|/.\\(\\)]+"
},
"attribute": {
"name": "entity.other.attribute-name.kdl",
"match": "(?![\\\\{\\}<>;\\[\\]\\=,])[\\w\\-_~!@#\\$%^&*+|/.]+(=)",
"captures": {
"1": {
"name": "punctuation.separator.key-value.kdl"
}
}
},
"null": {
"name": "constant.language.null.kdl",
"match": "\\bnull\\b"
},
"boolean": {
"name": "constant.language.boolean.kdl",
"match": "\\b(true|false)\\b"
},
"strings": {
"name": "string.quoted.double.kdl",
"begin": "\"",
"end": "\"",
"patterns": [
{
"name": "constant.character.escape.kdl",
"match": "\\\\(:?[nrtbf\\\\\"]|u\\{[a-fA-F0-9]{1,6}\\})"
}
]
},
"raw-strings": {
"name": "string.quoted.double.raw.kdl",
"begin": "b?r(#*)\"",
"end": "\"\\1"
},
"block_doc_comment": {
"comment": "Block documentation comment",
"name": "comment.block.documentation.kdl",
"begin": "/\\*[\\*!](?![\\*/])",
"end": "\\*/",
"patterns": [
{
"include": "#block_doc_comment"
},
{
"include": "#block_comment"
}
]
},
"block_comment": {
"comment": "Block comment",
"name": "comment.block.kdl",
"begin": "/\\*",
"end": "\\*/",
"patterns": [
{
"include": "#block_doc_comment"
},
{
"include": "#block_comment"
}
]
},
"line_comment": {
"comment": "Single-line comment",
"name": "comment.line.double-slash.rust",
"begin": "//",
"end": "$"
},
"slashdash_comment": {
"name": "comment.line.double-slash",
"comment": "Slashdash inline comment",
"begin": "(?<!^)/-",
"end": "\\s"
},
"slashdash_node_comment": {
"name": "comment.block",
"comment": "Slashdash node comment",
"begin": "(?<=^)/-",
"end": "}"
},
"slashdash_block_comment": {
"name": "comment.block",
"comment": "Slashdash block comment",
"begin": "/-{",
"end": "}"
}
},
"scopeName": "source.kdl"
}

72
package-lock.json generated
View File

@ -1964,6 +1964,15 @@
"integrity": "sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==",
"dev": true
},
"json5": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/json5/-/json5-2.1.3.tgz",
"integrity": "sha512-KXPvOm8K9IJKFM0bmdn8QXh7udDh1g/giieX0NLCaMnb4hEiVFqnop2ImTXCc5e0/oHz3LTqmHGtExn5hfMkOA==",
"dev": true,
"requires": {
"minimist": "^1.2.5"
}
},
"jsonfile": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-3.0.1.tgz",
@ -2426,6 +2435,32 @@
"wrappy": "1"
}
},
"onigasm": {
"version": "2.2.5",
"resolved": "https://registry.npmjs.org/onigasm/-/onigasm-2.2.5.tgz",
"integrity": "sha512-F+th54mPc0l1lp1ZcFMyL/jTs2Tlq4SqIHKIXGZOR/VkHkF9A7Fr5rRr5+ZG/lWeRsyrClLYRq7s/yFQ/XhWCA==",
"dev": true,
"requires": {
"lru-cache": "^5.1.1"
},
"dependencies": {
"lru-cache": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz",
"integrity": "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==",
"dev": true,
"requires": {
"yallist": "^3.0.2"
}
},
"yallist": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz",
"integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==",
"dev": true
}
}
},
"openurl": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/openurl/-/openurl-1.1.1.tgz",
@ -3620,6 +3655,37 @@
"integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==",
"dev": true
},
"shiki": {
"version": "0.2.7",
"resolved": "https://registry.npmjs.org/shiki/-/shiki-0.2.7.tgz",
"integrity": "sha512-bwVc7cdtYYHEO9O+XJ8aNOskKRfaQd5Y4ovLRfbQkmiLSUaR+bdlssbZUUhbQ0JAFMYcTcJ5tjG5KtnufttDHQ==",
"dev": true,
"requires": {
"onigasm": "^2.2.5",
"shiki-languages": "^0.2.7",
"shiki-themes": "^0.2.7",
"vscode-textmate": "^5.2.0"
}
},
"shiki-languages": {
"version": "0.2.7",
"resolved": "https://registry.npmjs.org/shiki-languages/-/shiki-languages-0.2.7.tgz",
"integrity": "sha512-REmakh7pn2jCn9GDMRSK36oDgqhh+rSvJPo77sdWTOmk44C5b0XlYPwJZcFOMJWUZJE0c7FCbKclw4FLwUKLRw==",
"dev": true,
"requires": {
"vscode-textmate": "^5.2.0"
}
},
"shiki-themes": {
"version": "0.2.7",
"resolved": "https://registry.npmjs.org/shiki-themes/-/shiki-themes-0.2.7.tgz",
"integrity": "sha512-ZMmboDYw5+SEpugM8KGUq3tkZ0vXg+k60XX6NngDK7gc1Sv6YLUlanpvG3evm57uKJvfXsky/S5MzSOTtYKLjA==",
"dev": true,
"requires": {
"json5": "^2.1.0",
"vscode-textmate": "^5.2.0"
}
},
"sigmund": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/sigmund/-/sigmund-1.0.1.tgz",
@ -4209,6 +4275,12 @@
"integrity": "sha1-wGavtYK7HLQSjWDqkjkulNXp2+w=",
"dev": true
},
"vscode-textmate": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/vscode-textmate/-/vscode-textmate-5.2.0.tgz",
"integrity": "sha512-Uw5ooOQxRASHgu6C7GVvUxisKXfSgW4oFlO+aa+PAkgmH89O3CXxEEzNRNtHSqtXFTl0nAC1uYj0GMSH27uwtQ==",
"dev": true
},
"which-module": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz",

View File

@ -19,7 +19,7 @@
"postcss": "^8.2.1",
"postcss-cli": "^8.3.1",
"prettier": "^2.2.1",
"tailwindcss": "^2.0.2"
},
"dependencies": {}
"tailwindcss": "^2.0.2",
"shiki": "^0.2.7"
}
}