语法高亮
Nextra 使用 Shiki 在构建时进行语法高亮。它非常可靠且性能良好。例如,在你的 Markdown 文件中添加以下内容:
```js
console.log('hello, world')
```
结果如下:
console.log('hello, world')
功能
内联代码
在行内也可以使用语法高亮,例如 let x = 1
,这是通过 {:}
语法完成的:
Inlined syntax highlighting is also supported `let x = 1{:jsx}` via:
高亮行
你可以在代码块后添加 {行号}
来高亮特定的行:
```js {1,4-5}
import { useState } from 'react'
function Counter() {
const [count, setCount] = useState(0)
return <button onClick={() => setCount(count + 1)}>{count}</button>
}
```
结果如下:
import { useState } from 'react'
function Counter() {
const [count, setCount] = useState(0)
return <button onClick={() => setCount(count + 1)}>{count}</button>
}
高亮子字符串
你可以使用 //
属性在代码块中高亮特定的子字符串:
```js /useState/
import { useState } from 'react'
function Counter() {
const [count, setCount] = useState(0)
return <button onClick={() => setCount(count + 1)}>{count}</button>
}
```
import { useState } from 'react'
function Counter() {
const [count, setCount] = useState(0)
return <button onClick={() => setCount(count + 1)}>{count}</button>
}
你可以只高亮部分出现的子字符串:
复制按钮
通过在代码块中添加 copy
属性,可以为用户提供复制代码的功能:
```js copy
console.log('hello, world')
```
结果如下:
console.log('hello, world')
[!NOTE] 你可以在
next.config.mjs
中设置defaultShowCopyCode: true
来全局启用此功能;启用后可用copy=false
来禁用。
行号
可以在代码块中添加 showLineNumbers
属性为代码块添加行号:
```js showLineNumbers
import { useState } from 'react'
function Counter() {
const [count, setCount] = useState(0)
return <button onClick={() => setCount(count + 1)}>{count}</button>
}
```
结果如下:
import { useState } from 'react'
function Counter() {
const [count, setCount] = useState(0)
return <button onClick={() => setCount(count + 1)}>{count}</button>
}
文件名和标题
可以通过添加 filename
属性来为代码块指定文件名或标题:
```js filename="example.js"
console.log('hello, world')
```
结果如下:
console.log('hello, world')
ANSI 高亮
可以高亮 ANSI 转义代码:
```ansi
[0m [0;32m✓[0m [0;2msrc/[0mindex[0;2m.test.ts (1)[0m
[0;2m Test Files [0m [0;1;32m1 passed[0;98m (1)[0m
[0;2m Tests [0m [0;1;32m1 passed[0;98m (1)[0m
[0;2m Start at [0m 23:32:41
[0;2m Duration [0m 11ms
[42;1;39;0m PASS [0;32m Waiting for file changes...[0m
[0;2mpress [0;1mh[0;2m to show help, press [0;1mq[0;2m to quit
```
结果如下:
✓ src/index.test.ts (1)
Test Files 1 passed (1)
Tests 1 passed (1)
Start at 23:32:41
Duration 11ms
PASS Waiting for file changes...
press h to show help, press q to quit
支持的语言
查看 此列表 以了解所有支持的语言。
使用动态内容
由于语法高亮在构建时完成,代码块无法使用动态内容。不过由于 MDX 的强大功能,仍可通过客户端 JS 实现替代方案。例如:
function hello() {
const x = 2 + 3
console.log(1)
}
此替代方案的限制是更新的内容不会被重新高亮。例如,如果我们将数字更新为 1 + 1
,它将被错误地高亮。
查看 代码 以了解其工作原理。
禁用语法高亮
你可以在 next.config.mjs
中将 codeHighlight
设置为 false
,以彻底禁用语法高亮。
Option | Type | Description |
---|---|---|
codeHighlight | boolean | Enable or disable syntax highlighting. Defaults to `true`. |
自定义语法规则
Shiki 支持 VSCode 的 TextMate 语法 ,你可以通过在 mdxOptions.rehypePrettyCodeOptions
中覆盖 getHighlighter
函数来引入自定义语言语法。
你可以在 next.config.mjs
中通过覆盖 mdxOptions.rehypePrettyCodeOptions
中的 getHighlighter
函数来提供这些语法规则:
import { BUNDLED_LANGUAGES } from 'shiki'
nextra({
// ... other Nextra config options
mdxOptions: {
rehypePrettyCodeOptions: {
getHighlighter: options =>
getHighlighter({
...options,
langs: [
...BUNDLED_LANGUAGES,
// custom grammar options, see the Shiki documentation for how to provide these options
{
id: 'my-lang',
scopeName: 'source.my-lang',
aliases: ['mylang'], // Along with id, aliases will be included in the allowed names you can use when writing Markdown
path: '../../public/syntax/grammar.tmLanguage.json'
}
]
})
}
}
})
自定义主题
在 mdxOptions.rehypePrettyCodeOptions
中可以提供自定义主题,而不是 依赖于 CSS Variables:
nextra({
// ... other Nextra config options
mdxOptions: {
rehypePrettyCodeOptions: {
// VSCode theme or built-in Shiki theme, see Shiki documentation for more information
theme: JSON.parse(
readFileSync('./public/syntax/arctis_light.json', 'utf8')
)
}
}
})
多主题(暗色和亮色)
可以将多个主题传入 theme
,以支持深色和浅色模式:
nextra({
// ... other Nextra config options
mdxOptions: {
rehypePrettyCodeOptions: {
theme: {
dark: 'nord',
light: 'min-light'
}
}
}
})