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>
This commit is contained in:
Bert Verhelst
2026-11-17 02:45:11 +08:00
committed by GitHub
co-authored by bertyhell
parent 4010f61453
commit 489dd408bb
8 changed files with 262 additions and 8 deletions
+1
View File
@@ -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",
+3
View File
@@ -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
+48 -3
View File
@@ -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<string, LanguageInfo> | null = null;
let infosPromise: Promise<Map<string, LanguageInfo>> | 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<LanguageDescription, LanguageSupport>();
const fencedProxies = new WeakMap<LanguageDescription, LanguageDescription>();
/**
* 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<LanguageSupport | null> {
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. */
+117
View File
@@ -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<DocBackend> 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"],
]);
});
});
+79
View File
@@ -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<string, Decoration>();
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<Decoration>[] = [];
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)]);
}
+8 -2
View File
@@ -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");
});
+3 -1
View File
@@ -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;
}
+3 -2
View File
@@ -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);
}