/

代码

你可以使用标准 Markdown 规范创建代码块:使用 4 个空格或 1 个制表符缩进,或者使用三个反引号或波浪号。

示例 1

```javascript
function greet(name) {
    return `Hello, ${name}!`;
}
```
function greet(name) {
    return `Hello, ${name}!`;
}

Quarkdown 还提供了一个更强大的 .code 文档 ↗ 块函数。

示例 2

.code lang:{javascript}
    function greet(name) {
        return `Hello, ${name}!`;
    }
function greet(name) {
    return `Hello, ${name}!`;
}

.code 与标准代码块

内容处理

标准代码块会原样渲染其内容,不做任何处理。而 .code 函数接受任意 Quarkdown 字符串作为其主体参数,这意味着你可以在将其输出显示为代码之前执行函数。当将 .code.read 结合以从文件加载代码片段时,这一点尤其有用:

示例 3

.code
    .read {assets/point.ts}
export class Point {
    x: number;
    y: number;

    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }
}

语言指定

标准围栏代码块会在起始分隔符之后指定语言,例如 ```markdown

.code 函数通过可选的 lang 参数指定语言,例如 .code {markdown}.code lang:{markdown}。若未指定,则会尝试自动检测。

示例 4

.code lang:{typescript}
    .read {assets/point.ts}
export class Point {
    x: number;
    y: number;

    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }
}

行号

标准代码块默认始终显示行号。而 .code 函数允许你使用可选的 linenumbers Boolean 参数来切换行号显示,该参数默认为 yes(等价于 true)。

示例 5

.code linenumbers:{no}
    .read {assets/point.ts}
export class Point {
    x: number;
    y: number;

    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }
}

聚焦行

.code 函数允许你聚焦到一组Range 行,从 1 开始。此功能需要启用行号。

示例 6

.code focus:{5..8}
    .read {assets/point.ts}
export class Point {
    x: number;
    y: number;

    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }
}

行内代码

正如 .code 是三个反引号(```)的动态替代方案,.codespan {text} 是行内反引号(`text`)的动态替代方案。这允许在其内容中调用函数。