---
title: "Code Snippet"
description: "Importing and displaying source code from your project."
canonical_url: "https://lab.nnsvn.me/docs/api/advanced/code-snippet"
---

> Index of every page on this site: [llms.txt](https://lab.nnsvn.me/llms.txt)

## Usage

<ct-steps :level="4">

#### Add the `CodeSnippet` component

```vue [components/content/CodeSnippet.vue]line-numbers height=400
<template>
  <MDC v-if="loadedCode" :value="md" class="not-first:mt-5" />
  <UiAlert v-else variant="destructive">
    <UiAlertTitle>Error</UiAlertTitle>
    <UiAlertDescription>
      Cannot load code: <ProseCodeInline>{{ file || url }}</ProseCodeInline>
    </UiAlertDescription>
  </UiAlert>
</template>

<script setup lang="ts">
const { file, url, language, title, highlights, meta, start, offset } = defineProps<{
  file?: string;
  url?: string;
  language: string;
  title?: string;
  highlights?: string;
  meta?: string;
  start?: number;
  offset?: number;
}>();

const loadedCode = ref('');

const rawFiles = import.meta.glob([
  // Path you want to import
], {
  query: '?raw',
  import: 'default',
};

if (file) {
  const importer = rawFiles[file];
  if (importer) {
    loadedCode.value = (await importer()) as string;
  }
} else if (url) {
  try {
    const data = await $fetch(url, { parseResponse: txt => txt });
    if (data) {
      loadedCode.value = data as string;
    }
  } catch {}
}

if (loadedCode.value && start && offset) {
  const lines = loadedCode.value.split('\n');
  loadedCode.value = lines.slice(Number(start || 1) - 1, Number(start || 1) - 1 + Number(offset)).join('\n');
}

const md = `
::div
\`\`\`\`${language} ${title && `[${title}]`} ${highlights && `{${highlights}}`} ${meta || ''}
${loadedCode.value}
\`\`\`\`
::
`;
</script>
```

#### Add paths you want to import

Use the [Glob Import pattern](https://vite.dev/guide/features#multiple-patterns) to add paths.

```ts [Line 25 to 30 in CodeSnippet.vue]
const rawFiles = import.meta.glob([
  // Path you want to import
  // e.g.
  '@/examples/**/*.{vue}',
], {
  query: '?raw',
  import: 'default',
};
```

<ct-alert icon="lucide:triangle-alert" type="danger">

**DO NOT** import files like `.env` or files from your `server/` directory.

</ct-alert>

<ct-alert icon="lucide:info">

The `/nuxt.config.ts` file can not be imported due to framework constraints

</ct-alert>

#### Use in markdown files

```mdc
::ct-code-snippet
---
file: /examples/file.vue
language: vue
---
::
```

</ct-steps>

### Highlighting Lines

```mdc
::ct-code-snippet
---
file: /examples/file.vue
language: vue
title: File
highlights: 9-11
meta: line-numbers height=300
---
::
```

Translates to:

```md
````ts [File] {9-11} line-numbers height=300
// code imported from /examples/file.vue
````
```

### Using Offset Props

```mdc
::ct-code-snippet
---
file: /examples/file.vue
language: vue
start: 6
offset: 4
---
::
```

### Fetching From a Remote URL

```mdc
::ct-code-snippet
---
url: https://raw.githubusercontent.com/No-Name-Studio-VN/Homepage/refs/heads/main/pages/index.vue
language: vue
---
::
```

## Props

<ct-field-group>
<ct-field name="file" type="string">

The path to the imported file

</ct-field>

<ct-field name="url" type="string">

A remote URL to fetch the code from

</ct-field>

<ct-field name="title" type="string">

Code block title

</ct-field>

<ct-field name="language" type="string">

Code block highlight language

</ct-field>

<ct-field name="highlights" type="string">

Code block highlights lines

</ct-field>

<ct-field name="meta" type="string">

Code block meta

</ct-field>

<ct-field name="start" type="number">

Starting line index

</ct-field>

<ct-field name="offset" type="number">

Number of lines to display from start

</ct-field>
</ct-field-group>

## Sitemap

See the full [sitemap](https://lab.nnsvn.me/sitemap.md) for all pages.
