All examples

Code blocks

Code blocks with an owned tokenizer: TypeScript, Python, SQL, JSON, shell and plain text, line numbers, stepped highlights and a caption from block data, copy and an RTL page. Fictional content.

Code blocks

blocks-code · blocks/CodeBlockView

blocks code
Props
  • block
  • copyable
  • highlights
  • hostStep
  • labels
  • lineNumbers
  • step

Live example loads in an isolated same-origin preview frame.

Source (blocks-code/CodeBlocksDemo.svelte)
<script lang="ts">
import { CodeBlockView } from '@texuli/blocks/code/renderers';
import { CODE_SAMPLES } from './samples.ts';

const first = CODE_SAMPLES[0]?.block;
</script>

<article class="demo" data-example="blocks-code" aria-labelledby="code-title">
  <h2 id="code-title">Code blocks</h2>

  <section class="case" aria-labelledby="code-steps">
    <h3 id="code-steps">Line numbers and highlighted lines</h3>
    <CodeBlockView block={first} step={1} />
  </section>

  {#each CODE_SAMPLES.slice(1) as sample (sample.label)}
    <div class="case">
      <h3>{sample.label}</h3>
      <CodeBlockView block={sample.block} />
    </div>
  {/each}

  <section class="case" dir="rtl" lang="ar" aria-labelledby="code-rtl">
    <h3 id="code-rtl">صفحة من اليمين إلى اليسار</h3>
    <CodeBlockView
      block={{
        type: 'code',
        lang: 'yaml',
        filename: 'deploy.yml',
        lineNumbers: true,
        code: 'name: harbor\nreplicas: 2\n# fictional',
      }}
      labels={{ copy: 'نسخ', copied: 'تم النسخ', copyName: 'نسخ رمز {name}' }}
    />
  </section>
</article>

<style>
  .demo {
    display: grid;
    gap: 1.25rem;
    max-inline-size: 46rem;
    padding: 1.5rem;
  }
  .case {
    display: grid;
    gap: 0.5rem;
  }
  .case h3,
  .demo h2 {
    margin: 0;
  }
</style>