Kod
Opanuj używanie kodu w swoich plikach MDX
opendocs używa biblioteki rehype-pretty-code
do obsługi kodu w plikach MDX. Pozwala to w dużym stopniu dostosować sposób wyświetlania kodu!
Więcej informacji znajdziesz w oficjalnej dokumentacji i przykładach!
Tytuły
Przykład:
import { App } from './App';Kod:
```js title="apps/web/src/index.tsx"
import { App } from './App';
```Podświetlanie kodu
Przykład:
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}Kod:
```js
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}
```Podświetlanie kodu inline
Przykład:
Wynik działania [1, 2, 3].join('-') to '1-2-3'.
Kod:
The result of `[1, 2, 3].join('-'){:js}` is `'1-2-3'{:js}`.Kontekstowe podświetlanie kodu inline
Na przykład, jeśli masz następujący blok kodu:
function getStringLength(str) {
return str.length;
}Gdy odnosimy się do getStringLength jako funkcji,
możemy pokolorować ją odpowiednio. Podobnie z function albo
str w porównaniu z str itd. Pozwala to
semantycznie powiązać kod inline z najbliższym blokiem kodu, do którego się odnosi.
Kod:
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.Podświetlanie słów
Przykład:
relative w-full pl-12 rounded-lg borderKod:
```css /pl-12/ /border/
relative w-full pl-12 rounded-lg border
```Podświetlanie linii
Przykład:
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}Kod:
```js {2-4,6}
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}
```Numeracja linii
Przykład:
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}Kod:
```js showLineNumbers
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}
```Diff
Przykład:
const alertVariants = cva(
- "relative w-full rounded-lg border",
+ "relative w-full pl-12 rounded-lg border"
)Kod:
```diff
const alertVariants = cva(
- "relative w-full rounded-lg border",
+ "relative w-full pl-12 rounded-lg border"
)
```Podświetlanie ANSI
Przykład:
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.jsxANSI inline: > Local: http://localhost:3000/
Kod:
```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}`Podświetlanie grup słów według id
Umieść id po # za słowami.
Pozwala to kolorować znaki inaczej w zależności od podanego id.
Przykład:
const [age, setAge] = useState(50);
const [name, setName] = useState("Taylor");Kod:
```js /age/#v /name/#v /setAge/#s /setName/#s /50/#i /"Taylor"/#i
const [age, setAge] = useState(50);
const [name, setName] = useState("Taylor");
```Aby zmienić lub dodać nowe style:
[data-chars-id='v'] {
@apply !text-pink-300 bg-rose-800/50 font-bold;
}