feat(macro): add $seq method

This commit is contained in:
purocean
2023-07-27 20:14:03 +08:00
parent f9bcca8b77
commit fcbabc8f8f
3 changed files with 16 additions and 0 deletions
+2
View File
@@ -641,6 +641,7 @@ If the expression needs to contain [\= or =\], please enter `[\=` or `=\]` to es
- application version: [= $ctx.version =]
- current document name: [= $doc.basename =]
- current time: [= $ctx.lib.dayjs().format('YYYY-MM-DD HH:mm') =]
- sequence: [= $seq`Figure-` =] | [= $seq`Figure-` =] | [= $seq`Figure-` =] | [= $seq`Table-` =] | [= $seq`Table-` =]
- qualifier escape: [= '[\= =\]' =]
- Arithmetic: [= (1 + 2) / 2 =]
- reference file (support 3 levels of nesting, you can use Front Matter variable defined in the target document):
@@ -680,6 +681,7 @@ variable name | type | description
`$afterMacro` | `(fn: (src: string) => string) => Result` | Define a macro-replaced callback function that can be used for further processing of the replaced text.
`$noop` | `() => Result` | no operation, Used for holding text space
`$doc` | `object` | Current document information
`$seq` | `(label: string) => Result` | Sequence
`$doc.basename` | `string` | File name of current document (no suffix)
`$doc.name` | `string` | File name of current document
`$doc.path` | `string` | Current document path
+2
View File
@@ -641,6 +641,7 @@ Front Matter 中的 `define` 字段可以定义一些文本替换映射。支持
- 应用版本:[= $ctx.version =]
- 当前文档名: [= $doc.basename =]
- 当前时间: [= $ctx.lib.dayjs().format('YYYY-MM-DD HH:mm') =]
- 计数器: [= $seq`图-` =] | [= $seq`图-` =] | [= $seq`图-` =] | [= $seq`表-` =] | [= $seq`表-` =]
- 限定符转义: [= '[\= =\]' =]
- 四则运算: [= (1 + 2) / 2 =]
- 引用文件(支持最多嵌套 3 层,可使用目标文档中定义的 Front Matter 变量)
@@ -679,6 +680,7 @@ Front Matter 中的 `define` 字段可以定义一些文本替换映射。支持
`$export` | `(key: string, val: any) => Result` | 定义一个本文档可以使用的变量
`$noop` | `() => Result` | 无操作函数,可用于文本占位使用
`$afterMacro` | `(fn: (src: string) => string) => Result` | 定义一个宏替换后的回调函数,可用于对替换后的文本进行进一步处理。
`$seq` | `(label: string) => Result` | 文档内部计数器
`$doc` | `object` | 当前文档信息
`$doc.basename` | `string` | 当前文档文件名(无后缀)
`$doc.name` | `string` | 当前文档文件名
+12
View File
@@ -31,6 +31,17 @@ const globalVars = {
$noop: noop,
}
function createSeq () {
const counters: Record<string, number> = {}
return function (str = '') {
if (!counters[str]) {
counters[str] = 0
}
counters[str]++
return `${str}${counters[str]}`
}
}
function lineCount (str: string) {
let s = 1
const len = str.length
@@ -309,6 +320,7 @@ export default {
const vars: Record<string, any> = { ...globalVars, ...env.attributes }
vars.$include = include.bind(null, { ...options, belongDoc: file, count: 0, vars })
vars.$seq = createSeq()
vars.$doc = {
basename: file.name ? file.name.substring(0, file.name.lastIndexOf('.')) : '',
...ctx.lib.lodash.pick(env.file, 'name', 'repo', 'path', 'content', 'status')