DokumentacijaDocumentationMdxCode

Koda

Obvladovanje kode v datotekah MDX

opendocs uporablja knjižnico rehype-pretty-code za obdelavo kode v datotekah MDX. To vam omogoča, da zelo prilagodite način prikaza kode!

Za več informacij si oglejte uradno dokumentacijo in primere!

Naslovi

Primer:

apps/web/src/index.tsx
import { App } from './App';

Koda:

```js title="apps/web/src/index.tsx"
import { App } from './App';
```

Označevanje kode

Primer:

module.exports = {
  theme: {
    extend: {
      colors: {
        warning: "hsl(var(--warning))",
        "warning-foreground": "hsl(var(--warning-foreground))",
      },
    },
  },
}

Koda:

```js
module.exports = {
  theme: {
    extend: {
      colors: {
        warning: "hsl(var(--warning))",
        "warning-foreground": "hsl(var(--warning-foreground))",
      },
    },
  },
}
```

Označevanje vrstične kode

Primer:

Rezultat izraza [1, 2, 3].join('-') je '1-2-3'.

Koda:

The result of `[1, 2, 3].join('-'){:js}` is `'1-2-3'{:js}`.

Označevanje vrstične kode z upoštevanjem konteksta

Na primer, če bi imeli naslednji blok kode:

function getStringLength(str) {
  return str.length;
}

Ko getStringLength omenjamo kot funkcijo, ga lahko tako tudi obarvamo. Enako velja za function ali str v primerjavi z str itd. To omogoča semantično povezovanje vrstične kode z najbližjim blokom kode, na katerega se nanaša.

Koda:

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.

Označevanje besed

Primer:

relative w-full pl-12 rounded-lg border

Koda:

```css /pl-12/ /border/
relative w-full pl-12 rounded-lg border
```

Označevanje vrstic

Primer:

module.exports = {
  theme: {
    extend: {
      colors: {
        warning: "hsl(var(--warning))",
        "warning-foreground": "hsl(var(--warning-foreground))",
      },
    },
  },
}

Koda:

```js {2-4,6}
module.exports = {
  theme: {
    extend: {
      colors: {
        warning: "hsl(var(--warning))",
        "warning-foreground": "hsl(var(--warning-foreground))",
      },
    },
  },
}
```

Številčenje vrstic

Primer:

module.exports = {
  theme: {
    extend: {
      colors: {
        warning: "hsl(var(--warning))",
        "warning-foreground": "hsl(var(--warning-foreground))",
      },
    },
  },
}

Koda:

```js showLineNumbers
module.exports = {
  theme: {
    extend: {
      colors: {
        warning: "hsl(var(--warning))",
        "warning-foreground": "hsl(var(--warning-foreground))",
      },
    },
  },
}
```

Diff

Primer:

const alertVariants = cva(
- "relative w-full rounded-lg border",
+ "relative w-full pl-12 rounded-lg border"
)

Koda:

```diff
const alertVariants = cva(
- "relative w-full rounded-lg border",
+ "relative w-full pl-12 rounded-lg border"
)
```

Označevanje ANSI

Primer:

  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

Vrstični ANSI: > Local: http://localhost:3000/

Koda:

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

Označevanje skupin besed po id

Za besedami dodajte id za #. To omogoča različno barvanje znakov glede na podani id.

Primer:

const [age, setAge] = useState(50);
const [name, setName] = useState("Taylor");

Koda:

```js /age/#v /name/#v /setAge/#s /setName/#s /50/#i /"Taylor"/#i
const [age, setAge] = useState(50);
const [name, setName] = useState("Taylor");
```

Za spremembo ali dodajanje novih slogov:

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