CodeHighlighter代码高亮
CodeHighlighter
代码高亮
用于高亮代码格式。
使用import { CodeHighlighter } from "@ant-design/x"; |
文档 编辑此页... |
版本自 2.1.0 起支持 |
使用import { CodeHighlighter } from "@ant-design/x"; |
文档 编辑此页... |
版本自 2.1.0 起支持 |
CodeHighlighter 组件用于需要展示带有语法高亮的代码片段的场景。
通用属性参考:通用属性。
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| lang | 代码语言类型 | string | - |
| children | 代码内容 | string | - |
| header | 头部内容,为 false 时不显示头部 | React.ReactNode | (() => React.ReactNode | false) | false | - |
| highlightProps | 代码高亮配置,透传给 react-syntax-highlighter | SyntaxHighlighterProps | - |
| prismLightMode | 是否使用 Prism 轻量模式,根据 lang 自动按需加载语言支持以减少打包体积 | boolean | true |
| 属性 | 说明 | 类型 | 版本 |
|---|---|---|---|
| nativeElement | 获取原生 DOM 节点 | HTMLElement | - |
| Token 名称 | 描述 | 类型 | 默认值 |
|---|---|---|---|
| colorBgTitle | 标题背景颜色 | string | rgba(0,0,0,0.06) |
| colorBorderCode | 代码块边框颜色 | string | #f0f0f0 |
| colorTextTitle | 标题文本颜色 | string | rgba(0,0,0,0.88) |
import React from 'react';
import { Button } from 'antd';
const App = () => (
<div>
<Button type="primary">Primary Button</Button>
</div>
);
export default App;.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>使用 prismLightMode 属性启用轻量模式,按需加载语言支持,可显著减少打包体积。
import React from 'react';
import { Button } from 'antd';
const App = () => (
<div>
<Button type="primary">Primary Button</Button>
</div>
);
export default App;import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>当前计数:{count}</p>
<button onClick={() => setCount(count + 1)}>增加</button>
</div>
);
}import React from 'react';
import { XMarkdown } from '@ant-design/x-markdown';
const App = () => <XMarkdown content='Hello World' />;
export default App;import React from 'react';
import { Button } from 'antd';
const App = () => (
<div>
<Button type="primary">Primary Button</Button>
</div>
);
export default App;.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>import React from 'react';
import { Button } from 'antd';
const App = () => (
<div>
<Button type="primary">Primary Button</Button>
</div>
);
export default App;import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>当前计数:{count}</p>
<button onClick={() => setCount(count + 1)}>增加</button>
</div>
);
}