Skip to content

Commit

Permalink
fix: toc not appear when the entry first rendered
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Aug 30, 2024
1 parent 0767b6b commit 5dc4fb5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/renderer/src/components/ui/markdown/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parseHtml } from "@renderer/lib/parse-html"
import type { RemarkOptions } from "@renderer/lib/parse-markdown"
import { parseMarkdown } from "@renderer/lib/parse-markdown"
import { cn } from "@renderer/lib/utils"
import { createElement, useMemo, useRef, useState } from "react"
import { createElement, Fragment, useMemo, useState } from "react"

import { MarkdownRenderContainerRefContext } from "./context"

Expand All @@ -17,16 +17,16 @@ export const Markdown: Component<
() => parseMarkdown(children, { ...stableRemarkOptions }).content,
[children, stableRemarkOptions],
)
const [refElement, setRefElement] = useState<HTMLElement | null>(null)

const ref = useRef<HTMLElement>(null)
return (
<MarkdownRenderContainerRefContext.Provider value={ref.current}>
<MarkdownRenderContainerRefContext.Provider value={refElement}>
<article
className={cn(
"prose relative cursor-auto select-text dark:prose-invert prose-th:text-left",
className,
)}
ref={ref}
ref={setRefElement}
>
{markdownElement}
</article>
Expand All @@ -48,6 +48,8 @@ export const HTML = <A extends keyof JSX.IntrinsicElements = "div">(
const { children, renderInlineStyle, as = "div", accessory, ...rest } = props
const stableRemarkOptions = useState({ renderInlineStyle })[0]

const [refElement, setRefElement] = useState<HTMLElement | null>(null)

const markdownElement = useMemo(
() =>
children &&
Expand All @@ -56,12 +58,12 @@ export const HTML = <A extends keyof JSX.IntrinsicElements = "div">(
}).toContent(),
[children, stableRemarkOptions],
)
const ref = useRef<HTMLElement>(null)

if (!markdownElement) return null
return (
<MarkdownRenderContainerRefContext.Provider value={ref.current}>
{createElement(as, { ...rest, ref }, markdownElement)}
{accessory && <span key={children}>{accessory}</span>}
<MarkdownRenderContainerRefContext.Provider value={refElement}>
{createElement(as, { ...rest, ref: setRefElement }, markdownElement)}
{accessory && <Fragment key={children}>{accessory}</Fragment>}
</MarkdownRenderContainerRefContext.Provider>
)
}
1 change: 1 addition & 0 deletions src/renderer/src/components/ui/markdown/components/Toc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const Toc: Component = ({ className }) => {
[]) as HTMLHeadingElement[],
[markdownElement],
)

const toc: ITocItem[] = useMemo(
() =>
Array.from($headings).map((el, idx) => {
Expand Down

0 comments on commit 5dc4fb5

Please sign in to comment.