javascript nodejs,玉转义标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6926247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:21:56  来源:igfitidea点击:

nodejs, jade escape markup

javascriptnode.jspug

提问by himakumar

I have an Express app using the default Jade view engine. When I try to render HTML as-is in a <pre>element, it gets rendered as actual DOM elements instead of literal characters.

我有一个使用默认 Jade 视图引擎的 Express 应用程序。当我尝试在<pre>元素中按原样呈现 HTML 时,它被呈现为实际的 DOM 元素而不是文字字符。

h1 Code Sample
pre
  code
    <div>some text</div>

Output:

输出:

<h1>Code Sample</h1>
<pre>
  <code>
    <div>some text</div>
  </code>
</pre>

How do I escape the HTML so that it gets rendered as follows?

如何转义 HTML 以使其呈现如下?

<h1>Code Sample</h1>
<pre>
  <code>
    &lt;div&gt;some text&lt;/div&gt;
  </code>
</pre>

回答by Daniel Baulig

Jade uses the bang to force unescaped output. So you turn regular output to unescaped output with the following construct: !=
If your content is inside an div tag you could do the following:

Jade 使用 bang 强制非转义输出。因此,您可以使用以下构造将常规输出转换为未转义的输出:!=
如果您的内容位于 div 标签内,您可以执行以下操作:

div!= content

回答by kumarharsh

As an addition, here is another use case which you need to consider:

另外,这是您需要考虑的另一个用例:

If you are extrapolating the HTML content using the #{...}, it will still give the wrong output. For that use case, you need the !{...}alternative.

如果您使用 推断 HTML 内容#{...},它仍然会给出错误的输出。对于该用例,您需要!{...}替代方案。

So,

所以,

div= varname

becomes

变成

div!= varname


And

div #{varname} is extrapolated badly

becomes

变成

div !{varname} is extrapolated perfectly

回答by KARASZI István

Actually the OP asks for the escaping, not the unescaping. Which I ran into today.

实际上 OP 要求转义,而不是非转义。我今天遇到了。

Let assume, that you have varNamevariable with <b>FooBar</b>content.

假设您有内容varName变量<b>FooBar</b>

Then this template will use the escaped value:

然后这个模板将使用转义值:

#foobar= varName

so it becomes:

所以它变成:

<div id="foobar">&lt;b&gt;FooBar&lt;/b&gt;</div>

If you use the bang operator:

如果您使用 bang 运算符:

#foobar!= varName

jade won't escape it, so it becomes:

玉也逃不掉,就变成了:

<div id="foobar"><b>FooBar</b></div>

回答by ewiggin

This is the official way:

这是官方方法:

code= '<div>This code is' + ' <escaped>!</div>'
<code>&lt;div&gt;This code is &lt;escaped&gt;!&lt;/div&gt;</code>

http://jade-lang.com/reference/#unescapedbufferedcode

http://jade-lang.com/reference/#unescapedbufferedcode

回答by Raine Revere

It's not built in to Jade, but you can do it with a filter:
(This can be added anywhere at the top of app.js.)

它不是内置于 Jade 中的,但您可以使用过滤器来实现:(
这可以添加到 app.js 顶部的任何位置。)

require('jade').filters.escape = function( block ) {
  return block
    .replace( /&/g, '&amp;'  )
    .replace( /</g, '&lt;'   )
    .replace( />/g, '&gt;'   )
    .replace( /"/g, '&quot;' )
    .replace( /#/g, '&#35;'  )
    .replace( /\/g, '\\'  )
    .replace( /\n/g, '\n'   );
}

Then use the 'escape' filter in your jade file:

然后在您的玉文件中使用“转义”过滤器:

h1 Code Sample
pre
  code
    :escape
      <div>some text</div>

Output:

输出:

<h1>Code Sample</h1>
<pre>
  <code>&lt;div&gt;hi&lt;/div&gt;</code>
</pre>

Source: Embedding escaped code in a Jade template

来源:在 Jade 模板中嵌入转义代码