Skip to content

Commit

Permalink
Add providers.json output plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Aug 31, 2024
1 parent 1204f26 commit eb6d49c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ const config: Config = {
},
],
'docusaurus-plugin-sass',
'./src/plugins/create-versions-json.ts'
'./src/plugins/create-versions-json.ts',
'./src/plugins/create-providers-json.ts'
],
themes: ["docusaurus-theme-openapi-docs"],

Expand Down
39 changes: 39 additions & 0 deletions src/plugins/create-providers-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fs from 'fs';
import path from 'path';
import { LoadContext, Plugin, PluginOptions } from '@docusaurus/types';
import logger from '@docusaurus/logger';
import Module from 'module';

const createProvidersJsonPlugin = (context: LoadContext, options: PluginOptions): Plugin => {
return {
name: 'docusaurus-plugin-create-providers-json',

async postBuild({ outDir, routes, plugins }) {
const originalResolveFilename = Module._resolveFilename

// This is a hack to allow us to import a custom module instead of the normal translation module
Module._resolveFilename = function (request, parent, isMain, options) {
if (request == '@docusaurus/Translate') {
request = path.resolve(__dirname, 'custom_modules', 'translate.ts');
}
return originalResolveFilename.call(this, request, parent, isMain, options);
};

// Import the providers data
const { providersData } = await import('../data/providers');

// Restore the original resolve function
Module._resolveFilename = originalResolveFilename

const providersFilePath = path.join(outDir, 'data/providers.json');

fs.mkdirSync(path.dirname(providersFilePath), { recursive: true });

fs.writeFileSync(providersFilePath, JSON.stringify(providersData, null, 2), 'utf-8');

logger.success(`Created providers.json file at ${providersFilePath}`);
}
};
};

export default createProvidersJsonPlugin;
4 changes: 4 additions & 0 deletions src/plugins/custom_modules/translate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This is a fake module that simulates @docusaurus/Translate
export function translate (data) {
return data.message
}

0 comments on commit eb6d49c

Please sign in to comment.