Skip to content

code

格式化行内代码或围栏代码块。

语法

knap
{{ value | code }}
{{ value | code:"typescript" }}

用法

  • 可选的语言参数会创建围栏代码块,并把该语言加到起始围栏之后。
  • 多行数据会自动变成围栏块。需要时反引号分隔符会加长,以安全容纳反引号。
  • 对于数组和对象(包括来自其他过滤器、已序列化的集合),其中的字符串取值会被递归格式化,而键和非字符串取值保持不变。

示例

行内

Data
json
{
  "source": "const answer = 42"
}
Template
knap
{{ source | code }}
Output
md
`const answer = 42`

语言块

Data
json
{
  "source": "const answer = 42"
}
Template
knap
{{ source | code:"typescript" }}
Output
md
```typescript
const answer = 42
```