코드 표시
코드 표시 (Code)
인라인 코드와 구문 강조 코드 블록을 클립보드 복사 기능과 함께 표시하는 방법을 다루는 가이드예요. 파일명, 줄 강조, 코드 diff 등 다양한 기능을 제공합니다.
출처: Nuxt UI
본문
인라인 코드와 구문 강조 코드 블록을 클립보드 복사 기능과 함께 표시해요.
코드 블록 (Code blocks)
코드 블록은 Shiki로 구동되는 구문 강조와 함께 ProsePre 컴포넌트가 렌더링해요.
```ts
export default defineNuxtConfig({
modules: ['@nuxt/ui']
})
```
/**
* Props for the ProseCode component
*/
interface ProseCodeProps {
lang?: string | undefined;
/**
* @default 'neutral'
*/
color?: "error" | "primary" | "secondary" | "success" | "info" | "warning" | "neutral" | undefined;
ui?: { base?: any; } | undefined;
}
export default defineAppConfig({
ui: {
prose: {
pre: {
slots: {
root: 'relative my-5 group',
header: 'flex items-center gap-1.5 border border-muted bg-default border-b-0 relative rounded-t-md px-4 py-3',
filename: 'text-default text-sm/6',
icon: 'size-4 shrink-0',
copy: 'absolute top-[11px] end-[11px] lg:opacity-0 lg:group-hover:opacity-100 lg:focus-visible:opacity-100 transition',
base: 'group font-mono text-sm/6 border border-muted bg-muted rounded-md px-4 py-3 whitespace-pre-wrap wrap-break-word overflow-x-auto outline-primary/25 focus-visible:outline-3 focus-visible:border-primary **:[.line]:block **:[.line.highlight]:-mx-4 **:[.line.highlight]:px-4 **:[.line.highlight]:bg-accented/50!'
},
variants: {
filename: {
true: {
root: '[&>pre]:rounded-t-none [&>pre]:my-0 my-5'
}
}
}
}
}
}
})
언어 (Language)
수십 가지 프로그래밍 언어에 대해 구문 강조가 제공돼요.
```vue
<script setup lang="ts">
const message = ref('Hello World!')
function updateMessage() {
message.value = 'Button clicked!'
}
</script>
<template>
<div>
<h1>{{ message }}</h1>
<UButton @click="updateMessage">
Click me
</UButton>
</div>
</template>
```
[!TIP]
기본적으로 라이트/다크 모드에는 각각
material-theme-lighter과material-theme-palenightShiki 테마가 사용돼요.@nuxt/content를 사용할 때는nuxt.config.ts의content.build.markdown.highlight키로, 독립형@nuxtjs/mdc를 사용할 때는mdc.highlight키로 테마를 바꿀 수 있어요. Comark을 사용할 때는shiki플러그인 옵션으로 테마를 구성합니다.Comark에서 다크 모드를 지원하려면 스타일시트에 다음 CSS를 추가하세요:
html.dark .shiki span { color: var(--shiki-dark) !important; background-color: var(--shiki-dark-bg) !important; font-style: var(--shiki-dark-font-style) !important; font-weight: var(--shiki-dark-font-weight) !important; text-decoration: var(--shiki-dark-text-decoration) !important; }
파일명 (Filename)
코드 블록은 자동 아이콘 감지와 함께 파일명 표시를 지원해요.
```ts [nuxt.config.ts]
export default defineNuxtConfig({
modules: ['@nuxt/ui']
})
```
[!TIP]
파일명 아이콘은
ProseCodeIcon컴포넌트가 렌더링하며, 앱 설정에서 커스터마이즈할 수 있는 미리 정의된 아이콘 집합을 포함합니다:export default defineAppConfig({ ui: { prose: { codeIcon: { terminal: 'i-lucide-square-terminal', config: 'i-lucide-settings', package: 'i-lucide-package' } } } })import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import ui from '@nuxt/ui/vite' export default defineConfig({ plugins: [ vue(), ui({ ui: { prose: { codeIcon: { terminal: 'i-lucide-square-terminal', config: 'i-lucide-settings', package: 'i-lucide-package' } } } }) ] })
[!NOTE]
이 파일명 아이콘들은 다른 컴포넌트가 사용하는
lucide아이콘과는 별개의vscode-icons컬렉션에서 가져온 것이며, 파일 확장자에 따라 온디맨드로 resolve 됩니다.@iconify-json/vscode-icons를 설치하면 다른 컬렉션과 마찬가지로 로컬과 오프라인에서 아이콘을 제공할 수 있어요.
복사 버튼 (Copy button)
모든 코드 블록에는 코드를 클립보드에 복사하는 복사 버튼이 내장되어 있어요.
copy prop을 false로 설정하면 버튼을 숨기거나, Button props를 전달해 버튼을 커스터마이즈할 수 있어요.
[!TIP]
앱 설정의
ui.icons.copy와ui.icons.copyCheck키로 아이콘을 바꿀 수 있어요:export default defineAppConfig({ ui: { icons: { copy: 'i-lucide-copy', copyCheck: 'i-lucide-copy-check' } } })import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import ui from '@nuxt/ui/vite' export default defineConfig({ plugins: [ vue(), ui({ ui: { icons: { copy: 'i-lucide-copy', copyCheck: 'i-lucide-copy-check' } } }) ] })
줄 강조 (Line highlighting)
중요한 부분에 주목을 끌기 위해 특정 줄을 강조할 수 있어요.
```ts [nuxt.config.ts] {2}
export default defineNuxtConfig({
modules: ['@nuxt/ui'], // This line is highlighted
css: ['~/assets/css/main.css']
})
```
코드 diff (Code diff)
diff 언어를 사용하면 코드 버전 간 변경 사항을 표시할 수 있어요.
```diff [nuxt.config.ts]
export default defineNuxtConfig({
modules: [
- '@nuxt/ui-pro'
+ '@nuxt/ui'
]
})
```
인라인 코드 (Inline code)
인라인 코드 스니펫은 ProseCode 컴포넌트가 렌더링해요.
<template>
<p>
<code>
inline code
</code>
</p>
</template>
`inline code`
:::
/**
* Props for the ProseCode component
*/
interface ProseCodeProps {
lang?: string | undefined;
/**
* @default 'neutral'
*/
color?: "error" | "primary" | "secondary" | "success" | "info" | "warning" | "neutral" | undefined;
ui?: { base?: any; } | undefined;
}
export default defineAppConfig({
ui: {
prose: {
code: {
base: 'px-1.5 py-0.5 text-sm font-mono font-medium rounded-md inline-block',
variants: {
color: {
primary: 'border border-primary/25 bg-primary/10 text-primary',
secondary: 'border border-secondary/25 bg-secondary/10 text-secondary',
success: 'border border-success/25 bg-success/10 text-success',
info: 'border border-info/25 bg-info/10 text-info',
warning: 'border border-warning/25 bg-warning/10 text-warning',
error: 'border border-error/25 bg-error/10 text-error',
neutral: 'border border-muted text-highlighted bg-muted'
}
},
defaultVariants: {
color: 'neutral'
}
}
}
}
})
색상 (Color)
color prop을 사용하면 인라인 코드의 색상을 바꿀 수 있어요. 기본값은 neutral이에요.
`inline code`{color="error"}
언어 (Lang)
lang prop을 사용하면 인라인 코드의 언어를 지정할 수 있어요.
`nuxt.config.ts`{lang="ts-type"}