最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React-Markdown Custom Component Declaration, How Can I Declare in the Renderer to Use a Custom Component? - Stack O

programmeradmin5浏览0评论

Problem

Using React-Markdown I can fully use my custom built ponents. But this is with specific pre-built keywords in the markdown. Like paragraph or images. That works PERFECTLY. But the problem is that these seem to all be pre-built words/conditions like paragraphs, headers, or images.

I can't find a way to add something new key word in my markdown like "CustomComponent" to be used. That's all I need at this point ><

This works just fine for me to make the markdown's image into a custom "footer" ponent I made elsewhere. I know it's ridiculous but it works. But I have no idea how to make this renderer accept/create a new keyword like "emoji" or "customComponent" or "somethingSilly".

let body = 
    `![Fullstack React](.png)`;

const renderers = {
    image: () => <Footer/>
};

<ReactMarkdown source={body} renderers={renderers} />;

Some past work I did:

Some documentation: .html .js#L50

Examples: =&file=/src/App.js

But nothing indicates how I can use "CustomComponent" to indicate to inject a custom ponent.

Use Case / Background

I'm trying to retrieve an article from my database that is formatted like so in markdown (basically a giant string). I'm using regular react with typescript and redux-- this is the only portion of my application that needs this.

"
# Title

## Here is a subtitle

Some text

<CustomComponentIMade/>

Even more text after.


<CustomComponentIMade/>

"

Problem

Using React-Markdown I can fully use my custom built ponents. But this is with specific pre-built keywords in the markdown. Like paragraph or images. That works PERFECTLY. But the problem is that these seem to all be pre-built words/conditions like paragraphs, headers, or images.

I can't find a way to add something new key word in my markdown like "CustomComponent" to be used. That's all I need at this point ><

This works just fine for me to make the markdown's image into a custom "footer" ponent I made elsewhere. I know it's ridiculous but it works. But I have no idea how to make this renderer accept/create a new keyword like "emoji" or "customComponent" or "somethingSilly".

let body = 
    `![Fullstack React](https://dzxbosgk90qga.cloudfront/fit-in/504x658/n/20190131015240478_fullstack-react-cover-medium%402x.png)`;

const renderers = {
    image: () => <Footer/>
};

<ReactMarkdown source={body} renderers={renderers} />;

Some past work I did:

Some documentation: https://reposhub./react/miscellaneous/rexxars-react-markdown.html https://github./rexxars/monmark-react-renderer/blob/master/src/monmark-react-renderer.js#L50

Examples: https://codesandbox.io/s/react-markdown-with-custom-renderers-961l3?from-embed=&file=/src/App.js

But nothing indicates how I can use "CustomComponent" to indicate to inject a custom ponent.

Use Case / Background

I'm trying to retrieve an article from my database that is formatted like so in markdown (basically a giant string). I'm using regular react with typescript and redux-- this is the only portion of my application that needs this.

"
# Title

## Here is a subtitle

Some text

<CustomComponentIMade/>

Even more text after.


<CustomComponentIMade/>

"
Share Improve this question asked Mar 23, 2021 at 12:45 NeuralCodeNeuralCode 1951 gold badge3 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

I know its most likely a little late for your purposes, but I've managed to solve this issue using a custom remark ponent.

Essentially you'll need to use the remark-directive plugin as well as a small custom remark plugin (I got this plugin straight from the remark-directive docs)

Then in react markdown you can specify the plugins, custom renderers and custom tags for eg.

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 ponents list
const ponents = {
  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 ponents={ponents} remarkPlugins={[directive, htmlDirectives]}>
    Some markdown with a :myTag[custom directive]{title="My custom tag"}
  </ReactMarkdown>,
  document.body
)

So in your markdown wherever you have something like :myTag[...]{...attributes} you should render the MyCustomComponent with attributes as props.

Sorry I haven't tested the code, but hopefully it municates the gist of things, if you need a working example let me know and I'll do my best to set one up.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论