4
4
* @module
5
5
*/
6
6
7
+ import * as path from 'path' ;
7
8
import { Application , DeclarationOption , RendererEvent } from 'typedoc' ;
8
- import { MarkdownPageEvent } from 'typedoc-plugin-markdown' ;
9
- import { addTableOfContents } from './helpers/add-toc.js' ;
9
+ import { fileURLToPath } from 'url' ;
10
10
import { getDefaultPlugins } from './helpers/get-default-plugins.js' ;
11
11
import * as options from './options/declarations.js' ;
12
12
import { parse } from './parse.js' ;
13
13
14
+ const __filename = fileURLToPath ( import . meta. url ) ;
15
+ const __dirname = path . dirname ( __filename ) ;
16
+
14
17
export function load ( app : Application ) {
15
18
Object . entries ( options ) . forEach ( ( [ name , option ] ) => {
16
19
app . options . addDeclaration ( {
@@ -19,28 +22,37 @@ export function load(app: Application) {
19
22
} as DeclarationOption ) ;
20
23
} ) ;
21
24
22
- app . renderer . on ( MarkdownPageEvent . END , ( event : MarkdownPageEvent ) => {
23
- const remarkPlugins = app . options . getValue ( 'remarkPlugins' ) as [ ] ;
24
- const remarkPluginsNames = remarkPlugins . map ( ( plugin ) =>
25
- Array . isArray ( plugin ) ? plugin [ 0 ] : plugin ,
26
- ) as string [ ] ;
27
-
28
- if ( remarkPluginsNames . includes ( 'remark-toc' ) ) {
29
- addTableOfContents ( event , remarkPlugins , remarkPluginsNames , app ) ;
30
- }
31
- } ) ;
32
-
33
25
app . renderer . postRenderAsyncJobs . push ( async ( output : RendererEvent ) => {
26
+ const remarkPlugins = app . options . getValue ( 'remarkPlugins' ) as [ ] ;
34
27
const defaultPlugins = getDefaultPlugins (
35
28
app . options . getValue ( 'defaultRemarkPlugins' ) ,
36
29
) ;
37
30
const userPlugins = app . options . getValue ( 'remarkPlugins' ) as string [ ] ;
38
31
const remarkStringifyOptions = app . options . getValue (
39
32
'remarkStringifyOptions' ,
40
33
) ;
34
+ const remarkPluginsNames = remarkPlugins . map ( ( plugin ) =>
35
+ Array . isArray ( plugin ) ? plugin [ 0 ] : plugin ,
36
+ ) as string [ ] ;
37
+
41
38
if ( output . urls ?. length ) {
42
39
await Promise . all (
43
40
output . urls ?. map ( async ( urlMapping ) => {
41
+ if ( remarkPluginsNames . includes ( 'remark-toc' ) ) {
42
+ const tocPluginIndex = remarkPluginsNames . findIndex (
43
+ ( name ) => name === 'remark-toc' ,
44
+ ) ;
45
+ const tocPlugin = remarkPlugins [ tocPluginIndex ] ;
46
+ const tocOptions = Array . isArray ( tocPlugin ) ? tocPlugin [ 1 ] : { } ;
47
+ defaultPlugins . push ( [
48
+ path . join ( __dirname , 'plugins' , 'add-toc.js' ) ,
49
+ {
50
+ reflection : urlMapping . model ,
51
+ typedocOptions : app . options ,
52
+ tocOptions,
53
+ } ,
54
+ ] ) ;
55
+ }
44
56
const filePath = `${ output . outputDirectory } /${ urlMapping . url } ` ;
45
57
return await parse (
46
58
filePath ,
0 commit comments