Neu*_*ode 3 javascript markdown typescript reactjs react-markdown
使用 React-Markdown,我可以充分使用我的自定义构建组件。但这是在降价中使用特定的预先构建的关键字。像段落或图像。效果非常好。但问题是,这些似乎都是预先构建的单词/条件,如段落、标题或图像。
我找不到在我的降价中添加新关键字的方法,例如要使用的“CustomComponent”。这就是我现在所需要的><
这对我来说很好用,可以将降价图像制作成我在其他地方制作的自定义“页脚”组件。我知道这很荒谬,但它确实有效。但我不知道如何让这个渲染器接受/创建一个新的关键字,如“emoji”或“customComponent”或“somethingSilly”。
let body =
``;
const renderers = {
image: () => <Footer/>
};
<ReactMarkdown source={body} renderers={renderers} />;
Run Code Online (Sandbox Code Playgroud)
一些文档: https://reposhub.com/react/miscellaneous/rexxars-react-markdown.html https://github.com/rexxars/commonmark-react-renderer/blob/master/src/commonmark-react-renderer。 js#L50
示例: https: //codesandbox.io/s/react-markdown-with-custom-renderers-961l3 ?from-embed=&file=/src/App.js
但没有任何内容表明我如何使用“CustomComponent”来指示注入自定义组件。
我正在尝试从我的数据库中检索一篇文章,该文章的格式类似于 Markdown 中的格式(基本上是一个巨大的字符串)。我正在使用 typescript 和 redux 进行常规反应——这是我的应用程序中唯一需要它的部分。
"
# Title
## Here is a subtitle
Some text
<CustomComponentIMade/>
Even more text after.
<CustomComponentIMade/>
"
Run Code Online (Sandbox Code Playgroud)
我知道这对于您的目的来说很可能有点晚了,但我已经设法使用自定义备注组件解决了这个问题。
本质上,您需要使用该remark-directive插件以及一个小型自定义备注插件(我直接从remark-directive文档中获得了该插件)
然后在 React Markdown 中,您可以指定插件、自定义渲染器和自定义标签,例如。
import React from 'react'
import ReactMarkdown from 'react-markdown'
import {render} from 'react-dom'
import directive from 'remark-directive'
import { MyCustomComponent } from './MyCustomComponent'
import { visit } from "unist-util-visit"
import { h } from "hastscript/html.js"
// react markdown components list
const components = {
image: () => <Footer/>,
myTag: MyCustomComponent
}
// remark plugin to add a custom tag to the AST
function htmlDirectives() {
return transform
function transform(tree) {
visit(tree, ['textDirective', 'leafDirective', 'containerDirective'], ondirective)
}
function ondirective(node) {
var data = node.data || (node.data = {})
var hast = h(node.name, node.attributes)
data.hName = hast.tagname
data.hProperties = hast.properties
}
}
render(
<ReactMarkdown components={components} remarkPlugins={[directive, htmlDirectives]}>
Some markdown with a :myTag[custom directive]{title="My custom tag"}
</ReactMarkdown>,
document.body
)
Run Code Online (Sandbox Code Playgroud)
因此,在你的降价中,无论你有类似的东西,:myTag[...]{...attributes}都应该将MyCustomComponentwithattributes作为 props 渲染。
抱歉,我还没有测试代码,但希望它传达了事情的要点,如果您需要一个有效的示例,请告诉我,我会尽力设置一个。
| 归档时间: |
|
| 查看次数: |
3074 次 |
| 最近记录: |