Code
Maîtriser les blocs de code dans vos fichiers MDX
opendocs utilise la bibliothèque rehype-pretty-code
pour gérer le code dans les fichiers MDX. Cela vous permet de personnaliser largement l'affichage du code !
Pour plus d'informations, consultez la documentation officielle et les exemples !
Titres
Exemple :
import { App } from './App';Code :
```js title="apps/web/src/index.tsx"
import { App } from './App';
```Mise en évidence du code
Exemple :
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))",
},
},
},
}
```Mise en évidence du code en ligne
Exemple :
Le résultat de [1, 2, 3].join('-') est '1-2-3'.
Code :
The result of `[1, 2, 3].join('-'){:js}` is `'1-2-3'{:js}`.Mise en évidence contextuelle du code en ligne
Par exemple, si vous aviez le bloc de code suivant :
function getStringLength(str) {
return str.length;
}Lorsque nous faisons référence à getStringLength comme fonction,
nous pouvons la colorer comme telle. Même chose avec function, ou
str par rapport à str, etc. Cela permet
de relier sémantiquement le code en ligne au bloc de code le plus proche auquel il fait référence.
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.Mise en évidence de mots
Exemple :
relative w-full pl-12 rounded-lg borderCode :
```css /pl-12/ /border/
relative w-full pl-12 rounded-lg border
```Mise en évidence de lignes
Exemple :
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))",
},
},
},
}
```Numérotation des lignes
Exemple :
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
Exemple :
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"
)
```Mise en évidence ANSI
Exemple :
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 en ligne : > 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}`Mise en évidence de groupes de mots par id
Placez un id après # après les mots.
Cela vous permet de colorer les caractères différemment selon l'id fourni.
Exemple :
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");
```Pour modifier ou ajouter de nouveaux styles :
[data-chars-id='v'] {
@apply !text-pink-300 bg-rose-800/50 font-bold;
}