Skip to content

Commit 20b0f76

Browse files
committed
Highlight ```rx blocks in Markdown (editor and preview)
The editor gets a TextMate injection into text.html.markdown that embeds source.rx, the way Svelte and Vue do it. The preview highlights with highlight.js, so markdown.js registers an rx language (TypeScript's plus the words the .rx grammar highlights) through markdown.markdownItPlugins; other fences keep the preview's own highlighter. Since the preview now activates the extension, the language server starts only once a .rx file is open. Extension 0.1.1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0117BoWPLMLsJjr7WiwaUgvd
1 parent 0f00d55 commit 20b0f76

7 files changed

Lines changed: 116 additions & 8 deletions

File tree

‎tooling/vscode-rx/.vscodeignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/**
22
client.js
33
build.js
44
.gitignore
5+
markdown.js

‎tooling/vscode-rx/README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ completion and type errors, asked of the TSX projection (spec/13 TY-1c) and
1313
mapped back onto the `.rx` (spec/11 CP-8), and the compiler's own diagnostics,
1414
each with its rule.
1515

16+
In Markdown, ` ```rx ` (or ` ```reactivescript `) blocks are highlighted too: in
17+
the editor with the full `.rx` grammar (`syntaxes/rx.markdown.tmLanguage.json`,
18+
injected into Markdown), and in the preview with a highlight.js language built
19+
on TypeScript's plus the `.rx` words (`markdown.js`). The language server only
20+
starts once a `.rx` file is open.
21+
1622
## Installing
1723

1824
From the VS Marketplace or Open VSX (Cursor, VSCodium), once published: search

‎tooling/vscode-rx/client.js‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
// The VS Code side: starts the .rx language server (tooling/rx-language-server) over IPC for .rx documents.
1+
// The VS Code side: starts the .rx language server (tooling/rx-language-server) over IPC once a .rx document is open,
2+
// and highlights ```rx blocks in the Markdown preview (markdown.js).
23
const vscode = require("vscode");
34
const path = require("node:path");
45
const fs = require("node:fs");
56
const { LanguageClient, TransportKind } = require("vscode-languageclient/node");
7+
const { extendMarkdownIt } = require("./markdown.js");
68

79
let client;
810

@@ -17,7 +19,8 @@ function tsdk(config) {
1719
return path.join(vscode.env.appRoot, "extensions", "node_modules", "typescript", "lib");
1820
}
1921

20-
function activate(context) {
22+
function start(context) {
23+
if (client) return;
2124
const config = vscode.workspace.getConfiguration("reactivescript");
2225
// the bundled server (node build.js), else the repository's, when the extension runs from a checkout
2326
const bundled = context.asAbsolutePath(path.join("dist", "server.js"));
@@ -30,7 +33,18 @@ function activate(context) {
3033
{ run, debug: { ...run, options: { ...run.options, execArgv: ["--nolazy", "--inspect=6009"] } } },
3134
{ documentSelector: [{ language: "reactivescript" }], initializationOptions: { typescript: { tsdk: tsdk(config) } } },
3235
);
33-
return client.start();
36+
client.start();
37+
}
38+
39+
/** The Markdown preview activates the extension too (markdown.markdownItPlugins); the server waits for a .rx file. */
40+
function activate(context) {
41+
const isRx = doc => doc.languageId === "reactivescript";
42+
if (vscode.workspace.textDocuments.some(isRx)) start(context);
43+
else {
44+
const watch = vscode.workspace.onDidOpenTextDocument(doc => { if (isRx(doc)) { watch.dispose(); start(context); } });
45+
context.subscriptions.push(watch);
46+
}
47+
return { extendMarkdownIt };
3448
}
3549

3650
function deactivate() {

‎tooling/vscode-rx/markdown.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// ```rx blocks in VS Code's Markdown preview. The preview highlights fences with highlight.js and colours its classes
2+
// from the theme, so .rx gets a highlight.js language: TypeScript's (with its JSX), plus the words .rx adds. The
3+
// editor side is a TextMate injection (syntaxes/rx.markdown.tmLanguage.json) that embeds the full .rx grammar.
4+
const hljs = require("highlight.js/lib/core");
5+
const typescript = require("highlight.js/lib/languages/typescript");
6+
7+
/** The words .rx adds to TypeScript's: the ones syntaxes/rx.tmLanguage.json highlights (spec/07). */
8+
const RX_KEYWORDS = [
9+
"source", "component", "action", "pure", "server", "client", "fn", "error", "context", "use", "provide", "pre",
10+
"latest", "peek", "pending", "optimistic", "affects", "refresh", "errored", "loading", "fallback", "portal",
11+
"deferred", "key", "rejects", "fails", "effect",
12+
];
13+
14+
hljs.registerLanguage("rx", h => {
15+
const lang = typescript(h);
16+
lang.name = "ReactiveScript";
17+
lang.aliases = ["reactivescript"];
18+
lang.keywords = { ...lang.keywords, keyword: [...new Set([...lang.keywords.keyword, ...RX_KEYWORDS])] };
19+
return lang;
20+
});
21+
22+
const isRx = lang => /^(rx|reactivescript)$/i.test((lang ?? "").trim().split(/\s+/)[0] ?? "");
23+
24+
/** `extendMarkdownIt` for the preview: ```rx fences go through the rx language; every other fence to the preview's
25+
* own highlighter, as before. */
26+
function extendMarkdownIt(md) {
27+
const previous = md.options.highlight;
28+
md.options.highlight = (code, lang, attrs) => {
29+
if (isRx(lang)) {
30+
try { return `<div>${hljs.highlight(code, { language: "rx", ignoreIllegals: true }).value}</div>`; } catch {}
31+
}
32+
return previous ? previous(code, lang, attrs) : "";
33+
};
34+
return md;
35+
}
36+
37+
module.exports = { extendMarkdownIt, hljs };

‎tooling/vscode-rx/package-lock.json‎

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tooling/vscode-rx/package.json‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "reactivescript",
33
"displayName": "ReactiveScript",
4-
"description": "ReactiveScript (.rx, .d.rx): highlighting, and TypeScript's types, hover, go to definition, completion and errors through the .rx language server, with the compiler's own diagnostics.",
5-
"version": "0.1.0",
4+
"description": "ReactiveScript (.rx, .d.rx): highlighting, and TypeScript's types, hover, go to definition, completion and errors through the .rx language server, with the compiler's own diagnostics; ```rx blocks highlighted in Markdown.",
5+
"version": "0.1.1",
66
"publisher": "solidjs",
77
"license": "MIT",
88
"engines": {
@@ -37,6 +37,16 @@
3737
"embeddedLanguages": {
3838
"meta.embedded.block.css": "css"
3939
}
40+
},
41+
{
42+
"scopeName": "markdown.rx.codeblock",
43+
"path": "./syntaxes/rx.markdown.tmLanguage.json",
44+
"injectTo": [
45+
"text.html.markdown"
46+
],
47+
"embeddedLanguages": {
48+
"meta.embedded.block.rx": "reactivescript"
49+
}
4050
}
4151
],
4252
"configuration": {
@@ -58,9 +68,11 @@
5868
"description": "TypeScript's lib folder. Empty: the workspace's node_modules/typescript, else the one VS Code ships."
5969
}
6070
}
61-
}
71+
},
72+
"markdown.markdownItPlugins": true
6273
},
6374
"dependencies": {
75+
"highlight.js": "^11.11.1",
6476
"vscode-languageclient": "^9.0.1"
6577
},
6678
"repository": {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"comment": "```rx (or ```reactivescript) fences in Markdown, highlighted with the .rx grammar (source.rx). Injected into text.html.markdown, as VS Code's own Markdown grammar does for the languages it knows.",
3+
"scopeName": "markdown.rx.codeblock",
4+
"fileTypes": [],
5+
"injectionSelector": "L:text.html.markdown",
6+
"patterns": [{ "include": "#rx-code-block" }],
7+
"repository": {
8+
"rx-code-block": {
9+
"begin": "(^|\\G)(\\s*)(`{3,}|~{3,})\\s*(?i:(rx|reactivescript)((\\s+|:|,|\\{|\\?)[^`]*)?$)",
10+
"beginCaptures": {
11+
"3": { "name": "punctuation.definition.markdown" },
12+
"4": { "name": "fenced_code.block.language.markdown" },
13+
"5": { "name": "fenced_code.block.language.attributes.markdown" }
14+
},
15+
"end": "(^|\\G)(\\2|\\s{0,3})(\\3)\\s*$",
16+
"endCaptures": { "3": { "name": "punctuation.definition.markdown" } },
17+
"name": "markup.fenced_code.block.markdown",
18+
"patterns": [
19+
{
20+
"begin": "(^|\\G)(\\s*)(.*)",
21+
"while": "(^|\\G)(?!\\s*([`~]{3,})\\s*$)",
22+
"contentName": "meta.embedded.block.rx",
23+
"patterns": [{ "include": "source.rx" }]
24+
}
25+
]
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)