Fix markdown rendering inside components

This commit is contained in:
Marcus Blättermann 2022-11-15 01:43:38 +01:00
parent 74e9a6cbe4
commit 07614e1625
No known key found for this signature in database
GPG Key ID: A1E1F04008AC450D
3 changed files with 32 additions and 10 deletions

View File

@ -13,7 +13,7 @@ import Icon from '../icon'
import Link, { OptionalLink } from '../link' import Link, { OptionalLink } from '../link'
import Infobox from '../infobox' import Infobox from '../infobox'
import Accordion from '../accordion' import Accordion from '../accordion'
import { join, arrayToObj, abbrNum, markdownToReact, isString, isEmptyObj } from '../util' import { join, arrayToObj, abbrNum, MarkdownToReact, isString, isEmptyObj } from '../util'
import siteMetadata from '../../../meta/site.json' import siteMetadata from '../../../meta/site.json'
import languages from '../../../meta/languages.json' import languages from '../../../meta/languages.json'
@ -295,7 +295,7 @@ const Model = ({
python -m spacy download {name} python -m spacy download {name}
</CodeBlock> </CodeBlock>
</Aside> </Aside>
{meta.description && markdownToReact(meta.description, MARKDOWN_COMPONENTS)} {meta.description & <MarkdownToReact markdown={meta.description} />}
{isError && error} {isError && error}
<Table> <Table>
<tbody> <tbody>
@ -317,7 +317,7 @@ const Model = ({
)} )}
</tbody> </tbody>
</Table> </Table>
{meta.notes && markdownToReact(meta.notes, MARKDOWN_COMPONENTS)} {meta.notes & <MarkdownToReact markdown={meta.notes} />}
{hasInteractiveCode && ( {hasInteractiveCode && (
<CodeBlock title="Try out the model" lang="python" executable={true}> <CodeBlock title="Try out the model" lang="python" executable={true}>
{[ {[

View File

@ -16,7 +16,7 @@ import Main from '../main'
import Footer from '../footer' import Footer from '../footer'
import { H3, H5, Label, InlineList } from '../typography' import { H3, H5, Label, InlineList } from '../typography'
import { YouTube, SoundCloud, Iframe } from '../embed' import { YouTube, SoundCloud, Iframe } from '../embed'
import { github, markdownToReact } from '../util' import { github, MarkdownToReact } from '../util'
import { nightly, legacy } from '../../../meta/dynamicMeta' import { nightly, legacy } from '../../../meta/dynamicMeta'
import universe from '../../../meta/universe.json' import universe from '../../../meta/universe.json'
@ -258,7 +258,11 @@ const Project = ({ data, components }) => (
/> />
)} )}
{data.description && <section>{markdownToReact(data.description, components)}</section>} {data.description && (
<section>
<MarkdownToReact markdown={data.description} />
</section>
)}
{data.code_example && ( {data.code_example && (
<CodeBlock title="Example" lang={data.code_language || 'python'}> <CodeBlock title="Example" lang={data.code_language || 'python'}>

View File

@ -1,9 +1,10 @@
import React, { Fragment } from 'react' import React, { Fragment, useEffect, useState } from 'react'
import { Parser as HtmlToReactParser } from 'html-to-react' import { Parser as HtmlToReactParser } from 'html-to-react'
import remark from 'remark'
import remark2react from 'remark-react'
import siteMetadata from '../../meta/site.json' import siteMetadata from '../../meta/site.json'
import { domain } from '../../meta/dynamicMeta' import { domain } from '../../meta/dynamicMeta'
import remarkPlugins from '../../plugins/index.mjs'
import { serialize } from 'next-mdx-remote/serialize'
import { MDXRemote } from 'next-mdx-remote'
const htmlToReactParser = new HtmlToReactParser() const htmlToReactParser = new HtmlToReactParser()
@ -85,8 +86,25 @@ export function htmlToReact(html) {
* for HTML elements. * for HTML elements.
* @returns {Node} - The converted React elements. * @returns {Node} - The converted React elements.
*/ */
export function markdownToReact(markdown, remarkReactComponents = {}) { export function MarkdownToReact({ markdown }) {
return remark().use(remark2react, { remarkReactComponents }).processSync(markdown).contents const [mdx, setMdx] = useState(null)
useEffect(() => {
const getMdx = async () => {
setMdx(
await serialize(markdown, {
parseFrontmatter: false,
mdxOptions: {
remarkPlugins,
},
})
)
}
getMdx()
}, [markdown])
return mdx ? <MDXRemote {...mdx} /> : <></>
} }
/** /**