vitepress-plugin-toolkit 0.5.0 → 0.7.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.
@@ -49,7 +49,7 @@ function isExternal(path) {
49
49
  * URL_PROTOCOL_RE.test('mailto:foo@example.com') // true
50
50
  * URL_PROTOCOL_RE.test('//cdn.example.com/lib.js') // false
51
51
  */
52
- const URL_PROTOCOL_RE = /^[a-z][a-z0-9+.-]*:/;
52
+ const URL_PROTOCOL_RE = /^[a-z][a-z0-9+.-]*:/i;
53
53
  /**
54
54
  * Checks whether the given link contains a URL protocol scheme or is a
55
55
  * protocol-relative URL.
@@ -50,7 +50,7 @@ function isExternal(path) {
50
50
  * URL_PROTOCOL_RE.test('mailto:foo@example.com') // true
51
51
  * URL_PROTOCOL_RE.test('//cdn.example.com/lib.js') // false
52
52
  */
53
- const URL_PROTOCOL_RE = /^[a-z][a-z0-9+.-]*:/;
53
+ const URL_PROTOCOL_RE = /^[a-z][a-z0-9+.-]*:/i;
54
54
  /**
55
55
  * Checks whether the given link contains a URL protocol scheme or is a
56
56
  * protocol-relative URL.
@@ -2,7 +2,7 @@
2
2
 
3
3
  .fade-in-enter-active,
4
4
  .fade-in-leave-active {
5
- transition: all var(--transition-duration) var(--transition-ease-in-out) !important;
5
+ transition: opacity var(--transition-duration) var(--transition-ease-in-out) !important;
6
6
  }
7
7
 
8
8
  .fade-in-enter-from,
@@ -1,7 +1,7 @@
1
1
  import { Matcher } from "picomatch";
2
+ import { MarkdownEnv, Plugin, SiteConfig } from "vitepress";
2
3
  import MarkdownIt from "markdown-it";
3
4
  import { RenderRule } from "markdown-it/lib/renderer.mjs";
4
- import { MarkdownEnv, Plugin, SiteConfig } from "vitepress";
5
5
 
6
6
  //#region src/shared/link.d.ts
7
7
  /**
@@ -126,6 +126,58 @@ interface SizeOptions {
126
126
  ratio?: number | string;
127
127
  }
128
128
  //#endregion
129
+ //#region src/node/markdown/clean-markdown-env.d.ts
130
+ /**
131
+ * Clean Markdown Environment
132
+ *
133
+ * 清理后的 Markdown 环境
134
+ */
135
+ interface CleanMarkdownEnv extends MarkdownEnv {
136
+ /**
137
+ * References
138
+ *
139
+ * 引用链接
140
+ */
141
+ references?: unknown;
142
+ /**
143
+ * Abbreviations
144
+ *
145
+ * 缩写词
146
+ */
147
+ abbreviations?: unknown;
148
+ /**
149
+ * Annotations
150
+ *
151
+ * 注释
152
+ */
153
+ annotations?: unknown;
154
+ }
155
+ declare const WHITE_KEYS_LIST: readonly ["cleanUrls", "path", "realPath", "relativePath", "localeIndex", "references", "abbreviations", "annotations"];
156
+ type WhiteKeysListUnion = (typeof WHITE_KEYS_LIST)[number];
157
+ /**
158
+ * Clean markdown environment for inline rendering.
159
+ *
160
+ * 清理 markdown 环境以用于行内渲染。
161
+ *
162
+ * When using `md.renderInline()` in custom renderers, some environment properties
163
+ * may cause issues. This function creates a clean environment object with only
164
+ * the necessary properties preserved.
165
+ *
166
+ * 在自定义渲染器中使用 `md.renderInline()` 时,某些环境属性可能会导致问题。
167
+ * 该函数创建一个只保留必要属性的干净环境对象。
168
+ *
169
+ * @param env - Markdown environment / Markdown 环境
170
+ * @param exclude - Keys to exclude / 要排除的键
171
+ * @returns Cleaned environment / 清理后的环境
172
+ *
173
+ * @example
174
+ * ```ts
175
+ * const cleanEnv = cleanMarkdownEnv(env)
176
+ * const rendered = md.renderInline(content, cleanEnv)
177
+ * ```
178
+ */
179
+ declare function cleanMarkdownEnv(env: MarkdownEnv, exclude?: WhiteKeysListUnion[]): CleanMarkdownEnv;
180
+ //#endregion
129
181
  //#region src/node/markdown/container.d.ts
130
182
  /**
131
183
  * Type for getting RenderRule parameters
@@ -309,7 +361,7 @@ declare const EXTENSION_AUDIOS: string[];
309
361
  * @param exclude - Patterns to exclude, can be string or array / 要排除的模式,可以是字符串或数组
310
362
  * @returns Matcher instance / 匹配器实例
311
363
  */
312
- declare function createMatcher(include?: string | string[], exclude?: string | string[]): Matcher | undefined;
364
+ declare function createMatcher(include?: string | string[], exclude?: string | string[]): Matcher;
313
365
  /**
314
366
  * Resolve include and exclude patterns into pattern and ignore arrays.
315
367
  * Converts various pattern formats into a standardized format for matching.
@@ -716,4 +768,4 @@ declare function getVitepressConfig(): SiteConfig;
716
768
  */
717
769
  declare function resolveRouteLink(url: string, env: MarkdownEnv): string;
718
770
  //#endregion
719
- export { BuiltinLocale, BuiltinLocales, ContainerOptions, EXTENSION_AUDIOS, EXTENSION_IMAGES, EXTENSION_VIDEOS, EXTERNAL_URL_RE, EmbedRuleBlockOptions, LogLevel, LogType, Logger, SizeOptions, URL_PROTOCOL_RE, createContainerPlugin, createContainerSyntaxPlugin, createEmbedRuleBlock, createLocales, createLogger, createMatcher, genHash, getLocaleWithPath, getVitepressConfig, iconPlugin, isBuild, isDev, isExternal, isLinkWithProtocol, logLevels, parseRect, resolveAttr, resolveAttrs, resolveMatcherPattern, resolveRouteLink, slugify, stringifyAttrs, treatAsHtml };
771
+ export { BuiltinLocale, BuiltinLocales, CleanMarkdownEnv, ContainerOptions, EXTENSION_AUDIOS, EXTENSION_IMAGES, EXTENSION_VIDEOS, EXTERNAL_URL_RE, EmbedRuleBlockOptions, LogLevel, LogType, Logger, SizeOptions, URL_PROTOCOL_RE, cleanMarkdownEnv, createContainerPlugin, createContainerSyntaxPlugin, createEmbedRuleBlock, createLocales, createLogger, createMatcher, genHash, getLocaleWithPath, getVitepressConfig, iconPlugin, isBuild, isDev, isExternal, isLinkWithProtocol, logLevels, parseRect, resolveAttr, resolveAttrs, resolveMatcherPattern, resolveRouteLink, slugify, stringifyAttrs, treatAsHtml };
@@ -1,5 +1,5 @@
1
1
  import container from "markdown-it-container";
2
- import { LRUCache, camelCase, deepMerge, isArray, isBoolean, isNull, isNumber, isPrimitive, isString, isUndefined, kebabCase, objectEntries, objectKeys, omit, slash, toArray, uniq } from "@pengzhanbo/utils";
2
+ import { LRUCache, attempt, camelCase, deepMerge, isArray, isBoolean, isNull, isNumber, isPrimitive, isString, isUndefined, kebabCase, objectEntries, objectKeys, omit, removeTrailingSlash, slash, toArray, uniq } from "@pengzhanbo/utils";
3
3
  import ansis from "ansis";
4
4
  import process from "node:process";
5
5
  import picomatch from "picomatch";
@@ -53,7 +53,7 @@ function isExternal(path) {
53
53
  * URL_PROTOCOL_RE.test('mailto:foo@example.com') // true
54
54
  * URL_PROTOCOL_RE.test('//cdn.example.com/lib.js') // false
55
55
  */
56
- const URL_PROTOCOL_RE = /^[a-z][a-z0-9+.-]*:/;
56
+ const URL_PROTOCOL_RE = /^[a-z][a-z0-9+.-]*:/i;
57
57
  /**
58
58
  * Checks whether the given link contains a URL protocol scheme or is a
59
59
  * protocol-relative URL.
@@ -79,6 +79,48 @@ function isLinkWithProtocol(link) {
79
79
  return URL_PROTOCOL_RE.test(link) || link.startsWith("//");
80
80
  }
81
81
  //#endregion
82
+ //#region src/node/markdown/clean-markdown-env.ts
83
+ const WHITE_KEYS_LIST = [
84
+ "cleanUrls",
85
+ "path",
86
+ "realPath",
87
+ "relativePath",
88
+ "localeIndex",
89
+ "references",
90
+ "abbreviations",
91
+ "annotations"
92
+ ];
93
+ /**
94
+ * Clean markdown environment for inline rendering.
95
+ *
96
+ * 清理 markdown 环境以用于行内渲染。
97
+ *
98
+ * When using `md.renderInline()` in custom renderers, some environment properties
99
+ * may cause issues. This function creates a clean environment object with only
100
+ * the necessary properties preserved.
101
+ *
102
+ * 在自定义渲染器中使用 `md.renderInline()` 时,某些环境属性可能会导致问题。
103
+ * 该函数创建一个只保留必要属性的干净环境对象。
104
+ *
105
+ * @param env - Markdown environment / Markdown 环境
106
+ * @param exclude - Keys to exclude / 要排除的键
107
+ * @returns Cleaned environment / 清理后的环境
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * const cleanEnv = cleanMarkdownEnv(env)
112
+ * const rendered = md.renderInline(content, cleanEnv)
113
+ * ```
114
+ */
115
+ function cleanMarkdownEnv(env, exclude = []) {
116
+ const result = {};
117
+ for (const key of WHITE_KEYS_LIST) {
118
+ if (exclude.includes(key)) continue;
119
+ result[key] = env[key];
120
+ }
121
+ return result;
122
+ }
123
+ //#endregion
82
124
  //#region src/node/utils/resolve-attrs.ts
83
125
  /**
84
126
  * Regular expression for matching attribute values
@@ -453,7 +495,7 @@ function resolveMatcherPattern(include, exclude) {
453
495
  };
454
496
  }
455
497
  function normalize(arr) {
456
- return isArray(arr) ? arr.sort((a, b) => a.localeCompare(b)) : arr;
498
+ return isArray(arr) ? [...arr].sort((a, b) => a.localeCompare(b)) : arr;
457
499
  }
458
500
  //#endregion
459
501
  //#region src/node/utils/logger.ts
@@ -534,6 +576,7 @@ function createLogger(prefix, defaultLevel = "info") {
534
576
  * ```
535
577
  */
536
578
  function parseRect(str, unit = "px") {
579
+ str = str.trim();
537
580
  if (Number.parseFloat(str) === Number(str)) return `${str}${unit}`;
538
581
  return str;
539
582
  }
@@ -788,7 +831,7 @@ function createLocales(builtinLocales, userLocales = {}) {
788
831
  locales[key] = localeData;
789
832
  break;
790
833
  }
791
- if (!locales.root) locales.root = builtinLocales[0][1];
834
+ if (!locales.root) locales.root = builtinLocales[0]?.[1] || {};
792
835
  deepMerge(locales, userLocales);
793
836
  return locales;
794
837
  }
@@ -817,7 +860,7 @@ function createLocales(builtinLocales, userLocales = {}) {
817
860
  */
818
861
  function getLocaleWithPath(path) {
819
862
  const locales = getVitepressConfig().userConfig?.locales || {};
820
- const keys = objectKeys(locales);
863
+ const keys = objectKeys(locales).sort((a, b) => b.length - a.length);
821
864
  const key = keys.find((locale) => path.startsWith(locale)) || keys[0] || "";
822
865
  if (!key || !locales[key]) return {
823
866
  lang: "",
@@ -864,10 +907,12 @@ const indexRE = /(^|.*\/)index.md(.*)$/i;
864
907
  function resolveRouteLink(url, env) {
865
908
  if (isExternal(url)) return url;
866
909
  const config = getVitepressConfig();
867
- if (url.startsWith("/")) return slash(config.site.base + url);
868
- if (url.startsWith("#")) return decodeURI(normalizeHash(url));
869
- const { pathname, protocol } = new URL(url, "http://a.com");
870
- if (!url.startsWith("#") && protocol.startsWith("http") && treatAsHtml(pathname)) {
910
+ if (url.startsWith("/")) return slash(removeTrailingSlash(config.site.base) + url);
911
+ if (url.startsWith("#")) return normalizeHash(url);
912
+ const [error, data] = attempt(() => new URL(url, "http://a.com"));
913
+ if (error) return url;
914
+ const { pathname, protocol } = data;
915
+ if (protocol.startsWith("http") && treatAsHtml(pathname)) {
871
916
  const indexMatch = url.match(indexRE);
872
917
  if (indexMatch) {
873
918
  const [, path, hash] = indexMatch;
@@ -895,4 +940,4 @@ function normalizeHash(str) {
895
940
  return str ? encodeURI(`#${slugify(decodeURI(str).slice(1))}`) : "";
896
941
  }
897
942
  //#endregion
898
- export { EXTENSION_AUDIOS, EXTENSION_IMAGES, EXTENSION_VIDEOS, EXTERNAL_URL_RE, URL_PROTOCOL_RE, createContainerPlugin, createContainerSyntaxPlugin, createEmbedRuleBlock, createLocales, createLogger, createMatcher, genHash, getLocaleWithPath, getVitepressConfig, iconPlugin, isBuild, isDev, isExternal, isLinkWithProtocol, logLevels, parseRect, resolveAttr, resolveAttrs, resolveMatcherPattern, resolveRouteLink, slugify, stringifyAttrs, treatAsHtml };
943
+ export { EXTENSION_AUDIOS, EXTENSION_IMAGES, EXTENSION_VIDEOS, EXTERNAL_URL_RE, URL_PROTOCOL_RE, cleanMarkdownEnv, createContainerPlugin, createContainerSyntaxPlugin, createEmbedRuleBlock, createLocales, createLogger, createMatcher, genHash, getLocaleWithPath, getVitepressConfig, iconPlugin, isBuild, isDev, isExternal, isLinkWithProtocol, logLevels, parseRect, resolveAttr, resolveAttrs, resolveMatcherPattern, resolveRouteLink, slugify, stringifyAttrs, treatAsHtml };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vitepress-plugin-toolkit",
3
3
  "type": "module",
4
- "version": "0.5.0",
4
+ "version": "0.7.0",
5
5
  "description": "Development toolkit for vitepress plugins",
6
6
  "author": "pengzhanbo <q942450674@outlook.com> (https://github.com/pengzhanbo/)",
7
7
  "license": "MIT",