简体   繁体   English

当我使用带有 javascript 的 create-react-app 时如何导入 md 文件?

[英]how to import md file when I use create-react-app with javascript?

I'm trying to run the blog example in material ui getting started page .我正在尝试在material ui 入门页面中运行博客示例。 the problem is that the source code is:问题是源代码是:

in blog.js在 blog.js 中

import post1 from './blog-post.1.md';

.
.
.
return( <Main>{post1}<Main/>);

and in Main.js:在 Main.js 中:

import ReactMarkdown from 'markdown-to-jsx';
export default function Main(props) {
  
  return <ReactMarkdown options={options} {...props} />;
}

if I run the code I get this output: /static/media/blog-post.1.0c315da1f0a7af641a3a.md instead of the data inside the MD file.如果我运行代码,我会得到这个 output: /static/media/blog-post.1.0c315da1f0a7af641a3a.md而不是 MD 文件中的数据。

I want to do the same,how can I import MD files in my create-react-app version?我也想这样做,如何在我的 create-react-app 版本中导入 MD 文件? (without typescript) (没有打字稿)

You'll need to read the markdown object and then load it into the ReactMarkdown component.您需要阅读 markdown object 然后将其加载到ReactMarkdown组件中。 Common methods of doing this use fetch as an intermediary.执行此操作的常用方法使用fetch作为中介。 For example:例如:

import * as React from "react";
import ReactMarkdown from "markdown-to-jsx";
import post from "./post.md";

export default function MarkdownImport() {
  let [readable, setReadable] = React.useState({ md: "" });

  React.useEffect(() => {
    fetch(post)
      .then((res) => res.text())
      .then((md) => {
        setReadable({ md });
      });
  }, []);

  return (
    <div>
      <ReactMarkdown children={readable.md} />
    </div>
  );
}

Working example: https://codesandbox.io/s/reactmarkdown-example-20vmg工作示例: https://codesandbox.io/s/reactmarkdown-example-20vmg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM