gpt4 book ai didi

grails - 在 GSP 中呈现 HTML

转载 作者:行者123 更新时间:2023-12-02 07:30:11 26 4
gpt4 key购买 nike

我在数据库表(博客文章)中有一些内容是受信任的内容,我想在屏幕上显示。此内容是 HTML,并包含一些使用 Prism.js 进行语法高亮显示的代码示例。由于 gsp 页面上的 HTML 编码,我需要使用原始方法按原样输出内容

${raw(post.content)}

这很好用,除了当我得到包装在我的代码示例的标签中的代码时。而不是将其显示为代码,而是输出原始 html,这不是我想要的。我不知何故需要对其中的文本进行编码,因为如果我不这样做,我最终会得到看起来像这样的东西。

enter image description here

我知道我可以在保存时进行编码,但我已经有数百篇不是这种情况的帖子。有什么想法吗?

最佳答案

在我的例子中,我必须抓取 View 中的原始内容

${raw(post.getEscapedContent())}

然后在域对象中我转义了代码块内的任何内容

/**
* I will return the content of a post with the necessary html escaping. To render html in code blocks we
* need to escape any html inside of <code></code>
* @return String
*/
def getEscapedContent(){
content.replaceAll(/(?ms)(<code.*?>)(.*?)(<\/code>)/) { it, open, code, close ->
open + code.encodeAsHTML() + close
}
}

关于grails - 在 GSP 中呈现 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22775794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com