11import * as React from 'react'
22import { MarkdownLink } from '~/components/MarkdownLink'
33import type { HTMLProps } from 'react'
4- import { createHighlighter , type HighlighterGeneric } from 'shiki/bundle/web'
5- import { transformerNotationDiff } from '@shikijs/transformers'
64import parse , {
75 attributesToProps ,
86 domToReact ,
@@ -13,10 +11,13 @@ import type { Mermaid } from 'mermaid'
1311import { useToast } from '~/components/ToastProvider'
1412import { twMerge } from 'tailwind-merge'
1513import { useMarkdownHeadings } from '~/components/MarkdownHeadingContext'
16- import { renderMarkdown } from '~/utils/markdown'
1714import { getNetlifyImageUrl } from '~/utils/netlifyImage'
1815import { Tabs } from '~/components/Tabs'
1916import { Copy } from 'lucide-react'
17+ import type {
18+ MarkdownHeading ,
19+ MarkdownRenderResult ,
20+ } from '~/utils/markdown/processor'
2021
2122type HeadingLevel = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'
2223
@@ -384,29 +385,35 @@ const options: HTMLReactParserOptions = {
384385
385386type 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}
0 commit comments