1305 字
7 分钟
Expressive Code 代码展示示例
2024-04-10
无标签

在这里,我们将探索使用 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 颜色:
森林绿 - RGB(34, 139, 34)
文本格式:粗体 暗淡 斜体 下划线
格式是: [ 配置 内容  [ 0m
实际使用的时候将上面的文本连在一起

编辑器与终端框架#

编辑器与终端框架

代码编辑器框架#

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

文本与行标记#

文本与行标记

标记整行和行范围js {1, 4, 7-8}#

// 第 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 '这行使用中性的默认标记类型'
}

为行标记添加标签jsx {“1”:5} del={“2”:7-8} ins={“3”:10-12}#

// 带标签的行标记.jsx
<button
role="button"
{...props}
value={value}
className={buttonClassName}
disabled={disabled}
active={active}
>
{children &&
!active &&
(typeof children === 'string' ? <span>{children}</span> : children)}
</button>

在单独行上添加长标签#

jsx {"1. Provide the value prop here:":5-6} del={"2. Remove the disabled and active states:":8-10} ins={"3. Add this to render the children inside the button:":12-15}

// 带标签的行标记.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() {
// 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.')

转义正斜杠#

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

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

function demo() {
console.log('These are inserted and deleted marker types');
// The return statement uses the default marker type
return true;
}

自动换行#

自动换行

按代码块配置自动换行#

// Example with 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'
// 这是一个非常长的字符串,除非容器极其宽,否则很可能无法适应可用空间(启用)
}

// Example with 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'
// 这是一个非常长的字符串,除非容器极其宽,否则很可能无法适应可用空间(禁用)
}

配置换行行的缩进#

// Example with preserveIndent (enabled by default)
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'
// 这是一个非常长的字符串,除非容器极其宽,否则很可能无法适应可用空间(启用)
}

// Example with 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(`计算结果: ${a} + ${b} = ${c}`)
return c
}
4 collapsed lines
// 从这到代码块末尾的所有代码将再次被折叠
engine.closeConnection()
engine.freeMemory()
engine.shutdown({ reason: '示例样板代码结束' })

行号#

行号

按代码块显示行号#

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

// 这个代码块禁用了行号
console.log('你好?')
console.log('抱歉,你知道我在第几行吗?')

更改起始行号#

console.log('来自第 5 行的问候!')
console.log('我在第 6 行')
Expressive Code 代码展示示例
https://derckhanna.github.io/ForestBlog-Source/posts/expressive-code/
作者
Snowy Owl hydrogen flare
发布于
2024-04-10
许可协议
CC BY-NC-SA 4.0