Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(editor): keep a fence title out of the code block language (#10257)

xiaoke12345678da3fb7d142dc
diff --git a/packages/views/editor/extensions/code-block.test.ts b/packages/views/editor/extensions/code-block.test.ts
index 59a00bb65a..fca8d5bc36 100644
--- a/packages/views/editor/extensions/code-block.test.ts
+++ b/packages/views/editor/extensions/code-block.test.ts
@@ -92,6 +92,37 @@ describe("code block Markdown save and reload", () => {
});
});
+describe("fence info string beyond the language", () => {
+ // The language is the info string's first word; the rest (a title, line
+ // marks) is metadata the editor keeps and writes back unchanged (#10255).
+ it.each([
+ ['mermaid title="Request flow"', "mermaid", 'title="Request flow"'],
+ ["ts title='client.ts' {1,3}", "ts", "title='client.ts' {1,3}"],
+ ["html title=Report", "html", "title=Report"],
+ ])("splits %j into language and metadata, and saves it verbatim", (info, language, meta) => {
+ const content = "graph LR\n A --> B";
+ const source = `Intro\n\n\`\`\`${info}\n${content}\n\`\`\`\n\nOutro`;
+ const saved = roundTrip(source, content, language);
+ expect(load(source).state.doc.child(1).attrs.meta).toBe(meta);
+ expect(saved).toBe(`Edited ${source}`);
+ });
+
+ it("has no metadata for a bare language", () => {
+ const editor = load("Intro\n\n```ts\nconst a = 1;\n```\n\nOutro");
+ expect(editor.state.doc.child(1).attrs.meta).toBeNull();
+ });
+
+ it("keeps the metadata through the HTML clipboard path", () => {
+ const editor = load('Intro\n\n```mermaid title="Request flow"\ngraph LR\n```\n\nOutro');
+ const html = editor.getHTML();
+ const pasted = load("");
+ pasted.commands.setContent(html);
+ const block = pasted.state.doc.child(1);
+ expect(block.attrs.language).toBe("mermaid");
+ expect(block.attrs.meta).toBe('title="Request flow"');
+ });
+});
+
describe("production preprocessing and large-document loading", () => {
it("keeps a URL literal after serialization grows the outer fence", () => {
const content = "https://example.com\nconst ticks = '`````';";
diff --git a/packages/views/editor/extensions/code-block.ts b/packages/views/editor/extensions/code-block.ts
index a5c16b5a07..ff36b3b6a7 100644
--- a/packages/views/editor/extensions/code-block.ts
+++ b/packages/views/editor/extensions/code-block.ts
@@ -1,6 +1,50 @@
import CodeBlockLowlight from "@tiptap/extension-code-block-lowlight";
+/**
+ * Splits a fence info string into its language (the first word) and the rest,
+ * e.g. ```mermaid title="Request flow" → `mermaid` + `title="Request flow"`.
+ * Everything that dispatches on the language (preview, highlighting, the
+ * label) reads `language`; `meta` is only carried so a save writes the info
+ * string back unchanged (#10255).
+ */
+export function splitFenceInfo(info: string | null | undefined): {
+ language: string | null;
+ meta: string | null;
+} {
+ const trimmed = (info ?? "").trim();
+ if (!trimmed) return { language: null, meta: null };
+ const space = trimmed.search(/\s/);
+ if (space === -1) return { language: trimmed, meta: null };
+ return {
+ language: trimmed.slice(0, space),
+ meta: trimmed.slice(space).trim() || null,
+ };
+}
+
export const CodeBlockExtension = CodeBlockLowlight.extend({
+ addAttributes() {
+ return {
+ ...this.parent?.(),
+ meta: {
+ default: null,
+ parseHTML: (element) => element.getAttribute("data-meta"),
+ renderHTML: (attributes) =>
+ attributes.meta ? { "data-meta": attributes.meta } : {},
+ },
+ };
+ },
+ parseMarkdown(token, helpers) {
+ // Same acceptance rule as @tiptap/extension-code-block: fenced or indented.
+ const raw = token.raw ?? "";
+ if (!raw.startsWith("```") && !raw.startsWith("~~~") && token.codeBlockStyle !== "indented") {
+ return [];
+ }
+ return helpers.createNode(
+ "codeBlock",
+ splitFenceInfo(token.lang),
+ token.text ? [helpers.createTextNode(token.text)] : [],
+ );
+ },
renderMarkdown(node, helpers) {
const content = helpers.renderChildren(node.content ?? []);
// A literal fence inside the code must not close the serialized block.
@@ -9,6 +53,9 @@ export const CodeBlockExtension = CodeBlockLowlight.extend({
fenceLength = Math.max(fenceLength, run.length + 1);
}
const fence = "`".repeat(fenceLength);
- return `${fence}${node.attrs?.language ?? ""}\n${content}\n${fence}`;
+ const language = node.attrs?.language ?? "";
+ const meta = node.attrs?.meta;
+ const info = meta ? `${language} ${meta}` : language;
+ return `${fence}${info}\n${content}\n${fence}`;
},
});