boltdocs 1.3.0 → 1.3.1

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 (101) hide show
  1. package/dist/node/index.js +21 -6
  2. package/dist/node/index.mjs +21 -6
  3. package/package.json +1 -1
  4. package/src/client/app/index.tsx +344 -344
  5. package/src/client/app/preload.tsx +56 -56
  6. package/src/client/index.ts +40 -40
  7. package/src/client/ssr.tsx +51 -51
  8. package/src/client/theme/components/CodeBlock/CodeBlock.tsx +76 -76
  9. package/src/client/theme/components/CodeBlock/index.ts +1 -1
  10. package/src/client/theme/components/PackageManagerTabs/PackageManagerTabs.tsx +154 -154
  11. package/src/client/theme/components/PackageManagerTabs/index.ts +1 -1
  12. package/src/client/theme/components/PackageManagerTabs/pkg-tabs.css +64 -64
  13. package/src/client/theme/components/Playground/Playground.tsx +124 -124
  14. package/src/client/theme/components/Playground/index.ts +1 -1
  15. package/src/client/theme/components/Playground/playground.css +168 -168
  16. package/src/client/theme/components/Video/Video.tsx +84 -84
  17. package/src/client/theme/components/Video/index.ts +1 -1
  18. package/src/client/theme/components/Video/video.css +41 -41
  19. package/src/client/theme/components/mdx/Admonition.tsx +80 -80
  20. package/src/client/theme/components/mdx/Badge.tsx +31 -31
  21. package/src/client/theme/components/mdx/Button.tsx +50 -50
  22. package/src/client/theme/components/mdx/Card.tsx +80 -80
  23. package/src/client/theme/components/mdx/List.tsx +57 -57
  24. package/src/client/theme/components/mdx/Tabs.tsx +94 -94
  25. package/src/client/theme/components/mdx/index.ts +18 -18
  26. package/src/client/theme/components/mdx/mdx-components.css +424 -424
  27. package/src/client/theme/icons/bun.tsx +62 -62
  28. package/src/client/theme/icons/deno.tsx +20 -20
  29. package/src/client/theme/icons/discord.tsx +12 -12
  30. package/src/client/theme/icons/github.tsx +15 -15
  31. package/src/client/theme/icons/npm.tsx +13 -13
  32. package/src/client/theme/icons/pnpm.tsx +72 -72
  33. package/src/client/theme/icons/twitter.tsx +12 -12
  34. package/src/client/theme/styles/markdown.css +343 -343
  35. package/src/client/theme/styles/variables.css +162 -162
  36. package/src/client/theme/styles.css +37 -37
  37. package/src/client/theme/ui/BackgroundGradient/BackgroundGradient.tsx +10 -10
  38. package/src/client/theme/ui/BackgroundGradient/index.ts +1 -1
  39. package/src/client/theme/ui/Breadcrumbs/Breadcrumbs.tsx +68 -68
  40. package/src/client/theme/ui/Breadcrumbs/index.ts +1 -1
  41. package/src/client/theme/ui/Footer/footer.css +32 -32
  42. package/src/client/theme/ui/Head/Head.tsx +69 -69
  43. package/src/client/theme/ui/Head/index.ts +1 -1
  44. package/src/client/theme/ui/LanguageSwitcher/LanguageSwitcher.tsx +125 -125
  45. package/src/client/theme/ui/LanguageSwitcher/index.ts +1 -1
  46. package/src/client/theme/ui/LanguageSwitcher/language-switcher.css +98 -98
  47. package/src/client/theme/ui/Layout/Layout.tsx +202 -202
  48. package/src/client/theme/ui/Layout/base.css +76 -76
  49. package/src/client/theme/ui/Layout/index.ts +2 -2
  50. package/src/client/theme/ui/Layout/pagination.css +72 -72
  51. package/src/client/theme/ui/Layout/responsive.css +36 -36
  52. package/src/client/theme/ui/Link/Link.tsx +254 -254
  53. package/src/client/theme/ui/Link/index.ts +2 -2
  54. package/src/client/theme/ui/Loading/Loading.tsx +10 -10
  55. package/src/client/theme/ui/Loading/index.ts +1 -1
  56. package/src/client/theme/ui/Loading/loading.css +30 -30
  57. package/src/client/theme/ui/Navbar/GithubStars.tsx +27 -27
  58. package/src/client/theme/ui/Navbar/Navbar.tsx +145 -145
  59. package/src/client/theme/ui/Navbar/index.ts +2 -2
  60. package/src/client/theme/ui/Navbar/navbar.css +233 -233
  61. package/src/client/theme/ui/NotFound/NotFound.tsx +19 -19
  62. package/src/client/theme/ui/NotFound/index.ts +1 -1
  63. package/src/client/theme/ui/NotFound/not-found.css +64 -64
  64. package/src/client/theme/ui/OnThisPage/OnThisPage.tsx +235 -235
  65. package/src/client/theme/ui/OnThisPage/index.ts +1 -1
  66. package/src/client/theme/ui/OnThisPage/toc.css +132 -132
  67. package/src/client/theme/ui/PoweredBy/PoweredBy.tsx +18 -18
  68. package/src/client/theme/ui/PoweredBy/index.ts +1 -1
  69. package/src/client/theme/ui/PoweredBy/powered-by.css +76 -76
  70. package/src/client/theme/ui/SearchDialog/SearchDialog.tsx +199 -199
  71. package/src/client/theme/ui/SearchDialog/index.ts +1 -1
  72. package/src/client/theme/ui/SearchDialog/search.css +152 -152
  73. package/src/client/theme/ui/Sidebar/Sidebar.tsx +204 -204
  74. package/src/client/theme/ui/Sidebar/index.ts +1 -1
  75. package/src/client/theme/ui/Sidebar/sidebar.css +236 -236
  76. package/src/client/theme/ui/ThemeToggle/ThemeToggle.tsx +69 -69
  77. package/src/client/theme/ui/ThemeToggle/index.ts +1 -1
  78. package/src/client/theme/ui/VersionSwitcher/VersionSwitcher.tsx +136 -136
  79. package/src/client/theme/ui/VersionSwitcher/index.ts +1 -1
  80. package/src/client/types.ts +50 -50
  81. package/src/client/utils.ts +26 -26
  82. package/src/node/cache.ts +408 -408
  83. package/src/node/config.ts +192 -192
  84. package/src/node/index.ts +21 -21
  85. package/src/node/mdx.ts +120 -120
  86. package/src/node/plugin/entry.ts +58 -58
  87. package/src/node/plugin/html.ts +55 -55
  88. package/src/node/plugin/index.ts +193 -193
  89. package/src/node/plugin/types.ts +11 -11
  90. package/src/node/routes/cache.ts +28 -28
  91. package/src/node/routes/index.ts +167 -167
  92. package/src/node/routes/parser.ts +153 -127
  93. package/src/node/routes/sorter.ts +42 -42
  94. package/src/node/routes/types.ts +49 -49
  95. package/src/node/ssg/index.ts +114 -114
  96. package/src/node/ssg/meta.ts +34 -34
  97. package/src/node/ssg/options.ts +13 -13
  98. package/src/node/ssg/sitemap.ts +54 -54
  99. package/src/node/utils.ts +134 -134
  100. package/tsconfig.json +20 -20
  101. package/tsup.config.ts +22 -22
@@ -1,192 +1,192 @@
1
- import path from "path";
2
- import { pathToFileURL } from "url";
3
- import fs from "fs";
4
- import type { Plugin as VitePlugin } from "vite";
5
-
6
- /**
7
- * Represents a single social link in the configuration.
8
- */
9
- export interface BoltdocsSocialLink {
10
- /** Identifier for the icon (e.g., 'github', 'twitter') */
11
- icon: "discord" | "x" | string;
12
- /** The URL the social link points to */
13
- link: string;
14
- }
15
-
16
- /**
17
- * Configuration for the site footer.
18
- */
19
- export interface BoltdocsFooterConfig {
20
- /** Text to display in the footer (HTML is supported) */
21
- text?: string;
22
- }
23
-
24
- /**
25
- * Theme-specific configuration options governing the appearance and navigation of the site.
26
- */
27
- export interface BoltdocsThemeConfig {
28
- /** The global title of the documentation site */
29
- title?: string;
30
- /** The global description of the site (used for SEO) */
31
- description?: string;
32
- /** URL path to the site logo */
33
- logo?: string;
34
- /** Items to display in the top navigation bar */
35
- navbar?: Array<{ text: string; link: string }>;
36
- /** Items to display in the sidebar, organized optionally by group URLs */
37
- sidebar?: Record<string, Array<{ text: string; link: string }>>;
38
- /** Social links to display in the navigation bar */
39
- socialLinks?: BoltdocsSocialLink[];
40
- /** Site footer configuration */
41
- footer?: BoltdocsFooterConfig;
42
- /** Whether to show breadcrumbs navigation (default: true) */
43
- breadcrumbs?: boolean;
44
- /** Path to a custom CSS file that overrides theme variables */
45
- customCss?: string;
46
- /** URL template for 'Edit this page'. Use :path as a placeholder. */
47
- editLink?: string;
48
- /** URL for the 'Community help' link. */
49
- communityHelp?: string;
50
- /** The current version of the project (e.g., 'v2.8.9'). Displayed in the Navbar. */
51
- version?: string;
52
- /** The GitHub repository in the format 'owner/repo' to fetch and display star count. */
53
- githubRepo?: string;
54
- /** Whether to show the 'Powered by LiteDocs' badge in the sidebar (default: true) */
55
- poweredBy?: boolean;
56
- /** Granular layout customization props */
57
- layoutProps?: {
58
- navbar?: any;
59
- sidebar?: any;
60
- toc?: any;
61
- background?: any;
62
- head?: any;
63
- breadcrumbs?: any;
64
- className?: string;
65
- style?: any;
66
- };
67
- /**
68
- * The syntax highlighting theme for code blocks.
69
- * Supports any Shiki theme name (e.g., 'github-dark', 'one-dark-pro', 'aurora-x').
70
- * Can also be an object for multiple themes (e.g., { light: 'github-light', dark: 'github-dark' }).
71
- * Default: 'one-dark-pro'
72
- */
73
- codeTheme?: string | Record<string, string>;
74
- }
75
-
76
- /**
77
- * Configuration for internationalization (i18n).
78
- */
79
- export interface BoltdocsI18nConfig {
80
- /** The default locale (e.g., 'en') */
81
- defaultLocale: string;
82
- /** Available locales and their display names (e.g., { en: 'English', es: 'Español' }) */
83
- locales: Record<string, string>;
84
- }
85
-
86
- /**
87
- * Configuration for documentation versioning.
88
- */
89
- export interface BoltdocsVersionsConfig {
90
- /** The default version (e.g., 'v2') */
91
- defaultVersion: string;
92
- /** Available versions and their display names (e.g., { v1: 'Version 1.x', v2: 'Version 2.x' }) */
93
- versions: Record<string, string>;
94
- }
95
-
96
- /**
97
- * Defines a Boltdocs plugin that can extend the build process and client-side functionality.
98
- */
99
- export interface BoltdocsPlugin {
100
- /** A unique name for the plugin */
101
- name: string;
102
- /** Whether to run this plugin before or after default ones (optional) */
103
- enforce?: "pre" | "post";
104
- /** Optional remark plugins to add to the MDX pipeline */
105
- remarkPlugins?: any[];
106
- /** Optional rehype plugins to add to the MDX pipeline */
107
- rehypePlugins?: any[];
108
- /** Optional Vite plugins to inject into the build process */
109
- vitePlugins?: VitePlugin[];
110
- /** Optional custom React components to register in MDX. Map of Name -> Module Path. */
111
- components?: Record<string, string>;
112
- }
113
-
114
- /**
115
- * The root configuration object for Boltdocs.
116
- */
117
- export interface BoltdocsConfig {
118
- /** The base URL of the site, used for generating the sitemap */
119
- siteUrl?: string;
120
- /** Configuration pertaining to the UI and appearance */
121
- themeConfig?: BoltdocsThemeConfig;
122
- /** The root directory containing markdown documentation files (default: 'docs') */
123
- docsDir?: string;
124
- /** Configuration for internationalization */
125
- i18n?: BoltdocsI18nConfig;
126
- /** Configuration for documentation versioning */
127
- versions?: BoltdocsVersionsConfig;
128
- /** Custom plugins for extending functionality */
129
- plugins?: BoltdocsPlugin[];
130
- }
131
-
132
- export const CONFIG_FILES = [
133
- "boltdocs.config.js",
134
- "boltdocs.config.mjs",
135
- "boltdocs.config.ts",
136
- ];
137
-
138
- /**
139
- * Loads user's configuration file (e.g., `boltdocs.config.js` or `boltdocs.config.ts`) if it exists,
140
- * merges it with the default configuration, and returns the final `BoltdocsConfig`.
141
- *
142
- * @param docsDir - The fallback/default documentation directory
143
- * @returns A promise resolving to the final merged configuration object
144
- */
145
- export async function resolveConfig(docsDir: string): Promise<BoltdocsConfig> {
146
- const projectRoot = process.cwd();
147
-
148
- const defaults: BoltdocsConfig = {
149
- docsDir: path.resolve(docsDir),
150
- themeConfig: {
151
- title: "Boltdocs",
152
- description: "A Vite documentation framework",
153
- navbar: [
154
- { text: "Home", link: "/" },
155
- { text: "Documentation", link: "/docs" },
156
- ],
157
- },
158
- };
159
-
160
- // Try to load user config
161
- for (const filename of CONFIG_FILES) {
162
- const configPath = path.resolve(projectRoot, filename);
163
- if (fs.existsSync(configPath)) {
164
- try {
165
- // Add a timestamp query parameter to bust the ESM cache
166
- const fileUrl = pathToFileURL(configPath).href + "?t=" + Date.now();
167
- const mod = await import(fileUrl);
168
- const userConfig = mod.default || mod;
169
-
170
- // Merge user themeConfig into defaults
171
- // Support new format where user exports BoltdocsConfig directly
172
- const userThemeConfig = userConfig.themeConfig || userConfig;
173
-
174
- return {
175
- docsDir: path.resolve(docsDir),
176
- themeConfig: {
177
- ...defaults.themeConfig,
178
- ...userThemeConfig,
179
- },
180
- i18n: userConfig.i18n,
181
- versions: userConfig.versions,
182
- siteUrl: userConfig.siteUrl,
183
- plugins: userConfig.plugins || [],
184
- };
185
- } catch (e) {
186
- console.warn(`[boltdocs] Failed to load config from ${filename}:`, e);
187
- }
188
- }
189
- }
190
-
191
- return defaults;
192
- }
1
+ import path from "path";
2
+ import { pathToFileURL } from "url";
3
+ import fs from "fs";
4
+ import type { Plugin as VitePlugin } from "vite";
5
+
6
+ /**
7
+ * Represents a single social link in the configuration.
8
+ */
9
+ export interface BoltdocsSocialLink {
10
+ /** Identifier for the icon (e.g., 'github', 'twitter') */
11
+ icon: "discord" | "x" | string;
12
+ /** The URL the social link points to */
13
+ link: string;
14
+ }
15
+
16
+ /**
17
+ * Configuration for the site footer.
18
+ */
19
+ export interface BoltdocsFooterConfig {
20
+ /** Text to display in the footer (HTML is supported) */
21
+ text?: string;
22
+ }
23
+
24
+ /**
25
+ * Theme-specific configuration options governing the appearance and navigation of the site.
26
+ */
27
+ export interface BoltdocsThemeConfig {
28
+ /** The global title of the documentation site */
29
+ title?: string;
30
+ /** The global description of the site (used for SEO) */
31
+ description?: string;
32
+ /** URL path to the site logo */
33
+ logo?: string;
34
+ /** Items to display in the top navigation bar */
35
+ navbar?: Array<{ text: string; link: string }>;
36
+ /** Items to display in the sidebar, organized optionally by group URLs */
37
+ sidebar?: Record<string, Array<{ text: string; link: string }>>;
38
+ /** Social links to display in the navigation bar */
39
+ socialLinks?: BoltdocsSocialLink[];
40
+ /** Site footer configuration */
41
+ footer?: BoltdocsFooterConfig;
42
+ /** Whether to show breadcrumbs navigation (default: true) */
43
+ breadcrumbs?: boolean;
44
+ /** Path to a custom CSS file that overrides theme variables */
45
+ customCss?: string;
46
+ /** URL template for 'Edit this page'. Use :path as a placeholder. */
47
+ editLink?: string;
48
+ /** URL for the 'Community help' link. */
49
+ communityHelp?: string;
50
+ /** The current version of the project (e.g., 'v2.8.9'). Displayed in the Navbar. */
51
+ version?: string;
52
+ /** The GitHub repository in the format 'owner/repo' to fetch and display star count. */
53
+ githubRepo?: string;
54
+ /** Whether to show the 'Powered by LiteDocs' badge in the sidebar (default: true) */
55
+ poweredBy?: boolean;
56
+ /** Granular layout customization props */
57
+ layoutProps?: {
58
+ navbar?: any;
59
+ sidebar?: any;
60
+ toc?: any;
61
+ background?: any;
62
+ head?: any;
63
+ breadcrumbs?: any;
64
+ className?: string;
65
+ style?: any;
66
+ };
67
+ /**
68
+ * The syntax highlighting theme for code blocks.
69
+ * Supports any Shiki theme name (e.g., 'github-dark', 'one-dark-pro', 'aurora-x').
70
+ * Can also be an object for multiple themes (e.g., { light: 'github-light', dark: 'github-dark' }).
71
+ * Default: 'one-dark-pro'
72
+ */
73
+ codeTheme?: string | Record<string, string>;
74
+ }
75
+
76
+ /**
77
+ * Configuration for internationalization (i18n).
78
+ */
79
+ export interface BoltdocsI18nConfig {
80
+ /** The default locale (e.g., 'en') */
81
+ defaultLocale: string;
82
+ /** Available locales and their display names (e.g., { en: 'English', es: 'Español' }) */
83
+ locales: Record<string, string>;
84
+ }
85
+
86
+ /**
87
+ * Configuration for documentation versioning.
88
+ */
89
+ export interface BoltdocsVersionsConfig {
90
+ /** The default version (e.g., 'v2') */
91
+ defaultVersion: string;
92
+ /** Available versions and their display names (e.g., { v1: 'Version 1.x', v2: 'Version 2.x' }) */
93
+ versions: Record<string, string>;
94
+ }
95
+
96
+ /**
97
+ * Defines a Boltdocs plugin that can extend the build process and client-side functionality.
98
+ */
99
+ export interface BoltdocsPlugin {
100
+ /** A unique name for the plugin */
101
+ name: string;
102
+ /** Whether to run this plugin before or after default ones (optional) */
103
+ enforce?: "pre" | "post";
104
+ /** Optional remark plugins to add to the MDX pipeline */
105
+ remarkPlugins?: any[];
106
+ /** Optional rehype plugins to add to the MDX pipeline */
107
+ rehypePlugins?: any[];
108
+ /** Optional Vite plugins to inject into the build process */
109
+ vitePlugins?: VitePlugin[];
110
+ /** Optional custom React components to register in MDX. Map of Name -> Module Path. */
111
+ components?: Record<string, string>;
112
+ }
113
+
114
+ /**
115
+ * The root configuration object for Boltdocs.
116
+ */
117
+ export interface BoltdocsConfig {
118
+ /** The base URL of the site, used for generating the sitemap */
119
+ siteUrl?: string;
120
+ /** Configuration pertaining to the UI and appearance */
121
+ themeConfig?: BoltdocsThemeConfig;
122
+ /** The root directory containing markdown documentation files (default: 'docs') */
123
+ docsDir?: string;
124
+ /** Configuration for internationalization */
125
+ i18n?: BoltdocsI18nConfig;
126
+ /** Configuration for documentation versioning */
127
+ versions?: BoltdocsVersionsConfig;
128
+ /** Custom plugins for extending functionality */
129
+ plugins?: BoltdocsPlugin[];
130
+ }
131
+
132
+ export const CONFIG_FILES = [
133
+ "boltdocs.config.js",
134
+ "boltdocs.config.mjs",
135
+ "boltdocs.config.ts",
136
+ ];
137
+
138
+ /**
139
+ * Loads user's configuration file (e.g., `boltdocs.config.js` or `boltdocs.config.ts`) if it exists,
140
+ * merges it with the default configuration, and returns the final `BoltdocsConfig`.
141
+ *
142
+ * @param docsDir - The fallback/default documentation directory
143
+ * @returns A promise resolving to the final merged configuration object
144
+ */
145
+ export async function resolveConfig(docsDir: string): Promise<BoltdocsConfig> {
146
+ const projectRoot = process.cwd();
147
+
148
+ const defaults: BoltdocsConfig = {
149
+ docsDir: path.resolve(docsDir),
150
+ themeConfig: {
151
+ title: "Boltdocs",
152
+ description: "A Vite documentation framework",
153
+ navbar: [
154
+ { text: "Home", link: "/" },
155
+ { text: "Documentation", link: "/docs" },
156
+ ],
157
+ },
158
+ };
159
+
160
+ // Try to load user config
161
+ for (const filename of CONFIG_FILES) {
162
+ const configPath = path.resolve(projectRoot, filename);
163
+ if (fs.existsSync(configPath)) {
164
+ try {
165
+ // Add a timestamp query parameter to bust the ESM cache
166
+ const fileUrl = pathToFileURL(configPath).href + "?t=" + Date.now();
167
+ const mod = await import(fileUrl);
168
+ const userConfig = mod.default || mod;
169
+
170
+ // Merge user themeConfig into defaults
171
+ // Support new format where user exports BoltdocsConfig directly
172
+ const userThemeConfig = userConfig.themeConfig || userConfig;
173
+
174
+ return {
175
+ docsDir: path.resolve(docsDir),
176
+ themeConfig: {
177
+ ...defaults.themeConfig,
178
+ ...userThemeConfig,
179
+ },
180
+ i18n: userConfig.i18n,
181
+ versions: userConfig.versions,
182
+ siteUrl: userConfig.siteUrl,
183
+ plugins: userConfig.plugins || [],
184
+ };
185
+ } catch (e) {
186
+ console.warn(`[boltdocs] Failed to load config from ${filename}:`, e);
187
+ }
188
+ }
189
+ }
190
+
191
+ return defaults;
192
+ }
package/src/node/index.ts CHANGED
@@ -1,21 +1,21 @@
1
- import { Plugin } from "vite";
2
- import { boltdocsPlugin } from "./plugin/index";
3
- import { boltdocsMdxPlugin } from "./mdx";
4
- import { BoltdocsPluginOptions } from "./plugin/index";
5
-
6
- import { resolveConfig } from "./config";
7
-
8
- export default async function boltdocs(
9
- options?: BoltdocsPluginOptions,
10
- ): Promise<Plugin[]> {
11
- const docsDir = options?.docsDir || "docs";
12
- const config = await resolveConfig(docsDir);
13
-
14
- return [...boltdocsPlugin(options, config), boltdocsMdxPlugin(config)];
15
- }
16
-
17
- export type { BoltdocsPluginOptions };
18
- export { generateStaticPages } from "./ssg";
19
- export type { SSGOptions } from "./ssg";
20
- export type { RouteMeta } from "./routes";
21
- export type { BoltdocsConfig, BoltdocsThemeConfig } from "./config";
1
+ import { Plugin } from "vite";
2
+ import { boltdocsPlugin } from "./plugin/index";
3
+ import { boltdocsMdxPlugin } from "./mdx";
4
+ import { BoltdocsPluginOptions } from "./plugin/index";
5
+
6
+ import { resolveConfig } from "./config";
7
+
8
+ export default async function boltdocs(
9
+ options?: BoltdocsPluginOptions,
10
+ ): Promise<Plugin[]> {
11
+ const docsDir = options?.docsDir || "docs";
12
+ const config = await resolveConfig(docsDir);
13
+
14
+ return [...boltdocsPlugin(options, config), boltdocsMdxPlugin(config)];
15
+ }
16
+
17
+ export type { BoltdocsPluginOptions };
18
+ export { generateStaticPages } from "./ssg";
19
+ export type { SSGOptions } from "./ssg";
20
+ export type { RouteMeta } from "./routes";
21
+ export type { BoltdocsConfig, BoltdocsThemeConfig } from "./config";
package/src/node/mdx.ts CHANGED
@@ -1,120 +1,120 @@
1
- import mdxPlugin from "@mdx-js/rollup";
2
- import remarkGfm from "remark-gfm";
3
- import remarkFrontmatter from "remark-frontmatter";
4
- import rehypeSlug from "rehype-slug";
5
- import rehypePrettyCode from "rehype-pretty-code";
6
- import type { Plugin } from "vite";
7
- import crypto from "crypto";
8
-
9
- import type { BoltdocsConfig } from "./config";
10
- import { TransformCache } from "./cache";
11
-
12
- /**
13
- * Persistent cache for MDX transformations.
14
- * Saves results to `.boltdocs/transform-mdx.json.gz`.
15
- */
16
- const mdxCache = new TransformCache("mdx");
17
- let mdxCacheLoaded = false;
18
-
19
- /**
20
- * Configures the MDX compiler for Vite using `@mdx-js/rollup`.
21
- * Includes standard remark and rehype plugins for GitHub Flavored Markdown (GFM),
22
- * frontmatter extraction, auto-linking headers, and syntax highlighting via `rehype-pretty-code`.
23
- *
24
- * Also wraps the plugin with a persistent cache to avoid re-compiling unchanged MDX files.
25
- *
26
- * @param config - The Boltdocs configuration containing custom plugins
27
- * @returns A Vite plugin configured for MDX parsing with caching
28
- */
29
- export function boltdocsMdxPlugin(config?: BoltdocsConfig): Plugin {
30
- const extraRemarkPlugins =
31
- config?.plugins?.flatMap((p) => p.remarkPlugins || []) || [];
32
- const extraRehypePlugins =
33
- config?.plugins?.flatMap((p) => p.rehypePlugins || []) || [];
34
-
35
- const baseMdxPlugin = mdxPlugin({
36
- remarkPlugins: [remarkGfm, remarkFrontmatter, ...extraRemarkPlugins],
37
- rehypePlugins: [
38
- rehypeSlug,
39
- ...extraRehypePlugins,
40
- [
41
- rehypePrettyCode,
42
- {
43
- theme: config?.themeConfig?.codeTheme || "one-dark-pro",
44
- keepBackground: false,
45
- },
46
- ],
47
- ],
48
- jsxRuntime: "automatic",
49
- providerImportSource: "@mdx-js/react",
50
- }) as Plugin;
51
-
52
- return {
53
- ...baseMdxPlugin,
54
- name: "vite-plugin-boltdocs-mdx",
55
-
56
- async buildStart() {
57
- hits = 0;
58
- total = 0;
59
- if (!mdxCacheLoaded) {
60
- mdxCache.load();
61
- mdxCacheLoaded = true;
62
- }
63
- if (baseMdxPlugin.buildStart) {
64
- await (baseMdxPlugin.buildStart as any).call(this);
65
- }
66
- },
67
-
68
- async transform(code, id, options) {
69
- if (!id.endsWith(".md") && !id.endsWith(".mdx")) {
70
- return (baseMdxPlugin.transform as any)?.call(this, code, id, options);
71
- }
72
-
73
- total++;
74
- // Create a cache key based on path, content, and config (simplified)
75
- const contentHash = crypto.createHash("md5").update(code).digest("hex");
76
- const cacheKey = `${id}:${contentHash}`;
77
-
78
- const cached = mdxCache.get(cacheKey);
79
- if (cached) {
80
- hits++;
81
- return { code: cached, map: null };
82
- }
83
-
84
- const result = await (baseMdxPlugin.transform as any).call(
85
- this,
86
- code,
87
- id,
88
- options,
89
- );
90
-
91
- if (result && typeof result === "object" && result.code) {
92
- mdxCache.set(cacheKey, result.code);
93
- } else if (typeof result === "string") {
94
- mdxCache.set(cacheKey, result);
95
- }
96
-
97
- return result;
98
- },
99
-
100
- async buildEnd() {
101
- mdxCache.save();
102
- await mdxCache.flush(); // Use instance flush or global flushCache
103
- if (baseMdxPlugin.buildEnd) {
104
- await (baseMdxPlugin.buildEnd as any).call(this);
105
- }
106
- },
107
- };
108
- }
109
-
110
- /**
111
- * Returns the current MDX cache statistics.
112
- * @returns An object with total and hit counts
113
- * @deprecated Removed for performance
114
- */
115
- export function getMdxCacheStats() {
116
- return { hits: 0, total: 0 };
117
- }
118
-
119
- let hits = 0;
120
- let total = 0;
1
+ import mdxPlugin from "@mdx-js/rollup";
2
+ import remarkGfm from "remark-gfm";
3
+ import remarkFrontmatter from "remark-frontmatter";
4
+ import rehypeSlug from "rehype-slug";
5
+ import rehypePrettyCode from "rehype-pretty-code";
6
+ import type { Plugin } from "vite";
7
+ import crypto from "crypto";
8
+
9
+ import type { BoltdocsConfig } from "./config";
10
+ import { TransformCache } from "./cache";
11
+
12
+ /**
13
+ * Persistent cache for MDX transformations.
14
+ * Saves results to `.boltdocs/transform-mdx.json.gz`.
15
+ */
16
+ const mdxCache = new TransformCache("mdx");
17
+ let mdxCacheLoaded = false;
18
+
19
+ /**
20
+ * Configures the MDX compiler for Vite using `@mdx-js/rollup`.
21
+ * Includes standard remark and rehype plugins for GitHub Flavored Markdown (GFM),
22
+ * frontmatter extraction, auto-linking headers, and syntax highlighting via `rehype-pretty-code`.
23
+ *
24
+ * Also wraps the plugin with a persistent cache to avoid re-compiling unchanged MDX files.
25
+ *
26
+ * @param config - The Boltdocs configuration containing custom plugins
27
+ * @returns A Vite plugin configured for MDX parsing with caching
28
+ */
29
+ export function boltdocsMdxPlugin(config?: BoltdocsConfig): Plugin {
30
+ const extraRemarkPlugins =
31
+ config?.plugins?.flatMap((p) => p.remarkPlugins || []) || [];
32
+ const extraRehypePlugins =
33
+ config?.plugins?.flatMap((p) => p.rehypePlugins || []) || [];
34
+
35
+ const baseMdxPlugin = mdxPlugin({
36
+ remarkPlugins: [remarkGfm, remarkFrontmatter, ...extraRemarkPlugins],
37
+ rehypePlugins: [
38
+ rehypeSlug,
39
+ ...extraRehypePlugins,
40
+ [
41
+ rehypePrettyCode,
42
+ {
43
+ theme: config?.themeConfig?.codeTheme || "one-dark-pro",
44
+ keepBackground: false,
45
+ },
46
+ ],
47
+ ],
48
+ jsxRuntime: "automatic",
49
+ providerImportSource: "@mdx-js/react",
50
+ }) as Plugin;
51
+
52
+ return {
53
+ ...baseMdxPlugin,
54
+ name: "vite-plugin-boltdocs-mdx",
55
+
56
+ async buildStart() {
57
+ hits = 0;
58
+ total = 0;
59
+ if (!mdxCacheLoaded) {
60
+ mdxCache.load();
61
+ mdxCacheLoaded = true;
62
+ }
63
+ if (baseMdxPlugin.buildStart) {
64
+ await (baseMdxPlugin.buildStart as any).call(this);
65
+ }
66
+ },
67
+
68
+ async transform(code, id, options) {
69
+ if (!id.endsWith(".md") && !id.endsWith(".mdx")) {
70
+ return (baseMdxPlugin.transform as any)?.call(this, code, id, options);
71
+ }
72
+
73
+ total++;
74
+ // Create a cache key based on path, content, and config (simplified)
75
+ const contentHash = crypto.createHash("md5").update(code).digest("hex");
76
+ const cacheKey = `${id}:${contentHash}`;
77
+
78
+ const cached = mdxCache.get(cacheKey);
79
+ if (cached) {
80
+ hits++;
81
+ return { code: cached, map: null };
82
+ }
83
+
84
+ const result = await (baseMdxPlugin.transform as any).call(
85
+ this,
86
+ code,
87
+ id,
88
+ options,
89
+ );
90
+
91
+ if (result && typeof result === "object" && result.code) {
92
+ mdxCache.set(cacheKey, result.code);
93
+ } else if (typeof result === "string") {
94
+ mdxCache.set(cacheKey, result);
95
+ }
96
+
97
+ return result;
98
+ },
99
+
100
+ async buildEnd() {
101
+ mdxCache.save();
102
+ await mdxCache.flush(); // Use instance flush or global flushCache
103
+ if (baseMdxPlugin.buildEnd) {
104
+ await (baseMdxPlugin.buildEnd as any).call(this);
105
+ }
106
+ },
107
+ };
108
+ }
109
+
110
+ /**
111
+ * Returns the current MDX cache statistics.
112
+ * @returns An object with total and hit counts
113
+ * @deprecated Removed for performance
114
+ */
115
+ export function getMdxCacheStats() {
116
+ return { hits: 0, total: 0 };
117
+ }
118
+
119
+ let hits = 0;
120
+ let total = 0;