1
1
import logger from '@docusaurus/logger' ;
2
2
import { visitParents } from 'unist-util-visit-parents' ;
3
- import { remove } from 'unist-util-remove ' ;
3
+ import { filter } from 'unist-util-filter ' ;
4
4
import fs from 'fs' ;
5
5
import path from 'path' ;
6
6
import { Node , Parent } from 'unist' ;
@@ -23,8 +23,6 @@ const fileContent = new Map<
23
23
const structureDefinitions = new Map < string , string > ( ) ;
24
24
const modifiers = new Set < Promise < void > > ( ) ;
25
25
26
- const EXCLUDE_LIST = [ 'browser-window-options' , 'web-preferences' ] ;
27
-
28
26
export default function attacher ( ) {
29
27
return transformer ;
30
28
}
@@ -52,8 +50,6 @@ async function transformer(tree: Parent, file: VFile) {
52
50
relativePath = `/${ locale } /docs/${ docPath } ` ;
53
51
}
54
52
55
- remove ( tree , 'heading' ) ;
56
-
57
53
if ( fileContent . has ( relativePath ) ) {
58
54
const { resolve } = fileContent . get ( relativePath ) ;
59
55
if ( resolve ) resolve ( tree ) ;
@@ -68,8 +64,8 @@ async function transformer(tree: Parent, file: VFile) {
68
64
const importNode = getJSXImport ( 'APIStructurePreview' ) ;
69
65
if ( modifiers . size ) {
70
66
tree . children . unshift ( importNode ) ;
71
- await Promise . all ( Array . from ( modifiers ) ) ;
72
67
}
68
+ await Promise . all ( Array . from ( modifiers ) ) ;
73
69
}
74
70
75
71
/**
@@ -84,9 +80,7 @@ const checkLinksandDefinitions = (node: Node): node is Link => {
84
80
structureDefinitions . set ( node . identifier , node . url ) ;
85
81
}
86
82
if ( isLink ( node ) && node . url . includes ( '/api/structures/' ) ) {
87
- return EXCLUDE_LIST . every (
88
- ( excludedFile ) => ! node . url . endsWith ( `/api/structures/${ excludedFile } ` )
89
- ) ;
83
+ return true ;
90
84
}
91
85
92
86
return false ;
@@ -100,7 +94,7 @@ function isStructureLinkReference(node: Node): node is LinkReference {
100
94
return isLinkReference ( node ) && structureDefinitions . has ( node . identifier ) ;
101
95
}
102
96
103
- function replaceLinkWithPreview ( node : Link | LinkReference ) {
97
+ function replaceLinkWithPreview ( node : Link | LinkReference , parents : Parent [ ] ) {
104
98
// depending on if the node is a direct link or a reference-style link,
105
99
// we get its URL differently.
106
100
let relativeStructureUrl : string ;
@@ -116,7 +110,6 @@ function replaceLinkWithPreview(node: Link | LinkReference) {
116
110
if ( relativeStructureUrl . endsWith ( '?inline' ) ) {
117
111
relativeStructureUrl = relativeStructureUrl . split ( '?inline' ) [ 0 ] ;
118
112
isInline = true ;
119
- console . log ( { isInline } ) ;
120
113
}
121
114
122
115
const relativeStructurePath = `${ relativeStructureUrl } .md` ;
@@ -173,8 +166,6 @@ function replaceLinkWithPreview(node: Link | LinkReference) {
173
166
174
167
const { promise } = fileContent . get ( relativeStructurePath ) ;
175
168
176
- // replace the raw link file with our JSX component.
177
- // See src/components/APIStructurePreview.jsx for implementation.
178
169
if (
179
170
( Array . isArray ( node . children ) &&
180
171
node . children . length > 0 &&
@@ -186,56 +177,45 @@ function replaceLinkWithPreview(node: Link | LinkReference) {
186
177
. then ( ( content ) => {
187
178
const title = ( node . children [ 0 ] as Text | InlineCode ) . value ;
188
179
189
- // replace the Link node with an MDX element in-place
190
- const previewNode = node as unknown as MdxJsxFlowElement ;
191
- previewNode . type = 'mdxJsxFlowElement' ;
192
- previewNode . name = 'APIStructurePreview' ;
193
- previewNode . children = [ ] ;
194
- previewNode . data = {
195
- _mdxExplicitJsx : true ,
196
- } ;
197
- previewNode . attributes = [
198
- {
199
- type : 'mdxJsxAttribute' ,
200
- name : 'url' ,
201
- value : `${ relativeStructureUrl } ` ,
202
- } ,
203
- {
204
- type : 'mdxJsxAttribute' ,
205
- name : 'title' ,
206
- value : title ,
207
- } ,
208
- {
209
- type : 'mdxJsxAttribute' ,
210
- name : 'content' ,
211
- value : JSON . stringify ( content ) ,
212
- } ,
213
- {
214
- type : 'mdxJsxAttribute' ,
215
- name : 'inline' ,
216
- value : {
217
- type : 'mdxJsxAttributeValueExpression' ,
218
- value : String ( isInline ) ,
219
- data : {
220
- estree : {
221
- type : 'Program' ,
222
- body : [
223
- {
224
- type : 'ExpressionStatement' ,
225
- expression : {
226
- type : 'Literal' ,
227
- value : isInline ,
228
- raw : String ( isInline ) ,
229
- } ,
230
- } ,
231
- ] ,
232
- sourceType : 'module' ,
233
- comments : [ ] ,
234
- } ,
235
- } ,
180
+ if ( isInline ) {
181
+ const siblings = parents [ parents . length - 1 ] . children ;
182
+ const filtered = filter (
183
+ content ,
184
+ ( node ) => node . type !== 'mdxjsEsm' && node . type !== 'heading'
185
+ ) ;
186
+ visitParents (
187
+ filtered ,
188
+ checkLinksandDefinitions ,
189
+ replaceLinkWithPreview
190
+ ) ;
191
+ siblings . push ( filtered ) ;
192
+ } else {
193
+ // replace the Link node with an MDX element in-place
194
+ const previewNode = node as unknown as MdxJsxFlowElement ;
195
+ previewNode . type = 'mdxJsxFlowElement' ;
196
+ previewNode . name = 'APIStructurePreview' ;
197
+ previewNode . children = [ ] ;
198
+ previewNode . data = {
199
+ _mdxExplicitJsx : true ,
200
+ } ;
201
+ previewNode . attributes = [
202
+ {
203
+ type : 'mdxJsxAttribute' ,
204
+ name : 'url' ,
205
+ value : `${ relativeStructureUrl } ` ,
236
206
} ,
237
- } ,
238
- ] ;
207
+ {
208
+ type : 'mdxJsxAttribute' ,
209
+ name : 'title' ,
210
+ value : title ,
211
+ } ,
212
+ {
213
+ type : 'mdxJsxAttribute' ,
214
+ name : 'content' ,
215
+ value : JSON . stringify ( content ) ,
216
+ } ,
217
+ ] ;
218
+ }
239
219
} )
240
220
. catch ( ( err ) => {
241
221
logger . error ( err ) ;
0 commit comments