-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Markdown Component May Output Blank Content (#15)
- Loading branch information
Showing
3 changed files
with
38 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,42 @@ | ||
import {useHljs, useMdConverter, useTeXRenderer} from '@website/hooks'; | ||
import {useEffect} from 'react'; | ||
import {useCallback, useEffect, useState} from 'react'; | ||
|
||
import {MarkdownView} from '../../shared-views/MarkdownView'; | ||
|
||
interface IProps { | ||
children: string; | ||
onRenderStart?: () => unknown; | ||
onRenderFinish?: () => unknown; | ||
} | ||
|
||
export function Markdown(props: IProps) { | ||
const {children, onRenderFinish} = props; | ||
const {loading: converterLoading, html} = useMdConverter(children); | ||
const {loading: hljsLoading, highlightedHtml} = useHljs(html); | ||
const {loading: texRendererLoading, renderedHtml} = useTeXRenderer( | ||
highlightedHtml, | ||
[], | ||
); | ||
const {children, onRenderStart, onRenderFinish} = props; | ||
const [renderedHtml, setRenderedHtml] = useState(''); | ||
|
||
const renderChildrenToHtml = useCallback(async () => { | ||
const [{highlightAll}, {converter}, {TeXRenderer}] = await Promise.all([ | ||
import('@website/hljs/csr'), | ||
import('@website/md-converter'), | ||
import('@website/tex-renderer/csr'), | ||
]); | ||
|
||
const rawHtml = converter.makeHtml(children); | ||
const codeHighlightedHtml = await highlightAll(rawHtml); | ||
const texRenderedHtml = | ||
await TeXRenderer.renderAllTexInHTML(codeHighlightedHtml); | ||
return texRenderedHtml; | ||
}, [children]); | ||
|
||
useEffect(() => { | ||
if (!converterLoading && !hljsLoading && !texRendererLoading) { | ||
if (onRenderFinish) onRenderFinish(); | ||
if (onRenderStart) { | ||
onRenderStart(); | ||
} | ||
}, [converterLoading, hljsLoading, texRendererLoading, onRenderFinish]); | ||
void renderChildrenToHtml().then((textRenderedHtml) => { | ||
setRenderedHtml(textRenderedHtml); | ||
if (onRenderFinish) { | ||
onRenderFinish(); | ||
} | ||
}); | ||
}, [onRenderFinish, onRenderStart, renderChildrenToHtml]); | ||
|
||
return <MarkdownView htmlContent={renderedHtml} />; | ||
} |