CodeHighlighter
Highlight formatted code. Aligned with React @ant-design/x CodeHighlighter.
Standalone component. The Markdown plugin
HighlightCodeis still available; the two serve different purposes (plugin ≠ this component).
When to use
- Show syntax-highlighted snippets with copy and header language info.
- Combine with XMarkdown via
components.codeto render fenced code blocks.
Examples
Basic
JavaScript Code
javascript
import { ref } from 'vue';
import { Button } from 'ant-design-vue';
const App = () => (
<div>
<Button type="primary">Primary Button</Button>
</div>
);
export default App;CSS Code
css
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}HTML Code
html
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>Prism Light Mode
使用 prismLightMode 按需加载语言,减小打包体积。
javascript
import { ref } from 'vue';
import { Button } from 'ant-design-vue';
const App = () => (
<div>
<Button type="primary">Primary Button</Button>
</div>
);
export default App;Custom header
Vue 计数器示例
JavaScript
import { ref } from 'vue';
function useCounter() {
const count = ref(0);
const inc = () => {
count.value += 1;
};
return { count, inc };
}With XMarkdown
API
CodeHighlighterProps
| Prop | Description | Type | Default | Version |
|---|---|---|---|---|
| lang | Language id | string | - | - |
| children | Code content (or default slot) | string | - | - |
| header | Header content; hidden when false / null | VNode | string | (() => VNode | string | false | null) | false | null | - | - |
| highlightProps | Extra options for the highlighter (showLineNumbers / startingLineNumber / codeTagProps, …) | CodeHighlighterHighlightProps | - | - |
| prismLightMode | Prism light mode (lazy-load by lang) | boolean | true | - |
| classNames | Semantic class names | Partial\<Record\<SemanticType, string\>\> | - | - |
| styles | Semantic styles | Partial\<Record\<SemanticType, CSSProperties\>\> | - | - |
| prefixCls | Class prefix | string | - | - |
| rootClassName | Extra class on the root | string | - | - |
Slots
| Slot | Description |
|---|---|
| default | Code content (string) |
| header | Custom header |
CodeHighlighter Ref
| Prop | Description | Type |
|---|---|---|
| nativeElement | Native DOM node | HTMLElement |
SemanticType
| Name | Description |
|---|---|
| root | Root |
| header | Header container |
| headerTitle | Title |
| code | Code container |
Semantic DOM
typescript
import { XMarkdown } from '@ant-design-x-vue/x-markdown';
const App = () => <XMarkdown content='Hello World' />;
export default App;root
根节点header
头部的容器headerTitle
标题code
代码容器
Design Token
| Token | Description | Default |
|---|---|---|
| colorBgTitle | Title background | colorFillContent |
| colorTextTitle | Title text | colorText |
| colorBorderCode | Code block border | colorBorderSecondary |
Contributors
Contributors
Loading contributors…