Code
Codeコンポーネントのドキュメントと例。
いつ使用するか
チュートリアルやドキュメントなどで、構文を強調表示してソースコードを表示する場合。
使用例
javascript
console.log('Hello World'); astro
<Code code="console.log('Hello World');" language='javascript' /> 行番号を表示する
`showLineNumbers={true}`を渡すことで、行番号を有効にできます。
greet.js
function greet(name) {
console.log(`Hello, ${name}!`);
}
greet('World'); astro
<Code
code={`function greet(name) {
console.log(\`Hello, \${name}!\`);
}
greet('World');`}
language='javascript'
showLineNumbers={true}
filename='greet.js'
/> ヘッダーとコピーボタンを非表示にする
`copyButton={false}`を使用してコピーボタンを無効にでき、`showHeader={false}`を使用してヘッダーを完全に非表示にできます。
git clone https://github.com/example/repo.git
cd repo
npm install astro
<Code
code={`git clone https://github.com/example/repo.git
cd repo
npm install`}
language='bash'
copyButton={false}
showHeader={false}
/> 使用方法
Codeコンポーネントを使用するには、その場所からインポートし、必要なプロパティを渡します。
astro
import Code from '@/components/data-display/Code.astro';
<Code
code="console.log('Hello World');"
language="javascript"
showLineNumbers={true}
filename="example.js"
/> APIリファレンス
| プロパティ | 説明 | タイプ | デフォルト |
|---|---|---|---|
| code | The source code to display. | string | - |
| language | The language of the source code for syntax highlighting. | string | 'javascript' |
| filename | The name of the file to display in the header. | string | - |
| showLineNumbers | Whether to show line numbers. | boolean | false |
| copyButton | Whether to show the copy button. | boolean | true |
| showHeader | Whether to show the header containing the filename/language and copy button. | boolean | true |
| class | Additional CSS classes to apply to the container. | string | - |