简体   繁体   English

在 webcomponent 中使用prismjs 显示带有语法高亮显示的代码片段

[英]Show code snippet with syntax highlighting using prismjs inside webcomponent

I am trying to show a code snippet using Prismjs for the syntax highlighting.我正在尝试使用 Prismjs 显示一个用于语法高亮显示的代码片段。

Some info about the project:关于该项目的一些信息:

i am using lit-element and i have a code snippet saved as a string, which i want to show on the page with some nice syntax highlighting (using prismjs for this).我正在使用 lit-element 并且我有一个保存为字符串的代码片段,我想在页面上显示一些很好的语法突出显示(为此使用prismjs)。

This is what the snippet looks like on the store (just a string):这是代码片段在商店中的样子(只是一个字符串):

商店中的片段值

Then inside a component i can render it as a string without problems, but i can't seem to get the syntax highlighting to work.然后在组件内部,我可以毫无问题地将其呈现为字符串,但我似乎无法使语法突出显示工作。

I am importing the styles and i also import prismjs at the top of the component like this: import Prism from 'prismjs' ;我正在导入样式,并且我还在组件顶部导入了 Prismjs,如下所示: import Prism from 'prismjs' ;

To test, i am rendering the snippet in two different ways: the first one as it is saved on the store, and the second one by using the prism.highlight() function.为了测试,我以两种不同的方式呈现代码片段:第一种是保存在商店中,第二种是使用prism.highlight()函数。

This is how they look like in the frontend:这是它们在前端的样子:

它在前端呈现的两种方式

and this is how i am rendering them in my component:这就是我在组件中渲染它们的方式:

<pre>
   <code class="stage__code language-css">${this._selectedSnippet.code}</code>
   <code class="stage__code language-css">${this._highlightedCode}</code>
</pre>

The this._selectedSnippet.code gets the value as it is saved in the store. this._selectedSnippet.code获取保存在商店中的值。

The this._highlightedCode get the value from this getter: this._highlightedCode从这个 getter 中获取值:

get _highlightedCode() {
   return Prism.highlight(this._selectedSnippet.code, Prism.languages.css, 'css')
}

On the first one i see the correct snippet on the code block, but prism isn't highlighting it.在第一个我看到代码块上的正确片段,但棱镜没有突出显示它。 On the second one i see that Prism added the classes it needs to highlight the code, but it is then rendered as a string (with the new markup added by prism).在第二个中,我看到 Prism 添加了突出显示代码所需的类,但随后将其呈现为字符串(带有由 Prism 添加的新标记)。

Any ideas on how i can fix this?关于如何解决这个问题的任何想法? Thanks!谢谢!

Just moments after writing this question i remembered something, which allowed me to fix this problem.写完这个问题后不久,我想起了一些事情,这让我能够解决这个问题。

I am writing it here, for the case someone else has the same problem.我写在这里,以防其他人有同样的问题。

lit-html has an unsafeHTML directive ( here the documentation ) which allows us to render as HTML instead of a string. lit-html 有一个unsafeHTML指令( 这里是文档),它允许我们呈现为 HTML 而不是字符串。 Of course this should be used carefully , as it can lead to safety problems.当然,这应该谨慎使用,因为它会导致安全问题。

So i ended up importing this directive like this:所以我最终像这样导入了这个指令:

import { unsafeHTML } from 'lit-html/directives/unsafe-html';

and then rendering the code block like this:然后像这样渲染代码块:

<code class="stage__code language-css" id="codeContainer">
   ${unsafeHTML(this._highlightedCode)}
</code>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM