This package is a wrapper for PrismJS. It works with line numbers and whitespace clean up out of the box. You can enable other plugins and languages as well. It was inspired by svelte-prism, another Svelte Prism package.
The repo has the Svelte code to run the demo.
Run npm install --save-dev svelte-prismjs
Load the css via CDN (if loading via CDN skip ahead to Option B) or...
npm install rollup-plugin-css-only
main.js
file.import "prismjs/plugins/line-numbers/prism-line-numbers.css";
import "prismjs/plugins/command-line/prism-command-line.css";
import "prismjs/plugins/line-highlight/prism-line-highlight.css";
import "prismjs/themes/prism.css";
import "prismjs/themes/prism-coy.css";
Be sure to double check link integrity at PRISM CDN
<!-- base theme -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/themes/prism.min.css"
integrity="sha512-tN7Ec6zAFaVSG3TpNAKtk4DOHNpSwKHxxrsiw4GHKESGPs5njn/0sMCUMl2svV4wo4BK/rCP7juYz+zx+l6oeQ=="
crossorigin="anonymous"
/>
<!-- coy theme -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/themes/prism-coy.min.css"
integrity="sha512-CKzEMG9cS0+lcH4wtn/UnxnmxkaTFrviChikDEk1MAWICCSN59sDWIF0Q5oDgdG9lxVrvbENSV1FtjLiBnMx7Q=="
crossorigin="anonymous"
/>
<!-- Number lines -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/plugins/line-numbers/prism-line-numbers.min.css"
integrity="sha512-cbQXwDFK7lj2Fqfkuxbo5iD1dSbLlJGXGpfTDqbggqjHJeyzx88I3rfwjS38WJag/ihH7lzuGlGHpDBymLirZQ=="
crossorigin="anonymous"
/>
Run npm install svelte-prismjs
You can either include the cdn links in your template.html
file or you can install the rollup-css-only-plugin
. It is basically what step 2 is for Svelte minus different directories. For Sapper be sure you put the file in the static folder.
Import Svelte-Prism
(Because Prism uses the window object we have to do some weird stuff to get it work. For now here is the work around.).
let Prism;
onMount(async () => {
// Load the prismjs first after the page is loaded
const prismModule = await import("svelte-prismjs");
await import("prismjs/components/prism-c.js");
await import("prism-svelte");
await import("prismjs/plugins/line-highlight/prism-line-highlight.js");
await import("prismjs/plugins/file-highlight/prism-file-highlight.js");
// Once everything is loaded load the prismjs module
Prism = prismModule.default;
});
<svelte:component this={Prism}>
{`let b = 3;
function helloWorld() {
console.log("Hello World");
}
`}
</svelte:component>
Run npm install svelte-prismjs
Go to scripts/base.config.js
and add the rollup-css-only-plugin
or go to the static/\_\_index.html
file. If you do use npm to include css you will have to add it to App.svelte
file under global styles.
let Prism;
onMount(async () => {
// Load the prismjs first after the page is loaded
const prismModule = await import("svelte-prismjs");
await import("prismjs/components/prism-c.js");
await import("prism-svelte");
await import("prismjs/plugins/line-highlight/prism-line-highlight.js");
await import("prismjs/plugins/file-highlight/prism-file-highlight.js");
// Once everything is loaded load the prismjs module
Prism = prismModule.default;
});
<svelte:component this={Prism}>
{`let b = 3;
function helloWorld() {
console.log("Hello World");
}
`}
</svelte:component>
<Prism>
{ `let b = 3; function helloWorld() { console.log("Hello World"); } `}
</Prism>
<Prism
showLineNumbers="{true}"
language="c"
code="{`int b= 3;
int c=32;
`}
/>
import "prismjs/components/prism-c.js";