From c41f4332c78a34ca51cf0ee38c0b362b4e39cc53 Mon Sep 17 00:00:00 2001 From: jlkiri Date: Sun, 27 Dec 2020 21:42:09 +0900 Subject: [PATCH] Add KDL syntax highlighting --- .eleventy.js | 23 ++++++ highlight_worker.js | 33 ++++++++ kdl.tmLanguage.json | 183 ++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 72 +++++++++++++++++ package.json | 6 +- 5 files changed, 314 insertions(+), 3 deletions(-) create mode 100644 highlight_worker.js create mode 100644 kdl.tmLanguage.json diff --git a/.eleventy.js b/.eleventy.js index b06a7a3..a3f6e15 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -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: { diff --git a/highlight_worker.js b/highlight_worker.js new file mode 100644 index 0000000..7c8a31c --- /dev/null +++ b/highlight_worker.js @@ -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); +}); \ No newline at end of file diff --git a/kdl.tmLanguage.json b/kdl.tmLanguage.json new file mode 100644 index 0000000..d31fbe1 --- /dev/null +++ b/kdl.tmLanguage.json @@ -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": "(?