highlight.js 高亮代码不换行..

补充下:
我是用Flask-PageDown来做的表单,实现markdown

class ArticleForm(Form):
    title = StringField(u'标题:', [InputRequired(message=u'标题不能为空'),validators.Length(min=4, max=32, message=u'标题不能小于4位,切不大于32位')])
    content = PageDownField(u"内容:", [InputRequired(message=u'内容不能为空')])
    alias = StringField(u'别名:', [InputRequired(message=u'别名不能为空'),validators.Length(max=255, message=u'不大于255位')])
    submit = SubmitField(u'发布文章')

然后用flask的makrdown 把markdown代码转为html

        html_text = markdown(text, output_format = 'html')
        #允许的标签
        allow_tags = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code', 'em','i','li', 'ol',     'pre', 'strong', 'ul', 'h1', 'h2', 'h3']
        #允许的属性
        #这样设置将不会过滤所有标签的class属性,和a标签的href,rel属性....
        attrs = {'*': ['class'],'a': ['href', 'rel'],'img': ['src', 'alt']}

        cleaned_text = bleach.clean(html_text,
            tags=allow_tags,
            strip=True,
            attributes=attrs,
        )

然后把cleaned_text插入数据库,然后通过

{{ data.content |markdown }}

把HTML代码渲染到模版

我用highlight.js,代码是可以高亮的,

但是不换行,

我看下官网说是这样说的的:

You can use any tags instead of <pre><code> to mark up your code. If you don't use a container that preserve line breaks you will need to configure highlight.js to use the <br> tag:


hljs.configure({useBR: true});

$('div.code').each(function(i, block) {
  hljs.highlightBlock(block);
});

我用了一下,但是提示 undefined 好像是说useBR undefined

求大神帮助...

阅读 11.2k
2 个回答

去掉hljs.configure({useBR: true});

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进