コード
MDX ファイルでコード表示を使いこなす
opendocs は MDX ファイル内のコードを扱うために rehype-pretty-code ライブラリを使用しています。
これにより、コードの表示方法を大きくカスタマイズできます。
詳しくは公式ドキュメントとサンプルをご覧ください。
タイトル
例:
import { App } from './App';コード:
```js title="apps/web/src/index.tsx"
import { App } from './App';
```コードハイライト
例:
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}コード:
```js
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}
```インラインコードハイライト
例:
The result of [1, 2, 3].join('-') is '1-2-3'.
コード:
The result of `[1, 2, 3].join('-'){:js}` is `'1-2-3'{:js}`.コンテキスト対応インラインコードハイライト
たとえば、次のコードブロックがあるとします。
function getStringLength(str) {
return str.length;
}getStringLength を関数として参照すると、
そのように色付けできます。function や、
str と str なども同様です。これにより、
インラインコードを参照先の最も近いコードブロックへ意味的に関連付けられます。
コード:
When we refer to `getStringLength{:.entity.name.function}` as a function,
we can color it as such. Same with `function{:.keyword}`, or
`str{:.variable.parameter}` vs. `str{:.variable.other.object}`, etc. This allows
semantically link inline code to the nearest block of code it refers to.単語ハイライト
例:
relative w-full pl-12 rounded-lg borderコード:
```css /pl-12/ /border/
relative w-full pl-12 rounded-lg border
```行ハイライト
例:
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}コード:
```js {2-4,6}
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}
```行番号
例:
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}コード:
```js showLineNumbers
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}
```Diff
例:
const alertVariants = cva(
- "relative w-full rounded-lg border",
+ "relative w-full pl-12 rounded-lg border"
)コード:
```diff
const alertVariants = cva(
- "relative w-full rounded-lg border",
+ "relative w-full pl-12 rounded-lg border"
)
```ANSI ハイライト
例:
vite v5.0.0 dev server running at:
> Local: http://localhost:3000/
> Network: use `--host` to expose
ready in 125ms.
8:38:02 PM [vite] hmr update /src/App.jsxInline ANSI: > Local: http://localhost:3000/
コード:
```ansi
[0;36m vite v5.0.0[0;32m dev server running at:[0m
> Local: [0;36mhttp://localhost:[0;36;1m3000[0;36m/[0m
> Network: [0;2muse `--host` to expose[0m
[0;36mready in 125ms.[0m
[0;2m8:38:02 PM[0m [0;36;1m[vite][0m [0;32mhmr update [0;2m/src/App.jsx
```
Inline ANSI: `> Local: [0;36mhttp://localhost:[0;36;1m3000[0;36m/[0m{:ansi}`ID による単語グループハイライト
単語の後、# の後ろに ID を付けます。
これにより、指定した ID に基づいて文字を異なる色にできます。
例:
const [age, setAge] = useState(50);
const [name, setName] = useState("Taylor");コード:
```js /age/#v /name/#v /setAge/#s /setName/#s /50/#i /"Taylor"/#i
const [age, setAge] = useState(50);
const [name, setName] = useState("Taylor");
```スタイルを変更または追加するには、次を編集します。
[data-chars-id='v'] {
@apply !text-pink-300 bg-rose-800/50 font-bold;
}