|
| 1 | +<script> |
| 2 | + /* eslint-disable no-undef */ |
| 3 | + // Svelte Imports |
| 4 | + import { afterUpdate, tick, onMount } from 'svelte' |
| 5 | +
|
| 6 | + // Prism Imports |
| 7 | + import 'prismjs' |
| 8 | + import 'prismjs/plugins/normalize-whitespace/prism-normalize-whitespace.js' |
| 9 | +
|
| 10 | + // The code being used |
| 11 | + export let code = '' |
| 12 | +
|
| 13 | + // link https://prismjs.com/#supported-languages |
| 14 | + // import from 'prismjs/components/prism-{lanugage-name}.js' |
| 15 | + // The language being rendered |
| 16 | + export let language = 'javascript' |
| 17 | +
|
| 18 | + // link https://prismjs.com/plugins/line-numbers/ |
| 19 | + // Turns on and off line numbers |
| 20 | + export let showLineNumbers = false |
| 21 | +
|
| 22 | + // Link https://prismjs.com/plugins/normalize-whitespace/ |
| 23 | + // Turns on and off cleanup plugin |
| 24 | + export let normalizeWhiteSpace = true |
| 25 | +
|
| 26 | + // The defualt config for cleanup white space |
| 27 | + export let normalizeWhiteSpaceConfig = { |
| 28 | + 'remove-trailing': true, |
| 29 | + 'remove-indent': true, |
| 30 | + 'left-trim': true, |
| 31 | + 'right-trim': true |
| 32 | + } |
| 33 | +
|
| 34 | + // CSS Classes specified by the user of the component |
| 35 | + export let classes = '' |
| 36 | +
|
| 37 | + // This is the fake coding element |
| 38 | + let fakeCodeEl |
| 39 | +
|
| 40 | + // This is pre Element |
| 41 | + let preEl |
| 42 | +
|
| 43 | + // This stored the formatted HTML to display |
| 44 | + let formattedCode = '' |
| 45 | +
|
| 46 | + // creates the prism classes |
| 47 | + $: prismClasses = `language-${language} ${ |
| 48 | + showLineNumbers ? 'line-numbers' : '' |
| 49 | + } ${normalizeWhiteSpace === true ? '' : 'no-whitespace-normalization'}` |
| 50 | +
|
| 51 | + onMount(() => { |
| 52 | + if (normalizeWhiteSpace) { |
| 53 | + /* eslint no-undef: 'warn' */ |
| 54 | + Prism.plugins.NormalizeWhitespace.setDefaults(normalizeWhiteSpaceConfig) |
| 55 | + } |
| 56 | + }) |
| 57 | +
|
| 58 | + afterUpdate(async (node) => { |
| 59 | + // code variable if they are using a prop |
| 60 | + // Have to use innerText because innerHTML will create weird escape characaters |
| 61 | + if (fakeCodeEl && fakeCodeEl.innerText !== '') { |
| 62 | + code = fakeCodeEl.innerText |
| 63 | + } |
| 64 | + // We need to wait till everything been rendered before we can |
| 65 | + // call highlightAll and load all the plugins |
| 66 | + await tick() |
| 67 | + // This will make sure all the plugins are loaded |
| 68 | + // Prism.highlight will not do that |
| 69 | + Prism.highlightAllUnder(preEl) |
| 70 | + }) |
| 71 | +
|
| 72 | + // Only run if Prism is defined and we code |
| 73 | + $: if (typeof Prism !== 'undefined' && code) { |
| 74 | + formattedCode = Prism.highlight(code, Prism.languages[language], language) |
| 75 | + } |
| 76 | +</script> |
| 77 | + |
| 78 | +<code style="display: none" bind:this={fakeCodeEl}> |
| 79 | + <slot /> |
| 80 | +</code> |
| 81 | +<pre class="{prismClasses} {classes}" bind:this={preEl} {...$$restProps}> |
| 82 | + <code |
| 83 | + class="language-{language}"> |
| 84 | + {@html formattedCode} |
| 85 | + </code> |
| 86 | +</pre> |
0 commit comments