Skip to content

Commit 1e7a32e

Browse files
committed
test
1 parent 006745e commit 1e7a32e

File tree

8 files changed

+79
-27
lines changed

8 files changed

+79
-27
lines changed

src/components/Doc.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import { GamHeader } from './Gam'
1414
import { Toc } from './Toc'
1515
import { TocMobile } from './TocMobile'
1616
import { DocFeedbackProvider } from './DocFeedbackProvider'
17+
import { MarkdownHeading } from '~/utils/markdown'
1718

1819
type DocProps = {
1920
title: string
2021
content: string
22+
html: { markup: string; headings: Array<MarkdownHeading> }
2123
repo: string
2224
branch: string
2325
filePath: string
@@ -33,6 +35,7 @@ type DocProps = {
3335
function DocContent({
3436
title,
3537
content,
38+
html,
3639
repo,
3740
branch,
3841
filePath,
@@ -43,9 +46,16 @@ function DocContent({
4346
libraryVersion,
4447
pagePath,
4548
}: DocProps) {
46-
const { headings } = useMarkdownHeadings()
49+
const { headings, setHeadings } = useMarkdownHeadings()
4750

48-
const isTocVisible = shouldRenderToc && headings && headings.length > 1
51+
React.useEffect(() => {
52+
if (html?.headings?.length) {
53+
setHeadings(html.headings)
54+
}
55+
}, [html?.headings, setHeadings])
56+
57+
const isTocVisible =
58+
shouldRenderToc && (html?.headings?.length ?? headings.length) > 1
4959

5060
const markdownContainerRef = React.useRef<HTMLDivElement>(null)
5161
const [activeHeadings, setActiveHeadings] = React.useState<Array<string>>([])
@@ -169,10 +179,18 @@ function DocContent({
169179
libraryId={libraryId}
170180
libraryVersion={libraryVersion}
171181
>
172-
<Markdown rawContent={content} />
182+
<Markdown
183+
htmlMarkup={html?.markup ?? ''}
184+
headingsOverride={html?.headings}
185+
rawContent={content}
186+
/>
173187
</DocFeedbackProvider>
174188
) : (
175-
<Markdown rawContent={content} />
189+
<Markdown
190+
htmlMarkup={html?.markup ?? ''}
191+
headingsOverride={html?.headings}
192+
rawContent={content}
193+
/>
176194
)}
177195
</div>
178196
<div className="h-12" />

src/components/FeedEntry.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export interface FeedEntry {
1414
title: string
1515
content: string
1616
excerpt?: string | null
17+
html?: {
18+
markup: string
19+
headings: Array<{ id: string; text: string; level: number }>
20+
}
1721
publishedAt: number
1822
createdAt: number
1923
updatedAt?: number
@@ -415,7 +419,11 @@ export function FeedEntry({
415419

416420
{/* Content */}
417421
<div className="text-xs text-gray-900 dark:text-gray-100 leading-snug mb-3">
418-
<Markdown rawContent={entry.content} />
422+
<Markdown
423+
htmlMarkup={entry.html?.markup ?? ''}
424+
headingsOverride={entry.html?.headings}
425+
rawContent={entry.content}
426+
/>
419427
</div>
420428

421429
{/* External Link */}

src/components/FeedEntryTimeline.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,11 @@ export function FeedEntryTimeline({
332332
!expanded && 'line-clamp-6',
333333
)}
334334
>
335-
<Markdown rawContent={entry.content} />
335+
<Markdown
336+
htmlMarkup={entry.html?.markup ?? ''}
337+
headingsOverride={entry.html?.headings}
338+
rawContent={entry.content}
339+
/>
336340
</div>
337341

338342
{/* Show more/less button */}

src/components/Markdown.tsx

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as React from 'react'
22
import { MarkdownLink } from '~/components/MarkdownLink'
33
import type { HTMLProps } from 'react'
4-
import { createHighlighter, type HighlighterGeneric } from 'shiki/bundle/web'
5-
import { transformerNotationDiff } from '@shikijs/transformers'
64
import parse, {
75
attributesToProps,
86
domToReact,
@@ -13,10 +11,13 @@ import type { Mermaid } from 'mermaid'
1311
import { useToast } from '~/components/ToastProvider'
1412
import { twMerge } from 'tailwind-merge'
1513
import { useMarkdownHeadings } from '~/components/MarkdownHeadingContext'
16-
import { renderMarkdown } from '~/utils/markdown'
1714
import { getNetlifyImageUrl } from '~/utils/netlifyImage'
1815
import { Tabs } from '~/components/Tabs'
1916
import { Copy } from 'lucide-react'
17+
import type {
18+
MarkdownHeading,
19+
MarkdownRenderResult,
20+
} from '~/utils/markdown/processor'
2021

2122
type HeadingLevel = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'
2223

@@ -384,29 +385,35 @@ const options: HTMLReactParserOptions = {
384385

385386
type MarkdownProps = {
386387
htmlMarkup: string
388+
headingsOverride?: MarkdownHeading[]
387389
rawContent?: string
388390
}
389391

390-
export function Markdown({ rawContent, htmlMarkup }: MarkdownProps) {
392+
export function Markdown({ rawContent, htmlMarkup, headingsOverride }: MarkdownProps) {
391393
const { setHeadings } = useMarkdownHeadings()
392394

393-
const rendered = React.useMemo(() => {
394-
if (rawContent) {
395-
return renderMarkdown(rawContent)
396-
}
397-
398-
return { markup: htmlMarkup, headings: [] }
399-
}, [rawContent, htmlMarkup])
400-
401395
React.useEffect(() => {
402-
setHeadings(rendered.headings)
403-
}, [rendered.headings, setHeadings])
396+
if (headingsOverride) {
397+
setHeadings(headingsOverride)
398+
} else {
399+
const { headings } = renderMarkdown(htmlMarkup || rawContent || '')
400+
setHeadings(headings)
401+
}
402+
}, [headingsOverride, htmlMarkup, rawContent, setHeadings])
404403

405-
return React.useMemo(() => {
406-
if (!rendered.markup) {
407-
return null
404+
const markup = React.useMemo(() => {
405+
if (htmlMarkup) {
406+
return htmlMarkup
408407
}
408+
if (rawContent) {
409+
return renderMarkdown(rawContent).markup
410+
}
411+
return ''
412+
}, [htmlMarkup, rawContent])
413+
414+
if (!markup) {
415+
return null
416+
}
409417

410-
return parse(rendered.markup, options)
411-
}, [rendered.markup])
418+
return React.useMemo(() => parse(markup, options), [markup])
412419
}

src/components/admin/FeedEntryEditor.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,10 @@ export function FeedEntryEditor({
483483
{excerpt}
484484
</p>
485485
)}
486-
<Markdown rawContent={content || '*No content yet*'} />
486+
<Markdown
487+
htmlMarkup=""
488+
rawContent={content || '*No content yet*'}
489+
/>
487490
</div>
488491
) : (
489492
<div className="text-center py-12 text-gray-400 dark:text-gray-500">

src/routes/_libraries/feed.$id.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,11 @@ function FeedEntryView({ entry }: { entry: FeedEntry }) {
364364

365365
{/* Content */}
366366
<div className="prose prose-sm dark:prose-invert max-w-none mb-4 prose-headings:mt-4 prose-headings:mb-2 prose-p:my-2 prose-ul:my-2 prose-ol:my-2 prose-li:my-0">
367-
<Markdown rawContent={entry.content} />
367+
<Markdown
368+
htmlMarkup={entry.html?.markup ?? ''}
369+
headingsOverride={entry.html?.headings}
370+
rawContent={entry.content}
371+
/>
368372
</div>
369373

370374
{/* External Link */}

src/utils/docs.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { notFound } from '@tanstack/react-router'
88
import { createServerFn } from '@tanstack/react-start'
99
import { z } from 'zod'
1010
import { setResponseHeader } from '@tanstack/react-start/server'
11+
import { renderMarkdown } from '~/utils/markdown'
1112

1213
export const loadDocs = async ({
1314
repo,
@@ -59,12 +60,15 @@ export const fetchDocs = createServerFn({ method: 'GET' })
5960
'max-age=300, stale-while-revalidate=300, durable',
6061
)
6162

63+
const html = renderMarkdown(frontMatter.content)
64+
6265
return {
6366
title: frontMatter.data?.title,
6467
description,
6568
filePath,
6669
content: frontMatter.content,
6770
frontmatter: frontMatter.data,
71+
html,
6872
}
6973
})
7074

src/utils/feed.functions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ import {
1111
buildFeedQueryConditions,
1212
filterByReleaseLevel,
1313
} from './feed.server'
14+
import { renderMarkdown } from '~/utils/markdown'
1415

1516
// Transform database entry to API response format
1617
function transformFeedEntry(entry: typeof feedEntries.$inferSelect) {
18+
const html = renderMarkdown(entry.content ?? '')
19+
1720
return {
1821
_id: entry.entryId,
1922
id: entry.entryId,
@@ -32,6 +35,7 @@ function transformFeedEntry(entry: typeof feedEntries.$inferSelect) {
3235
featured: entry.featured ?? false,
3336
autoSynced: entry.autoSynced,
3437
lastSyncedAt: entry.lastSyncedAt?.getTime(),
38+
html,
3539
}
3640
}
3741

0 commit comments

Comments
 (0)