Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(markdown): keep syntax-highlighter tokens inline (#18542)

Engel Nyste2603404da03
diff --git a/.pr/toml/chat-toml-after-1.png b/.pr/toml/chat-toml-after-1.png
new file mode 100644
index 0000000000..d8518b95da
Binary files /dev/null and b/.pr/toml/chat-toml-after-1.png differ
diff --git a/.pr/toml/chat-toml-after-2.png b/.pr/toml/chat-toml-after-2.png
new file mode 100644
index 0000000000..1150110e65
Binary files /dev/null and b/.pr/toml/chat-toml-after-2.png differ
diff --git a/.pr/toml/chat-toml-before.png b/.pr/toml/chat-toml-before.png
new file mode 100644
index 0000000000..10f3f7bead
Binary files /dev/null and b/.pr/toml/chat-toml-before.png differ
diff --git a/.pr/toml/files-toml-after.png b/.pr/toml/files-toml-after.png
new file mode 100644
index 0000000000..d3756a3388
Binary files /dev/null and b/.pr/toml/files-toml-after.png differ
diff --git a/.pr/toml/files-toml-before.png b/.pr/toml/files-toml-before.png
new file mode 100644
index 0000000000..9535c10bcb
Binary files /dev/null and b/.pr/toml/files-toml-before.png differ
diff --git a/__tests__/components/features/markdown/code.test.tsx b/__tests__/components/features/markdown/code.test.tsx
index c5ba1562af..4005d43469 100644
--- a/__tests__/components/features/markdown/code.test.tsx
+++ b/__tests__/components/features/markdown/code.test.tsx
@@ -1,8 +1,32 @@
+import { readFile } from "node:fs/promises";
+import { resolve } from "node:path";
+import { compile } from "@tailwindcss/node";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, it, expect } from "vitest";
import { code as Code } from "#/components/features/markdown/code";
+/**
+ * jsdom ignores rules inside `@layer` blocks, so lift them out and let the
+ * stylesheet's selector specificity decide the cascade.
+ */
+function unwrapCascadeLayers(css: string): string {
+ let out = css.replace(/@layer [\w\s,-]+;/g, "");
+ let start = out.search(/@layer [\w-]+\s*\{/);
+ while (start !== -1) {
+ const open = out.indexOf("{", start);
+ let depth = 1;
+ let end = open + 1;
+ for (; depth > 0; end += 1) {
+ if (out[end] === "{") depth += 1;
+ if (out[end] === "}") depth -= 1;
+ }
+ out = out.slice(0, start) + out.slice(open + 1, end - 1) + out.slice(end);
+ start = out.search(/@layer [\w-]+\s*\{/);
+ }
+ return out;
+}
+
describe("code (markdown)", () => {
it("should render inline code without a copy button", () => {
render(<Code>inline snippet</Code>);
@@ -34,4 +58,32 @@ describe("code (markdown)", () => {
expect(navigator.clipboard.readText()).resolves.toBe("line1\nline2"),
);
});
+
+ it("keeps highlighted tokens inline when their Prism type is also a Tailwind utility", async () => {
+ // Prism names TOML section headers `token table`, and Tailwind generates
+ // `.table { display: table }` whenever the word appears in the sources.
+ const compiler = await compile(
+ await readFile(resolve("src/tailwind.css"), "utf8"),
+ { base: resolve("src"), onDependency: () => {} },
+ );
+ const style = document.createElement("style");
+ style.textContent = unwrapCascadeLayers(
+ compiler.build(["table", "static"]),
+ );
+ document.head.appendChild(style);
+
+ try {
+ const { container } = render(
+ <Code className="language-toml">
+ {'[build-system]\nrequires = ["hatchling"]'}
+ </Code>,
+ );
+
+ const sectionHeader = container.querySelector(".token.table");
+ expect(sectionHeader).toHaveTextContent("build-system");
+ expect(getComputedStyle(sectionHeader!).display).toBe("inline");
+ } finally {
+ style.remove();
+ }
+ });
});
diff --git a/src/tailwind.css b/src/tailwind.css
index 7f9af96b34..f02b8a75ac 100644
--- a/src/tailwind.css
+++ b/src/tailwind.css
@@ -483,6 +483,19 @@ button.extension-module-card-interactive {
}
}
+/*
+ * Syntax-highlighter tokens are inline text runs. Prism names each span after
+ * its grammar type (`token table` for TOML section headers and Markdown
+ * tables, `token static`, ...), and Tailwind generates a utility for any of
+ * those words it finds in the sources (`.table` is `display: table`, which
+ * puts `[`, the section name and `]` on separate lines). Theme colors arrive
+ * as inline styles, so they still apply. Unlayered, so it outranks utilities.
+ */
+code .token {
+ display: inline;
+ position: static;
+}
+
/* Custom styling for xterm */
/* XTerm.js scrollbar styling - follows custom-scrollbar pattern */
.xterm-viewport::-webkit-scrollbar {