unplugin-docubook 1.1.0 → 1.1.2

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.
Files changed (55) hide show
  1. package/README.md +84 -108
  2. package/dist/astro.cjs +52 -13
  3. package/dist/astro.cjs.map +1 -1
  4. package/dist/astro.js +55 -13
  5. package/dist/astro.js.map +1 -1
  6. package/dist/bun.cjs +52 -13
  7. package/dist/bun.cjs.map +1 -1
  8. package/dist/bun.js +55 -13
  9. package/dist/bun.js.map +1 -1
  10. package/dist/components/index.cjs +4 -2
  11. package/dist/components/index.cjs.map +1 -1
  12. package/dist/components/index.js +4 -2
  13. package/dist/components/index.js.map +1 -1
  14. package/dist/esbuild.cjs +52 -13
  15. package/dist/esbuild.cjs.map +1 -1
  16. package/dist/esbuild.js +55 -13
  17. package/dist/esbuild.js.map +1 -1
  18. package/dist/farm.cjs +52 -13
  19. package/dist/farm.cjs.map +1 -1
  20. package/dist/farm.js +55 -13
  21. package/dist/farm.js.map +1 -1
  22. package/dist/index.cjs +52 -13
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.js +55 -13
  25. package/dist/index.js.map +1 -1
  26. package/dist/next.cjs +52 -13
  27. package/dist/next.cjs.map +1 -1
  28. package/dist/next.js +55 -13
  29. package/dist/next.js.map +1 -1
  30. package/dist/nuxt.cjs +52 -13
  31. package/dist/nuxt.cjs.map +1 -1
  32. package/dist/nuxt.js +55 -13
  33. package/dist/nuxt.js.map +1 -1
  34. package/dist/rolldown.cjs +52 -13
  35. package/dist/rolldown.cjs.map +1 -1
  36. package/dist/rolldown.js +55 -13
  37. package/dist/rolldown.js.map +1 -1
  38. package/dist/rollup.cjs +52 -13
  39. package/dist/rollup.cjs.map +1 -1
  40. package/dist/rollup.js +55 -13
  41. package/dist/rollup.js.map +1 -1
  42. package/dist/rspack.cjs +52 -13
  43. package/dist/rspack.cjs.map +1 -1
  44. package/dist/rspack.js +55 -13
  45. package/dist/rspack.js.map +1 -1
  46. package/dist/types.cjs.map +1 -1
  47. package/dist/vite.cjs +52 -13
  48. package/dist/vite.cjs.map +1 -1
  49. package/dist/vite.js +55 -13
  50. package/dist/vite.js.map +1 -1
  51. package/dist/webpack.cjs +52 -13
  52. package/dist/webpack.cjs.map +1 -1
  53. package/dist/webpack.js +55 -13
  54. package/dist/webpack.js.map +1 -1
  55. package/package.json +1 -1
package/dist/next.js CHANGED
@@ -1,3 +1,20 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
4
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __spreadValues = (a, b) => {
7
+ for (var prop in b || (b = {}))
8
+ if (__hasOwnProp.call(b, prop))
9
+ __defNormalProp(a, prop, b[prop]);
10
+ if (__getOwnPropSymbols)
11
+ for (var prop of __getOwnPropSymbols(b)) {
12
+ if (__propIsEnum.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ }
15
+ return a;
16
+ };
17
+
1
18
  // src/next.ts
2
19
  import { createWebpackPlugin } from "unplugin";
3
20
 
@@ -35,24 +52,49 @@ function createRegistry(framework) {
35
52
  DocuBookProvider: { exports: ["DocuBookProvider"], path: basePath }
36
53
  };
37
54
  }
38
- function getComponentRegistry(framework = "react") {
39
- return createRegistry(framework);
55
+ var registryCache = /* @__PURE__ */ new Map();
56
+ var regexCache = /* @__PURE__ */ new Map();
57
+ function getComponentRegistry(framework = "react", customComponents) {
58
+ if (!customComponents || Object.keys(customComponents).length === 0) {
59
+ const cached = registryCache.get(framework);
60
+ if (cached) return cached;
61
+ const registry = createRegistry(framework);
62
+ registryCache.set(framework, registry);
63
+ return registry;
64
+ }
65
+ return __spreadValues(__spreadValues({}, createRegistry(framework)), customComponents);
40
66
  }
41
- function buildComponentDetectionRegex(framework = "react") {
67
+ function buildComponentDetectionRegex(framework = "react", customComponents) {
68
+ if (customComponents && Object.keys(customComponents).length > 0) {
69
+ const registry2 = getComponentRegistry(framework, customComponents);
70
+ const names2 = Object.keys(registry2);
71
+ return new RegExp(`<(${names2.join("|")})(?=[\\s/>])`, "g");
72
+ }
73
+ const cached = regexCache.get(framework);
74
+ if (cached) {
75
+ cached.lastIndex = 0;
76
+ return cached;
77
+ }
42
78
  const registry = getComponentRegistry(framework);
43
79
  const names = Object.keys(registry);
44
- return new RegExp(`<(${names.join("|")})(?=[\\s/>])`, "g");
80
+ const regex = new RegExp(`<(${names.join("|")})(?=[\\s/>])`, "g");
81
+ regexCache.set(framework, regex);
82
+ return regex;
45
83
  }
46
84
 
47
85
  // src/core/transform.ts
48
86
  var DEFAULT_IMPORT_SOURCE = "unplugin-docubook/components";
49
87
  var DEFAULT_FRAMEWORK = "react";
50
- function detectComponents(code, framework = DEFAULT_FRAMEWORK) {
51
- const regex = buildComponentDetectionRegex(framework);
52
- const registry = getComponentRegistry(framework);
88
+ function stripIgnoredZones(code) {
89
+ return code.replace(/```[\s\S]*?```/g, "").replace(/`[^`]+`/g, "").replace(/\{\/\*[\s\S]*?\*\/\}/g, "").replace(/<!--[\s\S]*?-->/g, "").replace(/^import\s.+$/gm, "").replace(/^export\s.+$/gm, "");
90
+ }
91
+ function detectComponents(code, framework = DEFAULT_FRAMEWORK, customComponents) {
92
+ const strippedCode = stripIgnoredZones(code);
93
+ const regex = buildComponentDetectionRegex(framework, customComponents);
94
+ const registry = getComponentRegistry(framework, customComponents);
53
95
  const found = /* @__PURE__ */ new Set();
54
96
  let match;
55
- while ((match = regex.exec(code)) !== null) {
97
+ while ((match = regex.exec(strippedCode)) !== null) {
56
98
  const name = match[1];
57
99
  if (name && registry[name]) {
58
100
  found.add(name);
@@ -60,13 +102,13 @@ function detectComponents(code, framework = DEFAULT_FRAMEWORK) {
60
102
  }
61
103
  return Array.from(found);
62
104
  }
63
- function generateImports(componentNames, importSource, framework = DEFAULT_FRAMEWORK) {
64
- const registry = getComponentRegistry(framework);
105
+ function generateImports(componentNames, importSource, framework = DEFAULT_FRAMEWORK, customComponents) {
106
+ const registry = getComponentRegistry(framework, customComponents);
65
107
  const pathToExports = /* @__PURE__ */ new Map();
66
108
  for (const name of componentNames) {
67
109
  const meta = registry[name];
68
110
  if (!meta) continue;
69
- const fullPath = `${importSource}${meta.path}`;
111
+ const fullPath = meta.path.startsWith("/") ? `${importSource}${meta.path}` : meta.path;
70
112
  if (!pathToExports.has(fullPath)) {
71
113
  pathToExports.set(fullPath, /* @__PURE__ */ new Set());
72
114
  }
@@ -84,12 +126,12 @@ function generateImports(componentNames, importSource, framework = DEFAULT_FRAME
84
126
  function transformMdx(code, options = {}) {
85
127
  var _a, _b;
86
128
  const framework = (_a = options.framework) != null ? _a : DEFAULT_FRAMEWORK;
87
- const usedComponents = detectComponents(code, framework);
129
+ const usedComponents = detectComponents(code, framework, options.customComponents);
88
130
  if (usedComponents.length === 0) {
89
131
  return { code, hasTransformed: false };
90
132
  }
91
133
  const importSource = (_b = options.importSource) != null ? _b : DEFAULT_IMPORT_SOURCE;
92
- const importStatements = generateImports(usedComponents, importSource, framework);
134
+ const importStatements = generateImports(usedComponents, importSource, framework, options.customComponents);
93
135
  return {
94
136
  code: `${importStatements}
95
137
 
package/dist/next.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/next.ts","../src/index.ts","../src/core/registry.ts","../src/core/transform.ts"],"sourcesContent":["import { createWebpackPlugin } from 'unplugin'\nimport { unpluginFactory } from '.'\n\nexport default createWebpackPlugin(unpluginFactory)\n","import type { UnpluginFactory } from 'unplugin'\nimport type { Options } from './types'\nimport { createUnplugin } from 'unplugin'\nimport { transformMdx } from './core/transform'\n\nexport const unpluginFactory: UnpluginFactory<Options | undefined> = (\n options,\n) => ({\n name: 'unplugin-docubook',\n\n transformInclude(id) {\n // Only process .mdx and .md files\n return /\\.mdx?$/.test(id)\n },\n\n transform(code, _id) {\n const result = transformMdx(code, options)\n if (!result.hasTransformed) return\n return result.code\n },\n})\n\nexport const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)\n\nexport default unplugin\n","import type { Framework } from '../types'\n\nexport interface ComponentMeta {\n exports: string[]\n path: string\n}\n\nconst FRAMEWORK_PATHS: Record<Framework, string> = {\n react: '/react',\n vue: '/vue',\n svelte: '/svelte',\n}\n\nfunction createRegistry(framework: Framework): Record<string, ComponentMeta> {\n const basePath = FRAMEWORK_PATHS[framework]\n\n return {\n Note: { exports: ['Note'], path: basePath },\n Card: { exports: ['Card'], path: basePath },\n CardGroup: { exports: ['CardGroup'], path: basePath },\n Accordion: { exports: ['Accordion'], path: basePath },\n AccordionGroup: { exports: ['AccordionGroup'], path: basePath },\n Stepper: { exports: ['Stepper', 'StepperItem'], path: basePath },\n Kbd: { exports: ['Kbd'], path: basePath },\n Tooltip: { exports: ['Tooltip'], path: basePath },\n Youtube: { exports: ['Youtube'], path: basePath },\n Button: { exports: ['Button'], path: basePath },\n DocuLink: { exports: ['DocuLink'], path: basePath },\n DocuImage: { exports: ['DocuImage'], path: basePath },\n Files: { exports: ['Files'], path: basePath },\n Folder: { exports: ['Folder'], path: basePath },\n File: { exports: ['File'], path: basePath },\n Release: { exports: ['Release'], path: basePath },\n Changes: { exports: ['Changes'], path: basePath },\n Pre: { exports: ['Pre'], path: basePath },\n Copy: { exports: ['Copy'], path: basePath },\n DocuBookProvider: { exports: ['DocuBookProvider'], path: basePath },\n }\n}\n\nexport function getComponentRegistry(framework: Framework = 'react'): Record<string, ComponentMeta> {\n return createRegistry(framework)\n}\n\nexport function buildComponentDetectionRegex(framework: Framework = 'react'): RegExp {\n const registry = getComponentRegistry(framework)\n const names = Object.keys(registry)\n return new RegExp(`<(${names.join('|')})(?=[\\\\s/>])`, 'g')\n}\n","import type { Options, Framework } from '../types'\nimport { getComponentRegistry, buildComponentDetectionRegex } from './registry'\n\nconst DEFAULT_IMPORT_SOURCE = 'unplugin-docubook/components'\nconst DEFAULT_FRAMEWORK: Framework = 'react'\n\nexport function detectComponents(code: string, framework: Framework = DEFAULT_FRAMEWORK): string[] {\n const regex = buildComponentDetectionRegex(framework)\n const registry = getComponentRegistry(framework)\n const found = new Set<string>()\n let match: RegExpExecArray | null\n\n while ((match = regex.exec(code)) !== null) {\n const name = match[1]\n if (name && registry[name]) {\n found.add(name)\n }\n }\n\n return Array.from(found)\n}\n\nexport function generateImports(\n componentNames: string[],\n importSource: string,\n framework: Framework = DEFAULT_FRAMEWORK,\n): string {\n const registry = getComponentRegistry(framework)\n const pathToExports = new Map<string, Set<string>>()\n\n for (const name of componentNames) {\n const meta = registry[name]\n if (!meta) continue\n\n const fullPath = `${importSource}${meta.path}`\n if (!pathToExports.has(fullPath)) {\n pathToExports.set(fullPath, new Set())\n }\n for (const exp of meta.exports) {\n pathToExports.get(fullPath)!.add(exp)\n }\n }\n\n const lines: string[] = []\n for (const [path, exports] of pathToExports) {\n const names = Array.from(exports).sort().join(', ')\n lines.push(`import { ${names} } from '${path}'`)\n }\n\n return lines.join('\\n')\n}\n\nexport function transformMdx(\n code: string,\n options: Options = {},\n): { code: string; hasTransformed: boolean } {\n const framework = options.framework ?? DEFAULT_FRAMEWORK\n const usedComponents = detectComponents(code, framework)\n\n if (usedComponents.length === 0) {\n return { code, hasTransformed: false }\n }\n\n const importSource = options.importSource ?? DEFAULT_IMPORT_SOURCE\n const importStatements = generateImports(usedComponents, importSource, framework)\n\n return {\n code: `${importStatements}\\n\\n${code}`,\n hasTransformed: true,\n }\n}\n"],"mappings":";AAAA,SAAS,2BAA2B;;;ACEpC,SAAS,sBAAsB;;;ACK/B,IAAM,kBAA6C;AAAA,EAC/C,OAAO;AAAA,EACP,KAAK;AAAA,EACL,QAAQ;AACZ;AAEA,SAAS,eAAe,WAAqD;AACzE,QAAM,WAAW,gBAAgB,SAAS;AAE1C,SAAO;AAAA,IACH,MAAM,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,SAA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AY,IAAI,GAAG;AAAA,EACnD;AAEA,SAAO,MAAM,KAAK,IAAI;AAC1B;AAEO,SAAS,aACZ,MACA,UAAmB,CAAC,GACqB;AAvD7C;AAwDI,QAAM,aAAY,aAAQ,cAAR,YAAqB;AACvC,QAAM,iBAAiB,iBAAiB,MAAM,SAAS;AAEvD,MAAI,eAAe,WAAW,GAAG;AAC7B,WAAO,EAAE,MAAM,gBAAgB,MAAM;AAAA,EACzC;AAEA,QAAM,gBAAe,aAAQ,iBAAR,YAAwB;AAC7C,QAAM,mBAAmB,gBAAgB,gBAAgB,cAAc,SAAS;AAEhF,SAAO;AAAA,IACH,MAAM,GAAG,gBAAgB;AAAA;AAAA,EAAO,IAAI;AAAA,IACpC,gBAAgB;AAAA,EACpB;AACJ;;;AFjEO,IAAM,kBAAwD,CACjE,aACE;AAAA,EACF,MAAM;AAAA,EAEN,iBAAiB,IAAI;AAEjB,WAAO,UAAU,KAAK,EAAE;AAAA,EAC5B;AAAA,EAEA,UAAU,MAAM,KAAK;AACjB,UAAM,SAAS,aAAa,MAAM,OAAO;AACzC,QAAI,CAAC,OAAO,eAAgB;AAC5B,WAAO,OAAO;AAAA,EAClB;AACJ;;;ADjBA,IAAO,eAAQ,oBAAoB,eAAe;","names":[]}
1
+ {"version":3,"sources":["../src/next.ts","../src/index.ts","../src/core/registry.ts","../src/core/transform.ts"],"sourcesContent":["import { createWebpackPlugin } from 'unplugin'\nimport { unpluginFactory } from '.'\n\nexport default createWebpackPlugin(unpluginFactory)\n","import type { UnpluginFactory } from 'unplugin'\nimport type { Options } from './types'\nimport { createUnplugin } from 'unplugin'\nimport { transformMdx } from './core/transform'\n\nexport const unpluginFactory: UnpluginFactory<Options | undefined> = (\n options,\n) => ({\n name: 'unplugin-docubook',\n\n transformInclude(id) {\n // Only process .mdx and .md files\n return /\\.mdx?$/.test(id)\n },\n\n transform(code, _id) {\n const result = transformMdx(code, options)\n if (!result.hasTransformed) return\n return result.code\n },\n})\n\nexport const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)\n\nexport default unplugin\n","import type { Framework } from '../types'\n\nexport interface ComponentMeta {\n exports: string[]\n path: string\n}\n\nconst FRAMEWORK_PATHS: Record<Framework, string> = {\n react: '/react',\n vue: '/vue',\n svelte: '/svelte',\n}\n\nfunction createRegistry(framework: Framework): Record<string, ComponentMeta> {\n const basePath = FRAMEWORK_PATHS[framework]\n\n return {\n Note: { exports: ['Note'], path: basePath },\n Card: { exports: ['Card'], path: basePath },\n CardGroup: { exports: ['CardGroup'], path: basePath },\n Accordion: { exports: ['Accordion'], path: basePath },\n AccordionGroup: { exports: ['AccordionGroup'], path: basePath },\n Stepper: { exports: ['Stepper', 'StepperItem'], path: basePath },\n Kbd: { exports: ['Kbd'], path: basePath },\n Tooltip: { exports: ['Tooltip'], path: basePath },\n Youtube: { exports: ['Youtube'], path: basePath },\n Button: { exports: ['Button'], path: basePath },\n DocuLink: { exports: ['DocuLink'], path: basePath },\n DocuImage: { exports: ['DocuImage'], path: basePath },\n Files: { exports: ['Files'], path: basePath },\n Folder: { exports: ['Folder'], path: basePath },\n File: { exports: ['File'], path: basePath },\n Release: { exports: ['Release'], path: basePath },\n Changes: { exports: ['Changes'], path: basePath },\n Pre: { exports: ['Pre'], path: basePath },\n Copy: { exports: ['Copy'], path: basePath },\n DocuBookProvider: { exports: ['DocuBookProvider'], path: basePath },\n }\n}\n\nconst registryCache = new Map<Framework, Record<string, ComponentMeta>>()\nconst regexCache = new Map<Framework, RegExp>()\n\nexport function getComponentRegistry(\n framework: Framework = 'react',\n customComponents?: Record<string, ComponentMeta>,\n): Record<string, ComponentMeta> {\n if (!customComponents || Object.keys(customComponents).length === 0) {\n const cached = registryCache.get(framework)\n if (cached) return cached\n const registry = createRegistry(framework)\n registryCache.set(framework, registry)\n return registry\n }\n // Merge custom components with built-in (custom overrides built-in)\n return { ...createRegistry(framework), ...customComponents }\n}\n\nexport function buildComponentDetectionRegex(\n framework: Framework = 'react',\n customComponents?: Record<string, ComponentMeta>,\n): RegExp {\n // Custom components: always build fresh regex (not cached)\n if (customComponents && Object.keys(customComponents).length > 0) {\n const registry = getComponentRegistry(framework, customComponents)\n const names = Object.keys(registry)\n return new RegExp(`<(${names.join('|')})(?=[\\\\s/>])`, 'g')\n }\n\n const cached = regexCache.get(framework)\n if (cached) {\n cached.lastIndex = 0 // Reset regex state because of /g flag\n return cached\n }\n const registry = getComponentRegistry(framework)\n const names = Object.keys(registry)\n const regex = new RegExp(`<(${names.join('|')})(?=[\\\\s/>])`, 'g')\n regexCache.set(framework, regex)\n return regex\n}\n","import type { Options, Framework } from '../types'\nimport type { ComponentMeta } from './registry'\nimport { getComponentRegistry, buildComponentDetectionRegex } from './registry'\n\nconst DEFAULT_IMPORT_SOURCE = 'unplugin-docubook/components'\nconst DEFAULT_FRAMEWORK: Framework = 'react'\n\n/**\n * Strip content zones where component tags should NOT be detected:\n * - Fenced code blocks (triple backticks)\n * - Inline code (single backticks)\n * - MDX comments\n * - HTML comments\n * - Existing import/export statements\n */\nfunction stripIgnoredZones(code: string): string {\n return code\n .replace(/```[\\s\\S]*?```/g, '') // fenced code blocks\n .replace(/`[^`]+`/g, '') // inline code\n .replace(/\\{\\/\\*[\\s\\S]*?\\*\\/\\}/g, '') // MDX comments {/* */}\n .replace(/<!--[\\s\\S]*?-->/g, '') // HTML comments\n .replace(/^import\\s.+$/gm, '') // existing imports\n .replace(/^export\\s.+$/gm, '') // exports\n}\n\nexport function detectComponents(\n code: string,\n framework: Framework = DEFAULT_FRAMEWORK,\n customComponents?: Record<string, ComponentMeta>,\n): string[] {\n const strippedCode = stripIgnoredZones(code)\n const regex = buildComponentDetectionRegex(framework, customComponents)\n const registry = getComponentRegistry(framework, customComponents)\n const found = new Set<string>()\n let match: RegExpExecArray | null\n\n while ((match = regex.exec(strippedCode)) !== null) {\n const name = match[1]\n if (name && registry[name]) {\n found.add(name)\n }\n }\n\n return Array.from(found)\n}\n\nexport function generateImports(\n componentNames: string[],\n importSource: string,\n framework: Framework = DEFAULT_FRAMEWORK,\n customComponents?: Record<string, ComponentMeta>,\n): string {\n const registry = getComponentRegistry(framework, customComponents)\n const pathToExports = new Map<string, Set<string>>()\n\n for (const name of componentNames) {\n const meta = registry[name]\n if (!meta) continue\n\n // If path starts with '/', it's relative to importSource.\n // Otherwise, it's a full import path (e.g. '@/components/MyComp' or 'my-lib')\n const fullPath = meta.path.startsWith('/')\n ? `${importSource}${meta.path}`\n : meta.path\n\n if (!pathToExports.has(fullPath)) {\n pathToExports.set(fullPath, new Set())\n }\n for (const exp of meta.exports) {\n pathToExports.get(fullPath)!.add(exp)\n }\n }\n\n const lines: string[] = []\n for (const [path, exports] of pathToExports) {\n const names = Array.from(exports).sort().join(', ')\n lines.push(`import { ${names} } from '${path}'`)\n }\n\n return lines.join('\\n')\n}\n\nexport function transformMdx(\n code: string,\n options: Options = {},\n): { code: string; hasTransformed: boolean } {\n const framework = options.framework ?? DEFAULT_FRAMEWORK\n const usedComponents = detectComponents(code, framework, options.customComponents)\n\n if (usedComponents.length === 0) {\n return { code, hasTransformed: false }\n }\n\n const importSource = options.importSource ?? DEFAULT_IMPORT_SOURCE\n const importStatements = generateImports(usedComponents, importSource, framework, options.customComponents)\n\n return {\n code: `${importStatements}\\n\\n${code}`,\n hasTransformed: true,\n }\n}\n\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA,SAAS,2BAA2B;;;ACEpC,SAAS,sBAAsB;;;ACK/B,IAAM,kBAA6C;AAAA,EAC/C,OAAO;AAAA,EACP,KAAK;AAAA,EACL,QAAQ;AACZ;AAEA,SAAS,eAAe,WAAqD;AACzE,QAAM,WAAW,gBAAgB,SAAS;AAE1C,SAAO;AAAA,IACH,MAAM,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,SAAS;AAAA,IAC1C,MAAM,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,SAAS;AAAA,IAC1C,WAAW,EAAE,SAAS,CAAC,WAAW,GAAG,MAAM,SAAS;AAAA,IACpD,W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c,WAAW,QAAQ,gBAAgB;AAE1G,SAAO;AAAA,IACH,MAAM,GAAG,gBAAgB;AAAA;AAAA,EAAO,IAAI;AAAA,IACpC,gBAAgB;AAAA,EACpB;AACJ;;;AF/FO,IAAM,kBAAwD,CACjE,aACE;AAAA,EACF,MAAM;AAAA,EAEN,iBAAiB,IAAI;AAEjB,WAAO,UAAU,KAAK,EAAE;AAAA,EAC5B;AAAA,EAEA,UAAU,MAAM,KAAK;AACjB,UAAM,SAAS,aAAa,MAAM,OAAO;AACzC,QAAI,CAAC,OAAO,eAAgB;AAC5B,WAAO,OAAO;AAAA,EAClB;AACJ;;;ADjBA,IAAO,eAAQ,oBAAoB,eAAe;","names":["registry","names"]}
package/dist/nuxt.cjs CHANGED
@@ -2,7 +2,21 @@
2
2
  var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __spreadValues = (a, b) => {
10
+ for (var prop in b || (b = {}))
11
+ if (__hasOwnProp.call(b, prop))
12
+ __defNormalProp(a, prop, b[prop]);
13
+ if (__getOwnPropSymbols)
14
+ for (var prop of __getOwnPropSymbols(b)) {
15
+ if (__propIsEnum.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ }
18
+ return a;
19
+ };
6
20
  var __export = (target, all) => {
7
21
  for (var name in all)
8
22
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -62,24 +76,49 @@ function createRegistry(framework) {
62
76
  DocuBookProvider: { exports: ["DocuBookProvider"], path: basePath }
63
77
  };
64
78
  }
65
- function getComponentRegistry(framework = "react") {
66
- return createRegistry(framework);
79
+ var registryCache = /* @__PURE__ */ new Map();
80
+ var regexCache = /* @__PURE__ */ new Map();
81
+ function getComponentRegistry(framework = "react", customComponents) {
82
+ if (!customComponents || Object.keys(customComponents).length === 0) {
83
+ const cached = registryCache.get(framework);
84
+ if (cached) return cached;
85
+ const registry = createRegistry(framework);
86
+ registryCache.set(framework, registry);
87
+ return registry;
88
+ }
89
+ return __spreadValues(__spreadValues({}, createRegistry(framework)), customComponents);
67
90
  }
68
- function buildComponentDetectionRegex(framework = "react") {
91
+ function buildComponentDetectionRegex(framework = "react", customComponents) {
92
+ if (customComponents && Object.keys(customComponents).length > 0) {
93
+ const registry2 = getComponentRegistry(framework, customComponents);
94
+ const names2 = Object.keys(registry2);
95
+ return new RegExp(`<(${names2.join("|")})(?=[\\s/>])`, "g");
96
+ }
97
+ const cached = regexCache.get(framework);
98
+ if (cached) {
99
+ cached.lastIndex = 0;
100
+ return cached;
101
+ }
69
102
  const registry = getComponentRegistry(framework);
70
103
  const names = Object.keys(registry);
71
- return new RegExp(`<(${names.join("|")})(?=[\\s/>])`, "g");
104
+ const regex = new RegExp(`<(${names.join("|")})(?=[\\s/>])`, "g");
105
+ regexCache.set(framework, regex);
106
+ return regex;
72
107
  }
73
108
 
74
109
  // src/core/transform.ts
75
110
  var DEFAULT_IMPORT_SOURCE = "unplugin-docubook/components";
76
111
  var DEFAULT_FRAMEWORK = "react";
77
- function detectComponents(code, framework = DEFAULT_FRAMEWORK) {
78
- const regex = buildComponentDetectionRegex(framework);
79
- const registry = getComponentRegistry(framework);
112
+ function stripIgnoredZones(code) {
113
+ return code.replace(/```[\s\S]*?```/g, "").replace(/`[^`]+`/g, "").replace(/\{\/\*[\s\S]*?\*\/\}/g, "").replace(/<!--[\s\S]*?-->/g, "").replace(/^import\s.+$/gm, "").replace(/^export\s.+$/gm, "");
114
+ }
115
+ function detectComponents(code, framework = DEFAULT_FRAMEWORK, customComponents) {
116
+ const strippedCode = stripIgnoredZones(code);
117
+ const regex = buildComponentDetectionRegex(framework, customComponents);
118
+ const registry = getComponentRegistry(framework, customComponents);
80
119
  const found = /* @__PURE__ */ new Set();
81
120
  let match;
82
- while ((match = regex.exec(code)) !== null) {
121
+ while ((match = regex.exec(strippedCode)) !== null) {
83
122
  const name = match[1];
84
123
  if (name && registry[name]) {
85
124
  found.add(name);
@@ -87,13 +126,13 @@ function detectComponents(code, framework = DEFAULT_FRAMEWORK) {
87
126
  }
88
127
  return Array.from(found);
89
128
  }
90
- function generateImports(componentNames, importSource, framework = DEFAULT_FRAMEWORK) {
91
- const registry = getComponentRegistry(framework);
129
+ function generateImports(componentNames, importSource, framework = DEFAULT_FRAMEWORK, customComponents) {
130
+ const registry = getComponentRegistry(framework, customComponents);
92
131
  const pathToExports = /* @__PURE__ */ new Map();
93
132
  for (const name of componentNames) {
94
133
  const meta = registry[name];
95
134
  if (!meta) continue;
96
- const fullPath = `${importSource}${meta.path}`;
135
+ const fullPath = meta.path.startsWith("/") ? `${importSource}${meta.path}` : meta.path;
97
136
  if (!pathToExports.has(fullPath)) {
98
137
  pathToExports.set(fullPath, /* @__PURE__ */ new Set());
99
138
  }
@@ -111,12 +150,12 @@ function generateImports(componentNames, importSource, framework = DEFAULT_FRAME
111
150
  function transformMdx(code, options = {}) {
112
151
  var _a, _b;
113
152
  const framework = (_a = options.framework) != null ? _a : DEFAULT_FRAMEWORK;
114
- const usedComponents = detectComponents(code, framework);
153
+ const usedComponents = detectComponents(code, framework, options.customComponents);
115
154
  if (usedComponents.length === 0) {
116
155
  return { code, hasTransformed: false };
117
156
  }
118
157
  const importSource = (_b = options.importSource) != null ? _b : DEFAULT_IMPORT_SOURCE;
119
- const importStatements = generateImports(usedComponents, importSource, framework);
158
+ const importStatements = generateImports(usedComponents, importSource, framework, options.customComponents);
120
159
  return {
121
160
  code: `${importStatements}
122
161
 
package/dist/nuxt.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/nuxt.ts","../src/vite.ts","../src/index.ts","../src/core/registry.ts","../src/core/transform.ts","../src/webpack.ts"],"sourcesContent":["import type { Options } from './types'\nimport { addVitePlugin, addWebpackPlugin, defineNuxtModule } from '@nuxt/kit'\nimport vite from './vite'\nimport webpack from './webpack'\n\nexport default defineNuxtModule<Options>({\n meta: {\n name: 'nuxt-unplugin-docubook',\n configKey: 'docubookMdx',\n },\n defaults: {},\n setup(options, _nuxt) {\n addVitePlugin(() => vite(options))\n addWebpackPlugin(() => webpack(options))\n },\n})\n","import { createVitePlugin } from 'unplugin'\nimport { unpluginFactory } from '.'\n\nexport default createVitePlugin(unpluginFactory)\n","import type { UnpluginFactory } from 'unplugin'\nimport type { Options } from './types'\nimport { createUnplugin } from 'unplugin'\nimport { transformMdx } from './core/transform'\n\nexport const unpluginFactory: UnpluginFactory<Options | undefined> = (\n options,\n) => ({\n name: 'unplugin-docubook',\n\n transformInclude(id) {\n // Only process .mdx and .md files\n return /\\.mdx?$/.test(id)\n },\n\n transform(code, _id) {\n const result = transformMdx(code, options)\n if (!result.hasTransformed) return\n return result.code\n },\n})\n\nexport const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)\n\nexport default unplugin\n","import type { Framework } from '../types'\n\nexport interface ComponentMeta {\n exports: string[]\n path: string\n}\n\nconst FRAMEWORK_PATHS: Record<Framework, string> = {\n react: '/react',\n vue: '/vue',\n svelte: '/svelte',\n}\n\nfunction createRegistry(framework: Framework): Record<string, ComponentMeta> {\n const basePath = FRAMEWORK_PATHS[framework]\n\n return {\n Note: { exports: ['Note'], path: basePath },\n Card: { exports: ['Card'], path: basePath },\n CardGroup: { exports: ['CardGroup'], path: basePath },\n Accordion: { exports: ['Accordion'], path: basePath },\n AccordionGroup: { exports: ['AccordionGroup'], path: basePath },\n Stepper: { exports: ['Stepper', 'StepperItem'], path: basePath },\n Kbd: { exports: ['Kbd'], path: basePath },\n Tooltip: { exports: ['Tooltip'], path: basePath },\n Youtube: { exports: ['Youtube'], path: basePath },\n Button: { exports: ['Button'], path: basePath },\n DocuLink: { exports: ['DocuLink'], path: basePath },\n DocuImage: { exports: ['DocuImage'], path: basePath },\n Files: { exports: ['Files'], path: basePath },\n Folder: { exports: ['Folder'], path: basePath },\n File: { exports: ['File'], path: basePath },\n Release: { exports: ['Release'], path: basePath },\n Changes: { exports: ['Changes'], path: basePath },\n Pre: { exports: ['Pre'], path: basePath },\n Copy: { exports: ['Copy'], path: basePath },\n DocuBookProvider: { exports: ['DocuBookProvider'], path: basePath },\n }\n}\n\nexport function getComponentRegistry(framework: Framework = 'react'): Record<string, ComponentMeta> {\n return createRegistry(framework)\n}\n\nexport function buildComponentDetectionRegex(framework: Framework = 'react'): RegExp {\n const registry = getComponentRegistry(framework)\n const names = Object.keys(registry)\n return new RegExp(`<(${names.join('|')})(?=[\\\\s/>])`, 'g')\n}\n","import type { Options, Framework } from '../types'\nimport { getComponentRegistry, buildComponentDetectionRegex } from './registry'\n\nconst DEFAULT_IMPORT_SOURCE = 'unplugin-docubook/components'\nconst DEFAULT_FRAMEWORK: Framework = 'react'\n\nexport function detectComponents(code: string, framework: Framework = DEFAULT_FRAMEWORK): string[] {\n const regex = buildComponentDetectionRegex(framework)\n const registry = getComponentRegistry(framework)\n const found = new Set<string>()\n let match: RegExpExecArray | null\n\n while ((match = regex.exec(code)) !== null) {\n const name = match[1]\n if (name && registry[name]) {\n found.add(name)\n }\n }\n\n return Array.from(found)\n}\n\nexport function generateImports(\n componentNames: string[],\n importSource: string,\n framework: Framework = DEFAULT_FRAMEWORK,\n): string {\n const registry = getComponentRegistry(framework)\n const pathToExports = new Map<string, Set<string>>()\n\n for (const name of componentNames) {\n const meta = registry[name]\n if (!meta) continue\n\n const fullPath = `${importSource}${meta.path}`\n if (!pathToExports.has(fullPath)) {\n pathToExports.set(fullPath, new Set())\n }\n for (const exp of meta.exports) {\n pathToExports.get(fullPath)!.add(exp)\n }\n }\n\n const lines: string[] = []\n for (const [path, exports] of pathToExports) {\n const names = Array.from(exports).sort().join(', ')\n lines.push(`import { ${names} } from '${path}'`)\n }\n\n return lines.join('\\n')\n}\n\nexport function transformMdx(\n code: string,\n options: Options = {},\n): { code: string; hasTransformed: boolean } {\n const framework = options.framework ?? DEFAULT_FRAMEWORK\n const usedComponents = detectComponents(code, framework)\n\n if (usedComponents.length === 0) {\n return { code, hasTransformed: false }\n }\n\n const importSource = options.importSource ?? DEFAULT_IMPORT_SOURCE\n const importStatements = generateImports(usedComponents, importSource, framework)\n\n return {\n code: `${importStatements}\\n\\n${code}`,\n hasTransformed: true,\n }\n}\n","import { createWebpackPlugin } from 'unplugin'\nimport { unpluginFactory } from '.'\n\nexport default createWebpackPlugin(unpluginFactory)\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,iBAAkE;;;ACDlE,IAAAA,mBAAiC;;;ACEjC,sBAA+B;;;ACK/B,IAAM,kBAA6C;AAAA,EAC/C,OAAO;AAAA,EACP,KAAK;AAAA,EACL,QAAQ;AACZ;AAEA,SAAS,eAAe,WAAqD;AACzE,QAAM,WAAW,gBAAgB,SAAS;AAE1C,SAAO;AAAA,IACH,MAAM,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,SAAS;AAAA,IAC1C,MAAM,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,SAAS;AAAA,IAC1C,WAAW,EAAE,SAAS,CAAC,WAAW,GAAG,MAAM,SAAS;AAAA,IACpD,WAAW,EAAE,SAAS,CAAC,WAAW,GAAG,MAAM,SAAS;AAAA,IACpD,gBAAgB,EAAE,SAAS,CAAC,gBAAgB,GAAG,MAAM,SAAS;AAAA,IAC9D,SAAS,EAAE,SAAS,CAAC,WAAW,aAAa,GAAG,MAAM,SAAS;AAAA,IAC/D,KAAK,EAAE,SAAS,CAAC,KAAK,GAAG,MAAM,SAAS;AAAA,IACxC,SAAS,EAAE,SAAS,CAAC,SAAS,GAAG,MAAM,SAAS;AAAA,IAChD,SAAS,EAAE,SAAS,CAAC,SAAS,GAAG,MAAM,SAAS;AAAA,IACh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,aACE;AAAA,EACF,MAAM;AAAA,EAEN,iBAAiB,IAAI;AAEjB,WAAO,UAAU,KAAK,EAAE;AAAA,EAC5B;AAAA,EAEA,UAAU,MAAM,KAAK;AACjB,UAAM,SAAS,aAAa,MAAM,OAAO;AACzC,QAAI,CAAC,OAAO,eAAgB;AAC5B,WAAO,OAAO;AAAA,EAClB;AACJ;;;ADjBA,IAAO,mBAAQ,mCAAiB,eAAe;;;AIH/C,IAAAC,mBAAoC;AAGpC,IAAO,sBAAQ,sCAAoB,eAAe;;;ALElD,IAAO,mBAAQ,6BAA0B;AAAA,EACrC,MAAM;AAAA,IACF,MAAM;AAAA,IACN,WAAW;AAAA,EACf;AAAA,EACA,UAAU,CAAC;AAAA,EACX,MAAM,SAAS,OAAO;AAClB,kCAAc,MAAM,aAAK,OAAO,CAAC;AACjC,qCAAiB,MAAM,gBAAQ,OAAO,CAAC;AAAA,EAC3C;AACJ,CAAC;","names":["import_unplugin","exports","import_unplugin"]}
1
+ {"version":3,"sources":["../src/nuxt.ts","../src/vite.ts","../src/index.ts","../src/core/registry.ts","../src/core/transform.ts","../src/webpack.ts"],"sourcesContent":["import type { Options } from './types'\nimport { addVitePlugin, addWebpackPlugin, defineNuxtModule } from '@nuxt/kit'\nimport vite from './vite'\nimport webpack from './webpack'\n\nexport default defineNuxtModule<Options>({\n meta: {\n name: 'nuxt-unplugin-docubook',\n configKey: 'docubookMdx',\n },\n defaults: {},\n setup(options, _nuxt) {\n addVitePlugin(() => vite(options))\n addWebpackPlugin(() => webpack(options))\n },\n})\n","import { createVitePlugin } from 'unplugin'\nimport { unpluginFactory } from '.'\n\nexport default createVitePlugin(unpluginFactory)\n","import type { UnpluginFactory } from 'unplugin'\nimport type { Options } from './types'\nimport { createUnplugin } from 'unplugin'\nimport { transformMdx } from './core/transform'\n\nexport const unpluginFactory: UnpluginFactory<Options | undefined> = (\n options,\n) => ({\n name: 'unplugin-docubook',\n\n transformInclude(id) {\n // Only process .mdx and .md files\n return /\\.mdx?$/.test(id)\n },\n\n transform(code, _id) {\n const result = transformMdx(code, options)\n if (!result.hasTransformed) return\n return result.code\n },\n})\n\nexport const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)\n\nexport default unplugin\n","import type { Framework } from '../types'\n\nexport interface ComponentMeta {\n exports: string[]\n path: string\n}\n\nconst FRAMEWORK_PATHS: Record<Framework, string> = {\n react: '/react',\n vue: '/vue',\n svelte: '/svelte',\n}\n\nfunction createRegistry(framework: Framework): Record<string, ComponentMeta> {\n const basePath = FRAMEWORK_PATHS[framework]\n\n return {\n Note: { exports: ['Note'], path: basePath },\n Card: { exports: ['Card'], path: basePath },\n CardGroup: { exports: ['CardGroup'], path: basePath },\n Accordion: { exports: ['Accordion'], path: basePath },\n AccordionGroup: { exports: ['AccordionGroup'], path: basePath },\n Stepper: { exports: ['Stepper', 'StepperItem'], path: basePath },\n Kbd: { exports: ['Kbd'], path: basePath },\n Tooltip: { exports: ['Tooltip'], path: basePath },\n Youtube: { exports: ['Youtube'], path: basePath },\n Button: { exports: ['Button'], path: basePath },\n DocuLink: { exports: ['DocuLink'], path: basePath },\n DocuImage: { exports: ['DocuImage'], path: basePath },\n Files: { exports: ['Files'], path: basePath },\n Folder: { exports: ['Folder'], path: basePath },\n File: { exports: ['File'], path: basePath },\n Release: { exports: ['Release'], path: basePath },\n Changes: { exports: ['Changes'], path: basePath },\n Pre: { exports: ['Pre'], path: basePath },\n Copy: { exports: ['Copy'], path: basePath },\n DocuBookProvider: { exports: ['DocuBookProvider'], path: basePath },\n }\n}\n\nconst registryCache = new Map<Framework, Record<string, ComponentMeta>>()\nconst regexCache = new Map<Framework, RegExp>()\n\nexport function getComponentRegistry(\n framework: Framework = 'react',\n customComponents?: Record<string, ComponentMeta>,\n): Record<string, ComponentMeta> {\n if (!customComponents || Object.keys(customComponents).length === 0) {\n const cached = registryCache.get(framework)\n if (cached) return cached\n const registry = createRegistry(framework)\n registryCache.set(framework, registry)\n return registry\n }\n // Merge custom components with built-in (custom overrides built-in)\n return { ...createRegistry(framework), ...customComponents }\n}\n\nexport function buildComponentDetectionRegex(\n framework: Framework = 'react',\n customComponents?: Record<string, ComponentMeta>,\n): RegExp {\n // Custom components: always build fresh regex (not cached)\n if (customComponents && Object.keys(customComponents).length > 0) {\n const registry = getComponentRegistry(framework, customComponents)\n const names = Object.keys(registry)\n return new RegExp(`<(${names.join('|')})(?=[\\\\s/>])`, 'g')\n }\n\n const cached = regexCache.get(framework)\n if (cached) {\n cached.lastIndex = 0 // Reset regex state because of /g flag\n return cached\n }\n const registry = getComponentRegistry(framework)\n const names = Object.keys(registry)\n const regex = new RegExp(`<(${names.join('|')})(?=[\\\\s/>])`, 'g')\n regexCache.set(framework, regex)\n return regex\n}\n","import type { Options, Framework } from '../types'\nimport type { ComponentMeta } from './registry'\nimport { getComponentRegistry, buildComponentDetectionRegex } from './registry'\n\nconst DEFAULT_IMPORT_SOURCE = 'unplugin-docubook/components'\nconst DEFAULT_FRAMEWORK: Framework = 'react'\n\n/**\n * Strip content zones where component tags should NOT be detected:\n * - Fenced code blocks (triple backticks)\n * - Inline code (single backticks)\n * - MDX comments\n * - HTML comments\n * - Existing import/export statements\n */\nfunction stripIgnoredZones(code: string): string {\n return code\n .replace(/```[\\s\\S]*?```/g, '') // fenced code blocks\n .replace(/`[^`]+`/g, '') // inline code\n .replace(/\\{\\/\\*[\\s\\S]*?\\*\\/\\}/g, '') // MDX comments {/* */}\n .replace(/<!--[\\s\\S]*?-->/g, '') // HTML comments\n .replace(/^import\\s.+$/gm, '') // existing imports\n .replace(/^export\\s.+$/gm, '') // exports\n}\n\nexport function detectComponents(\n code: string,\n framework: Framework = DEFAULT_FRAMEWORK,\n customComponents?: Record<string, ComponentMeta>,\n): string[] {\n const strippedCode = stripIgnoredZones(code)\n const regex = buildComponentDetectionRegex(framework, customComponents)\n const registry = getComponentRegistry(framework, customComponents)\n const found = new Set<string>()\n let match: RegExpExecArray | null\n\n while ((match = regex.exec(strippedCode)) !== null) {\n const name = match[1]\n if (name && registry[name]) {\n found.add(name)\n }\n }\n\n return Array.from(found)\n}\n\nexport function generateImports(\n componentNames: string[],\n importSource: string,\n framework: Framework = DEFAULT_FRAMEWORK,\n customComponents?: Record<string, ComponentMeta>,\n): string {\n const registry = getComponentRegistry(framework, customComponents)\n const pathToExports = new Map<string, Set<string>>()\n\n for (const name of componentNames) {\n const meta = registry[name]\n if (!meta) continue\n\n // If path starts with '/', it's relative to importSource.\n // Otherwise, it's a full import path (e.g. '@/components/MyComp' or 'my-lib')\n const fullPath = meta.path.startsWith('/')\n ? `${importSource}${meta.path}`\n : meta.path\n\n if (!pathToExports.has(fullPath)) {\n pathToExports.set(fullPath, new Set())\n }\n for (const exp of meta.exports) {\n pathToExports.get(fullPath)!.add(exp)\n }\n }\n\n const lines: string[] = []\n for (const [path, exports] of pathToExports) {\n const names = Array.from(exports).sort().join(', ')\n lines.push(`import { ${names} } from '${path}'`)\n }\n\n return lines.join('\\n')\n}\n\nexport function transformMdx(\n code: string,\n options: Options = {},\n): { code: string; hasTransformed: boolean } {\n const framework = options.framework ?? DEFAULT_FRAMEWORK\n const usedComponents = detectComponents(code, framework, options.customComponents)\n\n if (usedComponents.length === 0) {\n return { code, hasTransformed: false }\n }\n\n const importSource = options.importSource ?? DEFAULT_IMPORT_SOURCE\n const importStatements = generateImports(usedComponents, importSource, framework, options.customComponents)\n\n return {\n code: `${importStatements}\\n\\n${code}`,\n hasTransformed: true,\n }\n}\n\n","import { createWebpackPlugin } from 'unplugin'\nimport { unpluginFactory } from '.'\n\nexport default createWebpackPlugin(unpluginFactory)\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,iBAAkE;;;ACDlE,IAAAA,mBAAiC;;;ACEjC,sBAA+B;;;ACK/B,IAAM,kBAA6C;AAAA,EAC/C,OAAO;AAAA,EACP,KAAK;AAAA,EACL,QAAQ;AACZ;AAEA,SAAS,eAAe,WAAqD;AAC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AQ,cAAR,YAAqB;AACvC,QAAM,iBAAiB,iBAAiB,MAAM,WAAW,QAAQ,gBAAgB;AAEjF,MAAI,eAAe,WAAW,GAAG;AAC7B,WAAO,EAAE,MAAM,gBAAgB,MAAM;AAAA,EACzC;AAEA,QAAM,gBAAe,aAAQ,iBAAR,YAAwB;AAC7C,QAAM,mBAAmB,gBAAgB,gBAAgB,cAAc,WAAW,QAAQ,gBAAgB;AAE1G,SAAO;AAAA,IACH,MAAM,GAAG,gBAAgB;AAAA;AAAA,EAAO,IAAI;AAAA,IACpC,gBAAgB;AAAA,EACpB;AACJ;;;AF/FO,IAAM,kBAAwD,CACjE,aACE;AAAA,EACF,MAAM;AAAA,EAEN,iBAAiB,IAAI;AAEjB,WAAO,UAAU,KAAK,EAAE;AAAA,EAC5B;AAAA,EAEA,UAAU,MAAM,KAAK;AACjB,UAAM,SAAS,aAAa,MAAM,OAAO;AACzC,QAAI,CAAC,OAAO,eAAgB;AAC5B,WAAO,OAAO;AAAA,EAClB;AACJ;;;ADjBA,IAAO,mBAAQ,mCAAiB,eAAe;;;AIH/C,IAAAC,mBAAoC;AAGpC,IAAO,sBAAQ,sCAAoB,eAAe;;;ALElD,IAAO,mBAAQ,6BAA0B;AAAA,EACrC,MAAM;AAAA,IACF,MAAM;AAAA,IACN,WAAW;AAAA,EACf;AAAA,EACA,UAAU,CAAC;AAAA,EACX,MAAM,SAAS,OAAO;AAClB,kCAAc,MAAM,aAAK,OAAO,CAAC;AACjC,qCAAiB,MAAM,gBAAQ,OAAO,CAAC;AAAA,EAC3C;AACJ,CAAC;","names":["import_unplugin","registry","names","exports","import_unplugin"]}
package/dist/nuxt.js CHANGED
@@ -1,3 +1,20 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
4
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __spreadValues = (a, b) => {
7
+ for (var prop in b || (b = {}))
8
+ if (__hasOwnProp.call(b, prop))
9
+ __defNormalProp(a, prop, b[prop]);
10
+ if (__getOwnPropSymbols)
11
+ for (var prop of __getOwnPropSymbols(b)) {
12
+ if (__propIsEnum.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ }
15
+ return a;
16
+ };
17
+
1
18
  // src/nuxt.ts
2
19
  import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from "@nuxt/kit";
3
20
 
@@ -38,24 +55,49 @@ function createRegistry(framework) {
38
55
  DocuBookProvider: { exports: ["DocuBookProvider"], path: basePath }
39
56
  };
40
57
  }
41
- function getComponentRegistry(framework = "react") {
42
- return createRegistry(framework);
58
+ var registryCache = /* @__PURE__ */ new Map();
59
+ var regexCache = /* @__PURE__ */ new Map();
60
+ function getComponentRegistry(framework = "react", customComponents) {
61
+ if (!customComponents || Object.keys(customComponents).length === 0) {
62
+ const cached = registryCache.get(framework);
63
+ if (cached) return cached;
64
+ const registry = createRegistry(framework);
65
+ registryCache.set(framework, registry);
66
+ return registry;
67
+ }
68
+ return __spreadValues(__spreadValues({}, createRegistry(framework)), customComponents);
43
69
  }
44
- function buildComponentDetectionRegex(framework = "react") {
70
+ function buildComponentDetectionRegex(framework = "react", customComponents) {
71
+ if (customComponents && Object.keys(customComponents).length > 0) {
72
+ const registry2 = getComponentRegistry(framework, customComponents);
73
+ const names2 = Object.keys(registry2);
74
+ return new RegExp(`<(${names2.join("|")})(?=[\\s/>])`, "g");
75
+ }
76
+ const cached = regexCache.get(framework);
77
+ if (cached) {
78
+ cached.lastIndex = 0;
79
+ return cached;
80
+ }
45
81
  const registry = getComponentRegistry(framework);
46
82
  const names = Object.keys(registry);
47
- return new RegExp(`<(${names.join("|")})(?=[\\s/>])`, "g");
83
+ const regex = new RegExp(`<(${names.join("|")})(?=[\\s/>])`, "g");
84
+ regexCache.set(framework, regex);
85
+ return regex;
48
86
  }
49
87
 
50
88
  // src/core/transform.ts
51
89
  var DEFAULT_IMPORT_SOURCE = "unplugin-docubook/components";
52
90
  var DEFAULT_FRAMEWORK = "react";
53
- function detectComponents(code, framework = DEFAULT_FRAMEWORK) {
54
- const regex = buildComponentDetectionRegex(framework);
55
- const registry = getComponentRegistry(framework);
91
+ function stripIgnoredZones(code) {
92
+ return code.replace(/```[\s\S]*?```/g, "").replace(/`[^`]+`/g, "").replace(/\{\/\*[\s\S]*?\*\/\}/g, "").replace(/<!--[\s\S]*?-->/g, "").replace(/^import\s.+$/gm, "").replace(/^export\s.+$/gm, "");
93
+ }
94
+ function detectComponents(code, framework = DEFAULT_FRAMEWORK, customComponents) {
95
+ const strippedCode = stripIgnoredZones(code);
96
+ const regex = buildComponentDetectionRegex(framework, customComponents);
97
+ const registry = getComponentRegistry(framework, customComponents);
56
98
  const found = /* @__PURE__ */ new Set();
57
99
  let match;
58
- while ((match = regex.exec(code)) !== null) {
100
+ while ((match = regex.exec(strippedCode)) !== null) {
59
101
  const name = match[1];
60
102
  if (name && registry[name]) {
61
103
  found.add(name);
@@ -63,13 +105,13 @@ function detectComponents(code, framework = DEFAULT_FRAMEWORK) {
63
105
  }
64
106
  return Array.from(found);
65
107
  }
66
- function generateImports(componentNames, importSource, framework = DEFAULT_FRAMEWORK) {
67
- const registry = getComponentRegistry(framework);
108
+ function generateImports(componentNames, importSource, framework = DEFAULT_FRAMEWORK, customComponents) {
109
+ const registry = getComponentRegistry(framework, customComponents);
68
110
  const pathToExports = /* @__PURE__ */ new Map();
69
111
  for (const name of componentNames) {
70
112
  const meta = registry[name];
71
113
  if (!meta) continue;
72
- const fullPath = `${importSource}${meta.path}`;
114
+ const fullPath = meta.path.startsWith("/") ? `${importSource}${meta.path}` : meta.path;
73
115
  if (!pathToExports.has(fullPath)) {
74
116
  pathToExports.set(fullPath, /* @__PURE__ */ new Set());
75
117
  }
@@ -87,12 +129,12 @@ function generateImports(componentNames, importSource, framework = DEFAULT_FRAME
87
129
  function transformMdx(code, options = {}) {
88
130
  var _a, _b;
89
131
  const framework = (_a = options.framework) != null ? _a : DEFAULT_FRAMEWORK;
90
- const usedComponents = detectComponents(code, framework);
132
+ const usedComponents = detectComponents(code, framework, options.customComponents);
91
133
  if (usedComponents.length === 0) {
92
134
  return { code, hasTransformed: false };
93
135
  }
94
136
  const importSource = (_b = options.importSource) != null ? _b : DEFAULT_IMPORT_SOURCE;
95
- const importStatements = generateImports(usedComponents, importSource, framework);
137
+ const importStatements = generateImports(usedComponents, importSource, framework, options.customComponents);
96
138
  return {
97
139
  code: `${importStatements}
98
140
 
package/dist/nuxt.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/nuxt.ts","../src/vite.ts","../src/index.ts","../src/core/registry.ts","../src/core/transform.ts","../src/webpack.ts"],"sourcesContent":["import type { Options } from './types'\nimport { addVitePlugin, addWebpackPlugin, defineNuxtModule } from '@nuxt/kit'\nimport vite from './vite'\nimport webpack from './webpack'\n\nexport default defineNuxtModule<Options>({\n meta: {\n name: 'nuxt-unplugin-docubook',\n configKey: 'docubookMdx',\n },\n defaults: {},\n setup(options, _nuxt) {\n addVitePlugin(() => vite(options))\n addWebpackPlugin(() => webpack(options))\n },\n})\n","import { createVitePlugin } from 'unplugin'\nimport { unpluginFactory } from '.'\n\nexport default createVitePlugin(unpluginFactory)\n","import type { UnpluginFactory } from 'unplugin'\nimport type { Options } from './types'\nimport { createUnplugin } from 'unplugin'\nimport { transformMdx } from './core/transform'\n\nexport const unpluginFactory: UnpluginFactory<Options | undefined> = (\n options,\n) => ({\n name: 'unplugin-docubook',\n\n transformInclude(id) {\n // Only process .mdx and .md files\n return /\\.mdx?$/.test(id)\n },\n\n transform(code, _id) {\n const result = transformMdx(code, options)\n if (!result.hasTransformed) return\n return result.code\n },\n})\n\nexport const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)\n\nexport default unplugin\n","import type { Framework } from '../types'\n\nexport interface ComponentMeta {\n exports: string[]\n path: string\n}\n\nconst FRAMEWORK_PATHS: Record<Framework, string> = {\n react: '/react',\n vue: '/vue',\n svelte: '/svelte',\n}\n\nfunction createRegistry(framework: Framework): Record<string, ComponentMeta> {\n const basePath = FRAMEWORK_PATHS[framework]\n\n return {\n Note: { exports: ['Note'], path: basePath },\n Card: { exports: ['Card'], path: basePath },\n CardGroup: { exports: ['CardGroup'], path: basePath },\n Accordion: { exports: ['Accordion'], path: basePath },\n AccordionGroup: { exports: ['AccordionGroup'], path: basePath },\n Stepper: { exports: ['Stepper', 'StepperItem'], path: basePath },\n Kbd: { exports: ['Kbd'], path: basePath },\n Tooltip: { exports: ['Tooltip'], path: basePath },\n Youtube: { exports: ['Youtube'], path: basePath },\n Button: { exports: ['Button'], path: basePath },\n DocuLink: { exports: ['DocuLink'], path: basePath },\n DocuImage: { exports: ['DocuImage'], path: basePath },\n Files: { exports: ['Files'], path: basePath },\n Folder: { exports: ['Folder'], path: basePath },\n File: { exports: ['File'], path: basePath },\n Release: { exports: ['Release'], path: basePath },\n Changes: { exports: ['Changes'], path: basePath },\n Pre: { exports: ['Pre'], path: basePath },\n Copy: { exports: ['Copy'], path: basePath },\n DocuBookProvider: { exports: ['DocuBookProvider'], path: basePath },\n }\n}\n\nexport function getComponentRegistry(framework: Framework = 'react'): Record<string, ComponentMeta> {\n return createRegistry(framework)\n}\n\nexport function buildComponentDetectionRegex(framework: Framework = 'react'): RegExp {\n const registry = getComponentRegistry(framework)\n const names = Object.keys(registry)\n return new RegExp(`<(${names.join('|')})(?=[\\\\s/>])`, 'g')\n}\n","import type { Options, Framework } from '../types'\nimport { getComponentRegistry, buildComponentDetectionRegex } from './registry'\n\nconst DEFAULT_IMPORT_SOURCE = 'unplugin-docubook/components'\nconst DEFAULT_FRAMEWORK: Framework = 'react'\n\nexport function detectComponents(code: string, framework: Framework = DEFAULT_FRAMEWORK): string[] {\n const regex = buildComponentDetectionRegex(framework)\n const registry = getComponentRegistry(framework)\n const found = new Set<string>()\n let match: RegExpExecArray | null\n\n while ((match = regex.exec(code)) !== null) {\n const name = match[1]\n if (name && registry[name]) {\n found.add(name)\n }\n }\n\n return Array.from(found)\n}\n\nexport function generateImports(\n componentNames: string[],\n importSource: string,\n framework: Framework = DEFAULT_FRAMEWORK,\n): string {\n const registry = getComponentRegistry(framework)\n const pathToExports = new Map<string, Set<string>>()\n\n for (const name of componentNames) {\n const meta = registry[name]\n if (!meta) continue\n\n const fullPath = `${importSource}${meta.path}`\n if (!pathToExports.has(fullPath)) {\n pathToExports.set(fullPath, new Set())\n }\n for (const exp of meta.exports) {\n pathToExports.get(fullPath)!.add(exp)\n }\n }\n\n const lines: string[] = []\n for (const [path, exports] of pathToExports) {\n const names = Array.from(exports).sort().join(', ')\n lines.push(`import { ${names} } from '${path}'`)\n }\n\n return lines.join('\\n')\n}\n\nexport function transformMdx(\n code: string,\n options: Options = {},\n): { code: string; hasTransformed: boolean } {\n const framework = options.framework ?? DEFAULT_FRAMEWORK\n const usedComponents = detectComponents(code, framework)\n\n if (usedComponents.length === 0) {\n return { code, hasTransformed: false }\n }\n\n const importSource = options.importSource ?? DEFAULT_IMPORT_SOURCE\n const importStatements = generateImports(usedComponents, importSource, framework)\n\n return {\n code: `${importStatements}\\n\\n${code}`,\n hasTransformed: true,\n }\n}\n","import { createWebpackPlugin } from 'unplugin'\nimport { unpluginFactory } from '.'\n\nexport default createWebpackPlugin(unpluginFactory)\n"],"mappings":";AACA,SAAS,eAAe,kBAAkB,wBAAwB;;;ACDlE,SAAS,wBAAwB;;;ACEjC,SAAS,sBAAsB;;;ACK/B,IAAM,kBAA6C;AAAA,EAC/C,OAAO;AAAA,EACP,KAAK;AAAA,EACL,QAAQ;AACZ;AAEA,SAAS,eAAe,WAAqD;AACzE,QAAM,WAAW,gBAAgB,SAAS;AAE1C,SAAO;AAAA,IACH,MAAM,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,SAAS;AAAA,IAC1C,MAAM,EAAE,SAAS,C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AAO,MAAM,KAAK,IAAI;AAC1B;AAEO,SAAS,aACZ,MACA,UAAmB,CAAC,GACqB;AAvD7C;AAwDI,QAAM,aAAY,aAAQ,cAAR,YAAqB;AACvC,QAAM,iBAAiB,iBAAiB,MAAM,SAAS;AAEvD,MAAI,eAAe,WAAW,GAAG;AAC7B,WAAO,EAAE,MAAM,gBAAgB,MAAM;AAAA,EACzC;AAEA,QAAM,gBAAe,aAAQ,iBAAR,YAAwB;AAC7C,QAAM,mBAAmB,gBAAgB,gBAAgB,cAAc,SAAS;AAEhF,SAAO;AAAA,IACH,MAAM,GAAG,gBAAgB;AAAA;AAAA,EAAO,IAAI;AAAA,IACpC,gBAAgB;AAAA,EACpB;AACJ;;;AFjEO,IAAM,kBAAwD,CACjE,aACE;AAAA,EACF,MAAM;AAAA,EAEN,iBAAiB,IAAI;AAEjB,WAAO,UAAU,KAAK,EAAE;AAAA,EAC5B;AAAA,EAEA,UAAU,MAAM,KAAK;AACjB,UAAM,SAAS,aAAa,MAAM,OAAO;AACzC,QAAI,CAAC,OAAO,eAAgB;AAC5B,WAAO,OAAO;AAAA,EAClB;AACJ;;;ADjBA,IAAO,eAAQ,iBAAiB,eAAe;;;AIH/C,SAAS,2BAA2B;AAGpC,IAAO,kBAAQ,oBAAoB,eAAe;;;ALElD,IAAO,eAAQ,iBAA0B;AAAA,EACrC,MAAM;AAAA,IACF,MAAM;AAAA,IACN,WAAW;AAAA,EACf;AAAA,EACA,UAAU,CAAC;AAAA,EACX,MAAM,SAAS,OAAO;AAClB,kBAAc,MAAM,aAAK,OAAO,CAAC;AACjC,qBAAiB,MAAM,gBAAQ,OAAO,CAAC;AAAA,EAC3C;AACJ,CAAC;","names":[]}
1
+ {"version":3,"sources":["../src/nuxt.ts","../src/vite.ts","../src/index.ts","../src/core/registry.ts","../src/core/transform.ts","../src/webpack.ts"],"sourcesContent":["import type { Options } from './types'\nimport { addVitePlugin, addWebpackPlugin, defineNuxtModule } from '@nuxt/kit'\nimport vite from './vite'\nimport webpack from './webpack'\n\nexport default defineNuxtModule<Options>({\n meta: {\n name: 'nuxt-unplugin-docubook',\n configKey: 'docubookMdx',\n },\n defaults: {},\n setup(options, _nuxt) {\n addVitePlugin(() => vite(options))\n addWebpackPlugin(() => webpack(options))\n },\n})\n","import { createVitePlugin } from 'unplugin'\nimport { unpluginFactory } from '.'\n\nexport default createVitePlugin(unpluginFactory)\n","import type { UnpluginFactory } from 'unplugin'\nimport type { Options } from './types'\nimport { createUnplugin } from 'unplugin'\nimport { transformMdx } from './core/transform'\n\nexport const unpluginFactory: UnpluginFactory<Options | undefined> = (\n options,\n) => ({\n name: 'unplugin-docubook',\n\n transformInclude(id) {\n // Only process .mdx and .md files\n return /\\.mdx?$/.test(id)\n },\n\n transform(code, _id) {\n const result = transformMdx(code, options)\n if (!result.hasTransformed) return\n return result.code\n },\n})\n\nexport const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)\n\nexport default unplugin\n","import type { Framework } from '../types'\n\nexport interface ComponentMeta {\n exports: string[]\n path: string\n}\n\nconst FRAMEWORK_PATHS: Record<Framework, string> = {\n react: '/react',\n vue: '/vue',\n svelte: '/svelte',\n}\n\nfunction createRegistry(framework: Framework): Record<string, ComponentMeta> {\n const basePath = FRAMEWORK_PATHS[framework]\n\n return {\n Note: { exports: ['Note'], path: basePath },\n Card: { exports: ['Card'], path: basePath },\n CardGroup: { exports: ['CardGroup'], path: basePath },\n Accordion: { exports: ['Accordion'], path: basePath },\n AccordionGroup: { exports: ['AccordionGroup'], path: basePath },\n Stepper: { exports: ['Stepper', 'StepperItem'], path: basePath },\n Kbd: { exports: ['Kbd'], path: basePath },\n Tooltip: { exports: ['Tooltip'], path: basePath },\n Youtube: { exports: ['Youtube'], path: basePath },\n Button: { exports: ['Button'], path: basePath },\n DocuLink: { exports: ['DocuLink'], path: basePath },\n DocuImage: { exports: ['DocuImage'], path: basePath },\n Files: { exports: ['Files'], path: basePath },\n Folder: { exports: ['Folder'], path: basePath },\n File: { exports: ['File'], path: basePath },\n Release: { exports: ['Release'], path: basePath },\n Changes: { exports: ['Changes'], path: basePath },\n Pre: { exports: ['Pre'], path: basePath },\n Copy: { exports: ['Copy'], path: basePath },\n DocuBookProvider: { exports: ['DocuBookProvider'], path: basePath },\n }\n}\n\nconst registryCache = new Map<Framework, Record<string, ComponentMeta>>()\nconst regexCache = new Map<Framework, RegExp>()\n\nexport function getComponentRegistry(\n framework: Framework = 'react',\n customComponents?: Record<string, ComponentMeta>,\n): Record<string, ComponentMeta> {\n if (!customComponents || Object.keys(customComponents).length === 0) {\n const cached = registryCache.get(framework)\n if (cached) return cached\n const registry = createRegistry(framework)\n registryCache.set(framework, registry)\n return registry\n }\n // Merge custom components with built-in (custom overrides built-in)\n return { ...createRegistry(framework), ...customComponents }\n}\n\nexport function buildComponentDetectionRegex(\n framework: Framework = 'react',\n customComponents?: Record<string, ComponentMeta>,\n): RegExp {\n // Custom components: always build fresh regex (not cached)\n if (customComponents && Object.keys(customComponents).length > 0) {\n const registry = getComponentRegistry(framework, customComponents)\n const names = Object.keys(registry)\n return new RegExp(`<(${names.join('|')})(?=[\\\\s/>])`, 'g')\n }\n\n const cached = regexCache.get(framework)\n if (cached) {\n cached.lastIndex = 0 // Reset regex state because of /g flag\n return cached\n }\n const registry = getComponentRegistry(framework)\n const names = Object.keys(registry)\n const regex = new RegExp(`<(${names.join('|')})(?=[\\\\s/>])`, 'g')\n regexCache.set(framework, regex)\n return regex\n}\n","import type { Options, Framework } from '../types'\nimport type { ComponentMeta } from './registry'\nimport { getComponentRegistry, buildComponentDetectionRegex } from './registry'\n\nconst DEFAULT_IMPORT_SOURCE = 'unplugin-docubook/components'\nconst DEFAULT_FRAMEWORK: Framework = 'react'\n\n/**\n * Strip content zones where component tags should NOT be detected:\n * - Fenced code blocks (triple backticks)\n * - Inline code (single backticks)\n * - MDX comments\n * - HTML comments\n * - Existing import/export statements\n */\nfunction stripIgnoredZones(code: string): string {\n return code\n .replace(/```[\\s\\S]*?```/g, '') // fenced code blocks\n .replace(/`[^`]+`/g, '') // inline code\n .replace(/\\{\\/\\*[\\s\\S]*?\\*\\/\\}/g, '') // MDX comments {/* */}\n .replace(/<!--[\\s\\S]*?-->/g, '') // HTML comments\n .replace(/^import\\s.+$/gm, '') // existing imports\n .replace(/^export\\s.+$/gm, '') // exports\n}\n\nexport function detectComponents(\n code: string,\n framework: Framework = DEFAULT_FRAMEWORK,\n customComponents?: Record<string, ComponentMeta>,\n): string[] {\n const strippedCode = stripIgnoredZones(code)\n const regex = buildComponentDetectionRegex(framework, customComponents)\n const registry = getComponentRegistry(framework, customComponents)\n const found = new Set<string>()\n let match: RegExpExecArray | null\n\n while ((match = regex.exec(strippedCode)) !== null) {\n const name = match[1]\n if (name && registry[name]) {\n found.add(name)\n }\n }\n\n return Array.from(found)\n}\n\nexport function generateImports(\n componentNames: string[],\n importSource: string,\n framework: Framework = DEFAULT_FRAMEWORK,\n customComponents?: Record<string, ComponentMeta>,\n): string {\n const registry = getComponentRegistry(framework, customComponents)\n const pathToExports = new Map<string, Set<string>>()\n\n for (const name of componentNames) {\n const meta = registry[name]\n if (!meta) continue\n\n // If path starts with '/', it's relative to importSource.\n // Otherwise, it's a full import path (e.g. '@/components/MyComp' or 'my-lib')\n const fullPath = meta.path.startsWith('/')\n ? `${importSource}${meta.path}`\n : meta.path\n\n if (!pathToExports.has(fullPath)) {\n pathToExports.set(fullPath, new Set())\n }\n for (const exp of meta.exports) {\n pathToExports.get(fullPath)!.add(exp)\n }\n }\n\n const lines: string[] = []\n for (const [path, exports] of pathToExports) {\n const names = Array.from(exports).sort().join(', ')\n lines.push(`import { ${names} } from '${path}'`)\n }\n\n return lines.join('\\n')\n}\n\nexport function transformMdx(\n code: string,\n options: Options = {},\n): { code: string; hasTransformed: boolean } {\n const framework = options.framework ?? DEFAULT_FRAMEWORK\n const usedComponents = detectComponents(code, framework, options.customComponents)\n\n if (usedComponents.length === 0) {\n return { code, hasTransformed: false }\n }\n\n const importSource = options.importSource ?? DEFAULT_IMPORT_SOURCE\n const importStatements = generateImports(usedComponents, importSource, framework, options.customComponents)\n\n return {\n code: `${importStatements}\\n\\n${code}`,\n hasTransformed: true,\n }\n}\n\n","import { createWebpackPlugin } from 'unplugin'\nimport { unpluginFactory } from '.'\n\nexport default createWebpackPlugin(unpluginFactory)\n"],"mappings":";;;;;;;;;;;;;;;;;;AACA,SAAS,eAAe,kBAAkB,wBAAwB;;;ACDlE,SAAS,wBAAwB;;;ACEjC,SAAS,sBAAsB;;;ACK/B,IAAM,kBAA6C;AAAA,EAC/C,OAAO;AAAA,EACP,KAAK;AAAA,EACL,QAAQ;AACZ;AAEA,SAAS,eAAe,WAAqD;AACzE,QAAM,WAAW,gBAAgB,SAAS;AAE1C,SAAO;AAAA,IACH,MAAM,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,SAAS;AAAA,IAC1C,MAAM,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,SAAS;AAAA,IAC1C,WAAW,EAAE,SAAS,CAAC,WAAW,GAAG,MAAM,SAAS;AAAA,IACpD,WAAW,EAAE,SAAS,CAAC,WAAW,GAAG,MAAM,SAAS;AAAA,IACpD,gBAAgB,EAAE,SAAS,CAAC,gBAAgB,GAAG,MAAM,SAAS;AAAA,IAC9D,SAAS,EAAE,SAAS,CAAC,WAAW,aAAa,GAAG,MAAM,SAAS;AAAA,IAC/D,KAAK,EAAE,SAAS,CAAC,KAAK,GAAG,MAAM,SAAS;AAAA,IACxC,SAAS,EAAE,SAAS,CAAC,SAAS,GAAG,MAAM,SAAS;AAAA,IAChD,SAAS,EAAE,SAAS,CAAC,SAAS,GAAG,MAAM,SAAS;AAAA,IAChD,QAAQ,EAAE,SAAS,CAAC,QAAQ,GAAG,MAAM,SAAS;AAAA,IAC9C,UAAU,EAAE,SAAS,CAAC,UAAU,GAAG,MAAM,SAAS;AAAA,IAClD,WAAW,EAAE,SAAS,CAAC,WAAW,GAAG,MAAM,SAAS;AAAA,IACpD,OAAO,EAAE,S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AA,EACf;AAAA,EACA,UAAU,CAAC;AAAA,EACX,MAAM,SAAS,OAAO;AAClB,kBAAc,MAAM,aAAK,OAAO,CAAC;AACjC,qBAAiB,MAAM,gBAAQ,OAAO,CAAC;AAAA,EAC3C;AACJ,CAAC;","names":["registry","names"]}
package/dist/rolldown.cjs CHANGED
@@ -2,7 +2,21 @@
2
2
  var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __spreadValues = (a, b) => {
10
+ for (var prop in b || (b = {}))
11
+ if (__hasOwnProp.call(b, prop))
12
+ __defNormalProp(a, prop, b[prop]);
13
+ if (__getOwnPropSymbols)
14
+ for (var prop of __getOwnPropSymbols(b)) {
15
+ if (__propIsEnum.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ }
18
+ return a;
19
+ };
6
20
  var __export = (target, all) => {
7
21
  for (var name in all)
8
22
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -59,24 +73,49 @@ function createRegistry(framework) {
59
73
  DocuBookProvider: { exports: ["DocuBookProvider"], path: basePath }
60
74
  };
61
75
  }
62
- function getComponentRegistry(framework = "react") {
63
- return createRegistry(framework);
76
+ var registryCache = /* @__PURE__ */ new Map();
77
+ var regexCache = /* @__PURE__ */ new Map();
78
+ function getComponentRegistry(framework = "react", customComponents) {
79
+ if (!customComponents || Object.keys(customComponents).length === 0) {
80
+ const cached = registryCache.get(framework);
81
+ if (cached) return cached;
82
+ const registry = createRegistry(framework);
83
+ registryCache.set(framework, registry);
84
+ return registry;
85
+ }
86
+ return __spreadValues(__spreadValues({}, createRegistry(framework)), customComponents);
64
87
  }
65
- function buildComponentDetectionRegex(framework = "react") {
88
+ function buildComponentDetectionRegex(framework = "react", customComponents) {
89
+ if (customComponents && Object.keys(customComponents).length > 0) {
90
+ const registry2 = getComponentRegistry(framework, customComponents);
91
+ const names2 = Object.keys(registry2);
92
+ return new RegExp(`<(${names2.join("|")})(?=[\\s/>])`, "g");
93
+ }
94
+ const cached = regexCache.get(framework);
95
+ if (cached) {
96
+ cached.lastIndex = 0;
97
+ return cached;
98
+ }
66
99
  const registry = getComponentRegistry(framework);
67
100
  const names = Object.keys(registry);
68
- return new RegExp(`<(${names.join("|")})(?=[\\s/>])`, "g");
101
+ const regex = new RegExp(`<(${names.join("|")})(?=[\\s/>])`, "g");
102
+ regexCache.set(framework, regex);
103
+ return regex;
69
104
  }
70
105
 
71
106
  // src/core/transform.ts
72
107
  var DEFAULT_IMPORT_SOURCE = "unplugin-docubook/components";
73
108
  var DEFAULT_FRAMEWORK = "react";
74
- function detectComponents(code, framework = DEFAULT_FRAMEWORK) {
75
- const regex = buildComponentDetectionRegex(framework);
76
- const registry = getComponentRegistry(framework);
109
+ function stripIgnoredZones(code) {
110
+ return code.replace(/```[\s\S]*?```/g, "").replace(/`[^`]+`/g, "").replace(/\{\/\*[\s\S]*?\*\/\}/g, "").replace(/<!--[\s\S]*?-->/g, "").replace(/^import\s.+$/gm, "").replace(/^export\s.+$/gm, "");
111
+ }
112
+ function detectComponents(code, framework = DEFAULT_FRAMEWORK, customComponents) {
113
+ const strippedCode = stripIgnoredZones(code);
114
+ const regex = buildComponentDetectionRegex(framework, customComponents);
115
+ const registry = getComponentRegistry(framework, customComponents);
77
116
  const found = /* @__PURE__ */ new Set();
78
117
  let match;
79
- while ((match = regex.exec(code)) !== null) {
118
+ while ((match = regex.exec(strippedCode)) !== null) {
80
119
  const name = match[1];
81
120
  if (name && registry[name]) {
82
121
  found.add(name);
@@ -84,13 +123,13 @@ function detectComponents(code, framework = DEFAULT_FRAMEWORK) {
84
123
  }
85
124
  return Array.from(found);
86
125
  }
87
- function generateImports(componentNames, importSource, framework = DEFAULT_FRAMEWORK) {
88
- const registry = getComponentRegistry(framework);
126
+ function generateImports(componentNames, importSource, framework = DEFAULT_FRAMEWORK, customComponents) {
127
+ const registry = getComponentRegistry(framework, customComponents);
89
128
  const pathToExports = /* @__PURE__ */ new Map();
90
129
  for (const name of componentNames) {
91
130
  const meta = registry[name];
92
131
  if (!meta) continue;
93
- const fullPath = `${importSource}${meta.path}`;
132
+ const fullPath = meta.path.startsWith("/") ? `${importSource}${meta.path}` : meta.path;
94
133
  if (!pathToExports.has(fullPath)) {
95
134
  pathToExports.set(fullPath, /* @__PURE__ */ new Set());
96
135
  }
@@ -108,12 +147,12 @@ function generateImports(componentNames, importSource, framework = DEFAULT_FRAME
108
147
  function transformMdx(code, options = {}) {
109
148
  var _a, _b;
110
149
  const framework = (_a = options.framework) != null ? _a : DEFAULT_FRAMEWORK;
111
- const usedComponents = detectComponents(code, framework);
150
+ const usedComponents = detectComponents(code, framework, options.customComponents);
112
151
  if (usedComponents.length === 0) {
113
152
  return { code, hasTransformed: false };
114
153
  }
115
154
  const importSource = (_b = options.importSource) != null ? _b : DEFAULT_IMPORT_SOURCE;
116
- const importStatements = generateImports(usedComponents, importSource, framework);
155
+ const importStatements = generateImports(usedComponents, importSource, framework, options.customComponents);
117
156
  return {
118
157
  code: `${importStatements}
119
158