在LIT WebComponent中使用Prism.js

发布于 2025-02-05 14:06:21 字数 706 浏览 3 评论 0原文

我将点亮的WebComponents与Vite +打字订阅模板一起使用。现在,我正在尝试创建自己的代码块Web-Component。对于语法突出显示,我想使用 prism js ,所以我去了他们的页面,选择了我最喜欢的语言,,选择了我最喜欢的语言,下载了CSS和JS文件,并尝试将它们添加到我的WebComponent中。

我只是将CSS文件复制到css``中,这应该很好。但是我对如何添加JS文件感到迷失。

使用的.js file:

  1. 这是我在tsconfig.json中
  2. set “ allowjs”:trueimport *作为从'../ prism.js''; in myfile.ts中的prism import *。

但是,即使我只是尝试运行prism.highlightall()我得到。

Uncaught TypeError: prism.highlightAll is not a function
    at myFile.ts:116:19

我仍然是JS/TS生态系统的新手,因此对任何帮助都非常感谢。

I use lit webcomponents with the vite + typescript template. Now i am trying to create my own code block web-component. For syntax highlighting I wanted to use prism js, so i went to their page, selected my favorite languages, downloaded both css and js file and tried adding them to my webcomponent.

I just copy-pasted the css file into an css``, which should work just fine. But i am rather lost on how to add the js file.

Here is what i did to use the .js file:

  1. Set "allowJs": true in the tsconfig.json.
  2. import * as prism from '../prism.js'; in myFile.ts, where i have my component.

But even if i just try to run prism.highlightAll() I get.

Uncaught TypeError: prism.highlightAll is not a function
    at myFile.ts:116:19

I am still rather new to the js/ts ecosystem so any help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尛丟丟 2025-02-12 14:06:21

如果有人找到了这个,我有点设法这样做:

  1. npm install prismjs -s(v1.28.0)
  2. npm install @types/prismjs -d(v1.26.0)
  3. 在我的.ts文件中使用WebComponent进行:
import * as Prism from 'prismjs';
import 'prismjs/components/prism-typescript'; // Language
import 'prismjs/components/prism-fsharp'; // Language
  1. 按预期使用它:prism.highlight(code,prism.languages [语言],语言)
  2. i复制& amp; amp; amp; amp; amp; amp; amp; amp; amp; CSS`` 块。

我从在这里

In case anyone finds this, i somewhat managed to do it:

  1. npm install prismjs -s (v1.28.0)
  2. npm install @types/prismjs -d (v1.26.0)
  3. In my .ts file with the webcomponent do:
import * as Prism from 'prismjs';
import 'prismjs/components/prism-typescript'; // Language
import 'prismjs/components/prism-fsharp'; // Language
  1. Use it as expected: Prism.highlight(code, Prism.languages[language], language)
  2. I copy&pasted the css into an lit webcomponent css`` block.

I got the idea from here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文