I have tried to include static markdown files, but all this module appears to do is present the file reference rather than the content of the file. For instance,
import React from 'react';
import ReactDOM from 'react-dom';
import ReactMarkdown from 'react-markdown'
const content = require('./content/thinstyle.md')
ReactDOM.render(
<ReactMarkdown source={content} />,
document.getElementById('root')
);
Is there some other method in this situation to stream the contents of the file. I'm not aware of any other include methods in Javascript.
Ok. I see what I'm doing wrong. The markdown doesn't get compiled into the production bundle the way I thought. I thought there was a way to compile the markdown into the javascript bundle. Is that possible? If not, I guess this has to be an ajax call.
You can use raw-loader to convert a markdown file to a string. Install it and enable it in your webpack config, something like the following:
module.exports = {
module: {
rules: [
{
test: /\.md$/,
use: 'raw-loader'
}
]
}
}
Hey, thanks! I know that wasn't exactly an issue with react-markdown, so thanks for the hint!
Happy to help 鈽猴笍
This probs trouble me a lot, may I know what's your solution eventually? Did you use any other package?
@zhanglinge
I use the 'file-loader' instead.
const content = require('../assets/doc/agreement.md')
....
componentDidMount() {
fetch(content).then(response => {
return response.text()
}).then(text => {
console.log(text)
this.setState({
agreementContent: text
})
}).catch(err => {
console.log(err)
})
}
you can also get for help from here:https://stackoverflow.com/questions/42928530/how-do-i-load-a-markdown-file-into-a-react-component
@wellenzhong I see, many thanks.
Is it possible to fetch markdown files from github repo's into my react website?
Is it possible to fetch markdown files from github repo's into my react website?
I successfully got this running in my functional component, hope it helps!
const [mdText, setMdText] = useState('');
useEffect(() => {
fetch("https://raw.githubusercontent.com/Beamanator/fdi/master/README.md")
.then((response) => {
if (response.ok) return response.text();
else return Promise.reject("Didn't fetch text correctly");
})
.then((text) => {
setMdText(text);
})
.catch((error) => console.error(error));
});
My goodness @Beamanator you have no idea how much this helps!
Most helpful comment
You can use raw-loader to convert a markdown file to a string. Install it and enable it in your webpack config, something like the following: