@xyd-js/content 0.0.0-build

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 (97) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/ISSUES.md +1 -0
  3. package/LICENSE +21 -0
  4. package/README.md +3 -0
  5. package/TODO.md +2 -0
  6. package/dist/index.d.ts +28 -0
  7. package/dist/index.js +1625 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/md.d.ts +72 -0
  10. package/dist/md.js +23508 -0
  11. package/dist/md.js.map +1 -0
  12. package/dist/mdToc-NBBxMJ4l.d.ts +12 -0
  13. package/dist/vite.d.ts +1066 -0
  14. package/dist/vite.js +20156 -0
  15. package/dist/vite.js.map +1 -0
  16. package/package.json +67 -0
  17. package/packages/md/index.ts +25 -0
  18. package/packages/md/plugins/component-directives/index.ts +3 -0
  19. package/packages/md/plugins/component-directives/mdComponentDirective.ts +577 -0
  20. package/packages/md/plugins/component-directives/types.ts +1 -0
  21. package/packages/md/plugins/component-directives/utils.ts +27 -0
  22. package/packages/md/plugins/composer/__fixtures__/1.single-example/input.md +7 -0
  23. package/packages/md/plugins/composer/__fixtures__/1.single-example/output.json +63 -0
  24. package/packages/md/plugins/composer/__fixtures__/2.single-example-with-name/input.md +7 -0
  25. package/packages/md/plugins/composer/__fixtures__/2.single-example-with-name/output.json +63 -0
  26. package/packages/md/plugins/composer/__fixtures__/3.multiple-examples/input.md +15 -0
  27. package/packages/md/plugins/composer/__fixtures__/3.multiple-examples/output.json +122 -0
  28. package/packages/md/plugins/composer/__fixtures__/4.example-groups/input.md +23 -0
  29. package/packages/md/plugins/composer/__fixtures__/4.example-groups/output.json +184 -0
  30. package/packages/md/plugins/composer/__tests__/mdComposer.test.ts +41 -0
  31. package/packages/md/plugins/composer/__tests__/testHelpers.ts +48 -0
  32. package/packages/md/plugins/composer/index.ts +1 -0
  33. package/packages/md/plugins/composer/mdComposer.ts +146 -0
  34. package/packages/md/plugins/developer-writing/index.ts +3 -0
  35. package/packages/md/plugins/developer-writing/mdCodeRehype.ts +81 -0
  36. package/packages/md/plugins/functions/__fixtures__/external.ts +4 -0
  37. package/packages/md/plugins/functions/__fixtures__/test-include.md +31 -0
  38. package/packages/md/plugins/functions/__fixtures__/test.js +11 -0
  39. package/packages/md/plugins/functions/__fixtures__/test.py +9 -0
  40. package/packages/md/plugins/functions/__fixtures__/test.ts +18 -0
  41. package/packages/md/plugins/functions/__tests__/mdFunctionImportCode.test.ts +314 -0
  42. package/packages/md/plugins/functions/__tests__/mdFunctionInclude.test.ts +44 -0
  43. package/packages/md/plugins/functions/__tests__/parseFunctionCall.test.ts +70 -0
  44. package/packages/md/plugins/functions/__tests__/testHelpers.ts +95 -0
  45. package/packages/md/plugins/functions/index.ts +15 -0
  46. package/packages/md/plugins/functions/mdFunctionChangelog.ts +135 -0
  47. package/packages/md/plugins/functions/mdFunctionImportCode.ts +92 -0
  48. package/packages/md/plugins/functions/mdFunctionInclude.ts +119 -0
  49. package/packages/md/plugins/functions/mdFunctionUniform.ts +79 -0
  50. package/packages/md/plugins/functions/types.ts +9 -0
  51. package/packages/md/plugins/functions/uniformProcessor.ts +349 -0
  52. package/packages/md/plugins/functions/utils.ts +457 -0
  53. package/packages/md/plugins/index.ts +125 -0
  54. package/packages/md/plugins/mdCode.ts +16 -0
  55. package/packages/md/plugins/mdHeadingId.ts +47 -0
  56. package/packages/md/plugins/mdImage.test.ts +59 -0
  57. package/packages/md/plugins/mdImage.ts +55 -0
  58. package/packages/md/plugins/mdImageRehype.ts +13 -0
  59. package/packages/md/plugins/mdPage.ts +35 -0
  60. package/packages/md/plugins/mdThemeSettings.ts +34 -0
  61. package/packages/md/plugins/mdToc.ts +229 -0
  62. package/packages/md/plugins/meta/index.ts +1 -0
  63. package/packages/md/plugins/meta/mdMeta.ts +198 -0
  64. package/packages/md/plugins/output-variables/__fixtures__/1.simple/input.md +22 -0
  65. package/packages/md/plugins/output-variables/__fixtures__/1.simple/output.json +191 -0
  66. package/packages/md/plugins/output-variables/__fixtures__/2.multiple-vars/input.md +21 -0
  67. package/packages/md/plugins/output-variables/__fixtures__/2.multiple-vars/output.json +127 -0
  68. package/packages/md/plugins/output-variables/__tests__/index.test.ts +28 -0
  69. package/packages/md/plugins/output-variables/__tests__/testHelpers.ts +36 -0
  70. package/packages/md/plugins/output-variables/index.ts +1 -0
  71. package/packages/md/plugins/output-variables/lib/const.ts +4 -0
  72. package/packages/md/plugins/output-variables/lib/factoryAttributes.ts +350 -0
  73. package/packages/md/plugins/output-variables/lib/factoryLabel.ts +135 -0
  74. package/packages/md/plugins/output-variables/lib/factoryName.ts +59 -0
  75. package/packages/md/plugins/output-variables/lib/index.ts +21 -0
  76. package/packages/md/plugins/output-variables/lib/outputVarsContainer.ts +328 -0
  77. package/packages/md/plugins/output-variables/lib/util.ts +494 -0
  78. package/packages/md/plugins/output-variables/remarkOutputVars.ts +22 -0
  79. package/packages/md/plugins/recmaOverrideComponents.ts +74 -0
  80. package/packages/md/plugins/rehypeHeading.ts +58 -0
  81. package/packages/md/plugins/types.ts +15 -0
  82. package/packages/md/plugins/utils/componentLike.ts +76 -0
  83. package/packages/md/plugins/utils/index.ts +2 -0
  84. package/packages/md/plugins/utils/injectCodeMeta.ts +59 -0
  85. package/packages/md/plugins/utils/mdParameters.test.ts +114 -0
  86. package/packages/md/plugins/utils/mdParameters.ts +249 -0
  87. package/packages/md/plugins/utils/mdastTypes.ts +42 -0
  88. package/packages/md/search/index.ts +257 -0
  89. package/packages/md/search/types.ts +36 -0
  90. package/packages/vite/index.ts +20 -0
  91. package/src/fs.ts +81 -0
  92. package/src/index.ts +7 -0
  93. package/src/navigation.ts +147 -0
  94. package/src/types.ts +8 -0
  95. package/tsconfig.json +49 -0
  96. package/tsup.config.ts +32 -0
  97. package/vitest.config.ts +17 -0
@@ -0,0 +1,349 @@
1
+ import * as path from 'node:path';
2
+ import * as fs from 'node:fs';
3
+ import * as os from 'node:os';
4
+ import { VFile } from 'vfile';
5
+
6
+ import { Metadata, Settings } from '@xyd-js/core';
7
+ import { sourcesToUniform, sourcesToUniformV2, type TypeDocReferenceContext } from '@xyd-js/sources/ts';
8
+ import { reactDocgenToUniform, uniformToReactUniform } from '@xyd-js/sources/react';
9
+ import { gqlSchemaToReferences } from "@xyd-js/gql"
10
+ import {oapSchemaToReferences, deferencedOpenAPI, uniformPluginXDocsSidebar} from "@xyd-js/openapi"
11
+
12
+ import { downloadContent, LineRange, parseImportPath, Region, resolvePathAlias } from './utils';
13
+ import uniform, { Reference, ReferenceContext } from '@xyd-js/uniform';
14
+ // TODO: rewrite to async
15
+
16
+ /**
17
+ * Process a uniform function call and return the references
18
+ *
19
+ * @param settings The settings object
20
+ * @param value The value containing the uniform function call
21
+ * @param file The VFile object
22
+ * @param resolveFrom Optional base directory to resolve relative paths from
23
+ * @returns A promise that resolves to the references or null if processing failed
24
+ */
25
+ export async function processUniformFunctionCall(
26
+ value: string,
27
+ file: VFile,
28
+ resolveFrom?: string,
29
+ settings?: Settings,
30
+ ): Promise<Reference[] | null> {
31
+ // Parse the import path to extract file path
32
+ const { filePath, regions, lineRanges } = parseImportPath(value);
33
+
34
+ // Resolve path aliases and get the base directory
35
+ let resolvedFilePath = resolvePathAlias(filePath, settings, file);
36
+
37
+ if (resolvedFilePath.startsWith("~/")) {
38
+ resolvedFilePath = path.join(process.cwd(), resolvedFilePath.slice(2));
39
+ }
40
+
41
+ // Process the uniform file
42
+ const references = await processUniformFile(resolvedFilePath, regions, lineRanges, file, resolveFrom);
43
+
44
+ if (!references) {
45
+ return null
46
+ }
47
+
48
+ const plugins = globalThis.__xydUserUniformVitePlugins || []
49
+ const matter = file.data?.matter as Metadata
50
+ if (matter?.openapi) {
51
+ plugins.push(uniformPluginXDocsSidebar)
52
+ }
53
+
54
+ const uniformRefs = uniform(references, {
55
+ plugins: [
56
+ ...plugins,
57
+ ]
58
+ })
59
+
60
+ return uniformRefs.references
61
+ }
62
+
63
+ async function processUniformFile(
64
+ filePath: string,
65
+ regions: Region[],
66
+ lineRanges: LineRange[],
67
+ file: VFile,
68
+ resolveFrom?: string
69
+ ): Promise<any[] | null> {
70
+ try {
71
+ if (!isSupportedProgrammingSource(filePath)) {
72
+ // TODO: openapi + graphql
73
+ throw new Error(`Unsupported file type: ${filePath}`);
74
+ }
75
+
76
+ let ext = extension(filePath);
77
+
78
+ const matter = file.data?.matter as Metadata
79
+ if (matter?.openapi) {
80
+ ext = "openapi"
81
+ }
82
+
83
+ if (isLocalPath(filePath)) {
84
+ const baseDir = resolveFrom || (file.dirname || process.cwd());
85
+ const resolvedFilePath = path.resolve(baseDir, filePath);
86
+
87
+ switch (ext) {
88
+ case 'ts':
89
+ case 'tsx': {
90
+ const packageDir = findClosestPackageJsonDir(
91
+ baseDir,
92
+ filePath,
93
+ );
94
+
95
+ if (packageDir) {
96
+ // Extract the relative file path from the package directory
97
+ const relativeFilePath = path.relative(packageDir, resolvedFilePath);
98
+
99
+ try {
100
+ let references: Reference[] = []
101
+
102
+ switch (ext) {
103
+ case 'ts': {
104
+ const typedocRefs = await sourcesToUniformV2(
105
+ packageDir,
106
+ [relativeFilePath]
107
+ )
108
+
109
+ if (!typedocRefs || !typedocRefs.references) {
110
+ console.error("Failed to process uniform file", filePath)
111
+ break
112
+ }
113
+
114
+ references = typedocRefs.references.filter(ref => {
115
+ const ctx = ref?.context as TypeDocReferenceContext
116
+
117
+ const pathMatch = ctx?.fileFullPath === relativeFilePath
118
+
119
+
120
+ if (regions.length > 0) {
121
+ const regionMatch = regions.some(region => {
122
+ return region.name === ctx?.symbolName // TODO: BETTER REGION API FOR TYPEDOC
123
+ })
124
+
125
+ return pathMatch && regionMatch
126
+ }
127
+
128
+ return pathMatch
129
+ })
130
+
131
+ break
132
+ }
133
+
134
+ case 'tsx': {
135
+ const resp = await sourcesToUniformV2(
136
+ packageDir,
137
+ [relativeFilePath]
138
+ )
139
+
140
+ if (!resp || !resp.references || !resp.projectJson) {
141
+ console.error("Failed to process uniform file", filePath)
142
+ return null
143
+ }
144
+ const typedocRefs = resp.references as Reference<TypeDocReferenceContext>[]
145
+
146
+ references = uniformToReactUniform(typedocRefs, resp.projectJson)
147
+
148
+ break
149
+ }
150
+ }
151
+
152
+ return references
153
+ } finally {
154
+ // Clean up the temporary directory when done
155
+ // cleanupTempFolder(tempDir);
156
+ }
157
+ } else {
158
+ console.error("package.json not found", filePath)
159
+ }
160
+ }
161
+
162
+ case 'graphql': {
163
+ const references = await gqlSchemaToReferences(resolvedFilePath, {
164
+ regions: regions.map(region => region.name),
165
+ });
166
+
167
+ return references;
168
+ }
169
+
170
+ case 'openapi': {
171
+ const schema = await deferencedOpenAPI(resolvedFilePath);
172
+ const references = oapSchemaToReferences(schema, {
173
+ regions: regions.map(region => region.name)
174
+ });
175
+
176
+ return references;
177
+ }
178
+
179
+ default: {
180
+ throw new Error(`Unsupported file extension: ${ext}`);
181
+ }
182
+ }
183
+
184
+ }
185
+
186
+ throw new Error("current implementation does not support remote files")
187
+
188
+ // For remote files, download the content
189
+ const content = await downloadContent(
190
+ filePath,
191
+ file,
192
+ resolveFrom,
193
+ );
194
+
195
+ // Fallback to creating temporary folder structure if no package.json found
196
+ const tempDir = await createTempFolderStructure(content);
197
+
198
+ try {
199
+ // Get the path to the package directory
200
+ const tempPackageDir = path.join(tempDir, 'packages', 'package');
201
+
202
+ // Process the content using sourcesToUniform
203
+ const references = await sourcesToUniformV2(
204
+ tempDir,
205
+ [tempPackageDir]
206
+ );
207
+
208
+ return references || null;
209
+ } finally {
210
+ // Clean up the temporary directory when done
211
+ cleanupTempFolder(tempDir);
212
+ }
213
+
214
+ } catch (error) {
215
+ console.error(`Error processing uniform file: ${filePath}`, error);
216
+ return null;
217
+ }
218
+ }
219
+
220
+
221
+ /**
222
+ * Creates a temporary folder structure with package.json, tsconfig.json, and src/index.ts
223
+ * @param content The content to be placed in src/index.ts
224
+ * @returns The path to the temporary directory
225
+ */
226
+ async function createTempFolderStructure(content: string): Promise<string> {
227
+ // Create a temporary directory
228
+ const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'xyd-uniform-'));
229
+
230
+ // Create the package directory
231
+ const packageDir = path.join(tempDir, 'packages', 'package');
232
+ fs.mkdirSync(packageDir, { recursive: true });
233
+
234
+ // Create the src directory
235
+ const srcDir = path.join(packageDir, 'src');
236
+ fs.mkdirSync(srcDir, { recursive: true });
237
+
238
+ // Create package.json
239
+ const packageJson = {
240
+ "name": "@xyd-sources-examples/package-a",
241
+ "main": "dist/index.js"
242
+ };
243
+ fs.writeFileSync(
244
+ path.join(packageDir, 'package.json'),
245
+ JSON.stringify(packageJson, null, 2)
246
+ );
247
+
248
+ // Create tsconfig.json
249
+ const tsconfigJson = {
250
+ "compilerOptions": {
251
+ "outDir": "./dist"
252
+ }
253
+ };
254
+ fs.writeFileSync(
255
+ path.join(packageDir, 'tsconfig.json'),
256
+ JSON.stringify(tsconfigJson, null, 2)
257
+ );
258
+
259
+ // Create src/index.ts with the provided content
260
+ fs.writeFileSync(path.join(srcDir, 'index.ts'), content);
261
+
262
+ return tempDir;
263
+ }
264
+
265
+ /**
266
+ * Cleans up the temporary folder structure
267
+ * @param tempDir The path to the temporary directory
268
+ */
269
+ function cleanupTempFolder(tempDir: string): void {
270
+ try {
271
+ // Recursively delete the temporary directory
272
+ fs.rmSync(tempDir, { recursive: true, force: true });
273
+ } catch (error) {
274
+ console.error(`Error cleaning up temporary directory: ${tempDir}`, error);
275
+ }
276
+ }
277
+
278
+ const supportedProgrammingExtensions: Record<string, boolean> = {
279
+ 'ts': true,
280
+ 'tsx': true,
281
+ 'graphql': true,
282
+ 'yaml': true,
283
+ 'yml': true,
284
+ 'json': true,
285
+
286
+ // TODO
287
+ // 'py': true,
288
+ // 'go': true,
289
+ // TODO: AND OTHER PROGRAMMING LANGUAGES IN THE FUTEURE
290
+ }
291
+
292
+ /**
293
+ * Check if a file is a programming source file based on its extension
294
+ * @param filePath The path to the file
295
+ * @returns True if the file is a programming source file
296
+ */
297
+ function isSupportedProgrammingSource(filePath: string) {
298
+ const ext = extension(filePath);
299
+
300
+ if (supportedProgrammingExtensions[ext]) {
301
+ return true;
302
+ }
303
+
304
+ return false;
305
+ }
306
+
307
+ function extension(filePath: string) {
308
+ return path.extname(filePath).toLowerCase().replace('.', '');
309
+ }
310
+
311
+ /**
312
+ * Find the closest package.json directory for a given file path
313
+ * @param baseDir The base directory to start searching from (used as cwd for resolving relative paths)
314
+ * @param filePath The path to the file (can be relative)
315
+ * @returns The path to the directory containing package.json, or null if not found
316
+ */
317
+ function findClosestPackageJsonDir(
318
+ baseDir: string,
319
+ filePath: string
320
+ ): string | null {
321
+ // Resolve the filePath relative to baseDir
322
+ const resolvedFilePath = path.resolve(baseDir, filePath);
323
+ let currentDir = path.dirname(resolvedFilePath);
324
+ const root = path.parse(currentDir).root;
325
+
326
+ while (currentDir !== root) {
327
+ try {
328
+ const files = fs.readdirSync(currentDir);
329
+ if (files.includes('package.json')) {
330
+ return currentDir;
331
+ }
332
+ } catch (error) {
333
+ // If we can't read the directory, move up
334
+ console.warn(`Cannot read directory ${currentDir}:`, error);
335
+ }
336
+ currentDir = path.dirname(currentDir);
337
+ }
338
+
339
+ return null;
340
+ }
341
+
342
+ /**
343
+ * Check if a path is a local file path (not a URL)
344
+ * @param filePath The path to check
345
+ * @returns True if the path is local
346
+ */
347
+ function isLocalPath(filePath: string): boolean {
348
+ return !filePath.startsWith('http://') && !filePath.startsWith('https://');
349
+ }