DocsDocumentationMdxCode

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:

apps/web/src/index.tsx
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 border

C?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.jsx

ANSI inline: > Local: http://localhost:3000/

C?digo:

```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.jsx
```
 
Inline ANSI: `> Local: http://localhost:3000/{: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:

apps/web/src/styles/mdx.css
[data-chars-id='v'] {
  @apply !text-pink-300 bg-rose-800/50 font-bold;
}