Code
ใช้งาน code ในไฟล์ MDX ของคุณให้เชี่ยวชาญ
opendocs ใช้ไลบรารี rehype-pretty-code
เพื่อจัดการ code ในไฟล์ MDX วิธีนี้ช่วยให้คุณปรับแต่งวิธีแสดง code ได้อย่างมาก!
สำหรับข้อมูลเพิ่มเติม โปรดดูเอกสารและตัวอย่างอย่างเป็นทางการ!
ชื่อเรื่อง
ตัวอย่าง:
import { App } from './App';Code:
```js title="apps/web/src/index.tsx"
import { App } from './App';
```การไฮไลต์ code
ตัวอย่าง:
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}Code:
```js
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}
```การไฮไลต์ inline code
ตัวอย่าง:
ผลลัพธ์ของ [1, 2, 3].join('-') คือ '1-2-3'
Code:
The result of `[1, 2, 3].join('-'){:js}` is `'1-2-3'{:js}`.การไฮไลต์ inline code ตามบริบท
ตัวอย่างเช่น หากคุณมีบล็อก code ต่อไปนี้:
function getStringLength(str) {
return str.length;
}เมื่อเราอ้างถึง getStringLength ในฐานะฟังก์ชัน
เราสามารถใส่สีให้สอดคล้องกันได้ เช่นเดียวกับ function หรือ
str เทียบกับ str เป็นต้น วิธีนี้ช่วยให้
inline code เชื่อมโยงเชิงความหมายกับบล็อก code ที่ใกล้ที่สุดที่อ้างถึง
Code:
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 borderCode:
```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))",
},
},
},
}Code:
```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))",
},
},
},
}Code:
```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"
)Code:
```diff
const alertVariants = cva(
- "relative w-full rounded-lg border",
+ "relative w-full pl-12 rounded-lg border"
)
```ANSI Highlight
ตัวอย่าง:
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/
Code:
```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");Code:
```js /age/#v /name/#v /setAge/#s /setName/#s /50/#i /"Taylor"/#i
const [age, setAge] = useState(50);
const [name, setName] = useState("Taylor");
```หากต้องการเปลี่ยนหรือเพิ่ม styles ใหม่:
[data-chars-id='v'] {
@apply !text-pink-300 bg-rose-800/50 font-bold;
}