C?digo
Domina el c?digo en tus archivos MDX
opendocs usa la biblioteca rehype-pretty-code
para gestionar el c?digo en archivos MDX. Esto te permite personalizar mucho la forma en que se muestra el c?digo.
Para m?s informaci?n, consulta la documentaci?n y los ejemplos oficiales.
T?tulos
Ejemplo:
import { App } from './App';C?digo:
```js title="apps/web/src/index.tsx"
import { App } from './App';
```Resaltado de c?digo
Ejemplo:
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}C?digo:
```js
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}
```Resaltado de c?digo inline
Ejemplo:
El resultado de [1, 2, 3].join('-') es '1-2-3'.
C?digo:
The result of `[1, 2, 3].join('-'){:js}` is `'1-2-3'{:js}`.Resaltado inline con contexto
Por ejemplo, si tuvieras el siguiente bloque de c?digo:
function getStringLength(str) {
return str.length;
}Cuando nos referimos a getStringLength como funci?n,
podemos colorearlo como tal. Lo mismo ocurre con function, o
str frente a str, etc. Esto permite
vincular sem?nticamente el c?digo inline al bloque de c?digo m?s cercano al que hace referencia.
C?digo:
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.Resaltado de palabras
Ejemplo:
relative w-full pl-12 rounded-lg borderC?digo:
```css /pl-12/ /border/
relative w-full pl-12 rounded-lg border
```Resaltado de l?neas
Ejemplo:
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}C?digo:
```js {2-4,6}
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}
```Numeraci?n de l?neas
Ejemplo:
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}C?digo:
```js showLineNumbers
module.exports = {
theme: {
extend: {
colors: {
warning: "hsl(var(--warning))",
"warning-foreground": "hsl(var(--warning-foreground))",
},
},
},
}
```Diff
Ejemplo:
const alertVariants = cva(
- "relative w-full rounded-lg border",
+ "relative w-full pl-12 rounded-lg border"
)C?digo:
```diff
const alertVariants = cva(
- "relative w-full rounded-lg border",
+ "relative w-full pl-12 rounded-lg border"
)
```Resaltado ANSI
Ejemplo:
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/
C?digo:
```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}`Resaltado de grupos de palabras por id
Coloca un id despu?s de # tras las palabras.
Esto te permite colorear caracteres de forma diferente seg?n el id indicado.
Ejemplo:
const [age, setAge] = useState(50);
const [name, setName] = useState("Taylor");C?digo:
```js /age/#v /name/#v /setAge/#s /setName/#s /50/#i /"Taylor"/#i
const [age, setAge] = useState(50);
const [name, setName] = useState("Taylor");
```Para cambiar o a?adir estilos nuevos:
[data-chars-id='v'] {
@apply !text-pink-300 bg-rose-800/50 font-bold;
}