Skip to content

Commit e52efdc

Browse files
authored
Merge pull request #6 from vikbert/master
Add syntax highlighting to docs
2 parents 511db90 + 83f487d commit e52efdc

File tree

4 files changed

+93
-5
lines changed

4 files changed

+93
-5
lines changed

docs/App.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script>
22
import { tick } from 'svelte'
33
import { SvelteToast, toast } from '../src'
4+
import Prism from './Prism.svelte'
45
56
// Hoist to `window` for debug
67
window.toast = toast
@@ -206,11 +207,10 @@ toast.set(id, { progress: 1 })`,
206207
</style>
207208

208209
<div class="container">
209-
210210
<div class="w-full h-64 px-2 mt-4 mb-8">
211-
<pre class="w-full h-full bg-gray-700 text-gray-200 font-mono shadow rounded-sm overflow-scroll p-4"><code>
212-
{code}
213-
</code></pre>
211+
<Prism classes="w-full h-full bg-gray-700 text-gray-200 font-mono shadow rounded-sm overflow-scroll p-4">
212+
{code}
213+
</Prism>
214214
</div>
215215

216216
<div class="flex flex-row flex-wrap items-center justify-center">

docs/Prism.svelte

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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>

docs/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
<title>svelte-toast | DEMO</title>
77
<meta name="description" content="Simple elegant toast notifications. Use it in Vanilla JS or as a Svelte component.">
88
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
9-
<link rel="stylesheet" href="build/global.css">
9+
<link rel="stylesheet" href="build/global.css">
10+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/themes/prism-tomorrow.min.css">
1011
<script defer src="build/bundle.js"></script>
1112
<script>
1213
(function(){

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"eslint-plugin-svelte3": "^2.7.3",
3636
"npm-run-all": "^4.1.5",
3737
"postcss": "^8.2.1",
38+
"prismjs": "^1.23.0",
3839
"rollup": "^2.34.2",
3940
"rollup-plugin-livereload": "^2.0.0",
4041
"rollup-plugin-svelte": "^7.0.0",

0 commit comments

Comments
 (0)