文档DocumentationMdxCode

代码

掌握 MDX 文件中的代码写法

opendocs 使用 rehype-pretty-code 库 处理 MDX 文件中的代码。这让你可以高度自定义代码的展示方式!

更多信息请参阅官方文档和示例!

标题

示例:

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

代码:

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

代码高亮

示例:

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

代码:

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

行内代码高亮

示例:

[1, 2, 3].join('-') 的结果是 '1-2-3'

代码:

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

感知上下文的行内代码高亮

例如,如果你有下面这段代码:

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

当我们把 getStringLength 作为函数来引用时, 就可以按函数为它着色。function,以及 strstr 等也同理。这可以 让行内代码在语义上关联到它所引用的最近代码块。

代码:

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.

单词高亮

示例:

relative w-full pl-12 rounded-lg border

代码:

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

行高亮

示例:

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

代码:

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

行号

示例:

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

代码:

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

Diff

示例:

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

代码:

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

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

行内 ANSI:> Local: http://localhost:3000/

代码:

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

按 id 分组高亮单词

在单词后面的 # 后添加 id。 这能让你根据给定 id 为字符设置不同颜色。

示例:

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

代码:

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

要更改或添加新样式:

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