docusaurus-plugin-glossary 2.0.2 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 mcclowes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -28,58 +28,36 @@ A comprehensive Docusaurus plugin that provides glossary functionality with an a
28
28
  npm install docusaurus-plugin-glossary
29
29
  ```
30
30
 
31
- 2. **Add to your `docusaurus.config.js`:**
31
+ 2. **Use the preset in your `docusaurus.config.js`:**
32
32
 
33
33
  ```javascript
34
- const glossaryPlugin = require('docusaurus-plugin-glossary');
35
-
36
34
  module.exports = {
37
- // ... other config
38
35
  presets: [
39
36
  [
40
- '@docusaurus/preset-classic',
37
+ 'docusaurus-plugin-glossary/preset',
41
38
  {
39
+ // Glossary configuration
40
+ glossary: {
41
+ glossaryPath: 'glossary/glossary.json',
42
+ routePath: '/glossary',
43
+ },
44
+ // Standard Docusaurus preset-classic options
42
45
  docs: {
43
- // Add the remark plugin to enable auto-linking in docs
44
- remarkPlugins: [
45
- glossaryPlugin.getRemarkPlugin(
46
- {
47
- glossaryPath: 'glossary/glossary.json',
48
- routePath: '/glossary',
49
- },
50
- { siteDir: __dirname }
51
- ),
52
- ],
46
+ sidebarPath: './sidebars.js',
53
47
  },
54
- pages: {
55
- // Add the remark plugin to enable auto-linking in pages
56
- remarkPlugins: [
57
- glossaryPlugin.getRemarkPlugin(
58
- {
59
- glossaryPath: 'glossary/glossary.json',
60
- routePath: '/glossary',
61
- },
62
- { siteDir: __dirname }
63
- ),
64
- ],
48
+ blog: {
49
+ showReadingTime: true,
50
+ },
51
+ theme: {
52
+ customCss: './src/css/custom.css',
65
53
  },
66
54
  },
67
55
  ],
68
56
  ],
69
- plugins: [
70
- [
71
- 'docusaurus-plugin-glossary',
72
- {
73
- glossaryPath: 'glossary/glossary.json', // Path to your glossary file
74
- routePath: '/glossary', // URL path for glossary page
75
- },
76
- ],
77
- ],
78
- // ... other config
79
57
  };
80
58
  ```
81
59
 
82
- **Note:** You need to configure the remark plugin in both the plugin AND the preset (for docs/pages) to enable auto-linking of terms.
60
+ **That's it!** The preset automatically configures the glossary plugin and remark plugin for you.
83
61
 
84
62
  3. **Create your glossary file at `glossary/glossary.json`:**
85
63
 
@@ -161,7 +139,41 @@ Create a JSON file at `glossary/glossary.json` (or your configured path) in your
161
139
 
162
140
  ### Step 2: Configure the Plugin
163
141
 
164
- Add the plugin to your `docusaurus.config.js`:
142
+ #### Option A: Using the Preset (Recommended)
143
+
144
+ The easiest way to configure the plugin is using the preset:
145
+
146
+ ```javascript
147
+ module.exports = {
148
+ presets: [
149
+ [
150
+ 'docusaurus-plugin-glossary/preset',
151
+ {
152
+ glossary: {
153
+ glossaryPath: 'glossary/glossary.json',
154
+ routePath: '/glossary',
155
+ },
156
+ // All standard preset-classic options work here
157
+ docs: {
158
+ sidebarPath: './sidebars.js',
159
+ },
160
+ blog: {
161
+ showReadingTime: true,
162
+ },
163
+ theme: {
164
+ customCss: './src/css/custom.css',
165
+ },
166
+ },
167
+ ],
168
+ ],
169
+ };
170
+ ```
171
+
172
+ The preset automatically configures both the glossary plugin and the remark plugin for automatic term detection.
173
+
174
+ #### Option B: Manual Configuration (Advanced)
175
+
176
+ If you need more control or want to use the plugin alongside the classic preset separately:
165
177
 
166
178
  ```javascript
167
179
  const glossaryPlugin = require('docusaurus-plugin-glossary');
@@ -202,16 +214,14 @@ module.exports = {
202
214
  [
203
215
  'docusaurus-plugin-glossary',
204
216
  {
205
- glossaryPath: 'glossary/glossary.json', // Path to your glossary file
206
- routePath: '/glossary', // URL path for the glossary page
217
+ glossaryPath: 'glossary/glossary.json',
218
+ routePath: '/glossary',
207
219
  },
208
220
  ],
209
221
  ],
210
222
  };
211
223
  ```
212
224
 
213
- **Required Configuration:** To enable automatic term detection and linking, you must configure the remark plugin in your preset (as shown above). The plugin provides a `getRemarkPlugin` helper to make this easier.
214
-
215
225
  **Alternative: Using remarkPlugin directly**
216
226
 
217
227
  You can also use the `remarkPlugin` export directly if you prefer:
@@ -258,16 +268,19 @@ This plugin uses a **hybrid approach** combining build-time transformation and r
258
268
  ### Build-Time: Remark Plugin
259
269
 
260
270
  The remark plugin automatically detects glossary terms in your markdown and:
271
+
261
272
  1. Transforms plain text terms into `<GlossaryTerm>` JSX components
262
273
  2. Automatically injects the necessary import statement (`import GlossaryTerm from '@theme/GlossaryTerm';`)
263
274
  3. This happens during the MDX compilation, before React renders anything
264
275
 
265
276
  **No manual imports needed!** When you write:
277
+
266
278
  ```markdown
267
279
  Our API uses REST principles.
268
280
  ```
269
281
 
270
282
  The remark plugin transforms it to:
283
+
271
284
  ```jsx
272
285
  import GlossaryTerm from '@theme/GlossaryTerm';
273
286
 
@@ -277,6 +290,7 @@ Our <GlossaryTerm term="API">API</GlossaryTerm> uses <GlossaryTerm term="REST">R
277
290
  ### Runtime: Client Modules
278
291
 
279
292
  The plugin uses Docusaurus's `getClientModules()` API to automatically load client-side code on every page. This ensures:
293
+
280
294
  - Glossary term functionality is available globally without configuration
281
295
  - Components initialize correctly on each route change
282
296
  - No performance impact from manual module loading
@@ -284,6 +298,7 @@ The plugin uses Docusaurus's `getClientModules()` API to automatically load clie
284
298
  ### Theme Components
285
299
 
286
300
  The `GlossaryTerm` component is provided via the theme system (`@theme/GlossaryTerm`), making it:
301
+
287
302
  - Available to all MDX files through automatic imports
288
303
  - Swizzlable for custom styling and behavior
289
304
  - Accessible to both the remark plugin and manual usage
@@ -543,6 +558,7 @@ npm run build
543
558
  ```
544
559
 
545
560
  This will:
561
+
546
562
  1. Compile TypeScript (`src/index.ts`) to JavaScript (`dist/index.js`)
547
563
  2. Copy JavaScript, CSS, and test files from `src/` to `dist/`
548
564
 
package/dist/preset.js ADDED
@@ -0,0 +1,146 @@
1
+ import glossaryPlugin, { getRemarkPlugin } from './index.js';
2
+ /**
3
+ * Docusaurus Glossary Preset
4
+ *
5
+ * A preset that extends @docusaurus/preset-classic with automatic glossary functionality.
6
+ * This preset automatically configures the remark plugin for docs and pages, so you don't
7
+ * need to manually add it to remarkPlugins.
8
+ *
9
+ * @example
10
+ * ```javascript
11
+ * export default {
12
+ * presets: [
13
+ * [
14
+ * 'docusaurus-plugin-glossary/preset',
15
+ * {
16
+ * // Glossary options
17
+ * glossary: {
18
+ * glossaryPath: 'glossary/glossary.json',
19
+ * routePath: '/glossary',
20
+ * },
21
+ * // Classic preset options
22
+ * docs: {
23
+ * sidebarPath: './sidebars.js',
24
+ * },
25
+ * blog: {
26
+ * showReadingTime: true,
27
+ * },
28
+ * theme: {
29
+ * customCss: './src/css/custom.css',
30
+ * },
31
+ * },
32
+ * ],
33
+ * ],
34
+ * };
35
+ * ```
36
+ *
37
+ * @param context - Docusaurus context
38
+ * @param options - Preset options including glossary and classic preset options
39
+ * @returns Preset configuration
40
+ */
41
+ export default function preset(context, options = {}) {
42
+ // Explicitly extract glossary and any Docusaurus-added properties that shouldn't go to classic preset
43
+ const { glossary = {}, id, ...restOptions } = options;
44
+ // Extract only valid classic preset options
45
+ const { docs, blog, pages, theme, gtag, googleAnalytics, googleTagManager, sitemap, debug, } = restOptions;
46
+ // Build classic options object with only defined properties
47
+ const classicOptions = {};
48
+ if (docs !== undefined)
49
+ classicOptions.docs = docs;
50
+ if (blog !== undefined)
51
+ classicOptions.blog = blog;
52
+ if (pages !== undefined)
53
+ classicOptions.pages = pages;
54
+ if (theme !== undefined)
55
+ classicOptions.theme = theme;
56
+ if (gtag !== undefined)
57
+ classicOptions.gtag = gtag;
58
+ if (googleAnalytics !== undefined)
59
+ classicOptions.googleAnalytics = googleAnalytics;
60
+ if (googleTagManager !== undefined)
61
+ classicOptions.googleTagManager = googleTagManager;
62
+ if (sitemap !== undefined)
63
+ classicOptions.sitemap = sitemap;
64
+ if (debug !== undefined)
65
+ classicOptions.debug = debug;
66
+ const { glossaryPath = 'glossary/glossary.json', routePath = '/glossary', } = glossary;
67
+ // Get the remark plugin configuration
68
+ const remarkPlugin = getRemarkPlugin({ glossaryPath, routePath }, { siteDir: context.siteDir });
69
+ // Extend docs configuration with glossary remark plugin
70
+ const docsConfig = classicOptions.docs || {};
71
+ const docsRemarkPlugins = docsConfig.remarkPlugins || [];
72
+ const extendedDocsConfig = {
73
+ ...docsConfig,
74
+ remarkPlugins: [...docsRemarkPlugins, remarkPlugin],
75
+ };
76
+ // Extend pages configuration with glossary remark plugin
77
+ const pagesConfig = classicOptions.pages || {};
78
+ const pagesRemarkPlugins = pagesConfig.remarkPlugins || [];
79
+ const extendedPagesConfig = {
80
+ ...pagesConfig,
81
+ remarkPlugins: [...pagesRemarkPlugins, remarkPlugin],
82
+ };
83
+ // Extend blog configuration with glossary remark plugin (optional)
84
+ const blogConfig = classicOptions.blog;
85
+ let extendedBlogConfig = blogConfig;
86
+ if (blogConfig && blogConfig !== false) {
87
+ const blogRemarkPlugins = typeof blogConfig === 'object' ? blogConfig.remarkPlugins || [] : [];
88
+ extendedBlogConfig =
89
+ typeof blogConfig === 'object'
90
+ ? {
91
+ ...blogConfig,
92
+ remarkPlugins: [...blogRemarkPlugins, remarkPlugin],
93
+ }
94
+ : blogConfig;
95
+ }
96
+ // Build the final classic preset options
97
+ const finalClassicOptions = {};
98
+ if (extendedDocsConfig !== undefined)
99
+ finalClassicOptions.docs = extendedDocsConfig;
100
+ if (extendedBlogConfig !== undefined)
101
+ finalClassicOptions.blog = extendedBlogConfig;
102
+ if (extendedPagesConfig !== undefined)
103
+ finalClassicOptions.pages = extendedPagesConfig;
104
+ if (theme !== undefined)
105
+ finalClassicOptions.theme = theme;
106
+ if (gtag !== undefined)
107
+ finalClassicOptions.gtag = gtag;
108
+ if (googleAnalytics !== undefined)
109
+ finalClassicOptions.googleAnalytics = googleAnalytics;
110
+ if (googleTagManager !== undefined)
111
+ finalClassicOptions.googleTagManager = googleTagManager;
112
+ if (sitemap !== undefined)
113
+ finalClassicOptions.sitemap = sitemap;
114
+ if (debug !== undefined)
115
+ finalClassicOptions.debug = debug;
116
+ const plugins = [
117
+ // Add the glossary plugin first
118
+ function glossaryPluginWrapper(context) {
119
+ return glossaryPlugin(context, glossary);
120
+ },
121
+ ];
122
+ // Add classic preset plugins individually
123
+ if (extendedDocsConfig)
124
+ plugins.push(['@docusaurus/plugin-content-docs', extendedDocsConfig]);
125
+ if (extendedBlogConfig && extendedBlogConfig !== false)
126
+ plugins.push(['@docusaurus/plugin-content-blog', extendedBlogConfig]);
127
+ if (extendedPagesConfig)
128
+ plugins.push(['@docusaurus/plugin-content-pages', extendedPagesConfig]);
129
+ if (gtag)
130
+ plugins.push(['@docusaurus/plugin-google-gtag', gtag]);
131
+ if (googleAnalytics)
132
+ plugins.push(['@docusaurus/plugin-google-analytics', googleAnalytics]);
133
+ if (googleTagManager)
134
+ plugins.push(['@docusaurus/plugin-google-tag-manager', googleTagManager]);
135
+ if (sitemap !== false)
136
+ plugins.push(['@docusaurus/plugin-sitemap', sitemap || {}]);
137
+ if (debug)
138
+ plugins.push(['@docusaurus/plugin-debug', {}]);
139
+ return {
140
+ themes: [
141
+ // Pass theme options (including customCss) to theme-classic
142
+ ['@docusaurus/theme-classic', theme || {}],
143
+ ],
144
+ plugins,
145
+ };
146
+ }
@@ -37,7 +37,7 @@ export default function remarkGlossaryTerms({
37
37
 
38
38
  // Check cache first to avoid repeated file reads
39
39
  const cached = glossaryCache.get(glossaryFilePath);
40
- if (cached && (now - cached.loadedAt) < CACHE_TTL) {
40
+ if (cached && now - cached.loadedAt < CACHE_TTL) {
41
41
  glossaryTerms = cached.terms;
42
42
  } else {
43
43
  // Cache miss or expired - load from file synchronously
@@ -56,7 +56,9 @@ export default function remarkGlossaryTerms({
56
56
 
57
57
  // Log only once per file (when cache is first populated)
58
58
  if (!cached && process.env.NODE_ENV !== 'production') {
59
- console.log(`[glossary-plugin] Loaded ${glossaryTerms.length} terms from ${glossaryPath}`);
59
+ console.log(
60
+ `[glossary-plugin] Loaded ${glossaryTerms.length} terms from ${glossaryPath}`
61
+ );
60
62
  }
61
63
  } else {
62
64
  // File doesn't exist - cache empty result to avoid repeated checks
@@ -70,7 +72,10 @@ export default function remarkGlossaryTerms({
70
72
  }
71
73
  }
72
74
  } catch (error) {
73
- console.warn(`[glossary-plugin] Failed to load glossary from ${glossaryPath}:`, error.message);
75
+ console.warn(
76
+ `[glossary-plugin] Failed to load glossary from ${glossaryPath}:`,
77
+ error.message
78
+ );
74
79
  // Cache the error to avoid repeated attempts
75
80
  if (glossaryPath && siteDir) {
76
81
  const glossaryFilePath = path.resolve(siteDir, glossaryPath);
@@ -315,10 +320,11 @@ export default function remarkGlossaryTerms({
315
320
  // Check for existing import
316
321
  const hasImport =
317
322
  Array.isArray(tree.children) &&
318
- tree.children.some(n =>
319
- n.type === 'mdxjsEsm' &&
320
- (n.value?.includes('@theme/GlossaryTerm') ||
321
- n.data?.estree?.body?.some(s => s.source?.value === '@theme/GlossaryTerm'))
323
+ tree.children.some(
324
+ n =>
325
+ n.type === 'mdxjsEsm' &&
326
+ (n.value?.includes('@theme/GlossaryTerm') ||
327
+ n.data?.estree?.body?.some(s => s.source?.value === '@theme/GlossaryTerm'))
322
328
  );
323
329
 
324
330
  if (!hasImport) {
@@ -10,7 +10,7 @@
10
10
  border-bottom-color: color-mix(in srgb, var(--ifm-font-color-base) 20%, transparent);
11
11
  cursor: help;
12
12
  transition: all 0.2s;
13
- }
13
+ }
14
14
 
15
15
  .glossaryTerm:hover {
16
16
  color: var(--ifm-color-primary-dark);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "docusaurus-plugin-glossary",
3
- "version": "2.0.2",
3
+ "version": "3.0.0",
4
4
  "description": "A Docusaurus plugin for creating and managing glossary terms with auto-generated pages and inline tooltips",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -10,6 +10,11 @@
10
10
  "require": "./dist/index.js",
11
11
  "default": "./dist/index.js"
12
12
  },
13
+ "./preset": {
14
+ "import": "./dist/preset.js",
15
+ "require": "./dist/preset.js",
16
+ "default": "./dist/preset.js"
17
+ },
13
18
  "./remark/glossary-terms": {
14
19
  "import": "./dist/remark/glossary-terms.js",
15
20
  "require": "./dist/remark/glossary-terms.js",
@@ -27,6 +32,8 @@
27
32
  "test": "jest",
28
33
  "test:watch": "jest --watch",
29
34
  "test:coverage": "jest --coverage",
35
+ "test:e2e": "playwright test",
36
+ "test:e2e:ui": "playwright test --ui",
30
37
  "example:start": "npm --prefix examples/docusaurus-v3 run start",
31
38
  "example:build": "npm --prefix examples/docusaurus-v3 run build",
32
39
  "example:serve": "npm --prefix examples/docusaurus-v3 run serve",
@@ -74,6 +81,7 @@
74
81
  "@babel/preset-typescript": "^7.28.5",
75
82
  "@docusaurus/tsconfig": "^3.9.2",
76
83
  "@docusaurus/types": "^3.9.2",
84
+ "@playwright/test": "^1.56.1",
77
85
  "@testing-library/jest-dom": "^6.9.1",
78
86
  "@testing-library/react": "^16.3.0",
79
87
  "@testing-library/user-event": "^14.6.1",