xonecas.io
53129
Pa

PAT / commit 3d7a451

fix: better styling for code

xonecas2025-12-16 3d7a451b68d9942844d03abfd052f85261e83b9a parent a3e2f6c

4 files changed

AGENTS.md+1−0
@@ -216,3 +216,4 @@ Agents MUST use only these existing dependencies. Do NOT add new dependencies wi
216216 - **DO** maintain existing code style and patterns
217217 - **DO** use existing utility functions from `src/lib/`
218218 - **DO** follow the App Router conventions for Next.js 16
219+- **DO** keep this file up to date as changes are made in the code
app/globals.css+12−16
@@ -3,7 +3,7 @@
33 @tailwind utilities;
44
55 .markdown-content {
6- @apply text-stone-800 dark:text-stone-200;
6+ @apply flex flex-col gap-6 text-stone-800 dark:text-stone-200;
77 }
88
99 .markdown-content h1,
@@ -12,7 +12,7 @@
1212 .markdown-content h4,
1313 .markdown-content h5,
1414 .markdown-content h6 {
15- @apply mb-1 font-bold;
15+ @apply font-bold;
1616 }
1717
1818 .markdown-content h1 {
@@ -27,15 +27,6 @@
2727 @apply text-lg;
2828 }
2929
30-.markdown-content p {
31- @apply mb-1;
32-}
33-
34-.markdown-content ul,
35-.markdown-content ol {
36- @apply mb-1 ml-6;
37-}
38-
3930 .markdown-content ul {
4031 @apply list-disc;
4132 }
@@ -44,12 +35,17 @@
4435 @apply list-decimal;
4536 }
4637
38+.markdown-content ul,
39+.markdown-content ol {
40+ @apply flex flex-col gap-0;
41+}
42+
4743 .markdown-content li {
48- @apply mb-1;
44+ @apply ml-4;
4945 }
5046
5147 .markdown-content blockquote {
52- @apply my-1 border-l-4 border-stone-500 pl-1 italic dark:border-stone-400;
48+ @apply pl-2 border-l-4 border-stone-500 italic dark:border-stone-400;
5349 }
5450
5551 .markdown-content a {
@@ -57,12 +53,12 @@
5753 }
5854
5955 .markdown-content table {
60- @apply mb-1 border-collapse border border-stone-500 dark:border-stone-400;
56+ @apply border-collapse border border-stone-500 dark:border-stone-400;
6157 }
6258
6359 .markdown-content th,
6460 .markdown-content td {
65- @apply border border-stone-500 px-1 py-1 dark:border-stone-400;
61+ @apply border border-stone-500 dark:border-stone-400;
6662 }
6763
6864 .markdown-content th {
@@ -71,5 +67,5 @@
7167
7268 /* Code block styling */
7369 .markdown-content pre {
74- @apply mb-1 overflow-x-auto p-1;
70+ @apply overflow-x-auto;
7571 }
app/highlight-theme.css+264−0
@@ -0,0 +1,264 @@
1+/* Custom highlight.js theme using Tailwind colors (stone, green, orange, indigo) */
2+
3+/* Reset code block inheritance */
4+.markdown-content pre,
5+.markdown-content pre code {
6+ color: inherit;
7+}
8+
9+/* Inline code - no background */
10+.markdown-content code:not(pre code) {
11+ color: inherit;
12+ background: transparent;
13+}
14+
15+/* Light mode - code blocks only */
16+.markdown-content pre {
17+ background: #f5f5f4; /* stone-100 */
18+ padding: 0.5rem;
19+}
20+
21+.markdown-content .hljs,
22+.markdown-content pre code.hljs,
23+.markdown-content pre .hljs,
24+pre code.hljs,
25+pre .hljs,
26+.hljs {
27+ background: transparent;
28+ color: #292524; /* stone-800 */
29+}
30+
31+.markdown-content .hljs-comment,
32+.markdown-content .hljs-quote,
33+.hljs-comment,
34+.hljs-quote {
35+ color: #78716c; /* stone-500 */
36+ font-style: italic;
37+}
38+
39+.markdown-content .hljs-keyword,
40+.markdown-content .hljs-selector-tag,
41+.markdown-content .hljs-subst,
42+.hljs-keyword,
43+.hljs-selector-tag,
44+.hljs-subst {
45+ color: #166534; /* green-800 */
46+ font-weight: 600;
47+}
48+
49+.markdown-content .hljs-number,
50+.markdown-content .hljs-literal,
51+.markdown-content .hljs-variable,
52+.markdown-content .hljs-template-variable,
53+.markdown-content .hljs-tag .hljs-attr,
54+.hljs-number,
55+.hljs-literal,
56+.hljs-variable,
57+.hljs-template-variable,
58+.hljs-tag .hljs-attr {
59+ color: #ea580c; /* orange-600 */
60+}
61+
62+.markdown-content .hljs-string,
63+.markdown-content .hljs-doctag,
64+.hljs-string,
65+.hljs-doctag {
66+ color: #c2410c; /* orange-700 */
67+}
68+
69+.markdown-content .hljs-title,
70+.markdown-content .hljs-section,
71+.markdown-content .hljs-selector-id,
72+.hljs-title,
73+.hljs-section,
74+.hljs-selector-id {
75+ color: #4f46e5; /* indigo-600 */
76+ font-weight: 600;
77+}
78+
79+.markdown-content .hljs-type,
80+.markdown-content .hljs-class .hljs-title,
81+.hljs-type,
82+.hljs-class .hljs-title {
83+ color: #6366f1; /* indigo-500 */
84+}
85+
86+.markdown-content .hljs-tag,
87+.markdown-content .hljs-name,
88+.markdown-content .hljs-attribute,
89+.hljs-tag,
90+.hljs-name,
91+.hljs-attribute {
92+ color: #166534; /* green-800 */
93+ font-weight: normal;
94+}
95+
96+.markdown-content .hljs-regexp,
97+.markdown-content .hljs-link,
98+.hljs-regexp,
99+.hljs-link {
100+ color: #c2410c; /* orange-700 */
101+}
102+
103+.markdown-content .hljs-symbol,
104+.markdown-content .hljs-bullet,
105+.hljs-symbol,
106+.hljs-bullet {
107+ color: #ea580c; /* orange-600 */
108+}
109+
110+.markdown-content .hljs-built_in,
111+.markdown-content .hljs-builtin-name,
112+.hljs-built_in,
113+.hljs-builtin-name {
114+ color: #4f46e5; /* indigo-600 */
115+}
116+
117+.markdown-content .hljs-meta,
118+.hljs-meta {
119+ color: #78716c; /* stone-500 */
120+}
121+
122+.hljs-deletion {
123+ background: #fee2e2; /* red-100 */
124+}
125+
126+.hljs-addition {
127+ background: #dcfce7; /* green-100 */
128+}
129+
130+.hljs-emphasis {
131+ font-style: italic;
132+}
133+
134+.hljs-strong {
135+ font-weight: 600;
136+}
137+
138+/* Dark mode */
139+.dark .markdown-content pre,
140+.dark .markdown-content pre code {
141+ color: inherit;
142+}
143+
144+/* Inline code - no background */
145+.dark .markdown-content code:not(pre code) {
146+ color: inherit;
147+ background: transparent;
148+}
149+
150+.dark .markdown-content pre {
151+ background: #0c0a09; /* stone-950 */
152+ padding: 0.5rem;
153+}
154+
155+.dark .markdown-content .hljs,
156+.dark .markdown-content pre code.hljs,
157+.dark .markdown-content pre .hljs,
158+.dark pre code.hljs,
159+.dark pre .hljs,
160+.dark .hljs {
161+ background: transparent;
162+ color: #e7e5e4; /* stone-200 */
163+}
164+
165+.dark .markdown-content .hljs-comment,
166+.dark .markdown-content .hljs-quote,
167+.dark .hljs-comment,
168+.dark .hljs-quote {
169+ color: #a8a29e; /* stone-400 */
170+ font-style: italic;
171+}
172+
173+.dark .markdown-content .hljs-keyword,
174+.dark .markdown-content .hljs-selector-tag,
175+.dark .markdown-content .hljs-subst,
176+.dark .hljs-keyword,
177+.dark .hljs-selector-tag,
178+.dark .hljs-subst {
179+ color: #22c55e; /* green-500 */
180+ font-weight: 600;
181+}
182+
183+.dark .markdown-content .hljs-number,
184+.dark .markdown-content .hljs-literal,
185+.dark .markdown-content .hljs-variable,
186+.dark .markdown-content .hljs-template-variable,
187+.dark .markdown-content .hljs-tag .hljs-attr,
188+.dark .hljs-number,
189+.dark .hljs-literal,
190+.dark .hljs-variable,
191+.dark .hljs-template-variable,
192+.dark .hljs-tag .hljs-attr {
193+ color: #fb923c; /* orange-400 */
194+}
195+
196+.dark .markdown-content .hljs-string,
197+.dark .markdown-content .hljs-doctag,
198+.dark .hljs-string,
199+.dark .hljs-doctag {
200+ color: #fdba74; /* orange-300 */
201+}
202+
203+.dark .markdown-content .hljs-title,
204+.dark .markdown-content .hljs-section,
205+.dark .markdown-content .hljs-selector-id,
206+.dark .hljs-title,
207+.dark .hljs-section,
208+.dark .hljs-selector-id {
209+ color: #818cf8; /* indigo-400 */
210+ font-weight: 600;
211+}
212+
213+.dark .markdown-content .hljs-type,
214+.dark .markdown-content .hljs-class .hljs-title,
215+.dark .hljs-type,
216+.dark .hljs-class .hljs-title {
217+ color: #a5b4fc; /* indigo-300 */
218+}
219+
220+.dark .markdown-content .hljs-tag,
221+.dark .markdown-content .hljs-name,
222+.dark .markdown-content .hljs-attribute,
223+.dark .hljs-tag,
224+.dark .hljs-name,
225+.dark .hljs-attribute {
226+ color: #22c55e; /* green-500 */
227+ font-weight: normal;
228+}
229+
230+.dark .markdown-content .hljs-regexp,
231+.dark .markdown-content .hljs-link,
232+.dark .hljs-regexp,
233+.dark .hljs-link {
234+ color: #fdba74; /* orange-300 */
235+}
236+
237+.dark .markdown-content .hljs-symbol,
238+.dark .markdown-content .hljs-bullet,
239+.dark .hljs-symbol,
240+.dark .hljs-bullet {
241+ color: #fb923c; /* orange-400 */
242+}
243+
244+.dark .markdown-content .hljs-built_in,
245+.dark .markdown-content .hljs-builtin-name,
246+.dark .hljs-built_in,
247+.dark .hljs-builtin-name {
248+ color: #818cf8; /* indigo-400 */
249+}
250+
251+.dark .markdown-content .hljs-meta,
252+.dark .hljs-meta {
253+ color: #a8a29e; /* stone-400 */
254+}
255+
256+.dark .hljs-deletion {
257+ background: #7f1d1d; /* red-900 */
258+}
259+
260+.dark .hljs-addition {
261+ background: #14532d; /* green-800 */
262+}
263+
264+
app/page.tsx+49−42
@@ -4,7 +4,7 @@ import ReactMarkdown from "react-markdown";
44 import remarkGfm from "remark-gfm";
55 import rehypeHighlight from "rehype-highlight";
66 import PaperPlaneIcon from "@/src/components/icons/PaperPlaneIcon";
7-import "highlight.js/styles/nord.css";
7+import "./highlight-theme.css";
88
99 type Thread = {
1010 id: number;
@@ -29,8 +29,9 @@ export default function Home() {
2929 const [currentThreadId, setCurrentThreadId] = useState<number | null>(null);
3030 const [threads, setThreads] = useState<Thread[]>([]);
3131 const [isLoading, setIsLoading] = useState(false);
32- const messagesEndRef = useRef<HTMLDivElement>(null);
3332 const messagesContainerRef = useRef<HTMLDivElement>(null);
33+ const messageRefs = useRef<Map<number, HTMLDivElement>>(new Map());
34+ const prevMessagesLengthRef = useRef<number>(0);
3435
3536 const applyTheme = (mode: "device" | "dark" | "light") => {
3637 let shouldBeDark = false;
@@ -86,18 +87,22 @@ export default function Home() {
8687 }, []);
8788
8889 useEffect(() => {
89- if (messagesContainerRef.current) {
90- messagesContainerRef.current.scrollTop = messagesContainerRef.current.scrollHeight;
90+ // Scroll to the start of the newly added message
91+ if (messages.length > prevMessagesLengthRef.current) {
92+ const newMessage = messages[messages.length - 1];
93+ // Use requestAnimationFrame to ensure DOM has updated
94+ requestAnimationFrame(() => {
95+ const messageElement = messageRefs.current.get(newMessage.id);
96+ if (messageElement && messagesContainerRef.current) {
97+ const container = messagesContainerRef.current;
98+ const messageTop = messageElement.offsetTop;
99+ container.scrollTop = messageTop;
100+ }
101+ });
102+ prevMessagesLengthRef.current = messages.length;
91103 }
92104 }, [messages]);
93105
94- useEffect(() => {
95- // Initial scroll to bottom
96- if (messagesContainerRef.current) {
97- messagesContainerRef.current.scrollTop = messagesContainerRef.current.scrollHeight;
98- }
99- }, []);
100-
101106 const handleThemeChange = (mode: "device" | "dark" | "light") => {
102107 setThemeMode(mode);
103108 localStorage.setItem("themeMode", mode);
@@ -108,29 +113,35 @@ export default function Home() {
108113 try {
109114 const res = await fetch(`/api/threads/${threadId}/messages`);
110115 const data = await res.json();
111- setMessages(data.messages || []);
116+ const loadedMessages = data.messages || [];
117+ setMessages(loadedMessages);
118+ prevMessagesLengthRef.current = loadedMessages.length;
112119 } catch (error) {
113120 console.error("Failed to load messages", error);
114121 }
115122 };
116123
117- const createNewThread = async () => {
124+ const createNewThread = async (titleOverride?: string): Promise<number | null> => {
118125 try {
126+ const title = titleOverride || (input.trim() ? input.substring(0, 20) : "New Thread");
119127 const res = await fetch("/api/threads", {
120128 method: "POST",
121129 headers: { "Content-Type": "application/json" },
122- body: JSON.stringify({ title: "New Thread" }),
130+ body: JSON.stringify({ title }),
123131 });
124132 const data = await res.json();
125133 const newThread = data.thread;
126134 setCurrentThreadId(newThread.id);
127135 setMessages([]);
136+ prevMessagesLengthRef.current = 0;
128137 // Reload threads to get the full list
129138 const threadsRes = await fetch("/api/threads");
130139 const threadsData = await threadsRes.json();
131140 setThreads(threadsData.threads || []);
141+ return newThread.id;
132142 } catch (error) {
133143 console.error("Failed to create thread", error);
144+ return null;
134145 }
135146 };
136147
@@ -148,22 +159,8 @@ export default function Home() {
148159 let threadId = currentThreadId;
149160 if (!threadId) {
150161 // Create a new thread if none exists
151- try {
152- const res = await fetch("/api/threads", {
153- method: "POST",
154- headers: { "Content-Type": "application/json" },
155- body: JSON.stringify({ title: input.substring(0, 50) }),
156- });
157- const data = await res.json();
158- threadId = data.thread.id;
159- setCurrentThreadId(threadId);
160- setMessages([]);
161- // Reload threads to ensure UI is in sync
162- const threadsRes = await fetch("/api/threads");
163- const threadsData = await threadsRes.json();
164- setThreads(threadsData.threads || []);
165- } catch (error) {
166- console.error("Failed to create thread", error);
162+ threadId = await createNewThread(input.substring(0, 20));
163+ if (!threadId) {
167164 setIsLoading(false);
168165 return;
169166 }
@@ -218,9 +215,11 @@ export default function Home() {
218215 setHistoryIndex(null);
219216 };
220217 const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
218+ // Only cycle through user messages, not assistant messages
219+ const userMessages = messages.filter((m) => m.role === "user");
220+
221221 if (e.key === "ArrowUp") {
222222 e.preventDefault();
223- const userMessages = messages.filter((m) => m.role === "user");
224223 const idx = historyIndex ?? userMessages.length;
225224 if (idx > 0) {
226225 const newIdx = idx - 1;
@@ -230,7 +229,6 @@ export default function Home() {
230229 } else if (e.key === "ArrowDown") {
231230 e.preventDefault();
232231 if (historyIndex !== null) {
233- const userMessages = messages.filter((m) => m.role === "user");
234232 const newIdx = historyIndex + 1;
235233 if (newIdx < userMessages.length) {
236234 setHistoryIndex(newIdx);
@@ -259,9 +257,9 @@ export default function Home() {
259257 <div className="flex min-w-0 flex-1 flex-col">
260258 <div
261259 ref={messagesContainerRef}
262- className="text-sm pt-2 px-2 flex-1 overflow-y-auto overflow-x-hidden bg-stone-100 dark:bg-stone-950"
260+ className="text-sm p-4 flex-1 overflow-y-auto overflow-x-hidden bg-stone-100 dark:bg-stone-950"
263261 >
264- <div className="flex min-h-full flex-col justify-end gap-3">
262+ <div className="flex min-h-full flex-col justify-end gap-8">
265263 {threads.length === 0 && messages.length === 0 && currentThreadId === null ? (
266264 <div className="min-w-0">
267265 <div className="markdown-content p-1 bg-stone-200 text-stone-800 dark:bg-stone-900 dark:text-stone-200">
@@ -276,9 +274,19 @@ Once you've created a thread, you can start chatting with me by typing a message
276274 </div>
277275 ) : (
278276 messages.map((msg) => (
279- <div key={msg.id} className="min-w-0">
277+ <div
278+ key={msg.id}
279+ ref={(el) => {
280+ if (el) {
281+ messageRefs.current.set(msg.id, el);
282+ } else {
283+ messageRefs.current.delete(msg.id);
284+ }
285+ }}
286+ className="min-w-0"
287+ >
280288 <div
281- className={`markdown-content p-1 text-stone-800 dark:text-stone-200 ${
289+ className={`markdown-content p-2 text-stone-800 dark:text-stone-200 ${
282290 msg.role === "assistant"
283291 ? "bg-stone-200 dark:bg-stone-900"
284292 : "bg-stone-300 dark:bg-stone-800 text-right"
@@ -301,10 +309,9 @@ Once you've created a thread, you can start chatting with me by typing a message
301309 </div>
302310 ))
303311 )}
304- <div ref={messagesEndRef} />
305312 </div>
306313 </div>
307- <form onSubmit={sendMessage} className="flex">
314+ <form onSubmit={sendMessage} className="flex shadow-[0_-4px_6px_-1px_rgba(0,0,0,0.1),0_-2px_4px_-1px_rgba(0,0,0,0.06)] dark:shadow-[0_-4px_6px_-1px_rgba(0,0,0,0.3),0_-2px_4px_-1px_rgba(0,0,0,0.2)]">
308315 <input
309316 type="text"
310317 value={input}
@@ -324,9 +331,9 @@ Once you've created a thread, you can start chatting with me by typing a message
324331 </form>
325332 </div>
326333 <div className="w-64 bg-stone-200 p-1 dark:bg-stone-900">
327- <div className="flex flex-col gap-2">
334+ <div className="flex flex-col gap-2 p-4">
328335 <button
329- onClick={createNewThread}
336+ onClick={() => createNewThread()}
330337 className="bg-green-900 px-1 py-1 text-stone-100 hover:bg-green-800 dark:bg-green-500 dark:text-stone-900 dark:hover:bg-green-600"
331338 >
332339 New Thread
@@ -341,7 +348,7 @@ Once you've created a thread, you can start chatting with me by typing a message
341348 handleThreadSelect(threadId);
342349 }
343350 }}
344- className="bg-stone-100 px-1 py-1 text-stone-800 focus:outline-none dark:bg-stone-950 dark:text-stone-200"
351+ className="bg-stone-100 px-3 py-1 text-stone-800 focus:outline-none dark:bg-stone-950 dark:text-stone-200"
345352 >
346353 <option value="">Select a thread...</option>
347354 {threads.map((thread) => (
@@ -356,7 +363,7 @@ Once you've created a thread, you can start chatting with me by typing a message
356363 <select
357364 value={themeMode}
358365 onChange={(e) => handleThemeChange(e.target.value as "device" | "dark" | "light")}
359- className="bg-stone-100 px-1 py-1 text-stone-800 focus:outline-none dark:bg-stone-950 dark:text-stone-200"
366+ className="bg-stone-100 px-3 py-1 text-stone-800 focus:outline-none dark:bg-stone-950 dark:text-stone-200"
360367 >
361368 <option value="device">Device</option>
362369 <option value="dark">Dark</option>