Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

markdown.md

208 lines6.0 KB

1---
2title: Highlight markdown code
3id: ui-markdown-highlight
4order: 9
5description: "Color fenced code in TextPart with TanStack Highlight."
6keywords:
7 - tanstack ai
8 - TextPart
9 - markdown
10 - highlight
11 - syntax highlighting
12---
13​
14Model replies include fenced code. `TextPart` renders those fences as plain text until you pass a `highlighter`.
15​
16Build a TanStack Highlight callback. Pass it to `TextPart`. Token colors then show on fenced blocks.
17​
18If your app still passes `remarkPlugins` or `rehypePlugins`, start with [TextPart markdown](../migration/text-part-markdown).
19​
20## 1. Install TanStack Highlight
21​
22```sh
23pnpm add @tanstack/highlight
24```
25​
26Use `@tanstack/highlight` 0.0.6 or newer. The Markdown adapter lives in that package.
27​
28## 2. Create the highlighter
29​
30Create `src/markdown-highlighter.ts`. Register only the languages your models emit.
31​
32```ts
33import { createHighlighter } from '@tanstack/highlight/core'
34import { js } from '@tanstack/highlight/languages/js'
35import { json } from '@tanstack/highlight/languages/json'
36import { plaintext } from '@tanstack/highlight/languages/plaintext'
37import { ts } from '@tanstack/highlight/languages/ts'
38import { tsx } from '@tanstack/highlight/languages/tsx'
39import { createTanStackMarkdownHighlighter } from '@tanstack/highlight/markdown'
40import type { CodeHighlighter } from '@tanstack/markdown'
41​
42const highlighter = createHighlighter({
43 languages: [plaintext, js, json, ts, tsx],
44})
45​
46export const highlightMarkdownCode: CodeHighlighter =
47 createTanStackMarkdownHighlighter(highlighter)
48```
49​
50Unknown fence languages render as escaped plain text.
51​
52> **CAUTION:** Highlighter output is inserted as trusted HTML. Use `createTanStackMarkdownHighlighter`. Do not pass a highlighter that returns unescaped source text.
53​
54## 3. Create the theme CSS
55​
56Create `src/markdown-highlight-theme.ts`. Point the selectors at a `.markdown-renderer` wrapper.
57​
58```ts
59import { createThemeCss } from '@tanstack/highlight/theme'
60import { githubDarkTheme } from '@tanstack/highlight/themes/github-dark'
61import { githubLightTheme } from '@tanstack/highlight/themes/github-light'
62​
63export const markdownHighlightCss = createThemeCss({
64 light: githubLightTheme,
65 dark: githubDarkTheme,
66 lightSelector: '.markdown-renderer',
67 darkSelector: '.dark .markdown-renderer',
68 codeBlockSelector: '.markdown-renderer pre.tm-code',
69 lineNumbersSelector: '.markdown-renderer .tm-code--line-numbers',
70})
71```
72​
73## 4. Pass it to TextPart
74​
75Put `markdown-renderer` on the `TextPart` wrapper so the theme CSS matches.
76​
77```tsx
78import { TextPart } from '@tanstack/ai-react/ui'
79import { highlightMarkdownCode } from './markdown-highlighter'
80​
81export function Reply({ content }: { content: string }) {
82 return (
83 <TextPart
84 content={content}
85 highlighter={highlightMarkdownCode}
86 className="markdown-renderer"
87 />
88 )
90```
91​
92## 5. Wire it into createChatHook
93​
94Inject the CSS once in `layout`. Register `TextPart` as `partsComponents.text`.
95​
96```tsx
97import { fetchServerSentEvents } from '@tanstack/ai-react'
98import { createChatHook, TextPart } from '@tanstack/ai-react/ui'
99import { highlightMarkdownCode } from './markdown-highlighter'
100import { markdownHighlightCss } from './markdown-highlight-theme'
101​
102const chatOptions = {
103 connection: fetchServerSentEvents('/api/chat'),
105​
106const { useAppChat, useChatContext } = createChatHook({
107 options: chatOptions,
108 components: {
109 input: () => {
110 const chat = useChatContext()
111 return (
112 <form
113 onSubmit={(event) => {
114 event.preventDefault()
115 const field = event.currentTarget.elements.namedItem('message')
116 if (!(field instanceof HTMLInputElement)) return
117 const text = field.value.trim()
118 if (!text) return
119 field.value = ''
120 void chat.sendMessage(text)
121 }}
122 >
123 <input name="message" />
124 <button type="submit">Send</button>
125 </form>
126 )
127 },
128 layout: ({ Messages, Input }) => (
129 <main>
130 <style>{markdownHighlightCss}</style>
131 <Messages />
132 <Input />
133 </main>
134 ),
135 message: ({ message, Parts }) => (
136 <article data-role={message.role}>
137 <Parts />
138 </article>
139 ),
140 },
141 partsComponents: {
142 text: ({ part }) => (
143 <TextPart
144 content={part.content}
145 highlighter={highlightMarkdownCode}
146 className="markdown-renderer"
147 />
148 ),
149 fallback: () => null,
150 },
151})
152​
153export function ChatScreen() {
154 const chat = useAppChat()
155 return <chat.AppChat />
157```
158​
159Send a prompt that returns a fenced `ts` block. The fence now shows token colors.
160​
161## Vue and Solid
162​
163Use the same highlighter file and the same theme file. The `TextPart` import changes. Inject `markdownHighlightCss` once at the app root.
164​
165### Vue
166​
167```ts
168import { defineComponent, h } from 'vue'
169import { TextPart } from '@tanstack/ai-vue/ui'
170import { highlightMarkdownCode } from './markdown-highlighter'
171​
172export default defineComponent({
173 props: { content: { type: String, required: true } },
174 setup(props) {
175 return () =>
176 h(TextPart, {
177 content: props.content,
178 highlighter: highlightMarkdownCode,
179 class: 'markdown-renderer',
180 })
181 },
182})
183```
184​
185### Solid
186​
187```tsx
188import { TextPart } from '@tanstack/ai-solid/ui'
189import { highlightMarkdownCode } from './markdown-highlighter'
190​
191export function Reply(props: { content: string }) {
192 return (
193 <TextPart
194 content={props.content}
195 highlighter={highlightMarkdownCode}
196 class="markdown-renderer"
197 />
198 )
200```
201​
202Vue and Solid have no `components` map. Tag swaps go through a TanStack Markdown `extension`.
203​
204## Later
205​
206- More languages: import them from `@tanstack/highlight/languages/*` and add them to `createHighlighter`.
207- Fence metadata and line numbers: see the [TanStack Markdown highlighting guide](https://tanstack.com/markdown/latest/docs/guides/syntax-highlighting).
208- React tag swaps (`a`, `img`): pass `components` on `TextPart`. That map uses normal HTML props, not `react-markdown` extras such as `inline`.