Code Blocks
How to configure code blocks in markdown.
Overview
SvelDocs uses rehype-pretty-code in the markdown pipeline to transform fenced code blocks into themed, styleable HTML. The plugin uses shiki behind the scenes.
Theme
Edit the code block theme in the src/lib/markdown/markdown.config.ts. By default it uses the GitHub color theme, but you can use any theme supported by shiki.
import rehypePrettyCode from 'rehype-pretty-code';
const markdownConfig = defineConfig({
...
rehypePlugins: [
[
rehypePrettyCode,
{
theme: {
light: 'github-light',
dark: 'github-dark'
},
keepBackground: false,
transformers: [transformerNotationDiff()]
}
]
]
}); Component
src/lib/markdown/components/code/pre.svelte is the component that replaces the default html <pre> element. This allows for:
- Language Badge
- Code Copy Button
- CSS Styling
Styling
Styling is configured within the component. You can style the component elements themselves inline, but rehype-pretty-code and shiki styling must be configured in the style section using :global.
Here is the provided default:
<style lang="postcss">
@reference "$css";
:global {
code[data-theme*=' '],
code[data-theme*=' '] span {
color: var(--shiki-light);
}
.dark code[data-theme*=' '],
.dark code[data-theme*=' '] span {
color: var(--shiki-dark);
}
[data-rehype-pretty-code-figure] {
@apply has-[pre:focus-visible]:ring-accent/50 has-[pre:focus-visible]:border-accent overflow-hidden rounded-md border shadow-xs transition-[border-color,box-shadow] has-[pre:focus-visible]:ring-2;
}
[data-rehype-pretty-code-title] {
@apply bg-primary border-b p-2 text-sm font-bold;
}
[data-rehype-pretty-code-caption] {
@apply text-muted-foreground bg-primary border-t p-2 text-sm;
}
[data-line] {
@apply inline-block px-4 hover:bg-[color-mix(in_oklch,currentColor,transparent_90%)];
}
[data-line].diff.remove {
@apply bg-red-500/10 opacity-75 hover:bg-[color-mix(in_oklch,var(--color-red-500),transparent_80%)];
}
[data-line].diff.add {
@apply bg-green-500/10 hover:bg-[color-mix(in_oklch,var(--color-green-500),transparent_80%)];
}
[data-line-numbers] {
counter-reset: line;
}
[data-line-numbers] > [data-line] {
@apply pl-0;
}
[data-line-numbers] > [data-line]::before {
counter-increment: line;
content: counter(line);
@apply text-muted-foreground/75;
}
[data-line-numbers-max-digits] {
& > [data-line]:hover::before {
@apply text-foreground bg-[color-mix(in_oklch,currentColor,transparent_90%)];
}
& > [data-line]::before {
width: calc(var(--lineNumbersMaxDigits) + 2rem);
@apply bg-secondary sticky left-0 mr-4 inline-block border-r px-4 text-right;
}
}
[data-highlighted-line] {
@apply bg-accent/10! hover:bg-[color-mix(in_oklch,var(--color-accent),transparent_80%)]!;
}
}
</style> Markdown Features
Use fenced-code metadata to control each block from markdown:
```ts
console.log('hello world');
``` console.log('hello world'); You can declare the language at top. ts is shortened for typescript.
Title
You can add a title to the code block by adding title="your title here":
```ts title="src/lib/my-awesome-file.ts"
console.log('hello world');
``` console.log('hello world'); Footer (Caption)
You can add a caption to the code block by adding caption="your caption here":
```ts caption="This runs during page load"
console.log('loaded');
``` console.log('loaded'); Highlight Lines
You can highlight specific lines by using curly-brace ranges after the language:
```ts {2,4-6}
const one = 1;
const two = 2;
const three = 3;
const four = 4;
const five = 5;
const six = 6;
``` const one = 1;
const two = 2;
const three = 3;
const four = 4;
const five = 5;
const six = 6; Line Numbers
You can show line numbers by using showLineNumbers:
```ts showLineNumbers
const one = 1;
const two = 2;
const three = 3;
const four = 4;
const five = 5;
const six = 6;
``` const one = 1;
const two = 2;
const three = 3;
const four = 4;
const five = 5;
const six = 6; You can start the line number at a specific number by using a curly-brace and a number:
```ts showLineNumbers{5}
const one = 1;
const two = 2;
const three = 3;
const four = 4;
const five = 5;
const six = 6;
``` const one = 1;
const two = 2;
const three = 3;
const four = 4;
const five = 5;
const six = 6; Diff Notation
You can show added and removed lines using notation diff comments.
```ts
const text = 'hi world!'; // [!code --]
const text = 'hello world!'; // [!code ++]
``` const text = 'hi world!';
const text = 'hello world!'; Kitchen Sink
Here is an example of all the markdown features combined.
```ts {2,4-6} title="kitchen-sink-example.ts" showLineNumbers
const one = 1;
const two = 2;
const three = 3;
const four = 4;
const five = 5;
const six = 6;
const text = 'hi world!'; // [!code --]
const text = 'hello world!'; // [!code ++]
``` const one = 1;
const two = 2;
const three = 3;
const four = 4;
const five = 5;
const six = 6;
const text = 'hi world!';
const text = 'hello world!'; File to Code Block
You can generate a code block from a file using the File Reader component.