1342 字
7 分钟
【博客指南】Expressive-Code示例
写在前面:本文将展示基于 Expressive Code 构建的增强型代码块显示效果。以下示例涵盖了从基础高亮到高级交互的各类场景,更多详细参数配置可参考官方文档。
核心功能演示
1. 语法高亮
常规语法高亮
支持主流编程语言的自动着色。
console.log('This code is syntax highlighted!')渲染 ANSI 转义序列
可以直接渲染终端输出中的 ANSI 颜色代码,非常适合展示 CLI 工具的输出结果。
ANSI colors:- Regular: Red Green Yellow Blue Magenta Cyan- Bold: Red Green Yellow Blue Magenta Cyan- Dimmed: Red Green Yellow Blue Magenta Cyan
256 colors (showing colors 160-177):160 161 162 163 164 165166 167 168 169 170 171172 173 174 175 176 177
Full RGB colors:ForestGreen - RGB(34, 139, 34)
Text formatting: Bold Dimmed Italic Underline2. 终端样式
代码编辑器样式
模拟 IDE 窗口,支持显示文件名或完整路径。
console.log('Title attribute example')<div>File name comment example</div>终端窗口样式
模拟命令行终端外观。
echo "This terminal frame has no title"Write-Output "This one has a title!"自定义窗口类型
你可以强制指定使用某种外框,或者完全移除外框。
echo "Look ma, no frame! (无边框模式)"# 强制使用代码编辑器样式,而非默认的终端样式function Watch-Tail { Get-Content -Tail 20 -Wait $args }New-Alias tail Watch-Tail3. 文本标记
标记整行与多行
通过行号或范围(如 7-8)来高亮特定代码行。
// Line 1 - 通过行号 {1} 选中// Line 2// Line 3// Line 4 - 通过行号 {4} 选中// Line 5// Line 6// Line 7 - 通过范围 {7-8} 选中// Line 8 - 通过范围 {7-8} 选中指定标记类型(高亮、新增、删除)
除了默认的高亮,还支持 ins(新增/绿色)和 del(删除/红色)样式。
function demo() { console.log('这一行被标记为删除 (del)') // 下面两行被标记为新增 (ins) console.log('this is the second inserted line')
return '这一行使用默认的中性标记 (mark)'}带标签的行标记
可以在高亮行的右侧添加文本标签,用于解释代码逻辑。
<button role="button" {...props} value={value} className={buttonClassName} disabled={disabled} active={active}> {children && !active && (typeof children === 'string' ? <span>{children}</span> : children)}</button>长标签文本布局
当标签文本较长时,会自动调整布局以保持美观。
<button role="button" {...props}
value={value} className={buttonClassName}
disabled={disabled} active={active}>
{children && !active && (typeof children === 'string' ? <span>{children}</span> : children)}</button>Diff 语法支持
直接支持标准的 diff 格式。
这一行将被标记为新增这一行将被标记为删除这是一个普通行--- a/README.md+++ b/README.md@@ -1,3 +1,4 @@+this is an actual diff file-all contents will remain unmodified no whitespace will be removed either混合使用 Diff 与语法高亮
你可以在保留 JavaScript 等语言高亮的同时,使用 diff 标记。
function thisIsJavaScript() { // 整个代码块将被高亮显示为 JavaScript // 同时我们仍然可以使用 diff 符号 console.log('Old code to be removed') console.log('New and shiny code!')}行内文本高亮
不标记整行,而是通过字符串匹配高亮行内的特定文本。
function demo() { // Mark any given text inside lines return 'Multiple matches of the given text are supported';}正则表达式匹配
支持使用正则进行灵活的文本匹配。
console.log('The words yes and yep will be marked.')转义处理
在正则模式中匹配正斜杠。
echo "Test" > /home/test.txt自定义行内标记样式
行内文本同样支持 ins 和 del 样式。
function demo() { console.log('These are inserted and deleted marker types'); // return 语句使用默认的标记类型 return true;}4. 自动换行
开启与关闭
控制长代码行是否自动换行。
// 开启自动换行 (wrap)function getLongString() { return 'This is a very long string that will most probably not fit into the available space unless the container is extremely wide'}// 关闭自动换行 (wrap=false)function getLongString() { return 'This is a very long string that will most probably not fit into the available space unless the container is extremely wide'}智能缩进保留
开启换行时,是否保留第二行的缩进对齐。
// 开启缩进保留 (默认行为)function getLongString() { return 'This is a very long string that will most probably not fit into the available space unless the container is extremely wide'}// 关闭缩进保留 (文字将顶格换行)function getLongString() { return 'This is a very long string that will most probably not fit into the available space unless the container is extremely wide'}插件功能演示
1. 代码折叠
支持将不重要的样板代码(Boilerplate)折叠隐藏,点击即可展开。
5 collapsed lines
// 这部分样板代码默认会被折叠import { someBoilerplateEngine } from '@example/some-boilerplate'import { evenMoreBoilerplate } from '@example/even-more-boilerplate'
const engine = someBoilerplateEngine(evenMoreBoilerplate())
// 这部分代码默认可见engine.doSomething(1, 2, 3, calcFn)
function calcFn() { // 这里也可以设置折叠区域3 collapsed lines
const a = 1 const b = 2 const c = a + b
// 这部分保持可见 console.log(`Calculation result: ${a} + ${b} = ${c}`) return c}
4 collapsed lines
// 结尾的代码再次折叠engine.closeConnection()engine.freeMemory()engine.shutdown({ reason: 'End of example boilerplate code' })2. 代码行号
控制行号显示
// 显式开启行号console.log('Greetings from line 2!')console.log('I am on line 3')// 显式禁用行号console.log('Hello?')console.log('Sorry, do you know what line I am on?')自定义起始行号
在展示代码片段(而非完整文件)时非常有用。
console.log('Greetings from line 5!')console.log('I am on line 6') 【博客指南】Expressive-Code示例
https://xingguang641.com/posts/blog/blog-guide/expressive-code/