From 489dd408bb04311ffa38868580084e495fc596a2 Mon Sep 17 00:00:00 2001 From: Bert Verhelst Date: Mon, 16 Nov 2026 19:45:11 +0100 Subject: [PATCH] fix(editor): Markdown code fences highlight in their language (#3821) Markdown came from @codemirror/language-data, which calls markdown() without codeLanguages, so fenced blocks had no language to nest. Markdown is now one of the editor's own modes: a fence's info string resolves through the editor's lookup (mode name or alias, then backend language id or extension), so a block gets the same patched mode as a file; unknown info strings stay plain. Block bodies carry data-language and the per-language colour scheme rules also match .cm-editor [data-language=X], so language colour changes apply inside blocks. @codemirror/lang-markdown (already in the lockfile via language-data) is now a direct dependency. Closes #3808 Co-authored-by: bertyhell <1710840+bertyhell@users.noreply.github.com> --- package.json | 1 + pnpm-lock.yaml | 3 + src/editor/core/cm/languages.ts | 51 +++++++++++- src/editor/core/cm/markdown.test.ts | 117 ++++++++++++++++++++++++++++ src/editor/core/cm/markdown.ts | 79 +++++++++++++++++++ src/platform/colorScheme.test.ts | 10 ++- src/platform/colorScheme.ts | 4 +- src/styles/tokens.css | 5 +- 8 files changed, 262 insertions(+), 8 deletions(-) create mode 100644 src/editor/core/cm/markdown.test.ts create mode 100644 src/editor/core/cm/markdown.ts diff --git a/package.json b/package.json index 1549fbf1b..2f0df20c5 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "@codemirror/lang-html": "^6.4.12", "@codemirror/lang-java": "^6.0.2", "@codemirror/lang-javascript": "^6.2.5", + "@codemirror/lang-markdown": "^6.5.2", "@codemirror/lang-sass": "^6.0.2", "@codemirror/lang-sql": "^6.10.0", "@codemirror/lang-vue": "^0.1.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f9cecb619..d6a0ff4ef 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -26,6 +26,9 @@ importers: '@codemirror/lang-javascript': specifier: ^6.2.5 version: 6.2.5 + '@codemirror/lang-markdown': + specifier: ^6.5.2 + version: 6.5.2 '@codemirror/lang-sass': specifier: ^6.0.2 version: 6.0.2 diff --git a/src/editor/core/cm/languages.ts b/src/editor/core/cm/languages.ts index 76e72f1fb..7af966de6 100644 --- a/src/editor/core/cm/languages.ts +++ b/src/editor/core/cm/languages.ts @@ -21,6 +21,7 @@ import type { DocBackend, LanguageInfo } from "../backend"; import { _languageMode, editorsFor } from "@/editor/api"; import { JS_TAGS } from "./jsTags"; import { docCommentTags } from "./docCommentTags"; +import type { FencedCodeMode } from "./markdown"; let infos: Map | null = null; let infosPromise: Promise> | null = null; @@ -175,6 +176,11 @@ const OWN_MODES = [ ); }, }), + LanguageDescription.of({ + name: "Markdown", + extensions: ["md", "markdown", "mkd"], + load: () => import("./markdown").then((m) => m.markdownSupport(fencedCodeMode)), + }), LanguageDescription.of({ name: "GoMod", alias: ["gomod", "gowork"], @@ -292,6 +298,44 @@ export async function languageDescriptionFor( return findLanguageDescription(uiMode, path); } +/** Supports `patchSupport` produced; `desc.support` is the unpatched one when its `load()` ran elsewhere. */ +const patched = new WeakMap(); +const fencedProxies = new WeakMap(); + +/** + * The mode for a Markdown fenced block's info string: a mode name or alias (`shell`, `bash`, + * `json`, `yml`), else a backend language id or file extension (`shellscript`, `py`, `kt`). + * The block gets the same mode and tag patches as a file of that language; a language without + * a mode (`text`) stays plain. + */ +export function fencedCodeMode(info: string): FencedCodeMode | null { + const name = info.toLowerCase(); + if (!name) return null; + let desc = findLanguageDescription(name, ""); + if (!desc) { + const uiMode = infos?.has(name) ? infos.get(name)!.uiMode : uiModeForPath(`.${name}`); + desc = uiMode ? findLanguageDescription(uiMode, "") : null; + } + if (!desc) return null; + const ready = patched.get(desc); + if (ready) return { name: desc.name, language: ready.language }; + // Markdown parses the block once the proxy has loaded the patched support. + let proxy = fencedProxies.get(desc); + if (!proxy) + fencedProxies.set( + desc, + (proxy = LanguageDescription.of({ + name: desc.name, + load: async () => { + const support = await loadSupport(desc); + if (!support) throw new Error(`failed to load language ${desc.name}`); + return support; + }, + })), + ); + return { name: desc.name, language: proxy }; +} + /** Loads (once) the CodeMirror language for `desc`. */ export function loadSupport(desc: LanguageDescription): Promise { let p = supports.get(desc.name); @@ -356,14 +400,15 @@ function patchSupport(desc: LanguageDescription, support: LanguageSupport): Lang extra && support.language instanceof LRLanguage ? support.language.configure({ props: [styleTags(extra)] }) : support.language; - const patched = new LanguageSupport(language, [ + const result = new LanguageSupport(language, [ support.support, docCommentTags(desc.name), EditorView.editorAttributes.of({ "data-language": desc.name }), ]); // `loadedSupport` reads `desc.support` synchronously; keep it on the patched instance. - (desc as { support: LanguageSupport | undefined }).support = patched; - return patched; + (desc as { support: LanguageSupport | undefined }).support = result; + patched.set(desc, result); + return result; } /** Already-loaded support (synchronous), so reopening a file paints highlighted at once. */ diff --git a/src/editor/core/cm/markdown.test.ts b/src/editor/core/cm/markdown.test.ts new file mode 100644 index 000000000..991b7b5a2 --- /dev/null +++ b/src/editor/core/cm/markdown.test.ts @@ -0,0 +1,117 @@ +import { afterEach, describe, expect, it } from "vitest"; +import { EditorState } from "@codemirror/state"; +import { LanguageDescription, syntaxTree } from "@codemirror/language"; +import { highlightTree, tagHighlighter, tags as t } from "@lezer/highlight"; +import { completedSyntaxState } from "@/test/syntax"; +import type { DocBackend, LanguageInfo } from "../backend"; +import { _resetLanguages, fencedCodeMode, findLanguageDescription, loadLanguageInfos, loadSupport } from "./languages"; +import { fencedCodeScopes } from "./markdown"; + +const SHELL = [ + "```shell", + "# list the pods that are not running", + 'namespace="${1:-default}"', + "if kubectl get pods | grep -v 'Running'; then", + " exit 1", + "fi", + "```", +]; +const JSON_BLOCK = ["```json", "{", ' "name": "example-service",', ' "replicas": 3,', ' "tier": null', "}", "```"]; +const YAML_BLOCK = ["```yaml", "# deployment settings", "name: example-service", "enabled: true", "```"]; +const TEXT_BLOCK = ["```text", "# not a comment", "name: plain", "```"]; +const DOC = [...SHELL, "", ...JSON_BLOCK, "", ...YAML_BLOCK, "", ...TEXT_BLOCK].join("\n"); + +const highlighter = tagHighlighter([ + { tag: t.comment, class: "comment" }, + { tag: t.keyword, class: "keyword" }, + { tag: [t.string, t.special(t.string)], class: "string" }, + { tag: t.number, class: "number" }, + { tag: [t.bool, t.null], class: "literal" }, + { tag: t.propertyName, class: "property" }, +]); + +async function markdownState(doc: string) { + const support = await loadSupport(findLanguageDescription("Markdown", "/p/README.md")!); + for (const info of ["shell", "json", "yaml"]) { + const language = fencedCodeMode(info)!.language; + // A block language not loaded yet: Markdown parses the block again once it is. + if (language instanceof LanguageDescription) await language.load(); + } + return completedSyntaxState(EditorState.create({ doc, extensions: [support!] })); +} + +function highlighted(state: EditorState) { + const found: Array<[string, string]> = []; + highlightTree(syntaxTree(state), highlighter, (from, to, cls) => found.push([state.sliceDoc(from, to), cls])); + return found; +} + +describe("Markdown fenced code (#3808)", () => { + afterEach(() => _resetLanguages()); + + it("resolves info strings like mode names, backend language ids and file extensions", async () => { + const language = (id: string, extensions: string[], uiMode: string | null) => + ({ id, name: id, extensions, uiMode, lineComment: null, blockComment: null, indent: null }) as LanguageInfo; + await loadLanguageInfos({ + languages: async () => [ + language("plaintext", ["txt", "text"], null), + language("shellscript", ["sh", "bash", "zsh"], "Shell"), + language("python", ["py"], "Python"), + language("kotlin", ["kt", "kts"], "Kotlin"), + language("csharp", ["cs"], "C#"), + ], + } as Partial as DocBackend); + for (const [info, name] of [ + ["shell", "Shell"], + ["bash", "Shell"], + ["sh", "Shell"], + ["zsh", "Shell"], + ["shellscript", "Shell"], + ["JSON", "JSON"], + ["yaml", "YAML"], + ["yml", "YAML"], + ["python", "Python"], + ["py", "Python"], + ["kotlin", "Kotlin"], + ["kt", "Kotlin"], + ["csharp", "C#"], + ["java", "Java"], + ["markdown", "Markdown"], + ] as const) + expect(fencedCodeMode(info)?.name, info).toBe(name); + for (const info of ["", "text", "txt", "plaintext", "nonsense"]) expect(fencedCodeMode(info), info).toBeNull(); + }); + + it("highlights shell, JSON and YAML blocks in their language and leaves unknown ones plain", async () => { + const found = highlighted(await markdownState(DOC)); + const has = (text: string, cls: string) => found.some(([s, c]) => s === text && c.split(" ").includes(cls)); + expect(has("# list the pods that are not running", "comment")).toBe(true); + expect(has("if", "keyword")).toBe(true); + expect(has("fi", "keyword")).toBe(true); + expect(has("'Running'", "string")).toBe(true); + expect(has('"name"', "property")).toBe(true); + expect(has("3", "number")).toBe(true); + expect(has("null", "literal")).toBe(true); + expect(has("# deployment settings", "comment")).toBe(true); + // YAML keys get the same keyword colour as in a YAML file. + expect(has("name", "keyword")).toBe(true); + expect(found.filter(([s]) => s.includes("not a comment") || s.includes("plain"))).toEqual([]); + }); + + it("scopes each recognised block body to its language for per-language colours", async () => { + const state = await markdownState(DOC); + const scopes: Array<[string, string]> = []; + fencedCodeScopes(state, [{ from: 0, to: state.doc.length }], fencedCodeMode).between( + 0, + state.doc.length, + (from, to, deco) => { + scopes.push([state.sliceDoc(from, to).split("\n")[0]!, deco.spec.attributes["data-language"]]); + }, + ); + expect(scopes).toEqual([ + ["# list the pods that are not running", "Shell"], + ["{", "JSON"], + ["# deployment settings", "YAML"], + ]); + }); +}); diff --git a/src/editor/core/cm/markdown.ts b/src/editor/core/cm/markdown.ts new file mode 100644 index 000000000..51d66ab8b --- /dev/null +++ b/src/editor/core/cm/markdown.ts @@ -0,0 +1,79 @@ +/** + * Markdown with fenced code blocks highlighted in the language their info string names. + * Each block body also carries that language as `data-language`, so a Color Scheme's + * per-language colours apply inside it as in a file of that language. + */ +import { syntaxTree, type Language, type LanguageDescription, LanguageSupport } from "@codemirror/language"; +import { markdown } from "@codemirror/lang-markdown"; +import type { EditorState, Range } from "@codemirror/state"; +import { Decoration, EditorView, ViewPlugin, type DecorationSet, type ViewUpdate } from "@codemirror/view"; + +export interface FencedCodeMode { + /** `LanguageDescription.name` of the mode, as an editor's `data-language`. */ + name: string; + language: Language | LanguageDescription; +} + +/** Resolves the first word of an info string (`shell`, `json title="a"` → `json`). */ +export type FencedCodeResolver = (info: string) => FencedCodeMode | null; + +const marks = new Map(); + +function scopeMark(name: string): Decoration { + let mark = marks.get(name); + if (!mark) marks.set(name, (mark = Decoration.mark({ attributes: { "data-language": name } }))); + return mark; +} + +/** `data-language` marks over the bodies of the fenced blocks in `ranges` whose info string resolves. */ +export function fencedCodeScopes( + state: EditorState, + ranges: readonly { from: number; to: number }[], + resolve: FencedCodeResolver, +): DecorationSet { + const marked: Range[] = []; + let last = -1; + for (const { from, to } of ranges) { + syntaxTree(state).iterate({ + from, + to, + enter: (node) => { + if (node.name !== "FencedCode") return; + const info = node.node.getChild("CodeInfo"); + const body = node.node.getChild("CodeText"); + if (info && body && body.from > last) { + const mode = resolve(state.sliceDoc(info.from, info.to).match(/^\S*/)![0]); + if (mode) marked.push(scopeMark(mode.name).range(body.from, body.to)); + last = body.from; + } + return false; + }, + }); + } + return Decoration.set(marked, true); +} + +function fencedCodeScopePlugin(resolve: FencedCodeResolver) { + return ViewPlugin.fromClass( + class { + decorations: DecorationSet; + constructor(view: EditorView) { + this.decorations = fencedCodeScopes(view.state, view.visibleRanges, resolve); + } + update(update: ViewUpdate) { + if (update.docChanged || update.viewportChanged || syntaxTree(update.state) !== syntaxTree(update.startState)) + this.decorations = fencedCodeScopes(update.state, update.view.visibleRanges, resolve); + } + }, + // Outer decorations wrap the highlight spans, so the scoped colour variables reach them. + { + provide: (plugin) => + EditorView.outerDecorations.of((view) => view.plugin(plugin)?.decorations ?? Decoration.none), + }, + ); +} + +export function markdownSupport(resolve: FencedCodeResolver): LanguageSupport { + const base = markdown({ codeLanguages: (info) => resolve(info)?.language ?? null }); + return new LanguageSupport(base.language, [base.support, fencedCodeScopePlugin(resolve)]); +} diff --git a/src/platform/colorScheme.test.ts b/src/platform/colorScheme.test.ts index 56d7a9c5d..59cb7bb1b 100644 --- a/src/platform/colorScheme.test.ts +++ b/src/platform/colorScheme.test.ts @@ -42,20 +42,26 @@ describe("color schemes", () => { expect(schemeTokens("light").get("--syn-php-variable")).toBe("#871094"); expect(schemeTokens("light").get("--syn-php-function-call")).toBe("#00627a"); const css = readFileSync("src/styles/tokens.css", "utf8"); - expect(css).toMatch(/\.cm-editor\[data-language="PHP"\] \{\s*--syn-variable: var\(--syn-php-variable\);/); + expect(css).toMatch( + /\.cm-editor\[data-language="PHP"\],\s*\.cm-editor \[data-language="PHP"\] \{\s*--syn-variable: var\(--syn-php-variable\);/, + ); // An imported PHP_VAR still wins: language overrides are emitted for the same element, !important. const imported = schemeCss( "theme-default", { dark: { "php.identifier": { foreground: "#48d8ee" } } }, document.documentElement, ); - expect(imported).toMatch(/\.cm-editor\[data-language="PHP"\] \{\n {2}--syn-variable: #48d8ee !important;/); + expect(imported).toMatch( + /\.cm-editor\[data-language="PHP"\], \.cm-editor \[data-language="PHP"\] \{\n {2}--syn-variable: #48d8ee !important;/, + ); }); it("offers a Go scheme page and scopes its overrides to Go editors", () => { expect(ATTRIBUTES.find((a) => a.id === "go.constant")?.path).toEqual(["Go"]); const css = schemeCss("dark", { dark: { "go.constant": { foreground: "#224466" } } }); expect(css).toContain('.cm-editor[data-language="Go"]'); + // A Go code block in a Markdown editor (#3808). + expect(css).toContain('.cm-editor [data-language="Go"]'); expect(css).toContain("--syn-constant: #224466 !important"); }); diff --git a/src/platform/colorScheme.ts b/src/platform/colorScheme.ts index a05a4e4bb..b3c0116fd 100644 --- a/src/platform/colorScheme.ts +++ b/src/platform/colorScheme.ts @@ -771,7 +771,9 @@ export function schemeCss( ] .filter(([k, v]) => /^--[\w-]+$/.test(k) && SAFE.test(v)) .map(([k, v]) => ` ${k}: ${v} !important;`); - if (languageBody.length) css += `.cm-editor[data-language="${language}"] {\n${languageBody.join("\n")}\n}\n`; + // The editor of that language, and a block of it in another editor (a Markdown code fence). + if (languageBody.length) + css += `.cm-editor[data-language="${language}"], .cm-editor [data-language="${language}"] {\n${languageBody.join("\n")}\n}\n`; } return css; } diff --git a/src/styles/tokens.css b/src/styles/tokens.css index 81d034a61..acf1f01ac 100644 --- a/src/styles/tokens.css +++ b/src/styles/tokens.css @@ -1573,8 +1573,9 @@ html { --bg-identifier-under-caret: #d9e6ff; --bg-identifier-under-caret-write: #e0e0cc; } -/* PHP editors: variables and function calls use the PHP colours (PHP_VAR / PHP_FUNCTION_CALL). */ -.cm-editor[data-language="PHP"] { +/* PHP editors and PHP code blocks: variables and function calls use the PHP colours (PHP_VAR / PHP_FUNCTION_CALL). */ +.cm-editor[data-language="PHP"], +.cm-editor [data-language="PHP"] { --syn-variable: var(--syn-php-variable); --syn-function-call: var(--syn-php-function-call); }