简体   繁体   English

如何在反应中导入 markdown 或 HTML 文件

[英]How to import a markdown or HTML file in react

I'm making a Reactjs app and I have a Readme.md markdown file that I would like to show in my app.我正在制作一个 Reactjs 应用程序,我有一个 Readme.md markdown 文件,我想在我的应用程序中显示。 I'm confused about how to proceed.我对如何进行感到困惑。 I'm describing what I tried below but it looks to me more complicated than it should and makes me feel like I'm approaching this wrong.我正在描述我在下面尝试的内容,但在我看来它比它应该的更复杂,让我觉得我正在接近这个错误。 I'd be interested in knowing how people generally deal with including static markdown or html content in a react app or more generally in javascript.我很想知道人们通常如何处理包括 static markdown 或 html 内容在反应应用程序中或更一般地在 ZDE9B9ED78EEEFEE2E9DC.

I see multiple paths:我看到多条路径:

  1. Translate the markdown file to html and find a way to include the html in the react App.将 markdown 文件翻译成 html 并找到将 html 包含在 react App 中的方法。
  2. Use a javascript markdown library (such as maybe https://remark.js.org/ ) to render the markdown directly使用 javascript markdown 库(比如可能是 https://remark.js.org/ )直接渲染 Z590FC197FE73DB0AZAA2EC036EE

The markdown also contains mathjax. markdown 还包含 mathjax。

I'd have thought route 1 was the best, because the markdown is a static file and i don't need to use an extra javascript library to render it dynamically.我认为路线 1 是最好的,因为 markdown 是一个 static 文件,我不需要使用额外的 javascript 库来动态呈现它。

So I generated the HTML from the Markdown with pandoc, put it in src/assets/readme.html and tried this in my App.js :因此,我使用 pandoc 从 Markdown 生成了 HTML ,将其放入src/assets/readme.html并在我的App.js中进行了尝试:

import React from 'react';
import readme from 'assets/method.html';

const App: React.FC = () => {
  return (
      <div dangerouslySetInnerHTML={ { __front: readme } }/>
  );
};

This doesn't work however, as it produces the error:但是,这不起作用,因为它会产生错误:

./src/assets/method.html 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

I checked the pointed URL but couldn't find any pointer to load HTML files.我检查了指向的 URL 但找不到任何指针来加载 HTML 文件。

This looks more complicated than it should, I'm wondering if I'm missing something.这看起来比它应该的更复杂,我想知道我是否遗漏了什么。

you can check the below code:您可以检查以下代码:

   fetchExternalHTML: function(fileName) {
  Ajax.getJSON('/myAPI/getExternalHTML/' + fileName).then(
    response => {
      this.setState({
        extHTML: response
      });
    }, err => {
      //handle your error here
    }
  );
}

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

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