瑚琏 Hulianv0.93.5413 组件0 组件
  • 开始
  • 组件
  • 区块
  • 页面
  • 模版
  • 更新
中文EN
  • 组件组件
  • 主题主题

文本

  • 文本
  • 标题
  • 分节标题
  • 排版容器
  • Markdown 渲染
  • 数学公式排版
  • 极光文字
  • 斜线投影文字
  • 高光文字
  • 渐变文字
  • 轮换词
  • 打字机
  • 星闪文字
  • 切字进场
  • 模糊解析
  • 翻面标题
  • 揭示扫光
  • 乱码解码
  • 故障撕裂
  • 环形文字
  • 滚动显影
  • 真实焦点
  • ASCII 字符画
  • 弧形跑马灯
  • 文字散落
  • 噪点模糊标题
  • 乱码悬停文字
  • 滚动浮现标题
  • 滚动跑马灯
  • 洗牌解密文字
  • 光标拖尾文字
  • 逐字符"压感"标题
  • 邻近可变字体

代码

  • 行内代码
  • 代码块
  • 代码片段
  • 代码对比
  • 按键

CodeDiff

code-diff

按行展示代码增删,可单栏也可左右双栏对照

用法

基础用法(unified 单栏)

传入 oldText / newText,自动逐行 diff,新增绿 / 删除红。

greet.ts+3−3
1-export function greet(name) {
2- const msg = "hi " + name;
3- console.log(msg);
1+export function greet(name, lang = "en") {
2+ const prefix = lang === "zh" ? "你好 " : "hi ";
3+ const msg = prefix + name;
44 return msg;
55 }
tsx
<CodeDiff filename="greet.ts" oldText={oldText} newText={newText} />

双栏对照(split)

mode="split" 左旧右新并排展示,便于大段改动对照。

greet.ts+3−3
1-export function greet(name) {
2- const msg = "hi " + name;
3- console.log(msg);
1+export function greet(name, lang = "en") {
2+ const prefix = lang === "zh" ? "你好 " : "hi ";
3+ const msg = prefix + name;
4 return msg;
4 return msg;
5 }
5 }
tsx
<CodeDiff mode="split" filename="greet.ts" oldText={oldText} newText={newText} />

纯新增(新建文件)

oldText 为空串时,每行都判为新增。

hello.txt+3−0
1+line 1
2+line 2
3+line 3
tsx
<CodeDiff filename="hello.txt" oldText="" newText={"line 1\nline 2\nline 3"} />

隐藏行号

showLineNumbers={false} 去掉两侧行号槽,节省横向空间。

greet.ts+3−3
-export function greet(name) {
- const msg = "hi " + name;
- console.log(msg);
+export function greet(name, lang = "en") {
+ const prefix = lang === "zh" ? "你好 " : "hi ";
+ const msg = prefix + name;
return msg;
}
tsx
<CodeDiff filename="greet.ts" oldText={oldText} newText={newText} showLineNumbers={false} />

何时用

展示两段文本的行级差异(PR 评审、agent 改动回放、配置变更对比),增绿删红、双行号、可挂行级批注。只展示静态代码用 CodeBlock;单行命令用 Snippet。diffLines/diffStat 是纯函数,可单独用于自定义渲染或统计。

导入

ts
import { CodeDiff, diffLines, diffStat } from "@hulianui/ui"

Props

名称类型默认说明
oldText*string-旧文本
newText*string-新文本
mode"unified" | "split""unified"unified 单栏 / split 双栏对照
filenamestring-头部文件名条;省略则不渲染头部
showLineNumbersbooleantrue显示行号槽
annotationsCodeDiffAnnotation[]-行锚定批注:在匹配行渲染 gutter 标记 + 行下方插入整宽 content 槽(仅 unified 模式插 content)
classNamestring-容器类名

CodeDiffAnnotation:{ side?: "old"|"new"(默认 "new"); line: number(1-based); gutter?: ReactNode; content?: ReactNode }。

禁忌 / 坑

  • annotations 的 content 槽仅在 unified 模式插入;split 模式只渲染 gutter 标记。需要在差异行下方挂 CodeReviewThread 等内容时用 mode="unified"。
  • annotations[].line 是 1-based 行号,按 side 对应旧文件或新文件,别和数组下标混淆。

相关

Code · CodeBlock · Snippet · Kbd · Text · Heading

Playground

greet.ts+3−3
1-export function greet(name) {
2- const msg = "hi " + name;
3- console.log(msg);
1+export function greet(name, lang = "en") {
2+ const prefix = lang === "zh" ? "你好 " : "hi ";
3+ const msg = prefix + name;
44 return msg;
55 }
<CodeDiff filename="greet.ts" oldText={old} newText={next} />
上一个代码片段排版下一个按键

本页

  • 用法
  • 文档
    • 何时用
    • 导入
    • Props
    • 禁忌 / 坑
    • 相关
  • Playground