1058 字
5 分钟
表现力代码示例

本文将展示在 Markdown 中使用 表现力代码 (Expressive Code) 渲染代码块的效果。以下示例均基于官方文档,如需深入了解核心配置,请参阅官方指南。

表现力代码 (Expressive Code)#

语法高亮 (Syntax Highlighting)#

语法高亮官方文档

常规语法高亮#

console.log('This code is syntax highlighted! // 此代码已启用语法高亮!')

渲染 ANSI 转义序列 (ANSI Escape Sequences)#

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)#

Editor & Terminal Frames

代码编辑器边框 (Code Editor Frames)#

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

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

终端边框 (Terminal Frames)#

Terminal window
echo "This terminal frame has no title"

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

覆盖边框类型 (Overriding Frame Types)#

echo "Look ma, no frame!"

PowerShell Profile.ps1
# Without overriding, this would be a terminal frame
function Watch-Tail { Get-Content -Tail 20 -Wait $args }
New-Alias tail Watch-Tail

文本与行标记 (Text & Line Markers)#

Text & Line Markers

标记整行与行范围 (Marking Full Lines & Line Ranges)#

// Line 1 - targeted by line number
// Line 2
// Line 3
// Line 4 - targeted by line number
// Line 5
// Line 6
// Line 7 - targeted by range "7-8"
// Line 8 - targeted by range "7-8"

选择行标记类型 (Selecting Line Marker Types) (mark, ins, del)#

line-markers.js
function demo() {
console.log('this line is marked as deleted')
// This line and the next one are marked as inserted
console.log('this is the second inserted line')
return 'this line uses the neutral default marker type'
}

为行标记添加标签 (Adding Labels to Line Markers)#

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>

单独行为长标签占位 (Adding Long Labels on Their Own Lines)#

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 语法 (Using Diff-like Syntax)#

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 语法 (Combining Syntax Highlighting with Diff-like Syntax)#

function thisIsJavaScript() {
// This entire block gets highlighted as JavaScript,
// and we can still add diff markers to it!
console.log('Old code to be removed')
console.log('New and shiny code!')
}

标记行内的特定文本 (Marking Individual Text Inside Lines)#

function demo() {
// Mark any given text inside lines
return 'Multiple matches of the given text are supported';
}

正则表达式 (Regular Expressions)#

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

转义正斜杠 (Escaping Forward Slashes)#

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

选择内联标记类型 (Selecting Inline Marker Types) (mark, ins, del)#

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

自动换行 (Word Wrap)#

Word Wrap

按代码块配置自动换行 (Configuring Word Wrap per Block)#

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

配置换行缩进 (Configuring Indentation of Wrapped Lines)#

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

可折叠区块 (Collapsible Sections)#

Collapsible Sections

5 collapsed lines
// All this boilerplate setup code will be collapsed
// 该区间内所有样板 (Boilerplate) 初始代码将被折叠
import { someBoilerplateEngine } from '@example/some-boilerplate'
import { evenMoreBoilerplate } from '@example/even-more-boilerplate'
const engine = someBoilerplateEngine(evenMoreBoilerplate())
// This part of the code will be visible by default
// 此部分核心逻辑代码默认保持展开可见
engine.doSomething(1, 2, 3, calcFn)
3 collapsed lines
function calcFn() {
// You can have multiple collapsed sections
// 单个代码块内允许声明多个折叠区间
const a = 1
const b = 2
const c = a + b
// This will remain visible
// 此处调试日志将保持可见
4 collapsed lines
console.log(`Calculation result: ${a} + ${b} = ${c}`)
return c
}
// All this code until the end of the block will be collapsed again
// 直至代码块结束的收尾逻辑将再次被折叠
engine.closeConnection()
engine.freeMemory()
engine.shutdown({ reason: 'End of example boilerplate code' })

行号 (Line Numbers)#

Line Numbers

按代码块显示行号 (Displaying Line Numbers per Block)#

// This code block will show line numbers
console.log('Greetings from line 2!')
console.log('I am on line 3')

// Line numbers are disabled for this block
console.log('Hello?')
console.log('Sorry, do you know what line I am on?')

更改起始行号 (Changing the Starting Line Number)#

console.log('Greetings from line 5!')
console.log('I am on line 6')
表现力代码示例
https://liaoyueyue.cn/posts/引导/表现力代码示例/
作者
lyyovo
发布于
2024-04-10
许可协议
CC BY-NC-SA 4.0