是否有任何简单的方法可以将 Markdown 文本从内容 api 转换为 html 代码以显示在 html 页面上。我曾尝试使用 pagedown 和一些类似的技术,但似乎没有一个对我有用。
问问题
9891 次
5 回答
17
于 2015-12-10T10:34:40.430 回答
3
我知道我迟到了,但这是使用车把的解决方案:
var marked = require('marked');
marked.setOptions({
renderer: new marked.Renderer(),
sanitize: true,
smartLists: true,
smartypants: true
});
//Home
router.get('/', (req, res) => {
client.getEntry('<ENTRY_ID>')
.then( (entry)=> {
entry.fields.body = marked(entry.fields.body);
res.render('static/index',
{
entry: entry,
user: req.user
});
}).catch( (err) => {
console.log(err);
})
});
然后在我们的 index.hbs 模板中,我们可以通过使用 {{{}}} 来调用 markdown 变量(entry.fields.body)来防止转义。
{{{entry.fields.body}}}
于 2017-11-20T03:29:00.287 回答
2
以下是我使用 React 的方法:
class NewsDetail extends React.Component {
render() {
const body = marked(this.props.body || "");
return (
<div className="news-detail">
<h2>{this.props.title}</h2>
<div dangerouslySetInnerHTML={ { __html: body } }></div>
</div>
);
}
}
降价内容存储在NewsDetail
标签的 body 属性中(通过一个将内容数据结构映射到我的应用程序结构的简短函数)。
HTML 页面有这个脚本标签来拉入marked
函数:
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.6/marked.min.js"></script>
于 2016-12-20T09:10:57.313 回答
2
我使用过 ReactMarkdown 模块:如果您还有一个 react 应用程序:https ://github.com/rexxars/react-markdown
例子:npm install --save react-markdown
const React = require('react')
const ReactDOM = require('react-dom')
const ReactMarkdown = require('react-markdown')
const input = '# This is a header\n\nAnd this is a paragraph'
ReactDOM.render(
<ReactMarkdown source={input} />,
document.getElementById('container')
)
我通过道具传递降价并在我的子组件中使用这个模块。
于 2018-04-11T12:07:28.603 回答
0
我也在反应应用程序中做了与玛格丽塔相同的事情,但在子组件中,我从内容中删除了我的降价。
我安装了 react-markdown 包
和
npm install react-markdown
import React from "react";
import ReactMarkdown from "react-markdown";
const AboutIntro = (props) => {
return (
<div>
<h2 className="about__intro-title">
{props.aboutTitle}
</h2>
<ReactMarkdown>
{props.aboutCopy}
</ReactMarkdown>
</div>
)
}
export default AboutIntro;
希望这可以帮助
于 2019-06-22T17:36:20.117 回答