1036 字
5 分钟
Expressive Code 代码块示例

这里展示代码块用 Expressive Code 渲染的效果。示例基于官方文档,想看更多细节可以去那边翻。

Expressive Code#

语法高亮#

语法高亮

普通语法高亮#

console.log('这段代码有语法高亮了!')

渲染 ANSI 转义序列#

Terminal window
ANSI 颜色:
- 常规: [31m红[0m [32m绿[0m [33m黄[0m [34m蓝[0m [35m品红[0m [36m青[0m
- 加粗: [1;31m红[0m [1;32m绿[0m [1;33m黄[0m [1;34m蓝[0m [1;35m品红[0m [1;36m青[0m
- 变暗: [2;31m红[0m [2;32m绿[0m [2;33m黄[0m [2;34m蓝[0m [2;35m品红[0m [2;36m青[0m
256 色(展示 160-177):
[38;5;160m160 [38;5;161m161 [38;5;162m162 [38;5;163m163 [38;5;164m164 [38;5;165m165[0m
[38;5;166m166 [38;5;167m167 [38;5;168m168 [38;5;169m169 [38;5;170m170 [38;5;171m171[0m
[38;5;172m172 [38;5;173m173 [38;5;174m174 [38;5;175m175 [38;5;176m176 [38;5;177m177[0m
完整 RGB 颜色:
[38;2;34;139;34mForestGreen - RGB(34, 139, 34)[0m
文字格式: [1m粗体[0m [2m变暗[0m [3m斜体[0m [4m下划线[0m

编辑器与终端外框#

编辑器与终端外框

代码编辑器外框#

my-test-file.js
console.log('这是 title 属性的示例')

src/content/index.html
<div>这是文件名注释的示例</div>

终端外框#

Terminal window
echo "这个终端外框没有标题"

PowerShell 终端示例
Write-Output "这个有标题!"

覆盖外框类型#

echo "看,没有外框!"

PowerShell Profile.ps1
# 不覆盖的话,这本来会渲染成终端外框
function Watch-Tail { Get-Content -Tail 20 -Wait $args }
New-Alias tail Watch-Tail

文本与行标记#

文本与行标记

标记整行和行范围#

// 第 1 行 —— 按行号标记
// 第 2 行
// 第 3 行
// 第 4 行 —— 按行号标记
// 第 5 行
// 第 6 行
// 第 7 行 —— 按范围 "7-8" 标记
// 第 8 行 —— 按范围 "7-8" 标记

选择行标记类型(mark、ins、del)#

line-markers.js
function demo() {
console.log('这一行标记为删除')
// 这一行和下一行标记为新增
console.log('这是第二行新增内容')
return '这一行用中性的默认标记'
}

给行标记加标签#

labeled-line-markers.jsx
<button
role="button"
{...props}
value={value}
className={buttonClassName}
disabled={disabled}
active={active}
>
{children &&
!active &&
(typeof children === 'string' ? <span>{children}</span> : children)}
</button>

长标签单独占一行#

labeled-line-markers.jsx
<button
role="button"
{...props}
value={value}
className={buttonClassName}
disabled={disabled}
active={active}
>
{children &&
!active &&
(typeof children === 'string' ? <span>{children}</span> : children)}
</button>

使用 diff 风格语法#

这一行会标记为新增
这一行会标记为删除
这是一行普通内容

--- a/README.md
+++ b/README.md
@@ -1,3 +1,4 @@
+这是一个真实的 diff 文件
+-所有内容保持不动
+ 空白也不会被删掉

语法高亮和 diff 语法一起用#

function thisIsJavaScript() {
// 整块按 JavaScript 高亮,
// 同时还能加 diff 标记!
console.log('要被删掉的旧代码')
console.log('崭新的新代码!')
}

标记行内的某段文字#

function demo() {
// 标记行内任意指定的文字
return 'Multiple matches of the given text are supported';
}

正则表达式#

console.log('The words yes and yep will be marked.')

转义正斜杠#

Terminal window
echo "Test" > /home/test.txt

选择行内标记类型(mark、ins、del)#

function demo() {
console.log('These are inserted and deleted marker types');
// return 语句用默认标记类型
return true;
}

自动换行#

自动换行

按代码块配置换行#

// 开启换行的例子
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'
}

配置换行后的缩进#

// preserveIndent 的例子(默认开启)
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'
}

// preserveIndent=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'
}

可折叠区块#

可折叠区块

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' })

行号#

行号

按代码块显示行号#

// 这个代码块会显示行号
console.log('来自第 2 行的问候!')
console.log('我在第 3 行')

// 这个块关闭了行号
console.log('喂?')
console.log('不好意思,你知道我在第几行吗?')

改变起始行号#

console.log('来自第 5 行的问候!')
console.log('我在第 6 行')
Expressive Code 代码块示例
https://xitide.top/posts/expressive-code/
作者
Furina
发布于
2024-04-10
许可协议
CC BY-NC-SA 4.0
成为你的避风港
妙脆角进行曲