1096 字
5 分钟
如何正确使用 Expressive Code

这里我们将展示利用 Expressive Code 渲染代码块,以下示例基于官方提供的示例修改而成。

Expressive Code#

语法高亮#

语法高亮

常规语法高亮#

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

渲染 ANSI 转义字符#

Terminal window
ANSI 颜色:
- 常规: 红色 绿色 黄色 蓝色 品红 青色
- 加粗: 红色 绿色 黄色 蓝色 品红 青色
- 变暗: 红色 绿色 黄色 蓝色 品红 青色
256 色 (显示的颜色: 160-177):
160 161 162 163 164 165
166 167 168 169 170 171
172 173 174 175 176 177
完整的 RGB 颜色:
Forest 绿色 - RGB(34, 139, 34)
文字格式: 加粗 变暗 斜体 下划线

编辑器 & 终端边框#

编辑器 & 终端边框

Code 编辑器边框#

my-test-file.js
console.log('这是编辑器标题示例')

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

终端边框#

Terminal window
echo "这是无标题的终端边框示例"

PowerShell terminal example
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" 确定

选择行数标记种类 (标记, 插入, 删除)#

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 '支持多次给定文字的匹配';
}

正则表达式#

console.log('词汇是的是哦将被匹配')

斜杠的转译#

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

选择行内标记种类#

function demo() {
console.log('这些是被插入删除的标记种类');
// 返回声明使用默认的标记格式
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'
}

// 关闭换行的示例
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'
}

可折叠段落#

可折叠段落

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('Hello?')
console.log('Sorry, do you know what line I am on?')

修改从第几行开始#

console.log('这是第 5 行!')
console.log('这是第 6 行')
如何正确使用 Expressive Code
https://www.meaqua.work/posts/expressive-code/
作者
Meaqua Diary
发布于
2024-04-10
许可协议
CC BY-NC-SA 4.0