3

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

0xAnon
  • 847
  • 9
  • 20

1 Answers1

7

Try to use raw-loader:

module.exports = {
  module: {
    rules: [
      {
        test: /\.md$/,
        use: 'raw-loader'
      }
    ]
  }
}
demkovych
  • 7,827
  • 3
  • 19
  • 25
  • oh seems like raw-loader was commented out in my config lemme build the dll and get back to you – 0xAnon Jun 29 '20 at 14:12
  • 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 – 0xAnon Jun 29 '20 at 14:20
  • sorry, I don't know. Try to move your file into the same directory or use `require` instead. – demkovych Jun 29 '20 at 14:25
  • 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 – 0xAnon Jun 29 '20 at 14:30
  • 1
    Try to use another plugin instead of react-markdown – demkovych Jun 29 '20 at 14:31
  • some issue with es6 import , instead of fetch used async/await with commonjs require, it works, thanks for you help haha – 0xAnon Jun 30 '20 at 02:41