sidebar-pptx-generator/references/component-codeblock.md
1.6 KB查看排版效果| 1 | --- |
| 2 | name: component-codeblock |
| 3 | description: CodeBlock component - syntax highlighting, language, theme, macOS header |
| 4 | --- |
| 5 | |
| 6 | # CodeBlock 组件规范 |
| 7 | |
| 8 | CodeBlock 组件用于展示语法高亮的代码块,支持多种编程语言,macOS 风格设计。 |
| 9 | |
| 10 | ## 核心属性 |
| 11 | |
| 12 | - **code**: 代码字符串 |
| 13 | - **language**: 编程语言,如 `"javascript"`, `"python"`, `"typescript"` |
| 14 | - **width**: 宽度(像素) |
| 15 | - **fontSize**: 字体大小(默认 12) |
| 16 | - **theme**: `"github-dark"` 或 `"github-light"`(默认 `"github-dark"`) |
| 17 | - **macHeader**: 是否显示 macOS 三色点(默认 true) |
| 18 | |
| 19 | ## 示例代码 |
| 20 | |
| 21 | ### 1. 基础 JavaScript 代码 |
| 22 | |
| 23 | ```jsx |
| 24 | <CodeBlock |
| 25 | code={`function fibonacci(n) { |
| 26 | if (n <= 1) return n; |
| 27 | return fibonacci(n - 1) + fibonacci(n - 2); |
| 28 | } |
| 29 | |
| 30 | console.log(fibonacci(10)); // 55`} |
| 31 | language='javascript' |
| 32 | width={700} |
| 33 | /> |
| 34 | ``` |
| 35 | |
| 36 | ### 2. Python 代码 |
| 37 | |
| 38 | ```jsx |
| 39 | <CodeBlock |
| 40 | code={`def quick_sort(arr): |
| 41 | if len(arr) <= 1: |
| 42 | return arr |
| 43 | pivot = arr[len(arr) // 2] |
| 44 | left = [x for x in arr if x < pivot] |
| 45 | middle = [x for x in arr if x == pivot] |
| 46 | right = [x for x in arr if x > pivot] |
| 47 | return quick_sort(left) + middle + quick_sort(right)`} |
| 48 | language='python' |
| 49 | width={800} |
| 50 | fontSize={14} |
| 51 | /> |
| 52 | ``` |
| 53 | |
| 54 | ### 3. TypeScript 代码(明亮主题) |
| 55 | |
| 56 | ```jsx |
| 57 | <CodeBlock |
| 58 | code={`interface User { |
| 59 | id: string; |
| 60 | name: string; |
| 61 | } |
| 62 | |
| 63 | class UserService { |
| 64 | private users: Map<string, User> = new Map(); |
| 65 | |
| 66 | addUser(user: User): void { |
| 67 | this.users.set(user.id, user); |
| 68 | } |
| 69 | }`} |
| 70 | language='typescript' |
| 71 | width={750} |
| 72 | theme='github-light' |
| 73 | background='#ffffff' |
| 74 | /> |
| 75 | ``` |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

