6

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;

error on console

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

3
  • you don't need any webpack plugins for that. Seems like your markdown code is wrong. Commented Jun 29, 2020 at 14:04
  • @demkovych i updated my question with error picture Commented Jun 29, 2020 at 14:07
  • 1
    I posted example using webpack loader "raw-loader" here stackoverflow.com/a/65744729/1418981 Commented Jan 15, 2021 at 23:12

1 Answer 1

10

Try to use raw-loader:

module.exports = {
  module: {
    rules: [
      {
        test: /\.md$/,
        use: 'raw-loader'
      }
    ]
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

oh seems like raw-loader was commented out in my config lemme build the dll and get back to you
btw this line fetch(AppMarkdown) .then(res => res.text()) .then(text => this.setState({ markdown: text })); seems to get my index.html content and set that content to markdown do you know why , i think i did pretty much everything the library said
sorry, I don't know. Try to move your file into the same directory or use require instead.
i moved it in the same directory , checked paths... , but the promise value that is being is returned is the index.html text , i dont know what went wrong, i will debug it more , this is annoying lol
Try to use another plugin instead of react-markdown
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.