feat(macro): add macro definition

This commit is contained in:
purocean
2021-12-04 23:43:20 +08:00
parent 3b55d423d2
commit 13d021fdc9
5 changed files with 55 additions and 26 deletions
+25 -12
View File
@@ -2,6 +2,9 @@
headingNumber: true
enableMacro: true
customVar: Hello
define:
--APP_NAME--: Yank Note
--APP_VERSION--: '[= $ctx.version =]'
---
# Yank-Note Features Instructions
@@ -451,8 +454,17 @@ Built-in variables
variable name | type | description
---- | ----- | ---
headingNumber | boolean | whether to enable the page title serial number
enableMacro | boolean | whether to enable macro replacement
`headingNumber` | `boolean` | whether to enable the page title serial number
`enableMacro` | `boolean` | whether to enable macro replacement
`define` | `Record<string, string>` | Macro definition, string replacing
### Definition
The `define` field can define some text replacement mappings. Supports definition in another file, supports macro expressions. For details, please refer to the Front Matter at the top of this document.
- App Name: --APP_NAME--
- App Version: --APP_VERSION--
- From Another File: --TEST_DEFINE--
## Macro Replacement
@@ -513,16 +525,17 @@ Macro code can use variables defined in Front Matter, or use the following built
variable name | type | description
---- | ----- | ---
`$ctx` | object | Editor `ctx`refer to [Plug-In Development GUide](PLUGIN.md) and [Api Document](https://yn-api-doc.vercel.app/modules/context.html)
`$include` | (path: string, trim = false) => Result | Introduce other document fragment methods
`$export` | (key: string, val: any) => Result | Define a variable that can be used in this document
`$doc` | object | Current document information
`$doc.basename` | string | File name of current document (no suffix)
`$doc.name` | string | File name of current document
`$doc.path` | string | Current document path
`$doc.repo` | string | Current document repository
`$doc.content` | string | Current document content
`$doc.status` | 'loaded', 'save-failed', 'saved' | Current document status
`$ctx` | `object` | Editor `ctx`refer to [Plug-In Development GUide](PLUGIN.md) and [Api Document](https://yn-api-doc.vercel.app/modules/context.html)
`$include` | `(path: string, trim = false) => Result` | Introduce other document fragment methods
`$export` | `(key: string, val: any) => Result` | Define a variable that can be used in this document
`$noop` | `() => Result` | no operation, Used for holding text space
`$doc` | `object` | Current document information
`$doc.basename` | `string` | File name of current document (no suffix)
`$doc.name` | `string` | File name of current document
`$doc.path` | `string` | Current document path
`$doc.repo` | `string` | Current document repository
`$doc.content` | `string` | Current document content
`$doc.status` | `'loaded', 'save-failed', 'saved'` | Current document status
## Command Line Parameters
+25 -12
View File
@@ -2,6 +2,9 @@
headingNumber: true
enableMacro: true
customVar: Hello
define:
--APP_NAME--: Yank Note
--APP_VERSION--: '[= $ctx.version =]'
---
# Yank-Note 特色功能使用说明
@@ -450,8 +453,17 @@ test 3
变量名 | 类型 | 描述
---- | ----- | ---
headingNumber | boolean | 是否开启页面标题序号编号
enableMacro | boolean | 是否开启宏替换
`headingNumber` | `boolean` | 是否开启页面标题序号编号
`enableMacro` | `boolean` | 是否开启宏替换
`define` | `Record<string, string>` | 宏定义,定义文本替换
### 文本替换
`define` 字段可以定义一些文本替换映射。支持在另一个文件定义,支持宏表达式。具体可参考本文件顶部 Front Matter 部分。
- 应用名: --APP_NAME--
- 应用版本: --APP_VERSION--
- 另一个文件的定义: --TEST_DEFINE--
## 宏替换
@@ -512,16 +524,17 @@ Yank Note 允许你在页面中嵌入宏,用以动态的替换文档。
变量名 | 类型 | 描述
---- | ----- | ---
`$ctx` | object | 编辑器 `ctx`,可参考[插件开发指南](PLUGIN.md) 和[Api 文档](https://yn-api-doc.vercel.app/modules/context.html)
`$include` | (path: string, trim = false) => Result | 引入其他文档片段方法
`$export` | (key: string, val: any) => Result | 定义一个本文档可以使用的变量
`$doc` | object | 当前文档信息
`$doc.basename` | string | 当前文档文件名(无后缀)
`$doc.name` | string | 当前文档文件名
`$doc.path` | string | 当前文档路径
`$doc.repo` | string | 当前文档仓库
`$doc.content` | string | 当前文档内容
`$doc.status` | 'loaded', 'save-failed', 'saved' | 当前文档状态
`$ctx` | `object` | 编辑器 `ctx`,可参考[插件开发指南](PLUGIN.md) 和[Api 文档](https://yn-api-doc.vercel.app/modules/context.html)
`$include` | `(path: string, trim = false) => Result` | 引入其他文档片段方法
`$export` | `(key: string, val: any) => Result` | 定义一个本文档可以使用的变量
`$noop` | `() => Result` | 无操作函数,可用于文本占位使用
`$doc` | `object` | 当前文档信息
`$doc.basename` | `string` | 当前文档文件名(无后缀)
`$doc.name` | `string` | 当前文档文件名
`$doc.path` | `string` | 当前文档路径
`$doc.repo` | `string` | 当前文档仓库
`$doc.content` | `string` | 当前文档内容
`$doc.status` | `'loaded', 'save-failed', 'saved'` | 当前文档状态
## 命令行参数
+2
View File
@@ -1,4 +1,6 @@
---
customVarFromOtherDoc: Hello, It's Me.
define:
--TEST_DEFINE--: definition from a fragment.
---
*Content from a fragment.*
+1 -1
View File
@@ -70,7 +70,7 @@ if (FLAG_DEMO) {
return Promise.resolve({ status: 'ok', message: 'success' })
} else if (method === 'GET') {
if ((url.searchParams.get('path') || '').includes('_FRAGMENT.md')) {
const content = "---\ncustomVarFromOtherDoc: Hello, It's Me.\n---\n*Content from a fragment.*"
const content = "---\ncustomVarFromOtherDoc: Hello, It's Me.\ndefine:\n --TEST_DEFINE--: definition from a fragment.\n---\n*Content from a fragment.*"
return Promise.resolve({
status: 'ok',
message: 'success',
+2 -1
View File
@@ -105,7 +105,8 @@ function transform (
// single line expression
return val.trim().replaceAll('\n', magicNewline)
} else {
return `[= define.${match} =]`
match = match.replace(/\n|'/, '\\$&')
return `[= define['${match}'] =]`
}
})
}