diff --git a/help/FEATURES.md b/help/FEATURES.md index 3d0704cb..fa4ec7e0 100644 --- a/help/FEATURES.md +++ b/help/FEATURES.md @@ -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 diff --git a/help/FEATURES_ZH-CN.md b/help/FEATURES_ZH-CN.md index 3d3363f4..638fe133 100644 --- a/help/FEATURES_ZH-CN.md +++ b/help/FEATURES_ZH-CN.md @@ -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` | 当前文档文件名 diff --git a/src/renderer/plugins/markdown-macro.ts b/src/renderer/plugins/markdown-macro.ts index a3ddcf3c..360cc9a8 100644 --- a/src/renderer/plugins/markdown-macro.ts +++ b/src/renderer/plugins/markdown-macro.ts @@ -31,6 +31,17 @@ const globalVars = { $noop: noop, } +function createSeq () { + const counters: Record = {} + 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 = { ...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')