markdown2
All checks were successful
Deploy to Server / deploy (push) Successful in 38s

This commit is contained in:
jeremygan2021
2026-02-24 16:32:52 +08:00
parent 009a2a59f3
commit c07f7028fc
3 changed files with 163 additions and 6 deletions

View File

@@ -0,0 +1,48 @@
import React, { useState } from 'react';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { vscDarkPlus } from 'react-syntax-highlighter/dist/esm/styles/prism';
import { CopyOutlined, CheckOutlined } from '@ant-design/icons';
import { Button, Tooltip } from 'antd';
const CodeBlock = ({ language, children, ...props }) => {
const [copied, setCopied] = useState(false);
const handleCopy = () => {
navigator.clipboard.writeText(String(children).replace(/\n$/, ''));
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
return (
<div style={{ position: 'relative', margin: '1em 0' }}>
<Tooltip title={copied ? "已复制" : "复制代码"}>
<Button
type="text"
icon={copied ? <CheckOutlined style={{ color: '#52c41a' }} /> : <CopyOutlined style={{ color: '#fff' }} />}
size="small"
onClick={handleCopy}
style={{
position: 'absolute',
top: 8,
right: 8,
zIndex: 1,
color: '#fff',
background: 'rgba(255, 255, 255, 0.1)',
border: 'none',
}}
/>
</Tooltip>
<SyntaxHighlighter
style={vscDarkPlus}
language={language}
PreTag="div"
customStyle={{ margin: 0, borderRadius: 8, padding: '1.5em 1em 1em 1em' }}
{...props}
>
{String(children).replace(/\n$/, '')}
</SyntaxHighlighter>
</div>
);
};
export default CodeBlock;