import { LoadContext , Plugin } from '@docusaurus/types' ; import { normalizeUrl } from '@docusaurus/utils' ; import { readFileSync } from 'fs' ; import { join } from 'path' ; export interface PluginOptions { debug ? : boolean ; pathFileName ? : string ; } export const DEFAULT_OPTIONS = { debug : false } ; export default function contributorsPlugin ( context , opts ) { const { baseUrl } = context . siteConfig ; const options = { ... DEFAULT_OPTIONS , ... opts } ; const { pathFileName , debug = true } = options ; if ( debug ) { console . log ( '[CONTRIBUTORS_PLUGIN] Opts Input:' , opts ) ; console . log ( '[CONTRIBUTORS_PLUGIN] Options:' , options ) ; } return { name : 'docusaurus-plugin-contributors' , async loadContent ( ) { let content = null ; const contributorsFilesName = pathFileName || join ( __dirname , 'contributors.json' ) ; try { content = JSON . parse ( readFileSync ( contributorsFilesName , 'utf8' ) ) ; return content ; } catch ( error ) { console . log ( 'error' , error ) ; return { error : true } ; } } , async contentLoaded ( { content , actions } ) { const { createData , addRoute } = actions ; if ( ! content ) { return ; } const contributorsJsonPath = await createData ( 'contributors.json' , JSON . stringify ( content ) ) ; const routeOptions = { path : normalizeUrl ( [ baseUrl , 'contributors' ] ) , component : '@site/src/components/Contributors.tsx' , modules : { contributors : contributorsJsonPath } , exact : true } ; if ( debug ) { console . error ( '[CONTRIBUTORS_PLUGIN] Route:' , routeOptions ) ; } addRoute ( routeOptions ) ; } } ; }