({ children, language = 'text' }: CodeBlockProps)
| 52 | } |
| 53 | |
| 54 | export default function CodeBlock({ children, language = 'text' }: CodeBlockProps) { |
| 55 | // 检测是否是 CodePen 格式的代码块 |
| 56 | const isCodePen = language === 'codepen' || children.includes('<!-- CodePen Demo -->'); |
| 57 | |
| 58 | if (isCodePen) { |
| 59 | // 解析 CodePen 格式的代码 |
| 60 | const lines = children.split('\n'); |
| 61 | let html = ''; |
| 62 | let css = ''; |
| 63 | let js = ''; |
| 64 | let currentSection = ''; |
| 65 | |
| 66 | for (const line of lines) { |
| 67 | if (line.includes('<!-- HTML -->')) { |
| 68 | currentSection = 'html'; |
| 69 | } else if (line.includes('<!-- CSS -->')) { |
| 70 | currentSection = 'css'; |
| 71 | } else if (line.includes('<!-- JavaScript -->')) { |
| 72 | currentSection = 'js'; |
| 73 | } else if (line.includes('<!-- CodePen Demo -->')) { |
| 74 | // 跳过注释行 |
| 75 | continue; |
| 76 | } else { |
| 77 | switch (currentSection) { |
| 78 | case 'html': |
| 79 | html += line + '\n'; |
| 80 | break; |
| 81 | case 'css': |
| 82 | css += line + '\n'; |
| 83 | break; |
| 84 | case 'js': |
| 85 | js += line + '\n'; |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return ( |
| 92 | <div className="my-6"> |
| 93 | <CodePenDemo html={html.trim()} css={css.trim()} js={js.trim()} /> |
| 94 | <div className="mt-4"> |
| 95 | <SyntaxHighlighter |
| 96 | language="html" |
| 97 | style={tomorrow} |
| 98 | customStyle={{ |
| 99 | margin: 0, |
| 100 | padding: '1rem', |
| 101 | borderRadius: '0.5rem', |
| 102 | fontSize: '0.875rem', |
| 103 | lineHeight: '1.4', |
| 104 | backgroundColor: '#2d3748', |
| 105 | }} |
| 106 | showLineNumbers={true} |
| 107 | wrapLines={true} |
| 108 | lineProps={{ |
| 109 | style: { |
| 110 | padding: '0.125rem 0', |
| 111 | minHeight: '1.4em', |
nothing calls this directly
no test coverage detected