Skip to content

Commit 821d45c

Browse files
committed
created system to create post-specific components
1 parent 30df76b commit 821d45c

5 files changed

Lines changed: 34 additions & 9 deletions

File tree

apps/sim/app/(landing)/studio/[slug]/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Avatar, AvatarFallback, AvatarImage } from '@/components/emcn'
55
import { FAQ } from '@/lib/blog/faq'
66
import { getAllPostMeta, getPostBySlug, getRelatedPosts } from '@/lib/blog/registry'
77
import { buildArticleJsonLd, buildBreadcrumbJsonLd, buildPostMetadata } from '@/lib/blog/seo'
8+
import { getBaseUrl } from '@/lib/core/utils/urls'
89
import { soehne } from '@/app/_styles/fonts/soehne/soehne'
910
import { BackLink } from '@/app/(landing)/studio/[slug]/back-link'
1011
import { ShareButton } from '@/app/(landing)/studio/[slug]/share-button'
@@ -99,7 +100,7 @@ export default async function Page({ params }: { params: Promise<{ slug: string
99100
</div>
100101
))}
101102
</div>
102-
<ShareButton url={`https://sim.ai/studio/${slug}`} title={post.title} />
103+
<ShareButton url={`${getBaseUrl()}/studio/${slug}`} title={post.title} />
103104
</div>
104105
</div>
105106
</div>

apps/sim/app/(landing)/studio/[slug]/share-button.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ export function ShareButton({ url, title }: ShareButtonProps) {
1414
const [copied, setCopied] = useState(false)
1515

1616
const handleCopyLink = async () => {
17-
await navigator.clipboard.writeText(url)
18-
setCopied(true)
19-
setTimeout(() => {
20-
setCopied(false)
17+
try {
18+
await navigator.clipboard.writeText(url)
19+
setCopied(true)
20+
setTimeout(() => {
21+
setCopied(false)
22+
setOpen(false)
23+
}, 1000)
24+
} catch {
2125
setOpen(false)
22-
}, 1000)
26+
}
2327
}
2428

2529
const handleShareTwitter = () => {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { DiffControlsDemo } from './components/diff-controls-demo'

apps/sim/lib/blog/mdx.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import clsx from 'clsx'
22
import Image from 'next/image'
33
import type { MDXRemoteProps } from 'next-mdx-remote/rsc'
44
import { CodeBlock } from '@/lib/blog/code'
5-
import { DiffControlsDemo } from '@/content/blog/v0-5/components/diff-controls-demo'
65

76
export const mdxComponents: MDXRemoteProps['components'] = {
8-
DiffControlsDemo,
97
img: (props: any) => (
108
<Image
119
src={props.src}

apps/sim/lib/blog/registry.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import type { BlogMeta, BlogPost, TagWithCount } from '@/lib/blog/schema'
1010
import { AuthorSchema, BlogFrontmatterSchema } from '@/lib/blog/schema'
1111
import { AUTHORS_DIR, BLOG_DIR, byDateDesc, ensureContentDirs, toIsoDate } from '@/lib/blog/utils'
1212

13+
const postComponentsRegistry: Record<string, Record<string, React.ComponentType>> = {}
14+
1315
let cachedMeta: BlogMeta[] | null = null
1416
let cachedAuthors: Record<string, any> | null = null
1517

@@ -99,6 +101,21 @@ export async function getAllTags(): Promise<TagWithCount[]> {
99101
.sort((a, b) => b.count - a.count || a.tag.localeCompare(b.tag))
100102
}
101103

104+
async function loadPostComponents(slug: string): Promise<Record<string, React.ComponentType>> {
105+
if (postComponentsRegistry[slug]) {
106+
return postComponentsRegistry[slug]
107+
}
108+
109+
try {
110+
const postComponents = await import(`@/content/blog/${slug}/components`)
111+
postComponentsRegistry[slug] = postComponents
112+
return postComponents
113+
} catch {
114+
postComponentsRegistry[slug] = {}
115+
return {}
116+
}
117+
}
118+
102119
export async function getPostBySlug(slug: string): Promise<BlogPost> {
103120
const meta = await scanFrontmatters()
104121
const found = meta.find((m) => m.slug === slug)
@@ -107,9 +124,13 @@ export async function getPostBySlug(slug: string): Promise<BlogPost> {
107124
const raw = await fs.readFile(mdxPath, 'utf-8')
108125
const { content, data } = matter(raw)
109126
const fm = BlogFrontmatterSchema.parse(data)
127+
128+
const postComponents = await loadPostComponents(slug)
129+
const mergedComponents = { ...mdxComponents, ...postComponents }
130+
110131
const compiled = await compileMDX({
111132
source: content,
112-
components: mdxComponents as any,
133+
components: mergedComponents as any,
113134
options: {
114135
parseFrontmatter: false,
115136
mdxOptions: {

0 commit comments

Comments
 (0)