Skip to content

Commit 307facd

Browse files
committed
feat: slightly better highlight for code
1 parent 8912620 commit 307facd

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

apps/docs/app/(home)/blog/[slug]/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Footer } from '@/components/footer';
1414
import { SciFiButton } from '@/components/landing/scifi-btn';
1515
import { Prose } from '@/components/prose';
1616
import { getPosts, getSinglePost } from '@/lib/blog-query';
17+
import type { Post } from '@/types/post';
1718

1819
const STRIP_HTML_REGEX = /<[^>]+>/g;
1920
const WORD_SPLIT_REGEX = /\s+/;
@@ -96,12 +97,11 @@ export default async function PostPage({
9697
}) {
9798
const { slug } = await params;
9899
const result = (await getSinglePost(slug)) as {
99-
post?: import('@/types/post').Post;
100+
post?: Post;
100101
error?: boolean;
101102
status?: number;
102103
statusText?: string;
103104
};
104-
105105
if (!result?.post) {
106106
return (
107107
<>

apps/docs/components/prose.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type { ComponentPropsWithoutRef } from 'react';
2+
import { enhanceCodeBlocks } from '@/lib/enhance-code-blocks';
23
import { cn } from '@/lib/utils';
34

45
interface ProseProps extends ComponentPropsWithoutRef<'article'> {
56
html: string;
67
}
78

89
export function Prose({ children, html, className }: ProseProps) {
10+
const enhancedHtml = html ? enhanceCodeBlocks(html) : '';
911
return (
1012
<article
1113
className={cn(
@@ -31,13 +33,6 @@ export function Prose({ children, html, className }: ProseProps) {
3133
'prose-li:marker:text-foreground/60',
3234
'prose-ol:my-4',
3335
'prose-ul:my-4',
34-
'prose-code:bg-muted',
35-
'prose-code:px-1.5',
36-
'prose-code:py-0.5',
37-
'prose-code:rounded',
38-
'prose-code:text-foreground',
39-
'prose-pre:bg-muted',
40-
'prose-pre:rounded',
4136
'prose-img:rounded',
4237
'prose-table:border-border',
4338
'prose-blockquote:border-l-2',
@@ -48,7 +43,11 @@ export function Prose({ children, html, className }: ProseProps) {
4843
)}
4944
>
5045
{/** biome-ignore lint/security/noDangerouslySetInnerHtml: Needed to render markdown */}
51-
{html ? <div dangerouslySetInnerHTML={{ __html: html }} /> : children}
46+
{html ? (
47+
<div dangerouslySetInnerHTML={{ __html: enhancedHtml }} />
48+
) : (
49+
children
50+
)}
5251
</article>
5352
);
5453
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Enhance HTML content with better code block styling
3+
*/
4+
export function enhanceCodeBlocks(html: string): string {
5+
let enhanced = html;
6+
7+
// First handle code blocks (pre > code)
8+
enhanced = enhanced.replace(
9+
/<pre><code([^>]*)>/g,
10+
'<pre class="not-prose relative my-6 overflow-x-auto rounded border border-border bg-muted p-4 font-mono text-sm leading-6 [&_code]:bg-transparent [&_code]:p-0"><code$1 class="block text-foreground">'
11+
);
12+
13+
// Handle pre blocks without inner code tag
14+
enhanced = enhanced.replace(
15+
/<pre(?![^>]*class=.*not-prose)([^>]*)>/g,
16+
'<pre$1 class="not-prose relative my-6 overflow-x-auto rounded border border-border bg-muted p-4 font-mono text-sm leading-6 text-foreground">'
17+
);
18+
19+
// Handle inline code (not inside pre blocks)
20+
enhanced = enhanced.replace(
21+
/<code(?![^>]*class=.*block)([^>]*)>/g,
22+
'<code$1 class="relative rounded bg-muted px-[0.3rem] py-[0.2rem] font-mono text-sm font-semibold text-foreground">'
23+
);
24+
25+
return enhanced;
26+
}

0 commit comments

Comments
 (0)