Skip to content

feat(CodeHighlighter): support showLineNumber, wrapLongLines, showCopyButton props#1976

Open
wryyyds7 wants to merge 2 commits into
ant-design:mainfrom
wryyyds7:feat/code-highlighter-props
Open

feat(CodeHighlighter): support showLineNumber, wrapLongLines, showCopyButton props#1976
wryyyds7 wants to merge 2 commits into
ant-design:mainfrom
wryyyds7:feat/code-highlighter-props

Conversation

@wryyyds7

@wryyyds7 wryyyds7 commented Jul 16, 2026

Copy link
Copy Markdown

🤔 This is a ...

  • 🆕 New feature
  • 🐞 Bug fix
  • 📝 Site / documentation improvement
  • 📽️ Demo improvement
  • 💄 Component style improvement
  • 🤖 TypeScript definition improvement
  • 📦 Bundle size optimization
  • ⚡️ Performance optimization
  • ⭐️ Feature enhancement
  • 🌐 Internationalization
  • 🛠 Refactoring
  • 🎨 Code style optimization
  • ✅ Test Case
  • 🔀 Branch merge
  • ⏩ Workflow
  • ⌨️ Accessibility improvement
  • ❓ Other (about what?)

🔗 Related Issues

Closes #1951

💡 Background and Solution

Background

CodeHighlighter 当前仅能通过 highlightProps 透传 react-syntax-highlighter 的配置,缺少常用的显式开关,使用不够直观。其中复制按钮更是无法单独控制显隐——只能通过 header={false} 隐藏整个 header。

Solution

CodeHighlighterProps 上新增三个显式配置项:

Prop Description Type Default
showLineNumber 是否显示行号 boolean false
wrapLongLines 是否自动换行(长行换行) boolean false
showCopyButton 是否显示复制按钮(仅在默认 header 下生效) boolean true

这三个 props 会合并到 SyntaxHighlighter 的 props 中(放在 highlightProps 之前),用户仍可通过 highlightProps 做更细粒度的覆盖。

📝 Change Log

Language Changelog
🇺🇸 English CodeHighlighter adds showLineNumber, wrapLongLines, and showCopyButton props for common configurations.
🇨🇳 Chinese CodeHighlighter 新增 showLineNumberwrapLongLinesshowCopyButton 三个显式配置项。

Summary by CodeRabbit

  • 新功能
    • 代码高亮组件新增可选配置:显示/隐藏行号、长行自动换行、控制复制按钮(仅在默认标题栏下生效)。
  • 文档
    • 补充相关属性说明与默认值,并新增“配置”示例。
  • 测试
    • 增加展示与组合场景的用例覆盖(行号、换行、复制按钮联动)。

…yButton props

Add explicit props to CodeHighlighter for common configurations that
previously required passing through highlightProps:

- showLineNumber: control line number display (default: false)
- wrapLongLines: control long line wrapping (default: false)
- showCopyButton: control copy button visibility in default header (default: true)

Closes ant-design#1951
@dosubot dosubot Bot added the enhancement New feature or request label Jul 16, 2026
@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: dc68c226-a57f-4895-9fc0-8e3c2604a527

📥 Commits

Reviewing files that changed from the base of the PR and between 1a1af77 and 1c8f9eb.

📒 Files selected for processing (6)
  • packages/x/components/code-highlighter/CodeHighlighter.tsx
  • packages/x/components/code-highlighter/__tests__/index.test.tsx
  • packages/x/components/code-highlighter/demo/configurations.tsx
  • packages/x/components/code-highlighter/index.en-US.md
  • packages/x/components/code-highlighter/index.zh-CN.md
  • packages/x/components/code-highlighter/interface.ts
🚧 Files skipped from review as they are similar to previous changes (5)
  • packages/x/components/code-highlighter/index.zh-CN.md
  • packages/x/components/code-highlighter/interface.ts
  • packages/x/components/code-highlighter/index.en-US.md
  • packages/x/components/code-highlighter/CodeHighlighter.tsx
  • packages/x/components/code-highlighter/tests/index.test.tsx

📝 Walkthrough

Walkthrough

CodeHighlighter 新增行号、长行换行和复制按钮配置,更新组件渲染逻辑,并补充接口定义、测试、演示页面及中英文文档。

Changes

CodeHighlighter 配置项

Layer / File(s) Summary
配置契约与渲染逻辑
packages/x/components/code-highlighter/interface.ts, packages/x/components/code-highlighter/CodeHighlighter.tsx
新增三个可选 Props 及默认值,并将其应用于高亮器和默认表头。
配置行为测试
packages/x/components/code-highlighter/__tests__/index.test.tsx
覆盖默认值、显式配置、自定义表头及多个配置组合,并调整高亮属性测试字段。
配置示例与文档
packages/x/components/code-highlighter/demo/configurations.tsx, packages/x/components/code-highlighter/index.en-US.md, packages/x/components/code-highlighter/index.zh-CN.md
新增配置演示,并补充中英文示例入口、属性说明和默认值。

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested labels: enhancement

Suggested reviewers: hylin

Poem

小兔挥耳代码亮,
行号长线排成行。
复制按钮听开关,
文档测试齐欢唱。
🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题简洁且准确概括了本次为 CodeHighlighter 增加显式配置 props 的主要改动。
Linked Issues check ✅ Passed [#1951] 代码、演示、单测和 API 文档都覆盖了显式配置项,符合验收要求。
Out of Scope Changes check ✅ Passed 改动都围绕 CodeHighlighter 的新配置、演示、测试和文档展开,未见明显无关变更。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces three new props to the CodeHighlighter component: showLineNumber, wrapLongLines, and showCopyButton, along with comprehensive test cases, a demo configuration, and updated documentation. The review feedback recommends renaming showLineNumber to the plural showLineNumbers across the codebase, interfaces, and documentation to maintain consistency with react-syntax-highlighter and industry standards. Additionally, it is suggested to strip trailing newlines from the text passed to the copy action to ensure the copied content matches the visual representation exactly.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

style = {},
highlightProps,
prismLightMode = true,
showLineNumber = false,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To maintain consistency with react-syntax-highlighter (which uses showLineNumbers) and other industry-standard code editors/highlighters (like Monaco Editor, CodeMirror, and Prism.js), it is highly recommended to use the plural form showLineNumbers instead of the singular showLineNumber.

Suggested change
showLineNumber = false,
showLineNumbers = false,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{lang}
</span>
<Actions.Copy text={children} />
{showCopyButton && <Actions.Copy text={children} />}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To ensure that the copied text matches exactly what is rendered visually, we should strip the trailing newline from the text passed to Actions.Copy, just like we do for the rendered code element (children.replace(/\n$/, '')).

Suggested change
{showCopyButton && <Actions.Copy text={children} />}
{showCopyButton && <Actions.Copy text={children.replace(/\n$/, '')} />}

<Highlighter
language={lang}
wrapLines={true}
showLineNumbers={showLineNumber}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Update the prop mapping to use the renamed showLineNumbers prop.

Suggested change
showLineNumbers={showLineNumber}
showLineNumbers={showLineNumbers}

* @descEN Whether to show line numbers
* @default false
*/
showLineNumber?: boolean;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Rename showLineNumber to showLineNumbers to match the plural naming convention used in react-syntax-highlighter and other standard editors.

Suggested change
showLineNumber?: boolean;
showLineNumbers?: boolean;

| classNames | Style class names | `string` | - |
| highlightProps | Code highlighting configuration | [`highlightProps`](https://github.com/react-syntax-highlighter/react-syntax-highlighter?tab=readme-ov-file#props) | - |
| prismLightMode | Whether to use Prism light mode to automatically load language support based on lang prop for smaller bundle size | `boolean` | `true` |
| showLineNumber | Whether to show line numbers | `boolean` | `false` |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Rename showLineNumber to showLineNumbers in the documentation to match the updated prop name.

Suggested change
| showLineNumber | Whether to show line numbers | `boolean` | `false` |
| showLineNumbers | Whether to show line numbers | `boolean` | `false` |

| header | 头部内容,为 `false` 时不显示头部 | `React.ReactNode \| (() => React.ReactNode \| false) \| false` | - |
| highlightProps | 代码高亮配置,透传给 react-syntax-highlighter | [`SyntaxHighlighterProps`](https://github.com/react-syntax-highlighter/react-syntax-highlighter?tab=readme-ov-file#props) | - |
| prismLightMode | 是否使用 Prism 轻量模式,根据 `lang` 自动按需加载语言支持以减少打包体积 | `boolean` | `true` |
| showLineNumber | 是否显示行号 | `boolean` | `false` |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Rename showLineNumber to showLineNumbers in the documentation to match the updated prop name.

Suggested change
| showLineNumber | 是否显示行号 | `boolean` | `false` |
| showLineNumbers | 是否显示行号 | `boolean` | `false` |

@wryyyds7

Copy link
Copy Markdown
Author

#1951

@Deniwn22 Deniwn22 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The happy path is clean. What happens if the third-party API returns 429 here? Might be worth handling that case explicitly.

… fix copy text trailing newline

- Rename showLineNumber to showLineNumbers to match react-syntax-highlighter
  naming convention (address review feedback)
- Strip trailing newline from Actions.Copy text to match rendered content
  (address review feedback)
@wryyyds7

Copy link
Copy Markdown
Author

Thanks for the review! This PR doesn't involve any third-party API calls — the only async operation here is the dynamic import() for lazy-loading Prism language modules, which is a local bundler-level operation and wouldn't return a 429. So I think we're good on that front.

The happy path is clean. What happens if the third-party API returns 429 here? Might be worth handling that case explicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Good First Issue] CodeHighlighter 支持更灵活的配置

3 participants