最近做了一个markdown的个人博客,基于node+express的,前端模版基于ejs,开始是将原始markdown返到页面的标签里,然后用 https://github.com/chjj/marked 解析的,高亮用的highlight官网的js,在html中是这么调用的:
jsmarked.setOptions({ renderer: new marked.Renderer(), gfm: true, tables: true, breaks: true, pedantic: false, sanitize: false, smartLists: false, smartypants: false, highlight: function (code) { return hljs.highlightAuto(code).value; } }); var article = $('#article').val(); $('.article').html(marked(article));
上边的代码时可行的。
现在我想直接在node里将markdown解析成html,是这么写的,
jsvar marked = require('marked'), hljs = require('../public/js/highlight/highlight.pack.js'); marked.setOptions({ renderer: new marked.Renderer(), gfm: true, tables: true, breaks: true, pedantic: false, sanitize: false, smartLists: false, smartypants: false, highlight: function (code) { return hljs.highlightAuto(code).value; } }); ... router.get('/a/:id', function (req, res) { Post.getOne(req.params.id, function (err, post) { if (err) { req.flash('error', err); return res.redirect('/'); } res.render('article', { title: post.title, curIndex: 0, article: marked(post.post), post: post, user: req.session.user, success: req.flash('success').toString(), error: req.flash('error').toString() }); }); });
但是提示 undefined is not a function,望高人指点,谢谢
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
虽然 highlight 官方说 highlight 兼容 nodejs,但始终没找到相关应用的文档……
报错的是指哪个
undefined,marked还是highlight,我最近也写了点类似的东西,marked+highlight是可以用的。