DokumentacjaDocumentationMdxCode

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:

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

Kod:

```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.jsx

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

Kod:

```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}`

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:

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