11import { UserConfig , PageIndexInfo , DocPlugin } from 'shared/types' ;
2- import { pluginLastUpdated } from './plugins/lastUpdated' ;
32import { AdditionalPage } from '@/shared/types/Plugin' ;
43
54type HookOptions = {
@@ -8,20 +7,35 @@ type HookOptions = {
87 pageData ?: PageIndexInfo ;
98} ;
109
11- function getPlugins ( config : UserConfig ) {
12- const plugins : DocPlugin [ ] = config . doc ?. plugins || [ ] ;
10+ let docPlugins : DocPlugin [ ] = [ ] ;
11+
12+ // The init function is used to initialize the doc plugins and will execute before the build process.
13+ export async function loadPlugins ( config : UserConfig ) {
14+ // Clear docPlugins first, for the watch mode
15+ docPlugins = [ ] ;
1316 const enableLastUpdated =
1417 config . doc . themeConfig ?. lastUpdated ||
1518 config . doc . themeConfig ?. locales ?. some ( locale => locale . lastUpdated ) ;
19+ const mediumZoomConfig = config . doc . mediumZoom ?? true ;
1620 if ( enableLastUpdated ) {
17- plugins . push ( pluginLastUpdated ( ) ) ;
21+ const { pluginLastUpdated } = await import ( './plugins/lastUpdated' ) ;
22+ docPlugins . push ( pluginLastUpdated ( ) ) ;
23+ }
24+ if ( mediumZoomConfig ) {
25+ const { pluginMediumZoom } = await import (
26+ '@modern-js/doc-plugin-medium-zoom'
27+ ) ;
28+ docPlugins . push (
29+ pluginMediumZoom (
30+ typeof mediumZoomConfig === 'object' ? mediumZoomConfig : undefined ,
31+ ) ,
32+ ) ;
1833 }
19- return plugins ;
34+ docPlugins . push ( ... ( config . doc . plugins || [ ] ) ) ;
2035}
2136
2237export async function modifyConfig ( hookOptions : HookOptions ) {
2338 const { config } = hookOptions ;
24- const docPlugins = getPlugins ( config ) ;
2539
2640 // config hooks
2741 for ( const plugin of docPlugins ) {
@@ -35,7 +49,6 @@ export async function modifyConfig(hookOptions: HookOptions) {
3549
3650export async function beforeBuild ( hookOptions : HookOptions ) {
3751 const { config, isProd = true } = hookOptions ;
38- const docPlugins = getPlugins ( config ) ;
3952
4053 // beforeBuild hooks
4154 return await Promise . all (
@@ -49,7 +62,6 @@ export async function beforeBuild(hookOptions: HookOptions) {
4962
5063export async function afterBuild ( hookOptions : HookOptions ) {
5164 const { config, isProd = true } = hookOptions ;
52- const docPlugins = getPlugins ( config ) ;
5365
5466 // afterBuild hooks
5567 return await Promise . all (
@@ -63,7 +75,6 @@ export async function afterBuild(hookOptions: HookOptions) {
6375
6476export async function extendPageData ( hookOptions : HookOptions ) : Promise < void > {
6577 const { pageData } = hookOptions ;
66- const docPlugins = getPlugins ( hookOptions . config ) ;
6778 // extendPageData hooks
6879 await Promise . all (
6980 docPlugins
@@ -78,7 +89,6 @@ export async function addPages(
7889 hookOptions : HookOptions ,
7990) : Promise < AdditionalPage [ ] > {
8091 const { config } = hookOptions ;
81- const docPlugins = getPlugins ( config ) ;
8292
8393 // addPages hooks
8494 const result = await Promise . all (
@@ -91,3 +101,23 @@ export async function addPages(
91101
92102 return result . flat ( ) ;
93103}
104+
105+ export async function globalUIComponents ( ) : Promise < string [ ] > {
106+ // globalUIComponents hooks
107+ const result = docPlugins . map ( plugin => {
108+ return plugin . globalUIComponents || [ ] ;
109+ } ) ;
110+
111+ return result . flat ( ) ;
112+ }
113+
114+ export async function globalStyles ( ) : Promise < string [ ] > {
115+ // globalStyles hooks
116+ const result = docPlugins
117+ . filter ( plugin => typeof plugin . globalStyles === 'string' )
118+ . map ( plugin => {
119+ return plugin . globalStyles ;
120+ } ) ;
121+
122+ return result ;
123+ }
0 commit comments