DokumentationDocumentationMdxCode

Kod

Bemästra kod i dina MDX-filer

opendocs använder biblioteket rehype-pretty-code för att hantera kod i MDX-filer. Det gör att du kan anpassa hur kod visas i stor utsträckning!

Mer information finns i den officiella dokumentationen och exemplen!

Titlar

Exempel:

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

Kod:

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

Kodmarkering

Exempel:

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))",
      },
    },
  },
}
```

Markering av inline-kod

Exempel:

Resultatet av [1, 2, 3].join('-') är '1-2-3'.

Kod:

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

Kontextmedveten markering av inline-kod

Om du till exempel hade följande kodblock:

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

När vi hänvisar till getStringLength som en funktion kan vi färglägga den som sådan. Detsamma gäller function, eller str kontra str osv. Det gör att inline-kod semantiskt kan kopplas till det närmaste kodblock den hänvisar till.

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.

Ordmarkering

Exempel:

relative w-full pl-12 rounded-lg border

Kod:

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

Radmarkering

Exempel:

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))",
      },
    },
  },
}
```

Radnumrering

Exempel:

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

Exempel:

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"
)
```

ANSI-markering

Exempel:

  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/

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

Ordgruppsmarkering efter id

Placera ett id efter # efter orden. Det gör att du kan färglägga tecken olika baserat på angivet id.

Exempel:

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");
```

För att ändra eller lägga till nya stilar:

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