feat: parse for thinking tags and grey them

This commit is contained in:
Ting-Wai To 2025-09-09 13:11:27 -07:00
parent 3ffd12c7ea
commit 01b60cab5f
2 changed files with 48 additions and 5 deletions

View file

@ -0,0 +1,36 @@
import { render } from "ink-testing-library";
import React from "react";
import { MarkdownRenderer } from "./MarkdownRenderer.js";
describe("MarkdownRenderer - thinking tags", () => {
it("handles multiple thinking tags", () => {
const content =
"Text <think>First thought</think> middle <think>Second thought</think> end";
const { lastFrame } = render(<MarkdownRenderer content={content} />);
expect(lastFrame()).toContain("First thought");
expect(lastFrame()).toContain("Second thought");
});
it("handles thinking tags with multiline content", () => {
const content = `<think>
This is a multiline
thinking block
</think>`;
const { lastFrame } = render(<MarkdownRenderer content={content} />);
expect(lastFrame()).toContain("This is a multiline");
expect(lastFrame()).toContain("thinking block");
});
it("ignores thinking tags inside code blocks", () => {
const content = "```\n<think>This should not be processed</think>\n```";
const { lastFrame } = render(<MarkdownRenderer content={content} />);
// The thinking tag should appear as literal text within the code block
expect(lastFrame()).toContain(
"<think>This should not be processed</think>",
);
});
});

View file

@ -2,10 +2,10 @@ import { Text } from "ink";
import React from "react";
import {
highlightCode,
detectLanguage,
SyntaxHighlighterTheme,
defaultTheme,
detectLanguage,
highlightCode,
SyntaxHighlighterTheme,
} from "./SyntaxHighlighter.js";
interface MarkdownRendererProps {
@ -28,6 +28,14 @@ const MarkdownRenderer: React.FC<MarkdownRendererProps> = React.memo(
return null; // This will be handled separately
},
},
{
regex: /<think>([\s\S]*?)<\/think>/g,
render: (content, key) => (
<Text key={key} color="gray">
{content.trim()}
</Text>
),
},
{
regex: /^(#{1,6})\s+(.+)$/gm,
render: (content, key) => (
@ -225,6 +233,5 @@ const MarkdownRenderer: React.FC<MarkdownRendererProps> = React.memo(
MarkdownRenderer.displayName = "MarkdownRenderer";
export { MarkdownRenderer };
export { defaultTheme, MarkdownRenderer };
export type { SyntaxHighlighterTheme };
export { defaultTheme };