@@ -23,8 +23,9 @@ import {
2323import chalk from "chalk" ;
2424import { Configuration } from "webpack" ;
2525
26- import { createMD } from "./markdown" ;
27- import { loadOpenapi } from "./openapi" ;
26+ import { createApiPageMD , createInfoPageMD } from "./markdown" ;
27+ import { readOpenapiFiles , processOpenapiFiles } from "./openapi" ;
28+ import { generateSidebars } from "./sidebars" ;
2829import { PluginOptions , LoadedContent } from "./types" ;
2930
3031export default function pluginOpenAPI (
@@ -58,11 +59,12 @@ export default function pluginOpenAPI(
5859 const { routeBasePath } = options ;
5960
6061 try {
61- const loadedApi = await loadOpenapi (
62- contentPath ,
62+ const openapiFiles = await readOpenapiFiles ( contentPath , { } ) ;
63+ const loadedApi = await processOpenapiFiles ( openapiFiles , {
6364 baseUrl,
64- routeBasePath
65- ) ;
65+ routeBasePath,
66+ siteDir : context . siteDir ,
67+ } ) ;
6668 return { loadedApi } ;
6769 } catch ( e ) {
6870 console . error ( chalk . red ( `Loading of api failed for "${ contentPath } "` ) ) ;
@@ -72,86 +74,53 @@ export default function pluginOpenAPI(
7274
7375 async contentLoaded ( { content, actions } ) {
7476 const { loadedApi } = content ;
75- const { routeBasePath, apiLayoutComponent, apiItemComponent } = options ;
77+ const {
78+ routeBasePath,
79+ apiLayoutComponent,
80+ apiItemComponent,
81+ sidebarCollapsed,
82+ sidebarCollapsible,
83+ } = options ;
7684 const { addRoute, createData } = actions ;
7785
7886 const sidebarName = `openapi-sidebar-${ pluginId } ` ;
87+ const sidebar = generateSidebars ( loadedApi , {
88+ sidebarCollapsible,
89+ sidebarCollapsed,
90+ } ) ;
7991
80- const sidebar = loadedApi . map ( ( category ) => {
92+ const promises = loadedApi . map ( async ( item ) => {
93+ const pageId = `site-${ routeBasePath } -${ item . id } ` ;
94+
95+ await createData (
96+ `${ docuHash ( pageId ) } .json` ,
97+ JSON . stringify ( item , null , 2 )
98+ ) ;
99+
100+ // TODO: "-content" should be inside hash to prevent name too long errors.
101+ const markdown = await createData (
102+ `${ docuHash ( pageId ) } -content.mdx` ,
103+ item . type === "api" ? createApiPageMD ( item ) : createInfoPageMD ( item )
104+ ) ;
81105 return {
82- type : "category" ,
83- label : category . title ,
84- collapsible : options . sidebarCollapsible ,
85- collapsed : options . sidebarCollapsed ,
86- items : category . items . map ( ( item ) => {
87- return {
88- href : item . permalink ,
89- label : item . title ,
90- type : "link" ,
91- className : item . deprecated
92- ? "menu__list-item--deprecated"
93- : undefined ,
94- } ;
95- } ) ,
106+ path : item . permalink ,
107+ component : apiItemComponent ,
108+ exact : true ,
109+ modules : {
110+ content : markdown ,
111+ } ,
112+ sidebar : sidebarName ,
96113 } ;
97114 } ) ;
98115
99- const promises = loadedApi . flatMap ( ( section ) => {
100- return section . items . map ( async ( item ) => {
101- const { id, title, description, permalink, previous, next, ...api } =
102- item ;
103-
104- const pageId = `site-${ routeBasePath } -${ id } ` ;
105-
106- await createData (
107- `${ docuHash ( pageId ) } .json` ,
108- JSON . stringify (
109- {
110- unversionedId : id ,
111- id,
112- isDocsHomePage : false , // TODO: Where does this come from?
113- title,
114- description,
115- // source: "@site/docs/tutorial-basics/congratulations.md",
116- // sourceDirName: "tutorial-basics",
117- slug : "/" + id , // TODO: Should this really be prepended with "/"?
118- permalink,
119- frontMatter : { } ,
120- sidebar : sidebarName ,
121- previous,
122- next,
123- api,
124- } ,
125- null ,
126- 2
127- )
128- ) ;
129-
130- // TODO: "-content" should be inside hash to prevent name too long errors.
131- const markdown = await createData (
132- `${ docuHash ( pageId ) } -content.mdx` ,
133- createMD ( item )
134- ) ;
135- return {
136- path : item . permalink ,
137- component : apiItemComponent ,
138- exact : true ,
139- modules : {
140- content : markdown ,
141- } ,
142- sidebar : sidebarName ,
143- } ;
144- } ) ;
145- } ) ;
146-
147116 // Important: the layout component should not end with /,
148117 // as it conflicts with the home doc
149118 // Workaround fix for https://github.com/facebook/docusaurus/issues/2917
150119 const apiBaseRoute = normalizeUrl ( [ baseUrl , routeBasePath ] ) ;
151120 const basePath = apiBaseRoute === "/" ? "" : apiBaseRoute ;
152121
153122 async function rootRoute ( ) {
154- const item = loadedApi [ 0 ] . items [ 0 ] ;
123+ const item = loadedApi [ 0 ] ;
155124 const pageId = `site-${ routeBasePath } -${ item . id } ` ;
156125
157126 return {
0 commit comments