docusaurus-plugin-glossary 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  A comprehensive Docusaurus plugin that provides glossary functionality with an auto-generated glossary page, searchable terms, and inline term tooltips.
4
4
 
5
+ > Compatibility: Fully compatible with Docusaurus v3 (MDX v3). If you were on a v2-era fork, please upgrade to the latest 1.x release of this plugin. No manual MDX pipeline wiring is required when `autoLinkTerms` is enabled (default).
6
+
5
7
  ## Features
6
8
 
7
9
  - **Auto-generated Glossary Page**: Displays all terms alphabetically with letter navigation
@@ -13,177 +15,77 @@ A comprehensive Docusaurus plugin that provides glossary functionality with an a
13
15
  - **Abbreviation Support**: Display full form of abbreviated terms
14
16
  - **Customizable**: Configure glossary path and route
15
17
 
16
- ## Installation
17
-
18
- ### For Local Use (Same Repository)
19
-
20
- 1. Copy the plugin directory to your Docusaurus site:
18
+ ## Quick Start
21
19
 
22
- ```
23
- src/plugins/docusaurus-plugin-glossary/
20
+ 1. **Install the plugin:**
21
+ ```bash
22
+ npm install docusaurus-plugin-glossary
24
23
  ```
25
24
 
26
- 2. Add the plugin to your `docusaurus.config.js`:
25
+ 2. **Add to your `docusaurus.config.js`:**
27
26
  ```javascript
28
- const glossaryPlugin = require.resolve('./src/plugins/docusaurus-plugin-glossary');
29
-
30
27
  module.exports = {
31
28
  // ... other config
32
29
  plugins: [
33
30
  [
34
- glossaryPlugin,
31
+ 'docusaurus-plugin-glossary',
35
32
  {
36
- glossaryPath: 'glossary/glossary.json', // optional, default: 'glossary/glossary.json'
37
- routePath: '/glossary', // optional, default: '/glossary'
38
- autoLinkTerms: true, // optional, default: true - automatically link terms in markdown
33
+ glossaryPath: 'glossary/glossary.json', // Path to your glossary file
34
+ routePath: '/glossary', // URL path for glossary page
35
+ autoLinkTerms: true, // Automatically link terms (default: true)
39
36
  },
40
37
  ],
41
38
  ],
42
39
  // ... other config
43
40
  };
44
41
  ```
42
+
43
+ **That’s it!** On Docusaurus v3, the remark plugin is automatically configured via the plugin’s `configureMarkdown` hook — no manual `markdown.remarkPlugins` setup needed.
45
44
 
46
- 3. **Enable automatic term detection** by adding the remark plugin to your markdown configuration:
47
- ```javascript
48
- const glossaryPlugin = require('./src/plugins/docusaurus-plugin-glossary');
49
-
50
- module.exports = {
51
- // ... other config
52
- markdown: {
53
- remarkPlugins: [
54
- [
55
- glossaryPlugin.remarkPlugin,
56
- {
57
- glossaryPath: 'glossary/glossary.json',
58
- routePath: '/glossary',
59
- siteDir: process.cwd(), // or your site directory
60
- },
61
- ],
62
- ],
63
- },
64
- // ... other config
65
- };
66
- ```
67
-
68
- ### For Separate Package (To Publish)
69
-
70
- To publish this as a separate npm package:
71
-
72
- 1. Create a new directory for the package:
73
-
74
- ```bash
75
- mkdir docusaurus-plugin-glossary
76
- cd docusaurus-plugin-glossary
77
- ```
78
-
79
- 2. Copy the plugin files:
80
-
81
- ```
82
- docusaurus-plugin-glossary/
83
- ├── index.js
84
- ├── components/
85
- │ ├── GlossaryPage.js
86
- │ └── GlossaryPage.module.css
87
- ├── remark/
88
- │ └── glossary-terms.js
89
- ├── theme/
90
- │ └── GlossaryTerm/
91
- │ ├── index.js
92
- │ └── styles.module.css
93
- ├── package.json
94
- └── README.md
95
- ```
96
-
97
- 3. Create a `package.json`:
98
-
45
+ 3. **Create your glossary file at `glossary/glossary.json`:**
99
46
  ```json
100
47
  {
101
- "name": "docusaurus-plugin-glossary",
102
- "version": "1.0.0",
103
- "description": "A Docusaurus plugin for creating and managing glossary terms",
104
- "main": "index.js",
105
- "files": [
106
- "index.js",
107
- "components/",
108
- "theme/",
109
- "remark/",
110
- "README.md",
111
- "LICENSE"
112
- ],
113
- "keywords": ["docusaurus", "glossary", "plugin", "documentation"],
114
- "peerDependencies": {
115
- "@docusaurus/core": "^3.0.0",
116
- "react": "^18.0.0",
117
- "react-dom": "^18.0.0"
118
- },
119
- "dependencies": {
120
- "fs-extra": "^11.0.0",
121
- "unist-util-visit": "^5.0.0"
122
- },
123
- "engines": {
124
- "node": ">=16.14"
125
- }
48
+ "description": "A collection of technical terms and their definitions",
49
+ "terms": [
50
+ {
51
+ "term": "API",
52
+ "abbreviation": "Application Programming Interface",
53
+ "definition": "A set of rules and protocols that allows different software applications to communicate with each other.",
54
+ "relatedTerms": ["REST", "GraphQL"]
55
+ },
56
+ {
57
+ "term": "REST",
58
+ "abbreviation": "Representational State Transfer",
59
+ "definition": "An architectural style for designing networked applications.",
60
+ "relatedTerms": ["API", "HTTP"]
61
+ }
62
+ ]
126
63
  }
127
64
  ```
128
65
 
129
- 4. Publish to npm:
130
-
66
+ 4. **Start your dev server:**
131
67
  ```bash
132
- npm publish
68
+ npm run start
133
69
  ```
134
70
 
135
- 5. Install in your Docusaurus site:
71
+ 5. **That's it!**
72
+ - Visit `/glossary` to see your glossary page
73
+ - Write markdown normally - terms will automatically be linked with tooltips
74
+ - Use `<GlossaryTerm>` component in MDX for manual control
136
75
 
137
- ```bash
138
- npm install docusaurus-plugin-glossary
139
- ```
76
+ ## Installation
140
77
 
141
- 6. Add to your `docusaurus.config.js`:
142
- ```javascript
143
- const glossaryPlugin = require('docusaurus-plugin-glossary');
144
-
145
- module.exports = {
146
- // ... other config
147
- plugins: [
148
- [
149
- 'docusaurus-plugin-glossary',
150
- {
151
- glossaryPath: 'glossary/glossary.json',
152
- routePath: '/glossary',
153
- },
154
- ],
155
- ],
156
- // ... other config
157
- };
158
- ```
78
+ ### Install from npm (Recommended)
159
79
 
160
- 7. **Enable automatic term detection** by adding the remark plugin to your markdown configuration:
161
- ```javascript
162
- const glossaryPlugin = require('docusaurus-plugin-glossary');
163
-
164
- module.exports = {
165
- // ... other config
166
- markdown: {
167
- remarkPlugins: [
168
- [
169
- glossaryPlugin.remarkPlugin,
170
- {
171
- glossaryPath: 'glossary/glossary.json',
172
- routePath: '/glossary',
173
- siteDir: process.cwd(), // or your site directory
174
- },
175
- ],
176
- ],
177
- },
178
- // ... other config
179
- };
180
- ```
80
+ ```bash
81
+ npm install docusaurus-plugin-glossary
82
+ ```
181
83
 
182
- ## Usage
84
+ ## Usage Guide
183
85
 
184
- ### 1. Create a Glossary Data File
86
+ ### Step 1: Create Your Glossary File
185
87
 
186
- Create a JSON file at `glossary/glossary.json` (or your configured path):
88
+ Create a JSON file at `glossary/glossary.json` (or your configured path) in your Docusaurus site root:
187
89
 
188
90
  ```json
189
91
  {
@@ -205,19 +107,92 @@ Create a JSON file at `glossary/glossary.json` (or your configured path):
205
107
  }
206
108
  ```
207
109
 
208
- ### 2. Glossary Data Structure
110
+ **Required fields:**
111
+ - `term` (string): The glossary term name
112
+ - `definition` (string): The term's definition
113
+
114
+ **Optional fields:**
115
+ - `abbreviation` (string): The full form if the term is an abbreviation
116
+ - `relatedTerms` (string[]): Array of related term names that link to other glossary entries
117
+ - `id` (string): Custom ID for linking (auto-generated from term name if not provided)
118
+
119
+ ### Step 2: Configure the Plugin
120
+
121
+ Add the plugin to your `docusaurus.config.js`:
122
+
123
+ ```javascript
124
+ module.exports = {
125
+ plugins: [
126
+ [
127
+ 'docusaurus-plugin-glossary',
128
+ {
129
+ glossaryPath: 'glossary/glossary.json', // Path to your glossary file
130
+ routePath: '/glossary', // URL path for the glossary page
131
+ autoLinkTerms: true, // Automatically detect and link terms (default: true)
132
+ },
133
+ ],
134
+ ],
135
+ };
136
+ ```
137
+
138
+ **Automatic Configuration:** The remark plugin is automatically configured when `autoLinkTerms` is `true` (the default). You don't need to manually configure `markdown.remarkPlugins`!
139
+
140
+ **Advanced: Manual Remark Plugin Configuration**
141
+
142
+ If you need more control or want to disable automatic detection, you can manually configure the remark plugin:
143
+
144
+ ```javascript
145
+ const glossaryPlugin = require('docusaurus-plugin-glossary');
146
+
147
+ module.exports = {
148
+ plugins: [
149
+ [
150
+ 'docusaurus-plugin-glossary',
151
+ {
152
+ glossaryPath: 'glossary/glossary.json',
153
+ routePath: '/glossary',
154
+ autoLinkTerms: false, // Disable automatic configuration
155
+ },
156
+ ],
157
+ ],
158
+ markdown: {
159
+ remarkPlugins: [
160
+ [
161
+ glossaryPlugin.remarkPlugin,
162
+ {
163
+ glossaryPath: 'glossary/glossary.json',
164
+ routePath: '/glossary',
165
+ siteDir: process.cwd(),
166
+ },
167
+ ],
168
+ ],
169
+ },
170
+ };
171
+ ```
172
+
173
+ ## Docusaurus v3 Notes and Troubleshooting
174
+
175
+ - **MDX imports**: The plugin injects `import GlossaryTerm from '@theme/GlossaryTerm';` automatically when it auto-links a term. If you’re writing MDX manually, you can also import and use it yourself:
176
+
177
+ ```mdx
178
+ import GlossaryTerm from '@theme/GlossaryTerm';
179
+
180
+ Our <GlossaryTerm term="API" /> uses <GlossaryTerm term="REST">RESTful</GlossaryTerm> principles.
181
+ ```
209
182
 
210
- Each term object can include:
183
+ - **No tooltips or no auto-linking?**
184
+ - Confirm you’re on `@docusaurus/core@^3` and `react@^18`.
185
+ - Ensure the plugin is listed in `plugins` and `autoLinkTerms` is not disabled.
186
+ - Visit `/glossary`. If the page or route fails to render, verify your `glossaryPath` file exists and contains a `terms` array.
187
+ - If you previously used a local patch for `1.0.0`, remove it when using `1.0.2+`; the plugin bundles the v3-compatible theme and remark integration.
211
188
 
212
- - `term` (required): The glossary term
213
- - `definition` (required): The term's definition
214
- - `abbreviation` (optional): The full form if the term is an abbreviation
215
- - `relatedTerms` (optional): Array of related term names
216
- - `id` (optional): Custom ID for linking (auto-generated from term if not provided)
189
+ - **Opting out of auto-linking**: set `autoLinkTerms: false` and add the remark plugin manually (see above), or only use the `<GlossaryTerm />` component where you want explicit control.
217
190
 
218
- ### 3. Automatic Term Detection
191
+ ### Step 3: Use Glossary Terms in Your Content
219
192
 
220
- When the remark plugin is configured (see Installation), glossary terms are automatically detected and linked in all markdown files. Simply write your content normally:
193
+ #### Option A: Automatic Detection (Recommended)
194
+
195
+ With the remark plugin configured, glossary terms are **automatically detected and linked** in all your markdown files. Simply write your content normally:
221
196
 
222
197
  ```markdown
223
198
  Our API uses REST principles to provide a simple interface.
@@ -225,45 +200,66 @@ Our API uses REST principles to provide a simple interface.
225
200
  This project supports webhooks for real-time notifications.
226
201
  ```
227
202
 
228
- Terms like "API", "REST", and "webhooks" will automatically be detected if they're defined in your glossary and will appear with:
229
- - Dotted underline styling
230
- - Tooltip showing definition on hover
231
- - Link to full glossary page entry
203
+ Terms like "API", "REST", and "webhooks" will automatically be:
204
+ - Detected if they're defined in your glossary
205
+ - Styled with a dotted underline
206
+ - Display a tooltip with the definition on hover
207
+ - Link to the full glossary page entry
232
208
 
233
- **Note**: Automatic detection works for whole words only and respects word boundaries. Terms inside code blocks, links, or existing MDX components are not processed.
209
+ **Limitations:**
210
+ - Only whole words are matched (respects word boundaries)
211
+ - Terms inside code blocks, links, or existing MDX components are **not** processed
212
+ - Matching is case-insensitive
234
213
 
235
- ### 4. Using the GlossaryTerm Component Manually
214
+ #### Option B: Manual Component Usage
236
215
 
237
- For more control or when automatic detection isn't sufficient, you can manually import and use the `GlossaryTerm` component in your MDX files:
216
+ For more control or when automatic detection isn't sufficient, use the `GlossaryTerm` component in MDX files:
238
217
 
239
218
  ```jsx
240
219
  import GlossaryTerm from '@theme/GlossaryTerm';
241
220
 
242
- This website uses an <GlossaryTerm term="API" definition="Application Programming Interface">API</GlossaryTerm> to fetch data.
221
+ This website uses an <GlossaryTerm term="API">API</GlossaryTerm> to fetch data.
243
222
 
244
- // Or with default display (term name):
245
- We use <GlossaryTerm term="REST" definition="Representational State Transfer" /> for our web services.
246
- ```
223
+ // Or with explicit definition (overrides glossary entry):
224
+ We use <GlossaryTerm term="REST" definition="Representational State Transfer" /> for our services.
247
225
 
248
- The component features:
226
+ // Or with children content:
227
+ Our <GlossaryTerm term="API" definition="Application Programming Interface">RESTful API</GlossaryTerm> is available.
228
+ ```
249
229
 
250
- - Dotted underline styling
251
- - Tooltip showing definition on hover
252
- - Link to full glossary page entry
253
- - Accessible with keyboard navigation
230
+ **Component props:**
231
+ - `term` (required): The term name (used to look up definition from glossary)
232
+ - `definition` (optional): Override the definition from the glossary file
233
+ - `children` (optional): Custom text to display (defaults to term name)
254
234
 
255
- ### 5. Accessing the Glossary Page
235
+ ### Step 4: Access the Glossary Page
256
236
 
257
237
  The glossary page is automatically available at `/glossary` (or your configured `routePath`).
258
238
 
259
- Features:
260
-
239
+ **Features:**
261
240
  - Alphabetical grouping with letter navigation
262
- - Real-time search
263
- - Related terms linking
241
+ - Real-time search across terms and definitions
242
+ - Clickable related terms
264
243
  - Responsive design
265
244
  - Dark mode support
266
245
 
246
+ **Add to navigation:**
247
+
248
+ To add the glossary to your navbar, update your `docusaurus.config.js`:
249
+
250
+ ```javascript
251
+ module.exports = {
252
+ themeConfig: {
253
+ navbar: {
254
+ items: [
255
+ {to: '/glossary', label: 'Glossary', position: 'left'},
256
+ // ... other items
257
+ ],
258
+ },
259
+ },
260
+ };
261
+ ```
262
+
267
263
  ## Configuration Options
268
264
 
269
265
  | Option | Type | Default | Description |
@@ -316,12 +312,26 @@ themeConfig: {
316
312
 
317
313
  ## Examples
318
314
 
319
- ### Example 1: Technical Documentation
315
+ See the [Usage Guide](#usage-guide) section above for complete examples. Here are additional examples:
316
+
317
+ ### Complete Glossary Example
320
318
 
321
319
  ```json
322
320
  {
323
321
  "description": "Technical terms used in our documentation",
324
322
  "terms": [
323
+ {
324
+ "term": "API",
325
+ "abbreviation": "Application Programming Interface",
326
+ "definition": "A set of rules and protocols that allows different software applications to communicate with each other.",
327
+ "relatedTerms": ["REST", "GraphQL", "Webhook"]
328
+ },
329
+ {
330
+ "term": "REST",
331
+ "abbreviation": "Representational State Transfer",
332
+ "definition": "An architectural style for designing networked applications.",
333
+ "relatedTerms": ["API", "HTTP"]
334
+ },
325
335
  {
326
336
  "term": "Webhook",
327
337
  "definition": "An HTTP callback that occurs when something happens; a simple event-notification via HTTP POST.",
@@ -331,9 +341,7 @@ themeConfig: {
331
341
  }
332
342
  ```
333
343
 
334
- ### Example 2: Automatic Term Detection
335
-
336
- With the remark plugin configured, you can simply write markdown normally:
344
+ ### Writing Content with Automatic Detection
337
345
 
338
346
  ```markdown
339
347
  ---
@@ -348,7 +356,7 @@ Webhooks are supported for real-time event notifications.
348
356
 
349
357
  The terms "API", "RESTful", and "Webhooks" will automatically be detected and linked if they're defined in your glossary.
350
358
 
351
- ### Example 3: Using in MDX Manually
359
+ ### Using the Component in MDX
352
360
 
353
361
  ```mdx
354
362
  ---
@@ -359,8 +367,8 @@ import GlossaryTerm from '@theme/GlossaryTerm';
359
367
 
360
368
  # Getting Started with Our API
361
369
 
362
- Our <GlossaryTerm term="API" definition="Application Programming Interface" />
363
- uses <GlossaryTerm term="REST" definition="Representational State Transfer">RESTful</GlossaryTerm>
370
+ Our <GlossaryTerm term="API" />
371
+ uses <GlossaryTerm term="REST">RESTful</GlossaryTerm>
364
372
  principles to provide a simple and consistent interface.
365
373
  ```
366
374
 
@@ -421,11 +429,13 @@ The remark plugin (`remark/glossary-terms.js`) automatically detects glossary te
421
429
 
422
430
  ### Automatic term detection not working
423
431
 
424
- - Ensure the remark plugin is configured in `markdown.remarkPlugins` in `docusaurus.config.js`
425
- - Check that `glossaryPath` and `siteDir` are correctly configured in the remark plugin options
426
- - Verify your glossary file exists and contains terms
432
+ - Ensure `autoLinkTerms` is `true` (the default) in your plugin configuration
433
+ - The remark plugin is automatically configured, so you don't need to manually add it to `markdown.remarkPlugins`
434
+ - Verify your glossary file exists at the configured `glossaryPath` and contains terms
435
+ - Check that terms in your content match the terms in your glossary (matching is case-insensitive but respects word boundaries)
427
436
  - Try clearing cache with `npm run clear` and restarting the dev server
428
437
  - Note that terms inside code blocks, links, or MDX components are not processed
438
+ - If you've manually configured the remark plugin, ensure `siteDir` points to the correct Docusaurus site directory
429
439
 
430
440
  ### Styles not applying
431
441
 
package/index.js CHANGED
@@ -30,6 +30,41 @@ function glossaryPlugin(context, options = {}) {
30
30
  return {
31
31
  name: 'docusaurus-plugin-glossary',
32
32
 
33
+ configureMarkdown(markdownConfig) {
34
+ // Automatically configure the remark plugin if autoLinkTerms is enabled
35
+ if (autoLinkTerms) {
36
+ if (!markdownConfig.remarkPlugins) {
37
+ markdownConfig.remarkPlugins = [];
38
+ }
39
+
40
+ const remarkPlugin = require('./remark/glossary-terms');
41
+
42
+ // Check if the remark plugin is already configured
43
+ const isAlreadyConfigured = markdownConfig.remarkPlugins.some(
44
+ (plugin) => {
45
+ if (Array.isArray(plugin) && plugin[0]) {
46
+ // Check if it's our remark plugin by comparing the function reference
47
+ return plugin[0] === remarkPlugin;
48
+ }
49
+ // Also check if it's directly the remark plugin function
50
+ return plugin === remarkPlugin;
51
+ }
52
+ );
53
+
54
+ // Only add if not already configured
55
+ if (!isAlreadyConfigured) {
56
+ markdownConfig.remarkPlugins.push([
57
+ remarkPlugin,
58
+ {
59
+ glossaryPath,
60
+ routePath,
61
+ siteDir: context.siteDir,
62
+ }
63
+ ]);
64
+ }
65
+ }
66
+ },
67
+
33
68
  async loadContent() {
34
69
  // Load glossary terms from JSON file
35
70
  const glossaryFilePath = path.resolve(context.siteDir, glossaryPath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "docusaurus-plugin-glossary",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "A Docusaurus plugin for creating and managing glossary terms with auto-generated pages and inline tooltips",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -168,6 +168,7 @@ function remarkGlossaryTerms({
168
168
  }
169
169
 
170
170
  return (tree) => {
171
+ let usedGlossaryTerm = false;
171
172
  visit(tree, 'text', (node, index, parent) => {
172
173
  // Skip text nodes inside code blocks, links, or existing MDX components
173
174
  if (
@@ -199,6 +200,12 @@ function remarkGlossaryTerms({
199
200
  };
200
201
  }
201
202
  }
203
+ if (
204
+ replacement.type === 'mdxJsxFlowElement' ||
205
+ replacement.type === 'mdxJsxTextElement'
206
+ ) {
207
+ usedGlossaryTerm = true;
208
+ }
202
209
  return replacement;
203
210
  });
204
211
 
@@ -207,6 +214,40 @@ function remarkGlossaryTerms({
207
214
  return index + newNodes.length - 1; // Return new index to continue
208
215
  }
209
216
  });
217
+
218
+ // Inject MDX import for GlossaryTerm if we used it anywhere in this file
219
+ if (usedGlossaryTerm) {
220
+ const importNode = {
221
+ type: 'mdxjsEsm',
222
+ value: "import GlossaryTerm from '@theme/GlossaryTerm';",
223
+ data: {
224
+ estree: {
225
+ type: 'Program',
226
+ sourceType: 'module',
227
+ body: [
228
+ {
229
+ type: 'ImportDeclaration',
230
+ specifiers: [
231
+ {
232
+ type: 'ImportDefaultSpecifier',
233
+ local: { type: 'Identifier', name: 'GlossaryTerm' }
234
+ }
235
+ ],
236
+ source: { type: 'Literal', value: '@theme/GlossaryTerm' }
237
+ }
238
+ ]
239
+ }
240
+ }
241
+ };
242
+
243
+ // Avoid duplicate imports if already present
244
+ const hasExistingImport = Array.isArray(tree.children) && tree.children.some(
245
+ (n) => n.type === 'mdxjsEsm' && typeof n.value === 'string' && n.value.includes("@theme/GlossaryTerm")
246
+ );
247
+ if (!hasExistingImport) {
248
+ tree.children.unshift(importNode);
249
+ }
250
+ }
210
251
  };
211
252
  }
212
253
 
@@ -33,10 +33,11 @@
33
33
  white-space: normal;
34
34
  max-width: 300px;
35
35
  min-width: 200px;
36
- z-index: 1000;
36
+ z-index: 9999;
37
37
  opacity: 0;
38
38
  pointer-events: none;
39
39
  transition: opacity 0.2s;
40
+ visibility: hidden;
40
41
  }
41
42
 
42
43
  .tooltip::after {
@@ -63,6 +64,7 @@
63
64
  .tooltipVisible {
64
65
  opacity: 1;
65
66
  pointer-events: auto;
67
+ visibility: visible;
66
68
  }
67
69
 
68
70
  .tooltip strong {