docusaurus-plugin-llms 0.2.2 → 0.3.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 +246 -16
- package/lib/generator-current.d.ts +44 -0
- package/lib/generator-current.js +398 -0
- package/lib/generator.d.ts +4 -2
- package/lib/generator.js +163 -71
- package/lib/index.js +174 -10
- package/lib/null-handling-guide.d.ts +47 -0
- package/lib/null-handling-guide.js +290 -0
- package/lib/processor.d.ts +0 -10
- package/lib/processor.js +217 -80
- package/lib/types.d.ts +10 -0
- package/lib/utils.d.ts +141 -7
- package/lib/utils.js +429 -34
- package/package.json +2 -2
- package/src/generator.ts +206 -86
- package/src/index.ts +202 -14
- package/src/null-handling-guide.ts +321 -0
- package/src/processor.ts +303 -126
- package/src/types.ts +15 -0
- package/src/utils.ts +530 -59
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NULL AND UNDEFINED HANDLING GUIDE
|
|
3
|
+
*
|
|
4
|
+
* This file documents the standardized patterns for null/undefined handling
|
|
5
|
+
* across the docusaurus-plugin-llms codebase.
|
|
6
|
+
*
|
|
7
|
+
* PRINCIPLES:
|
|
8
|
+
* 1. Be explicit about null/undefined checks - avoid loose truthy checks
|
|
9
|
+
* 2. Use optional chaining for safe property access on optional values
|
|
10
|
+
* 3. Validate required parameters early with explicit checks
|
|
11
|
+
* 4. Distinguish between "missing" (undefined/null) and "falsy" (0, '', false)
|
|
12
|
+
*
|
|
13
|
+
* PATTERNS:
|
|
14
|
+
*/
|
|
15
|
+
export {};
|
|
16
|
+
/**
|
|
17
|
+
* QUICK REFERENCE:
|
|
18
|
+
*
|
|
19
|
+
* 1. Optional parameter check:
|
|
20
|
+
* if (value !== undefined && value !== null) { ... }
|
|
21
|
+
*
|
|
22
|
+
* 2. Safe property access:
|
|
23
|
+
* const prop = obj?.property
|
|
24
|
+
*
|
|
25
|
+
* 3. Type validation with null check:
|
|
26
|
+
* if (typeof value !== 'object' || value === null) { throw ... }
|
|
27
|
+
*
|
|
28
|
+
* 4. Non-empty string:
|
|
29
|
+
* if (typeof value === 'string' && value.trim() !== '') { ... }
|
|
30
|
+
*
|
|
31
|
+
* 5. Non-empty array:
|
|
32
|
+
* if (value?.length) { ... }
|
|
33
|
+
*
|
|
34
|
+
* 6. Default values:
|
|
35
|
+
* const result = value ?? defaultValue
|
|
36
|
+
*
|
|
37
|
+
* 7. Boolean with default:
|
|
38
|
+
* const enabled = value !== false // undefined and null become true
|
|
39
|
+
*
|
|
40
|
+
* 8. Required parameter:
|
|
41
|
+
* if (value === undefined || value === null) { throw ... }
|
|
42
|
+
*
|
|
43
|
+
* AVOID:
|
|
44
|
+
* - if (value) { ... } // Too loose, catches falsy values
|
|
45
|
+
* - value || defaultValue // Replaces ALL falsy values, not just null/undefined
|
|
46
|
+
* - !value // Too loose for validation
|
|
47
|
+
*/
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* NULL AND UNDEFINED HANDLING GUIDE
|
|
4
|
+
*
|
|
5
|
+
* This file documents the standardized patterns for null/undefined handling
|
|
6
|
+
* across the docusaurus-plugin-llms codebase.
|
|
7
|
+
*
|
|
8
|
+
* PRINCIPLES:
|
|
9
|
+
* 1. Be explicit about null/undefined checks - avoid loose truthy checks
|
|
10
|
+
* 2. Use optional chaining for safe property access on optional values
|
|
11
|
+
* 3. Validate required parameters early with explicit checks
|
|
12
|
+
* 4. Distinguish between "missing" (undefined/null) and "falsy" (0, '', false)
|
|
13
|
+
*
|
|
14
|
+
* PATTERNS:
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
// ============================================================================
|
|
18
|
+
// PATTERN 1: Checking for null OR undefined (most common)
|
|
19
|
+
// ============================================================================
|
|
20
|
+
/**
|
|
21
|
+
* Use when you need to check if a value exists (not null and not undefined).
|
|
22
|
+
* This is the most common check for optional parameters or properties.
|
|
23
|
+
*/
|
|
24
|
+
function checkIfDefined_GOOD(value) {
|
|
25
|
+
if (value !== undefined && value !== null) {
|
|
26
|
+
// Value is defined and not null
|
|
27
|
+
console.log(value.toUpperCase());
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* AVOID: Loose truthy check - this catches 0, '', false, NaN
|
|
32
|
+
*/
|
|
33
|
+
function checkIfDefined_BAD(value) {
|
|
34
|
+
if (value) {
|
|
35
|
+
// PROBLEM: This rejects empty strings, 0, false, etc.
|
|
36
|
+
console.log(value.toUpperCase());
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// ============================================================================
|
|
40
|
+
// PATTERN 2: Optional chaining for safe property access
|
|
41
|
+
// ============================================================================
|
|
42
|
+
/**
|
|
43
|
+
* Use optional chaining when accessing properties on values that might be
|
|
44
|
+
* null or undefined. This avoids TypeError exceptions.
|
|
45
|
+
*/
|
|
46
|
+
function safePropertyAccess_GOOD(obj) {
|
|
47
|
+
const value = obj?.prop;
|
|
48
|
+
if (value !== undefined && value !== null) {
|
|
49
|
+
console.log(value);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* AVOID: Manual null checks before property access (verbose)
|
|
54
|
+
*/
|
|
55
|
+
function safePropertyAccess_BAD(obj) {
|
|
56
|
+
if (obj && obj.prop) {
|
|
57
|
+
// Verbose and misses the case where prop is explicitly false/0
|
|
58
|
+
console.log(obj.prop);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// ============================================================================
|
|
62
|
+
// PATTERN 3: Type-specific validation with explicit null checks
|
|
63
|
+
// ============================================================================
|
|
64
|
+
/**
|
|
65
|
+
* Use when validating optional parameters that must be a specific type if provided.
|
|
66
|
+
* Check both that the value is defined AND that it has the correct type.
|
|
67
|
+
*/
|
|
68
|
+
function validateOptionalString_GOOD(value) {
|
|
69
|
+
if (value !== undefined && value !== null) {
|
|
70
|
+
if (typeof value !== 'string') {
|
|
71
|
+
throw new Error('Value must be a string if provided');
|
|
72
|
+
}
|
|
73
|
+
// Now we know value is a string
|
|
74
|
+
console.log(value.trim());
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Can be combined into a single check when appropriate
|
|
79
|
+
*/
|
|
80
|
+
function validateOptionalString_GOOD_COMPACT(value) {
|
|
81
|
+
if (value !== undefined && typeof value !== 'string') {
|
|
82
|
+
throw new Error('Value must be a string if provided');
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// ============================================================================
|
|
86
|
+
// PATTERN 4: Non-empty string validation
|
|
87
|
+
// ============================================================================
|
|
88
|
+
/**
|
|
89
|
+
* Use when you need a string that is not only defined but also not empty.
|
|
90
|
+
* This is common for required fields that have been parsed from user input.
|
|
91
|
+
*/
|
|
92
|
+
function validateNonEmptyString_GOOD(value) {
|
|
93
|
+
if (typeof value === 'string' && value.trim() !== '') {
|
|
94
|
+
// Value is a non-empty string
|
|
95
|
+
console.log(value);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
throw new Error('Value must be a non-empty string');
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* AVOID: Loose check that doesn't validate the type
|
|
103
|
+
*/
|
|
104
|
+
function validateNonEmptyString_BAD(value) {
|
|
105
|
+
if (value && value.trim()) {
|
|
106
|
+
// PROBLEM: Assumes value has .trim() method
|
|
107
|
+
console.log(value);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
// ============================================================================
|
|
111
|
+
// PATTERN 5: Array validation
|
|
112
|
+
// ============================================================================
|
|
113
|
+
/**
|
|
114
|
+
* Use explicit checks for arrays, checking both existence and array type.
|
|
115
|
+
*/
|
|
116
|
+
function validateOptionalArray_GOOD(value) {
|
|
117
|
+
if (value !== undefined && value !== null) {
|
|
118
|
+
if (!Array.isArray(value)) {
|
|
119
|
+
throw new Error('Value must be an array if provided');
|
|
120
|
+
}
|
|
121
|
+
// Now we know value is an array
|
|
122
|
+
console.log(value.length);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Check for non-empty arrays using optional chaining and length check
|
|
127
|
+
*/
|
|
128
|
+
function checkNonEmptyArray_GOOD(value) {
|
|
129
|
+
if (value?.length) {
|
|
130
|
+
// Array exists and has at least one element
|
|
131
|
+
console.log(value[0]);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* AVOID: Loose truthy check on arrays - empty arrays are truthy!
|
|
136
|
+
*/
|
|
137
|
+
function checkNonEmptyArray_BAD(value) {
|
|
138
|
+
if (value && value.length > 0) {
|
|
139
|
+
// Works but is verbose compared to optional chaining
|
|
140
|
+
console.log(value[0]);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
// ============================================================================
|
|
144
|
+
// PATTERN 6: Object validation
|
|
145
|
+
// ============================================================================
|
|
146
|
+
/**
|
|
147
|
+
* Use explicit null check for objects since typeof null === 'object'
|
|
148
|
+
*/
|
|
149
|
+
function validateOptionalObject_GOOD(value) {
|
|
150
|
+
if (value !== undefined && value !== null) {
|
|
151
|
+
if (typeof value !== 'object') {
|
|
152
|
+
throw new Error('Value must be an object if provided');
|
|
153
|
+
}
|
|
154
|
+
// Now we know value is an object (not null)
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* CRITICAL: Always check for null when using typeof to validate objects
|
|
159
|
+
*/
|
|
160
|
+
function validateOptionalObject_BAD(value) {
|
|
161
|
+
if (value !== undefined) {
|
|
162
|
+
if (typeof value !== 'object') {
|
|
163
|
+
// PROBLEM: typeof null === 'object', so null passes this check!
|
|
164
|
+
throw new Error('Value must be an object if provided');
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// ============================================================================
|
|
169
|
+
// PATTERN 7: Boolean validation
|
|
170
|
+
// ============================================================================
|
|
171
|
+
/**
|
|
172
|
+
* Use explicit type check for booleans, not truthiness.
|
|
173
|
+
* This distinguishes between missing (undefined) and false.
|
|
174
|
+
*/
|
|
175
|
+
function validateOptionalBoolean_GOOD(value) {
|
|
176
|
+
if (value !== undefined && value !== null && typeof value !== 'boolean') {
|
|
177
|
+
throw new Error('Value must be a boolean if provided');
|
|
178
|
+
}
|
|
179
|
+
// Can safely use value as boolean | undefined | null
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* When you need to treat undefined as a specific boolean value
|
|
183
|
+
*/
|
|
184
|
+
function withDefaultBoolean_GOOD(value) {
|
|
185
|
+
// Explicit: undefined becomes true, false stays false
|
|
186
|
+
return value !== false;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* AVOID: Coercing to boolean implicitly
|
|
190
|
+
*/
|
|
191
|
+
function withDefaultBoolean_BAD(value) {
|
|
192
|
+
// PROBLEM: value = 0 or '' would also become false
|
|
193
|
+
return !!value;
|
|
194
|
+
}
|
|
195
|
+
// ============================================================================
|
|
196
|
+
// PATTERN 8: Default values with nullish coalescing
|
|
197
|
+
// ============================================================================
|
|
198
|
+
/**
|
|
199
|
+
* Use nullish coalescing (??) for default values.
|
|
200
|
+
* This only replaces null/undefined, not other falsy values.
|
|
201
|
+
*/
|
|
202
|
+
function withDefault_GOOD(value) {
|
|
203
|
+
return value ?? 'default';
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* AVOID: Logical OR for defaults - it replaces ALL falsy values
|
|
207
|
+
*/
|
|
208
|
+
function withDefault_BAD(value) {
|
|
209
|
+
// PROBLEM: value = '' or 0 would also use the default
|
|
210
|
+
return value || 'default';
|
|
211
|
+
}
|
|
212
|
+
// ============================================================================
|
|
213
|
+
// PATTERN 9: Early validation for required parameters
|
|
214
|
+
// ============================================================================
|
|
215
|
+
/**
|
|
216
|
+
* Validate required parameters at function entry with explicit checks.
|
|
217
|
+
*/
|
|
218
|
+
function requireParameter_GOOD(value) {
|
|
219
|
+
if (value === undefined || value === null) {
|
|
220
|
+
throw new Error('Parameter is required');
|
|
221
|
+
}
|
|
222
|
+
// Now TypeScript knows value is string
|
|
223
|
+
console.log(value.toUpperCase());
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* AVOID: Loose validation that doesn't distinguish null/undefined from falsy
|
|
227
|
+
*/
|
|
228
|
+
function requireParameter_BAD(value) {
|
|
229
|
+
if (!value) {
|
|
230
|
+
// PROBLEM: Also throws for empty string, 0, false
|
|
231
|
+
throw new Error('Parameter is required');
|
|
232
|
+
}
|
|
233
|
+
console.log(value.toUpperCase());
|
|
234
|
+
}
|
|
235
|
+
// ============================================================================
|
|
236
|
+
// PATTERN 10: Converting truthy checks to explicit checks
|
|
237
|
+
// ============================================================================
|
|
238
|
+
/**
|
|
239
|
+
* When checking if a value should be used (but preserving falsy values)
|
|
240
|
+
*/
|
|
241
|
+
function explicitCheck_GOOD(value) {
|
|
242
|
+
if (value !== undefined && value !== null) {
|
|
243
|
+
// Now can use value = 0, '', false safely
|
|
244
|
+
console.log(value);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* AVOID: Truthy check when falsy values are valid
|
|
249
|
+
*/
|
|
250
|
+
function explicitCheck_BAD(value) {
|
|
251
|
+
if (value) {
|
|
252
|
+
// PROBLEM: Rejects value = 0, '', false
|
|
253
|
+
console.log(value);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
// ============================================================================
|
|
257
|
+
// SUMMARY
|
|
258
|
+
// ============================================================================
|
|
259
|
+
/**
|
|
260
|
+
* QUICK REFERENCE:
|
|
261
|
+
*
|
|
262
|
+
* 1. Optional parameter check:
|
|
263
|
+
* if (value !== undefined && value !== null) { ... }
|
|
264
|
+
*
|
|
265
|
+
* 2. Safe property access:
|
|
266
|
+
* const prop = obj?.property
|
|
267
|
+
*
|
|
268
|
+
* 3. Type validation with null check:
|
|
269
|
+
* if (typeof value !== 'object' || value === null) { throw ... }
|
|
270
|
+
*
|
|
271
|
+
* 4. Non-empty string:
|
|
272
|
+
* if (typeof value === 'string' && value.trim() !== '') { ... }
|
|
273
|
+
*
|
|
274
|
+
* 5. Non-empty array:
|
|
275
|
+
* if (value?.length) { ... }
|
|
276
|
+
*
|
|
277
|
+
* 6. Default values:
|
|
278
|
+
* const result = value ?? defaultValue
|
|
279
|
+
*
|
|
280
|
+
* 7. Boolean with default:
|
|
281
|
+
* const enabled = value !== false // undefined and null become true
|
|
282
|
+
*
|
|
283
|
+
* 8. Required parameter:
|
|
284
|
+
* if (value === undefined || value === null) { throw ... }
|
|
285
|
+
*
|
|
286
|
+
* AVOID:
|
|
287
|
+
* - if (value) { ... } // Too loose, catches falsy values
|
|
288
|
+
* - value || defaultValue // Replaces ALL falsy values, not just null/undefined
|
|
289
|
+
* - !value // Too loose for validation
|
|
290
|
+
*/
|
package/lib/processor.d.ts
CHANGED
|
@@ -15,14 +15,4 @@ export declare function processMarkdownFile(filePath: string, baseDir: string, s
|
|
|
15
15
|
ignorePaths?: string[];
|
|
16
16
|
addPaths?: string[];
|
|
17
17
|
}, excludeImports?: boolean, removeDuplicateHeadings?: boolean, resolvedUrl?: string): Promise<DocInfo | null>;
|
|
18
|
-
/**
|
|
19
|
-
* Process files based on include patterns, ignore patterns, and ordering
|
|
20
|
-
* @param context - Plugin context
|
|
21
|
-
* @param allFiles - All available files
|
|
22
|
-
* @param includePatterns - Patterns for files to include
|
|
23
|
-
* @param ignorePatterns - Patterns for files to ignore
|
|
24
|
-
* @param orderPatterns - Patterns for ordering files
|
|
25
|
-
* @param includeUnmatched - Whether to include unmatched files
|
|
26
|
-
* @returns Processed files
|
|
27
|
-
*/
|
|
28
18
|
export declare function processFilesWithPatterns(context: PluginContext, allFiles: string[], includePatterns?: string[], ignorePatterns?: string[], orderPatterns?: string[], includeUnmatched?: boolean): Promise<DocInfo[]>;
|