vitepress-tuck 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,13 +13,13 @@ Enhance vitepress configuration and provide plugin development capabilities.
13
13
  Install
14
14
 
15
15
  ```bash
16
- npm
16
+ # npm
17
17
  npm install vitepress vitepress-tuck
18
18
 
19
- pnpm
19
+ # pnpm
20
20
  pnpm add vitepress vitepress-tuck
21
21
 
22
- yarn
22
+ # yarn
23
23
  yarn add vitepress vitepress-tuck
24
24
  ```
25
25
 
package/dist/index.d.ts CHANGED
@@ -1,23 +1,58 @@
1
1
  import { DefaultTheme, UserConfig } from "vitepress";
2
2
 
3
3
  //#region src/types.d.ts
4
+ /**
5
+ * Represents a VitePress plugin in the vitepress-tuck ecosystem.
6
+ *
7
+ * A plugin extends a subset of VitePress's `UserConfig` fields (markdown, vite,
8
+ * vue, and lifecycle hooks) and adds a `name` plus an optional `client`
9
+ * descriptor for automatic client-side injection via the
10
+ * `virtual:enhance-app` virtual module.
11
+ *
12
+ * 表示 vitepress-tuck 生态中的 VitePress 插件。
13
+ *
14
+ * 插件扩展了 VitePress `UserConfig` 的部分字段(markdown、vite、vue 及生命周期
15
+ * 钩子),并新增 `name` 与可选的 `client` 描述符,用于通过
16
+ * `virtual:enhance-app` 虚拟模块自动注入客户端代码。
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * export const myPlugin = definePlugin((options?: MyOptions) => ({
21
+ * name: 'vitepress-plugin-my',
22
+ * markdown: { config: (md) => md.use(myMarkdownPlugin) },
23
+ * client: { enhance: true },
24
+ * }))
25
+ * ```
26
+ */
4
27
  interface VitepressPlugin extends Pick<UserConfig, 'markdown' | 'vite' | 'vue' | 'buildEnd' | 'transformHead' | 'transformHtml' | 'transformPageData' | 'postRender'> {
5
28
  /**
6
- * 插件名称
29
+ * Unique name of the plugin, also used as the client module specifier.
30
+ *
31
+ * 插件的唯一名称,同时用作客户端模块标识符。
7
32
  */
8
33
  name: string;
9
34
  /**
10
- * import enhanceApp from 'virtual:enhance-app' 注入到客户端代码中。
35
+ * Client-side injection descriptor consumed by the `virtual:enhance-app` module.
11
36
  *
12
- * `.vitepress/theme/index.ts` 中调用 enhanceApp(ctx),
13
- * 完成插件的客户端代码的自动注入,插件使用者无需手动添加。
37
+ * When set, the built-in virtual plugin generates
38
+ * `import enhanceApp from 'virtual:enhance-app'` and invokes it inside
39
+ * `.vitepress/theme/index.ts` via `enhanceApp(ctx)`, so plugin consumers do
40
+ * not need to wire up client code manually.
41
+ *
42
+ * 客户端注入描述符,由 `virtual:enhance-app` 模块消费。
43
+ *
44
+ * 设置后,内置虚拟插件会生成 `import enhanceApp from 'virtual:enhance-app'`,
45
+ * 并在 `.vitepress/theme/index.ts` 中通过 `enhanceApp(ctx)` 调用,
46
+ * 完成插件客户端代码的自动注入,插件使用者无需手动添加。
14
47
  */
15
48
  client?: {
16
49
  /**
17
- * 添加自定义的 import 语句.
50
+ * Custom import statements injected into the generated client module.
51
+ *
52
+ * 向生成的客户端模块注入自定义 import 语句。
18
53
  *
19
54
  * @example
20
- * 导入 css 文件
55
+ * Import a CSS file / 导入 CSS 文件
21
56
  * ```ts
22
57
  * {
23
58
  * imports: ['import "vitepress-plugin-xxx/styles/index.css"'],
@@ -26,12 +61,16 @@ interface VitepressPlugin extends Pick<UserConfig, 'markdown' | 'vite' | 'vue' |
26
61
  */
27
62
  imports?: string[];
28
63
  /**
64
+ * Name of the named `enhanceApp` function exported from
65
+ * `vitepress-plugin-xxx/client`. This function is invoked as
66
+ * `enhanceApp(ctx)` inside `vitepress/theme/index.ts`.
67
+ *
29
68
  * 插件包 `vitepress-plugin-xxx/client` 中导出的具名 `enhanceApp` 函数名。
30
- * 该函数会在 `vitepress/theme/index.ts` `enhanceApp(ctx)` 调用。
69
+ * 该函数会在 `vitepress/theme/index.ts` 中的 `enhanceApp(ctx)` 调用。
31
70
  *
32
- * - 未设置时,默认不注入 enhanceApp 函数
33
- * - 设置为 `true` 时,默认函数名为 `enhanceApp`
34
- * - 设置为字符串时,函数名为该字符串
71
+ * - When unset, no `enhanceApp` function is injected / 未设置时,默认不注入 enhanceApp 函数
72
+ * - When `true`, the function name defaults to `enhanceApp` / 设置为 `true` 时,默认函数名为 `enhanceApp`
73
+ * - When a string, that string is used as the function name / 设置为字符串时,函数名为该字符串
35
74
  *
36
75
  * @example
37
76
  * ```ts
@@ -43,20 +82,93 @@ interface VitepressPlugin extends Pick<UserConfig, 'markdown' | 'vite' | 'vue' |
43
82
  enhance?: string | boolean;
44
83
  };
45
84
  }
85
+ /**
86
+ * User configuration extension that adds a `plugins` field to VitePress's
87
+ * `UserConfig`.
88
+ *
89
+ * 用户配置扩展,为 VitePress 的 `UserConfig` 新增 `plugins` 字段。
90
+ */
46
91
  interface PluginsConfig {
47
92
  /**
48
- * vitepress 插件
93
+ * List of vitepress-tuck plugins to be processed by `defineConfig`.
94
+ *
95
+ * 由 `defineConfig` 处理的 vitepress-tuck 插件列表。
49
96
  */
50
97
  plugins?: VitepressPlugin[];
51
98
  }
52
99
  //#endregion
53
100
  //#region src/define-config.d.ts
101
+ /**
102
+ * Defines a VitePress configuration with plugin lifecycle management.
103
+ *
104
+ * This is the core function of vitepress-tuck. It accepts a standard VitePress
105
+ * `UserConfig` extended with a `plugins` field, then iterates over every plugin
106
+ * to extract client config, lifecycle hooks, and VitePress config fragments.
107
+ *
108
+ * Hook merging strategy:
109
+ * - **Parallel/concurrent**: `markdown.config`, `buildEnd`, `transformHead` run
110
+ * via `Promise.all`; `transformHead` results are concatenated.
111
+ * - **Sequential/chained**: `transformHtml`, `transformPageData`, `postRender`
112
+ * run in order, with each hook receiving the previous hook's result.
113
+ *
114
+ * User-provided hooks (from `userConfig`) are appended as the final step of
115
+ * each chain so they always run after plugin hooks. The returned config is a
116
+ * standard VitePress `UserConfig`, fully compatible with native VitePress.
117
+ *
118
+ * 定义带插件生命周期管理的 VitePress 配置。
119
+ *
120
+ * 这是 vitepress-tuck 的核心函数。它接收一个扩展了 `plugins` 字段的标准
121
+ * VitePress `UserConfig`,然后遍历所有插件,提取 client 配置、生命周期钩子和
122
+ * VitePress 配置片段。
123
+ *
124
+ * 钩子合并策略:
125
+ * - **并发类**:`markdown.config`、`buildEnd`、`transformHead` 通过 `Promise.all`
126
+ * 并发执行;`transformHead` 的结果会被拼接合并。
127
+ * - **顺序链式类**:`transformHtml`、`transformPageData`、`postRender` 按顺序执行,
128
+ * 每个钩子接收上一个钩子的结果。
129
+ *
130
+ * 用户提供的钩子(来自 `userConfig`)作为每条链的最后一步追加,确保始终在
131
+ * 插件钩子之后执行。返回值为标准 VitePress `UserConfig`,完全兼容原生 VitePress。
132
+ *
133
+ * @param config - VitePress user config with an optional `plugins` field / 带可选 `plugins` 字段的 VitePress 用户配置
134
+ * @returns A standard VitePress `UserConfig` with plugin hooks merged in / 合并了插件钩子的标准 VitePress `UserConfig`
135
+ * @example
136
+ * ```ts
137
+ * import { defineConfig } from 'vitepress-tuck'
138
+ * import { qrcode } from 'vitepress-plugin-qrcode'
139
+ *
140
+ * export default defineConfig({
141
+ * plugins: [qrcode()],
142
+ * title: 'My Site',
143
+ * // standard VitePress options...
144
+ * })
145
+ * ```
146
+ */
54
147
  declare function defineConfig<ThemeConfig = DefaultTheme.Config>(config: UserConfig<NoInfer<ThemeConfig>> & PluginsConfig): UserConfig<NoInfer<ThemeConfig>>;
55
148
  //#endregion
56
149
  //#region src/define-plugin.d.ts
57
150
  /**
58
- * 提供一种 vitepress 插件的定义方式,简化用户配置的复杂度,将复杂度转移到 vitepress 插件中
151
+ * Defines a VitePress plugin with type inference.
152
+ *
153
+ * This is an identity function that preserves the plugin factory type while
154
+ * enabling TypeScript to infer the options type `T` from the factory signature.
155
+ * It simplifies user configuration by moving complexity into the plugin itself.
156
+ *
157
+ * 定义 VitePress 插件,提供类型推断。
158
+ *
159
+ * 这是一个恒等函数,保留插件工厂类型,同时让 TypeScript 从工厂签名推断选项
160
+ * 类型 `T`。它将复杂度转移到插件内部,从而简化用户配置。
161
+ *
162
+ * @param plugin - Plugin factory function returning a `VitepressPlugin` / 返回 `VitepressPlugin` 的插件工厂函数
163
+ * @returns The same plugin factory function / 相同的插件工厂函数
164
+ * @example
165
+ * ```ts
166
+ * export const myPlugin = definePlugin((options?: MyOptions) => ({
167
+ * name: 'vitepress-plugin-my',
168
+ * markdown: { config: (md) => md.use(myMarkdownPlugin) },
169
+ * }))
170
+ * ```
59
171
  */
60
- declare function definePlugin<T>(plugin: (option?: T) => VitepressPlugin): (option?: T) => VitepressPlugin;
172
+ declare function definePlugin<T>(plugin: (options?: T) => VitepressPlugin): (options?: T) => VitepressPlugin;
61
173
  //#endregion
62
174
  export { PluginsConfig, VitepressPlugin, defineConfig, definePlugin };
package/dist/index.js CHANGED
@@ -2,7 +2,26 @@ import { isString, toTruthy } from "@pengzhanbo/utils";
2
2
  import { mergeConfig } from "vitepress";
3
3
  //#region src/define-plugin.ts
4
4
  /**
5
- * 提供一种 vitepress 插件的定义方式,简化用户配置的复杂度,将复杂度转移到 vitepress 插件中
5
+ * Defines a VitePress plugin with type inference.
6
+ *
7
+ * This is an identity function that preserves the plugin factory type while
8
+ * enabling TypeScript to infer the options type `T` from the factory signature.
9
+ * It simplifies user configuration by moving complexity into the plugin itself.
10
+ *
11
+ * 定义 VitePress 插件,提供类型推断。
12
+ *
13
+ * 这是一个恒等函数,保留插件工厂类型,同时让 TypeScript 从工厂签名推断选项
14
+ * 类型 `T`。它将复杂度转移到插件内部,从而简化用户配置。
15
+ *
16
+ * @param plugin - Plugin factory function returning a `VitepressPlugin` / 返回 `VitepressPlugin` 的插件工厂函数
17
+ * @returns The same plugin factory function / 相同的插件工厂函数
18
+ * @example
19
+ * ```ts
20
+ * export const myPlugin = definePlugin((options?: MyOptions) => ({
21
+ * name: 'vitepress-plugin-my',
22
+ * markdown: { config: (md) => md.use(myMarkdownPlugin) },
23
+ * }))
24
+ * ```
6
25
  */
7
26
  function definePlugin(plugin) {
8
27
  return plugin;
@@ -10,6 +29,32 @@ function definePlugin(plugin) {
10
29
  //#endregion
11
30
  //#region src/builtin-plugins/virtual-enhance-app.ts
12
31
  let uuid = 0;
32
+ /**
33
+ * Creates a Vite virtual module plugin that generates the
34
+ * `virtual:enhance-app` module on the fly.
35
+ *
36
+ * The generated module imports every plugin's client entry and chains their
37
+ * `enhanceApp(ctx)` calls inside a single default export. Users import this
38
+ * module once in `.vitepress/theme/index.ts` to activate all plugin client
39
+ * side effects.
40
+ *
41
+ * 创建 Vite 虚拟模块插件,动态生成 `virtual:enhance-app` 模块。
42
+ *
43
+ * 生成的模块导入每个插件的客户端入口,并在单个默认导出中链式调用它们的
44
+ * `enhanceApp(ctx)`。用户在 `.vitepress/theme/index.ts` 中导入该模块一次,
45
+ * 即可激活所有插件的客户端副作用。
46
+ *
47
+ * @param options - Options describing imports and enhance functions / 描述导入与 enhance 函数的选项
48
+ * @returns A Vite `Plugin` instance / Vite `Plugin` 实例
49
+ * @example
50
+ * `Generated module (conceptual)`:
51
+ * ```ts
52
+ * import { enhanceApp as enhanceApp$0 } from 'vitepress-plugin-foo/client'
53
+ * export default function enhanceApp(ctx) {
54
+ * enhanceApp$0(ctx)
55
+ * }
56
+ * ```
57
+ */
13
58
  function virtualEnhanceApp(options) {
14
59
  const moduleId = "virtual:enhance-app";
15
60
  const resolveId = `\0${moduleId}`;
@@ -36,12 +81,35 @@ ${enhanceCode.join("\n")}
36
81
  }
37
82
  };
38
83
  }
84
+ /**
85
+ * Plugin factory wrapping `virtualEnhanceApp` as a vitepress-tuck plugin.
86
+ *
87
+ * 将 `virtualEnhanceApp` 包装为 vitepress-tuck 插件的工厂函数。
88
+ *
89
+ * @param options - Options forwarded to `virtualEnhanceApp` / 转发给 `virtualEnhanceApp` 的选项
90
+ * @returns A `VitepressPlugin` that registers the virtual module / 注册虚拟模块的 `VitepressPlugin`
91
+ */
39
92
  const virtualEnhanceAppPlugin = definePlugin((options) => ({
40
93
  name: "virtual-enhance-app",
41
94
  vite: { plugins: [virtualEnhanceApp(options || {})] }
42
95
  }));
43
96
  //#endregion
44
97
  //#region src/builtin-plugins/index.ts
98
+ /**
99
+ * Returns the list of built-in plugins shipped with vitepress-tuck.
100
+ *
101
+ * Currently includes the `virtual:enhance-app` Vite plugin (for client code
102
+ * injection) and a `vitepress-tuck:deps` plugin that marks
103
+ * `vitepress-plugin-toolkit` as SSR non-external.
104
+ *
105
+ * 返回 vitepress-tuck 自带的内置插件列表。
106
+ *
107
+ * 当前包含用于客户端代码注入的 `virtual:enhance-app` Vite 插件,以及将
108
+ * `vitepress-plugin-toolkit` 标记为 SSR 非外部依赖的 `vitepress-tuck:deps` 插件。
109
+ *
110
+ * @param options - Options for built-in plugin construction / 内置插件构建选项
111
+ * @returns Array of built-in `VitepressPlugin` instances / 内置 `VitepressPlugin` 实例数组
112
+ */
45
113
  function builtinPlugins(options) {
46
114
  return [virtualEnhanceAppPlugin(options.enhanceApp), () => ({
47
115
  name: "vitepress-tuck:deps",
@@ -50,6 +118,52 @@ function builtinPlugins(options) {
50
118
  }
51
119
  //#endregion
52
120
  //#region src/define-config.ts
121
+ /**
122
+ * Defines a VitePress configuration with plugin lifecycle management.
123
+ *
124
+ * This is the core function of vitepress-tuck. It accepts a standard VitePress
125
+ * `UserConfig` extended with a `plugins` field, then iterates over every plugin
126
+ * to extract client config, lifecycle hooks, and VitePress config fragments.
127
+ *
128
+ * Hook merging strategy:
129
+ * - **Parallel/concurrent**: `markdown.config`, `buildEnd`, `transformHead` run
130
+ * via `Promise.all`; `transformHead` results are concatenated.
131
+ * - **Sequential/chained**: `transformHtml`, `transformPageData`, `postRender`
132
+ * run in order, with each hook receiving the previous hook's result.
133
+ *
134
+ * User-provided hooks (from `userConfig`) are appended as the final step of
135
+ * each chain so they always run after plugin hooks. The returned config is a
136
+ * standard VitePress `UserConfig`, fully compatible with native VitePress.
137
+ *
138
+ * 定义带插件生命周期管理的 VitePress 配置。
139
+ *
140
+ * 这是 vitepress-tuck 的核心函数。它接收一个扩展了 `plugins` 字段的标准
141
+ * VitePress `UserConfig`,然后遍历所有插件,提取 client 配置、生命周期钩子和
142
+ * VitePress 配置片段。
143
+ *
144
+ * 钩子合并策略:
145
+ * - **并发类**:`markdown.config`、`buildEnd`、`transformHead` 通过 `Promise.all`
146
+ * 并发执行;`transformHead` 的结果会被拼接合并。
147
+ * - **顺序链式类**:`transformHtml`、`transformPageData`、`postRender` 按顺序执行,
148
+ * 每个钩子接收上一个钩子的结果。
149
+ *
150
+ * 用户提供的钩子(来自 `userConfig`)作为每条链的最后一步追加,确保始终在
151
+ * 插件钩子之后执行。返回值为标准 VitePress `UserConfig`,完全兼容原生 VitePress。
152
+ *
153
+ * @param config - VitePress user config with an optional `plugins` field / 带可选 `plugins` 字段的 VitePress 用户配置
154
+ * @returns A standard VitePress `UserConfig` with plugin hooks merged in / 合并了插件钩子的标准 VitePress `UserConfig`
155
+ * @example
156
+ * ```ts
157
+ * import { defineConfig } from 'vitepress-tuck'
158
+ * import { qrcode } from 'vitepress-plugin-qrcode'
159
+ *
160
+ * export default defineConfig({
161
+ * plugins: [qrcode()],
162
+ * title: 'My Site',
163
+ * // standard VitePress options...
164
+ * })
165
+ * ```
166
+ */
53
167
  function defineConfig(config) {
54
168
  const hooks = {
55
169
  buildEnd: [],
@@ -65,6 +179,16 @@ function defineConfig(config) {
65
179
  imports: [],
66
180
  enhances: []
67
181
  };
182
+ /**
183
+ * Iterates a list of plugins, extracting client config, lifecycle hooks, and
184
+ * VitePress config fragments into the shared accumulators (`hooks`,
185
+ * `enhanceApp`, `mergedConfig`).
186
+ *
187
+ * 遍历插件列表,将 client 配置、生命周期钩子和 VitePress 配置片段提取到
188
+ * 共享累加器(`hooks`、`enhanceApp`、`mergedConfig`)中。
189
+ *
190
+ * @param plugins - Plugins to process / 待处理的插件列表
191
+ */
68
192
  const processPlugins = (plugins) => {
69
193
  plugins.forEach((plugin) => {
70
194
  const { name, client, buildEnd, transformHead, transformHtml, transformPageData, postRender, ...customConfig } = plugin;
package/package.json CHANGED
@@ -1,15 +1,19 @@
1
1
  {
2
2
  "name": "vitepress-tuck",
3
3
  "type": "module",
4
- "version": "0.2.0",
4
+ "version": "0.4.0",
5
5
  "description": "Enhance vitepress configuration, provide plugins capability.",
6
6
  "author": "pengzhanbo <q942450674@outlook.com> (https://github.com/pengzhanbo/)",
7
7
  "license": "MIT",
8
+ "homepage": "https://tuck.pengzhanbo.cn/",
8
9
  "repository": {
9
10
  "type": "git",
10
11
  "url": "git+https://github.com/pengzhanbo/vitepress-tuck.git",
11
12
  "directory": "packages/vitepress-tuck"
12
13
  },
14
+ "bugs": {
15
+ "url": "https://github.com/pengzhanbo/vitepress-tuck/issues"
16
+ },
13
17
  "keywords": [
14
18
  "vitepress",
15
19
  "vitepress-plugin"