简体   繁体   English

如何使用与节点一起安装的Prism插件

[英]How to use Prism plugins installed with node

I'm using prism to show my documentation. 我正在使用棱镜来显示我的文档。 So there I went to show some beautiful code. 所以我去展示一些漂亮的代码。 To install prism there is two ways: manual install, or with node. 要安装棱镜,有两种方法:手动安装或使用节点。 I used node to install it. 我用node来安装它。 But to use its plugins there is no doc that explain how: the only available example is: 但是要使用它的插件,没有文档可以解释如何:唯一可用的例子是:

var code = "var data = 1;";
var html = Prism.highlight(code, Prism.languages.javascript);

and then show the html... in my case I want to use the line numbers. 然后显示html ...在我的情况下,我想使用行号。 How to? 如何?

In your html create the following: 在你的html中创建以下内容:

<pre class="line-numbers"><code id="formattedBlock" class="language-javascript"></code></pre>

Do not forget to import related CSS files in the header 不要忘记在标题中导入相关的CSS文件

 <link rel="stylesheet" href="themes/prism.css" />
 <link rel="stylesheet" href="themes/plugins/prism-line-numbers.css" />

Make sure you require the neccessary libraries 确保您需要必要的库

var Prism = require('prismjs/components/prism-core.min');
require('prismjs/components/prism-javascript.min');
require('prismjs/plugins/line-numbers/prism-line-numbers.min');

After what example you wrote is correct: 在你写的例子之后是正确的:

var code = "var data = 1;";
var html = Prism.highlight(code, Prism.languages.javascript);

Just append the html result into formattedBlock element eg with jQuery: 只需将html结果附加到formattedBlock元素中,例如使用jQuery:

$('#formattedBlock').append(html);

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

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