The Highlight component has the option to add line numbers. See the following examples.
Add numberLine props to add line numbers.
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
<script lang="ts">
import { Highlight } from 'svelte-rune-highlight';
import typescript from 'highlight.js/lib/languages/typescript';
const examples = import.meta.glob('./*.md', {
query: '?raw',
import: 'default',
eager: true
});
const tsLang = {
name: 'typescript',
register: typescript
};
</script>
<Highlight language={tsLang} code={examples['./sample-1.md'] as string} numberLine />
Add hideBorder props to hide the border.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<script lang="ts">
import { Highlight } from 'svelte-rune-highlight';
import typescript from 'highlight.js/lib/languages/typescript';
const examples = import.meta.glob('./*.md', {
query: '?raw',
import: 'default',
eager: true
});
const tsLang = {
name: 'typescript',
register: typescript
};
</script>
<Highlight language={tsLang} code={examples['./sample-1.md'] as string} hideBorder numberLine />
Add wrapLines props to wrap lines.
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
<script lang="ts">
import { Highlight } from 'svelte-rune-highlight';
import typescript from 'highlight.js/lib/languages/typescript';
const examples = import.meta.glob('./*.md', {
query: '?raw',
import: 'default',
eager: true
});
const tsLang = {
name: 'typescript',
register: typescript
};
</script>
<Highlight language={tsLang} code={examples['./sample-1.md'] as string} wrapLines numberLine />
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
<script lang="ts">
import { Highlight } from 'svelte-rune-highlight';
import typescript from 'highlight.js/lib/languages/typescript';
const examples = import.meta.glob('./*.md', {
query: '?raw',
import: 'default',
eager: true
});
const tsLang = {
name: 'typescript',
register: typescript
};
</script>
<Highlight language={tsLang} code={examples['./sample-1.md'] as string} numberLine backgroundColor="red" />
Use one of 'static' | 'relative' | 'abolute' | 'sticky' | undefined. The default value is 'sticky'.
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
<script lang="ts">
import { Highlight } from 'svelte-rune-highlight';
import typescript from 'highlight.js/lib/languages/typescript';
const examples = import.meta.glob('./*.md', {
query: '?raw',
import: 'default',
eager: true
});
const tsLang = {
name: 'typescript',
register: typescript
};
</script>
<Highlight language={tsLang} code={examples['./sample-1.md'] as string} numberLine backgroundColor="#326bfc" position="relative" />
Use the startingLineNumber props to set the start line number.
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
<script lang="ts">
import { Highlight } from 'svelte-rune-highlight';
import typescript from 'highlight.js/lib/languages/typescript';
const examples = import.meta.glob('./*.md', {
query: '?raw',
import: 'default',
eager: true
});
const tsLang = {
name: 'typescript',
register: typescript
};
</script>
<Highlight language={tsLang} code={examples['./sample-1.md'] as string} numberLine startingLineNumber={42} />
Use the highlightedLines and highlightedBackground props to highlight lines.
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
<script lang="ts">
import { Highlight } from 'svelte-rune-highlight';
import typescript from 'highlight.js/lib/languages/typescript';
const examples = import.meta.glob('./*.md', {
query: '?raw',
import: 'default',
eager: true
});
const tsLang = {
name: 'typescript',
register: typescript
};
</script>
<Highlight language={tsLang} code={examples['./sample-1.md'] as string} numberLine highlightedLines={[2, 4]} --highlighted-background="#be46d4" />
Use the --line-number-color, --border-color --padding-left, --padding-right, and --highlighted-background props to customize the styles.
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
<script lang="ts">
import { Highlight } from 'svelte-rune-highlight';
import typescript from 'highlight.js/lib/languages/typescript';
const examples = import.meta.glob('./*.md', {
query: '?raw',
import: 'default',
eager: true
});
const tsLang = {
name: 'typescript',
register: typescript
};
</script>
<Highlight
language={tsLang}
code={examples['./sample-1.md'] as string}
numberLine
--highlighted-background="#be46d4"
--border-color="#2bff0f"
--line-number-color="red"
--padding-left="2em"
--padding-right="2em"
/>
export type UserRole = 'admin' | 'editor' | 'viewer';
export interface User {
id: number;
name: string;
email: string;
role: UserRole;
isActive: boolean;
createdAt: Date;
}
export interface Post {
id: string;
title: string;
content: string;
authorId: number;
tags?: string[];
}
export type ApiResponse<T> = {
data: T;
success: boolean;
error?: string;
};
export type Nullable<T> = T | null;
<script lang="ts">
import { HighlightAuto } from '$lib';
const code = `export type UserRole = 'admin' | 'editor' | 'viewer';
export interface User {
id: number;
name: string;
email: string;
role: UserRole;
isActive: boolean;
createdAt: Date;
}
export interface Post {
id: string;
title: string;
content: string;
authorId: number;
tags?: string[];
}
export type ApiResponse<T> = {
data: T;
success: boolean;
error?: string;
};
export type Nullable<T> = T | null;
`;
</script>
<HighlightAuto {code} />
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
<script lang="ts">
import { HighlightSvelte } from 'svelte-rune-highlight';
const examples = import.meta.glob('../../examples/*.svelte', {
query: '?raw',
import: 'default',
eager: true
});
</script>
<HighlightSvelte code={examples['../../examples/Numberline.svelte'] as string} numberLine />
Use highlightedLines and/or highlightedRanges props to highlight lines as the following example.
[start, end] ranges are inclusive and 1‑based.
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
<script lang="ts">
import { HighlightSvelte } from 'svelte-rune-highlight';
const examples = import.meta.glob('./*.*', {
query: '?raw',
import: 'default',
eager: true
});
</script>
<HighlightSvelte
code={examples['./sample-1.md'] as string}
numberLine
highlightedRanges={[
[12, 14],
[7, 9]
]}
highlightedLines={[1, 3]}
--highlighted-background="#3669f7"
/>