|
| 1 | +# svelte-preprocess-highlight |
| 2 | + |
| 3 | +> Svelte preprocessor that syntax highlights code using [highlight.js](https://github.com/highlightjs/highlight.js) |
| 4 | +
|
| 5 | +This preprocessor uses `highlight.js` to syntax highlight and [Prettier](https://github.com/prettier/prettier) to format the text. This approach can greatly decrease the amount of client-side JavaScript because the transformations are done at compile time. |
| 6 | + |
| 7 | +Bundle sizes: |
| 8 | + |
| 9 | +```diff |
| 10 | +highlight.js@11.5.1 |
| 11 | +- 896 kB (minified) |
| 12 | + |
| 13 | +prettier@2.7.1 |
| 14 | +- 423.2 kB (minified) |
| 15 | +``` |
| 16 | + |
| 17 | +**Original** |
| 18 | + |
| 19 | +```svelte |
| 20 | +<pre data-language="typescript"> |
| 21 | +{` |
| 22 | + const sum = (a: number, b: number) => a + b; |
| 23 | +`} |
| 24 | +</pre> |
| 25 | +``` |
| 26 | + |
| 27 | +**Processed** |
| 28 | + |
| 29 | +<!-- prettier-ignore-start --> |
| 30 | +```svelte |
| 31 | +<pre><code class="hljs"><span class="hljs-keyword">const</span> <span class="hljs-title function_">sum</span> = (<span class="hljs-params">a: <span class="hljs-built_in">number</span>, b: <span class="hljs-built_in">number</span></span>) => a + b; |
| 32 | +</code></pre> |
| 33 | +``` |
| 34 | +<!-- prettier-ignore-end --> |
| 35 | + |
| 36 | +## Limitations |
| 37 | + |
| 38 | +The preprocessor only works for static text; the result must be deterministic. For dynamic use cases, you must include `highlight.js` and Prettier on the client-side. |
| 39 | + |
| 40 | +For example, the following will not work because the code must be re-highlighted when it changes. |
| 41 | + |
| 42 | +```svelte |
| 43 | +<pre> |
| 44 | + {toggleFunctionCode |
| 45 | + ? "const sum = (a: number, b: number) => a + b;" |
| 46 | + : "const difference = (a: number, b: number) => a - b;"} |
| 47 | +</pre> |
| 48 | +``` |
| 49 | + |
| 50 | +## Installation |
| 51 | + |
| 52 | +```bash |
| 53 | +# Yarn |
| 54 | +yarn add -D svelte-preprocess-highlight |
| 55 | + |
| 56 | +# NPM |
| 57 | +npm i -D svelte-preprocess-highlight |
| 58 | + |
| 59 | +# pnpm |
| 60 | +pnpm i -D svelte-preprocess-highlight |
| 61 | +``` |
| 62 | + |
| 63 | +## Set-up |
| 64 | + |
| 65 | +Add `highlight` to the list of Svelte preprocessors. |
| 66 | + |
| 67 | +### SvelteKit |
| 68 | + |
| 69 | +```js |
| 70 | +// svelte.config.js |
| 71 | +import { highlight } from "svelte-preprocess-highlight"; |
| 72 | + |
| 73 | +const config = { |
| 74 | + preprocess: [highlight()], |
| 75 | +}; |
| 76 | + |
| 77 | +export default config; |
| 78 | +``` |
| 79 | + |
| 80 | +### Vite |
| 81 | + |
| 82 | +```js |
| 83 | +// vite.config.js |
| 84 | +import { defineConfig } from "vite"; |
| 85 | +import { svelte } from "@sveltejs/vite-plugin-svelte"; |
| 86 | +import { highlight } from "svelte-preprocess-highlight"; |
| 87 | + |
| 88 | +export default defineConfig({ |
| 89 | + plugins: [ |
| 90 | + svelte({ |
| 91 | + preprocess: [highlight()], |
| 92 | + }), |
| 93 | + ], |
| 94 | +}); |
| 95 | +``` |
| 96 | + |
| 97 | +## Usage |
| 98 | + |
| 99 | +Use a `pre` element with a `data-language` attribute to denote what to highlight. The code to highlight should be placed inside of the `pre` element. |
| 100 | + |
| 101 | +**Single Line** |
| 102 | + |
| 103 | +```svelte |
| 104 | +<pre data-language="typescript">{"const sum = (a: number, b: number) => a + b;"}</pre> |
| 105 | +``` |
| 106 | + |
| 107 | +**Multi-line** |
| 108 | + |
| 109 | +```svelte |
| 110 | +<pre data-language="typescript"> |
| 111 | +{` |
| 112 | + const sum = (a: number, b: number) => a + b; |
| 113 | +
|
| 114 | + const difference = (a: number, b: number) => a - b; |
| 115 | +`} |
| 116 | +</pre> |
| 117 | +``` |
| 118 | + |
| 119 | +## Options |
| 120 | + |
| 121 | +### `ignorePath` |
| 122 | + |
| 123 | +By default, the preprocessor will ignore files in `node_modules` and auto-generated files by SvelteKit (located in `.svelte-kit`). |
| 124 | + |
| 125 | +Use the `ignorePath` option to customize files to ignore. |
| 126 | + |
| 127 | +```js |
| 128 | +highlight({ |
| 129 | + ignorePath: (filename) => { |
| 130 | + // Ignore file names that do not end with `.svelte` |
| 131 | + if (!/\.(svelte)$/.test(filename)) return true; |
| 132 | + |
| 133 | + // Ignore file names that do not contain "demo" |
| 134 | + return !/demo/.test(filename); |
| 135 | + }, |
| 136 | +}); |
| 137 | +``` |
| 138 | + |
| 139 | +### `prettierOptions` |
| 140 | + |
| 141 | +The text is formatted by Prettier before being highlighted. |
| 142 | + |
| 143 | +Pass custom [Prettier options](https://prettier.io/docs/en/options.html) to `prettierOptions`. |
| 144 | + |
| 145 | +```js |
| 146 | +highlight({ |
| 147 | + prettierOptions: { |
| 148 | + printWidth: 100, |
| 149 | + svelteStrictMode: true, |
| 150 | + }, |
| 151 | +}); |
| 152 | +``` |
| 153 | + |
| 154 | +## Changelog |
| 155 | + |
| 156 | +[CHANGELOG.md](CHANGELOG.md) |
| 157 | + |
| 158 | +## License |
| 159 | + |
| 160 | +[MIT](LICENSE) |
0 commit comments