Replies: 1 comment 2 replies
-
|
We also use As the result, here is the code of our component, that renders markdown: import React from "react"
import { ADMONITION_TYPES } from "@mdxeditor/editor"
import Markdown, { UrlTransform } from "react-markdown"
import remarkBreaks from "remark-breaks"
import remarkDirective from "remark-directive"
import remarkGfm from "remark-gfm"
import rehypeRaw from "rehype-raw"
import { h } from "hastscript"
import { visit } from "unist-util-visit"
import type { Root as ITree } from "mdast"
interface Props {
markdown: string
}
const processMarkdownText = (markdownText: string) =>
markdownText
// Replacing <iframe/> with a <iframe></iframe> and adding <p></p> after each <iframe></iframe>
.replace(/<iframe(.*?)\/>/g, "<iframe$1></iframe><p></p>")
const admonitionPlugin = () => {
return (tree: ITree) => {
visit(tree, node => {
const { type: nodeType } = node
if (nodeType === "textDirective") {
const data = node.data || (node.data = {})
data.hName = "span"
data.hChildren = [{ type: "text", value: `:${node.name}` }]
} else if (nodeType === "leafDirective" || nodeType === "containerDirective") {
const { name: nodeName } = node
const data = node.data || (node.data = {})
if (nodeType === "leafDirective" && nodeName === "video") {
data.hName = "video"
data.hProperties = h("video", node.attributes || {}).properties
return
}
const tagName = "div"
data.hName = tagName
data.hProperties = {
className: ADMONITION_TYPES.includes(nodeName as (typeof ADMONITION_TYPES)[number])
? `admonition-${nodeName}`
: null,
...h(tagName, node.attributes || {}).properties,
}
}
})
}
}
export const GFMMarkdown: React.FC<Props> = ({ markdown }) => {
const processedText = processMarkdownText(markdown)
const encodeIframeSrc: UrlTransform = (url, key, node) => {
if (node.tagName === "iframe" && key === "src") {
const u = new URL(url, window.location.href)
if (/^https?:$/.exec(u.protocol)) {
return encodeURI(url)
}
}
return url
}
return (
<Markdown
remarkPlugins={[remarkBreaks, remarkDirective, remarkGfm, admonitionPlugin]}
rehypePlugins={[rehypeRaw]}
urlTransform={encodeIframeSrc}
>
{processedText}
</Markdown>
)
}PS: You may not need the same monstrous component though, the minimal set is:
|
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


Uh oh!
There was an error while loading. Please reload this page.
-
I'm using a
<MDXEditor>component in a form to save markdown. Then, I'm using a<Markdown>component fromreact-markdownto render that markdown. I'm hoping to get suggestions on what plugins to use withreact-markdownso that the rendered markdown looks the same as what it does in the<MDXEditor>(i.e. WYSIWYG).I notice that I need to include the
remark-gfmplugin to render the table markdown generated by<MDXEditor>. Are there other plugins that I might need to use to get the same rendering withreact-markdownas with@mdx-editor/editor?Alternatively, are people using something other than
react-markdownto render the markdown generated by the editor?Beta Was this translation helpful? Give feedback.
All reactions