|
| 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 }; |
0 commit comments