1054 字
5 分钟
Expressive Code 示例
在这里,我们将探索使用 Expressive Code 的代码块效果。所提供的示例基于官方文档,你可以参考官方文档获取更多详情。
Expressive Code
语法高亮
常规语法高亮
console.log('这段代码带有语法高亮!')渲染 ANSI 转义序列
ANSI colors:- Regular: [31mRed[0m [32mGreen[0m [33mYellow[0m [34mBlue[0m [35mMagenta[0m [36mCyan[0m- Bold: [1;31mRed[0m [1;32mGreen[0m [1;33mYellow[0m [1;34mBlue[0m [1;35mMagenta[0m [1;36mCyan[0m- Dimmed: [2;31mRed[0m [2;32mGreen[0m [2;33mYellow[0m [2;34mBlue[0m [2;35mMagenta[0m [2;36mCyan[0m
256 colors (showing colors 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
Full RGB colors:[38;2;34;139;34mForestGreen - RGB(34, 139, 34)[0m
Text formatting: [1mBold[0m [2mDimmed[0m [3mItalic[0m [4mUnderline[0m编辑器与终端框架
代码编辑器框架
console.log('标题属性示例')<div>文件名注释示例</div>终端框架
echo "这个终端框架没有标题"Write-Output "这个有标题!"覆盖框架类型
echo "看,没有框架!"# 如果不覆盖,这会是一个终端框架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)
function demo() { console.log('这行被标记为已删除') // 这行和下一行被标记为已插入 console.log('这是第二行已插入内容')
return '这行使用中性的默认标记类型'}为行标记添加标签
<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 风格语法
这行将被标记为已插入这行将被标记为已删除这是普通行--- 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('单词 yes 和 yep 将被标记。')转义正斜杠
echo "Test" > /home/test.txt选择行内标记类型(mark、ins、del)
function demo() { console.log('这些是已插入和已删除的标记类型'); // 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'}配置换行后的缩进
// 保留缩进的示例 (默认启用)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(`计算结果:${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://fuwari.vercel.app/posts/expressive-code/