Skip to content

Commit 9c7618b

Browse files
fix(web): gitignore fences in the rich-text composer highlight as ini, as chat does (pingdotgg#35603)
1 parent d9a97e7 commit 9c7618b

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

‎apps/web/src/components/chat/MarkdownCodeBlock.logic.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ const CODE_FENCE_LANGUAGE_REGEX = /(?:^|\s)language-([^\s]+)/;
77

88
export function extractFenceLanguage(className: string | undefined): string {
99
const match = className?.match(CODE_FENCE_LANGUAGE_REGEX);
10-
const raw = match?.[1] ?? "text";
10+
return highlighterLanguageOf(match?.[1] ?? "text");
11+
}
12+
13+
export function highlighterLanguageOf(language: string): string {
1114
// Shiki doesn't bundle a gitignore grammar; ini is a close match.
12-
return raw === "gitignore" ? "ini" : raw;
15+
return language === "gitignore" ? "ini" : language;
1316
}
1417

1518
const FENCE_TITLE_ATTR_REGEX = /(?:^|\s)(?:title|file(?:name)?)=(?:"([^"]+)"|'([^']+)'|(\S+))/i;

‎apps/web/src/components/composerCodeBlockHighlight.test.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe("highlighting edited fences", () => {
4545
vi.unstubAllGlobals();
4646
});
4747

48-
async function mountFence(text: string, resolveTheme = () => THEME) {
48+
async function mountFence(text: string, resolveTheme = () => THEME, language = "ts") {
4949
vi.useFakeTimers();
5050
const highlighter = fakeHighlighter((content) =>
5151
content.startsWith("x") ? "#ff0000" : undefined,
@@ -60,9 +60,7 @@ describe("highlighting edited fences", () => {
6060
],
6161
content: {
6262
type: "doc",
63-
content: [
64-
{ type: "codeBlock", attrs: { language: "ts" }, content: [{ type: "text", text }] },
65-
],
63+
content: [{ type: "codeBlock", attrs: { language }, content: [{ type: "text", text }] }],
6664
},
6765
});
6866
vi.stubGlobal("document", { documentElement: {} });
@@ -134,4 +132,10 @@ describe("highlighting edited fences", () => {
134132
]);
135133
expect(fence.colored()).toEqual(["x".repeat(3_000), "x".repeat(3_000)]);
136134
});
135+
136+
it("highlights a gitignore fence with the ini grammar, as chat does", async () => {
137+
await mountFence("*.log\n!keep.log", () => THEME, 'gitignore title=".gitignore"');
138+
139+
expect(vi.mocked(getSyntaxHighlighterPromise)).toHaveBeenLastCalledWith("ini");
140+
});
137141
});

‎apps/web/src/components/composerCodeBlockHighlight.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Node as ProseMirrorNode } from "@tiptap/pm/model";
66
import type { DiffThemeName } from "~/lib/diffRendering";
77
import { languageOfInfoString } from "~/composer-code-languages";
88
import { getSyntaxHighlighterPromise } from "~/lib/syntaxHighlighting";
9+
import { highlighterLanguageOf } from "./chat/MarkdownCodeBlock.logic";
910

1011
interface HighlightedBlock {
1112
readonly signature: string;
@@ -119,8 +120,9 @@ export function composerCodeBlockHighlight(options: {
119120
void Promise.allSettled(
120121
pending.map(async ({ node }) => {
121122
// Stored info includes metadata after the language; Shiki needs only the name.
122-
const language =
123-
languageOfInfoString(String(node.attrs.language ?? "")) || "text";
123+
const language = highlighterLanguageOf(
124+
languageOfInfoString(String(node.attrs.language ?? "")) || "text",
125+
);
124126
const signature = blockSignature(node, theme);
125127
// Cache failure as plain text to clear retained colors.
126128
const highlighter = await getSyntaxHighlighterPromise(language).catch(() => null);

0 commit comments

Comments
 (0)