Code

Show a block of code for any programming language.

This component adds some features to the basic Shiki setup such as line numbers and a copy button.

Look to the right!
PropTypeDescription
codestringRequired Code formatted by Shiki
langstringRequired Code syntax highlighting language
lineNumbersbooleanShow line numbers (default: true)
startingLineNumbernumberNumber of first line (default: 1)
showCopybooleanShow the copy button (default: true)
copyTextstringText for the copy button (default: 'code')
copiedTextstringText for the copy button after click (default: 'copied')
copiedDurationnumberDuration copiedText shows, in ms (default: 2000)
shikiobject
shiki.highlighterobjectShiki highlighter options (e.g. theme and langs)
shiki.cdnstringPath passed to setCDN
<script lang="ts" context="module">
const shikiOptions = {
  highlighter: {
    theme: 'material-theme-palenight',
    langs: ['css', 'html', 'javascript', 'shell', 'svelte'],
  },
  cdn: '/assets/shiki',
};
</script>

<script lang="ts">
import Code from '../components/code.svelte';

const code = `<script lang="ts" context="module">
const shikiOptions = {
  highlighter: {
    theme: 'material-theme-palenight',
    langs: ['css', 'html', 'javascript', 'shell', 'svelte'],
  },
  cdn: '/assets/shiki',
};
</script>

<script lang="ts">
import Code from '../components/code.svelte';

const code = '<h1>This has to stop</h1>';
</script>

<Code {code} shiki={shikiOptions} lang="svelte" />`;
</script>

<Code {code} shiki={shikiOptions} lang="svelte" />