Skip to content

Commit bfe7bd8

Browse files
authored
Externalize using create() and drop size threshold requirement (#1280)
* refactor(plugin): externalize JSON props at source in create() Implements slorber's suggestion from PR #1279 to handle externalization at the source (in create()) rather than post-processing MDX strings. Changes: - Add externalization context system to utils.ts - Modify create() to detect large JSON props and externalize inline - Remove externalizeJsonProps.ts (no longer needed) - Update index.ts to use context-based approach This is more architecturally clean as the externalization decision happens where the component is created, not via regex parsing afterward. * refactor(plugin): drop size threshold for externalization Always externalize eligible props (responses, parameters, body, schema) regardless of size. This simplifies the logic and provides consistent behavior - users either have externalization on or off, no edge cases. * refactor(plugin): use one JSON file per component with spread props Implements slorber's suggestion to externalize all props for a component into a single JSON file and use spread syntax: <StatusCodes {...require('./add-pet.StatusCodes.json')} /> Instead of separate files per prop: <StatusCodes responses={require('./add-pet.responses.json')} /> Benefits: - Cleaner file organization (one file per component vs per prop) - Simpler logic (no need to track which props to externalize) - All props for a component are colocated in one file * refactor(plugin): use runWithExternalization wrapper for cleaner API Provides a cleaner API similar to AsyncLocalStorage pattern but uses module-level state internally (AsyncLocalStorage isn't available in browser bundles that Docusaurus creates). The runWithExternalization() function wraps generation with try/finally to ensure context is always cleaned up, providing the same safety guarantees as AsyncLocalStorage.run().
1 parent 77f891e commit bfe7bd8

File tree

3 files changed

+188
-251
lines changed

3 files changed

+188
-251
lines changed

packages/docusaurus-plugin-openapi-docs/src/index.ts

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ import {
2121
createSchemaPageMD,
2222
createTagPageMD,
2323
} from "./markdown";
24-
import {
25-
externalizeJsonPropsSimple,
26-
ExternalizedJsonFile,
27-
} from "./markdown/externalizeJsonProps";
24+
import { ExternalFile, runWithExternalization } from "./markdown/utils";
2825
import { processOpenapiFiles, readOpenapiFiles } from "./openapi";
2926
import { OptionsSchema } from "./options";
3027
import generateSidebarSlice from "./sidebars";
@@ -337,8 +334,21 @@ custom_edit_url: null
337334
if (downloadUrl) {
338335
item.downloadUrl = downloadUrl;
339336
}
340-
const markdown = pageGeneratorByType[item.type](item as any);
341-
item.markdown = markdown;
337+
338+
// Generate markdown, with externalization for API and schema pages
339+
let externalFiles: ExternalFile[] = [];
340+
if (
341+
options.externalJsonProps &&
342+
(item.type === "api" || item.type === "schema")
343+
) {
344+
const result = runWithExternalization(item.id, () =>
345+
pageGeneratorByType[item.type](item as any)
346+
);
347+
item.markdown = result.result;
348+
externalFiles = result.files;
349+
} else {
350+
item.markdown = pageGeneratorByType[item.type](item as any);
351+
}
342352
if (isSchemasOnly && item.type !== "schema") {
343353
return;
344354
}
@@ -388,32 +398,18 @@ custom_edit_url: null
388398
);
389399
}
390400

391-
let finalView = view;
392-
let jsonFilesToWrite: ExternalizedJsonFile[] = [];
393-
394-
// Externalize large JSON props if enabled
395-
if (options.externalJsonProps) {
396-
const result = externalizeJsonPropsSimple(view, item.id);
397-
finalView = result.mdx;
398-
jsonFilesToWrite = result.jsonFiles;
399-
400-
// Write JSON files
401-
for (const jsonFile of jsonFilesToWrite) {
402-
const jsonPath = `${outputDir}/${jsonFile.filename}`;
403-
if (!fs.existsSync(jsonPath)) {
404-
fs.writeFileSync(jsonPath, jsonFile.content, "utf8");
405-
console.log(
406-
chalk.green(`Successfully created "${jsonPath}"`)
407-
);
408-
}
401+
// Write externalized JSON files
402+
for (const jsonFile of externalFiles) {
403+
const jsonPath = `${outputDir}/${jsonFile.filename}`;
404+
if (!fs.existsSync(jsonPath)) {
405+
fs.writeFileSync(jsonPath, jsonFile.content, "utf8");
406+
console.log(
407+
chalk.green(`Successfully created "${jsonPath}"`)
408+
);
409409
}
410410
}
411411

412-
fs.writeFileSync(
413-
`${outputDir}/${item.id}.api.mdx`,
414-
finalView,
415-
"utf8"
416-
);
412+
fs.writeFileSync(`${outputDir}/${item.id}.api.mdx`, view, "utf8");
417413
console.log(
418414
chalk.green(
419415
`Successfully created "${outputDir}/${item.id}.api.mdx"`
@@ -499,24 +495,16 @@ custom_edit_url: null
499495
throw Error("Schema must have title defined");
500496
}
501497
// eslint-disable-next-line testing-library/render-result-naming-convention
502-
let schemaView = render(schemaMdTemplate, item);
503-
let jsonFilesToWrite: ExternalizedJsonFile[] = [];
504-
505-
// Externalize large JSON props if enabled
506-
if (options.externalJsonProps) {
507-
const result = externalizeJsonPropsSimple(schemaView, item.id);
508-
schemaView = result.mdx;
509-
jsonFilesToWrite = result.jsonFiles;
510-
511-
// Write JSON files in schemas directory
512-
for (const jsonFile of jsonFilesToWrite) {
513-
const jsonPath = `${outputDir}/schemas/${jsonFile.filename}`;
514-
if (!fs.existsSync(jsonPath)) {
515-
fs.writeFileSync(jsonPath, jsonFile.content, "utf8");
516-
console.log(
517-
chalk.green(`Successfully created "${jsonPath}"`)
518-
);
519-
}
498+
const schemaView = render(schemaMdTemplate, item);
499+
500+
// Write externalized JSON files in schemas directory
501+
for (const jsonFile of externalFiles) {
502+
const jsonPath = `${outputDir}/schemas/${jsonFile.filename}`;
503+
if (!fs.existsSync(jsonPath)) {
504+
fs.writeFileSync(jsonPath, jsonFile.content, "utf8");
505+
console.log(
506+
chalk.green(`Successfully created "${jsonPath}"`)
507+
);
520508
}
521509
}
522510

packages/docusaurus-plugin-openapi-docs/src/markdown/externalizeJsonProps.ts

Lines changed: 0 additions & 201 deletions
This file was deleted.

0 commit comments

Comments
 (0)