2017-03-21 80 views

回答

10

我用markedGitHub)。

我先导入这样的:

import marked from "marked"; 

我再取阵营的componentWillMount事件中我* .MD文件,并使用marked(text)其存储在我的组件的状态(text是响应):

componentWillMount() { 
    const readmePath = require("./Readme.md"); 

    fetch(readmePath) 
    .then(response => { 
     return response.text() 
    }) 
    .then(text => { 
     this.setState({ 
     markdown: marked(text) 
     }) 
    }) 
} 

...最后我用dangerouslySetInnerHTML属性呈现在页面上:

render() { 
    const { markdown } = this.state; 

    return (
    <section> 
     <article dangerouslySetInnerHTML={{__html: markdown}}></article> 
    </section> 
) 
} 
+0

尝试导入“标记”和Typescript无法找到声明文件 – PizzaHead

+0

我不熟悉TypeScript,但是你需要安装首先打包。 'npm install marked --save'应该可以做到。 –

+0

啊,发现我需要安装一个类型定义来使Typescript快乐。 https://www.npmjs.com/package/@types/marked。谢谢我的朋友 – PizzaHead

0

我建议你使用react-markdown。你可以试试demo。它可以让你写:

var React = require('react'); 
var Markdown = require('react-markdown'); 

React.render(
    <Markdown source="# Your markdown here" />, 
    document.getElementById('content') 
); 
+0

有什么办法导入库而不是需要?就像从“react-markdown”导入MarkDown一样。 TypeScript对我很生气 – PizzaHead

+0

是的,如果你是env处理es6的话,它们都是一样的import –