Skip to content

Commit 56be0ea

Browse files
committed
docs(highlight-auto): support subset of languages
1 parent dadffa3 commit 56be0ea

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

README.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,27 @@ The `HighlightAuto` component uses the [highlightAuto API](https://highlightjs.r
164164
<HighlightAuto {code} />
165165
```
166166

167+
### Limiting Language Detection
168+
169+
You can restrict language detection to a specific subset using the `languages` prop. This can improve performance and accuracy by limiting the languages considered during auto-detection.
170+
171+
See the [highlight.js documentation on language subset restriction](https://highlightjs.readthedocs.io/en/latest/api.html#highlightauto-value-languagesubset) for more details.
172+
173+
```svelte
174+
<script>
175+
import { HighlightAuto } from "svelte-highlight";
176+
import github from "svelte-highlight/styles/github";
177+
178+
const code = "const x = 42;";
179+
</script>
180+
181+
<svelte:head>
182+
{@html github}
183+
</svelte:head>
184+
185+
<HighlightAuto {code} languages={["javascript", "typescript"]} />
186+
```
187+
167188
## Line Numbers
168189

169190
Use the `LineNumbers` component to render the highlighted code with line numbers.
@@ -489,13 +510,20 @@ In the example below, the `HighlightAuto` component and injected styles are dyna
489510

490511
#### Props
491512

492-
| Name | Type | Default value |
493-
| :------ | :-------- | :------------- |
494-
| code | `any` | N/A (required) |
495-
| langtag | `boolean` | `false` |
513+
| Name | Type | Default value |
514+
| :-------- | :--------------- | :------------- |
515+
| code | `any` | N/A (required) |
516+
| languages | `LanguageName[]` | `undefined` |
517+
| langtag | `boolean` | `false` |
496518

497519
`$$restProps` are forwarded to the top-level `pre` element.
498520

521+
**Note:** `LanguageName` is a union type of all supported language names, providing autocomplete and type safety. You can import it from `svelte-highlight`:
522+
523+
```ts
524+
import type { LanguageName } from "svelte-highlight";
525+
```
526+
499527
#### Dispatched Events
500528

501529
- **on:highlight**: fired after `code` is highlighted

www/components/globals/Index.svelte

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,30 @@
157157
subtitle="Auto-highlighting will result in a larger bundle size. Specify a language if possible."
158158
/>
159159
</Column>
160+
<Column xlg={6} lg={6} md={12}>
161+
<p class="mb-5">
162+
Optionally, you can restrict language detection to a specific subset using
163+
the <code class="code">languages</code> prop. This can improve performance
164+
and accuracy.
165+
</p>
166+
</Column>
167+
<Column xlg={10} lg={10} md={12}>
168+
<HighlightSvelte
169+
code={`<script>
170+
import { HighlightAuto } from "svelte-highlight";
171+
import atomOneDark from "svelte-highlight/styles/atom-one-dark";
172+
173+
const code = "const x = 42;";
174+
<\/script>
175+
176+
<svelte:head>
177+
{@html atomOneDark}
178+
</svelte:head>
179+
180+
<HighlightAuto {code} languages={["javascript", "typescript"]} />`}
181+
class={THEME_MODULE_NAME}
182+
/>
183+
</Column>
160184
</Row>
161185

162186
<Row class="mb-9">

0 commit comments

Comments
 (0)