简体   繁体   中英

Webpack loader for .md file import for “react-markdown” npm library?

When i import a .md file, it gave me error, saying that it cannot read this particular .md file syntax , I know there needs to be some kind of loader for it to parse the import, but when i looked online there was a loader called 'markdown-loader' which was only for marked npm package.

I am using react-markdown package to read md files

/* eslint-disable react/prefer-stateless-function */
import React, { Component } from 'react';
import ReactMarkdown from 'react-markdown';
import AppMarkdown from './posts/sample.md';

// import PropTypes from 'prop-types';

class CardDetails extends Component {
  constructor() {
    super();
    this.state = { markdown: '' };
  }

  componentDidMount() {
    // Get the contents from the Markdown file and put them in the React state, so we can reference it in render() below.
     fetch(AppMarkdown)
      .then(res => res.text())
      .then(text => this.setState({ markdown: text }));
  }

  render() {
    const { markdown } = this.state;
    return <ReactMarkdown source={markdown} />;
  }
}

CardDetails.propTypes = {};
export default CardDetails;

控制台错误

here's my markdown content sample.md

# React & Markdown App

- Benefits of using React... but...
- Write layout in Markdown!

i could not find package, i looked everywhere, do you know about the loader? Thankyou

Try to use raw-loader:

module.exports = {
  module: {
    rules: [
      {
        test: /\.md$/,
        use: 'raw-loader'
      }
    ]
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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