Skip to content

Commit 85efbfe

Browse files
committed
✨ feat: Snippet组件使用代码高亮
1 parent 273ee5c commit 85efbfe

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/Snippet/Snippet.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Flex } from 'antd';
22
import { memo } from 'react';
33

44
import CopyButton from '@/CopyButton';
5+
import SyntaxHighlighter from '@/Highlighter/SyntaxHighlighter';
56
import Spotlight from '@/Spotlight';
67
import { DivProps } from '@/types';
78

@@ -17,6 +18,11 @@ export interface SnippetProps extends DivProps {
1718
* @default true
1819
*/
1920
copyable?: boolean;
21+
/**
22+
* @description The language of the content inside the Snippet component
23+
* @default 'tsx'
24+
*/
25+
language?: string;
2026
/**
2127
* @description Whether add spotlight background
2228
* @default false
@@ -34,13 +40,24 @@ export interface SnippetProps extends DivProps {
3440
}
3541

3642
const Snippet = memo<SnippetProps>(
37-
({ symbol, children, copyable = true, type = 'ghost', spotlight, className, ...rest }) => {
43+
({
44+
symbol,
45+
language = 'tsx',
46+
children,
47+
copyable = true,
48+
type = 'ghost',
49+
spotlight,
50+
className,
51+
...rest
52+
}) => {
3853
const { styles, cx } = useStyles(type);
3954

4055
return (
4156
<Flex align={'center'} className={cx(styles.container, className)} gap={8} {...rest}>
4257
{spotlight && <Spotlight />}
43-
<Flex>{[symbol, children].filter(Boolean).join(' ')}</Flex>
58+
<SyntaxHighlighter language={language}>
59+
{[symbol, children].filter(Boolean).join(' ')}
60+
</SyntaxHighlighter>
4461
{copyable && <CopyButton content={children} size={{ blockSize: 24, fontSize: 14 }} />}
4562
</Flex>
4663
);

src/Snippet/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Snippet 片段
2+
title: Snippet 代码片段
33
description: Snippet组件 用于显示带有语法高亮的代码片段。可以通过在内容前添加符号和选择语言进行定制。
44
group:
55
title: 站点

0 commit comments

Comments
 (0)