710 字
5 分钟
Expressive Code 示例

下面来看看使用 Expressive Code 时,代码块会呈现出怎样的效果。文中的示例取自官方文档,需要更多细节可以查阅该文档。

Expressive Code#

语法高亮#

Syntax Highlighting

常规语法高亮#

console.log('This code is syntax highlighted!')

渲染 ANSI 转义序列#

Terminal window
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 165
166 167 168 169 170 171
172 173 174 175 176 177
Full RGB colors:
ForestGreen - RGB(34, 139, 34)
Text formatting: Bold Dimmed Italic Underline

编辑器与终端外框#

Editor & Terminal Frames

代码编辑器外框#

my-test-file.js
console.log('Title attribute example')

src/content/index.html
<div>File name comment example</div>

终端外框#

Terminal window
echo "This terminal frame has no title"

PowerShell terminal example
Write-Output "This one has a title!"

覆盖外框类型#

echo "Look ma, no frame!"

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

文本与行标记#

Text & Line Markers

标记整行与行范围#

// 第 1 行 —— 由行号定位
// Line 2
// Line 3
// 第 4 行 —— 由行号定位
// Line 5
// Line 6
// 第 7 行 —— 由范围 "7-8" 定位
// 第 8 行 —— 由范围 "7-8" 定位

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

line-markers.js
function demo() {
console.log('this line is marked as deleted')
// 这一行与下一行被标记为新增
console.log('this is the second inserted line')
return 'this line uses the neutral default marker type'
}

为行标记添加标签#

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 语法#

this line will be marked as inserted
this line will be marked as deleted
this is a regular line

--- 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 语法#

function thisIsJavaScript() {
// 整个代码块按 JavaScript 高亮,
// 同时依然可以为它添加 diff 标记!
console.log('Old code to be removed')
console.log('New and shiny code!')
}

标记行内的个别文本#

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

正则表达式#

console.log('单词 yes 与 yep 会被标记出来。')

转义正斜杠#

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

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

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

自动换行#

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

配置折行后的缩进#

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

可折叠区块#

Collapsible Sections

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

行号#

Line Numbers

为单个代码块显示行号#

// 这个代码块会显示行号
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://www.dafeiyu.site/posts/expressive-code/
作者
蓝色大肥鱼
发布于
2024-04-10
许可协议
CC BY-NC-SA 4.0

分享文章

生成精美分享图或复制链接,与更多人分享本文。

继续阅读

沿着主题读

基于共同的标签与分类

换条路线

从其他文章中稳定抽取

评论

0 条评论

最后更新于 ,距今已过 932 天

部分内容可能已过时