@@ -10,10 +10,11 @@ import YAML from "yaml";
10
10
import { marked } from "marked" ;
11
11
import { cache } from "react" ;
12
12
import { current } from "consts" ;
13
- import { load as parseHtml } from 'cheerio' ;
13
+ import { load as parseHtml } from "cheerio" ;
14
+ import { Chapters } from "types" ;
14
15
15
16
export const MyOctokit = Octokit . plugin ( throttling ) ;
16
- const sidebarMemoryCache = new Map ( )
17
+ const sidebarMemoryCache = new Map ( ) ;
17
18
18
19
type Options = {
19
20
method ?: string ;
@@ -26,10 +27,13 @@ function toAbsoluteUrl(url: string, githubPath: string): string {
26
27
return url ;
27
28
} catch ( err ) {
28
29
if ( path . isAbsolute ( url ) ) {
29
- return url . replace ( ' index.md' , '' ) . replace ( ' .md' , '' ) ;
30
+ return url . replace ( " index.md" , "" ) . replace ( " .md" , "" ) ;
30
31
}
31
32
32
- return path . normalize ( `/docs/${ path . dirname ( githubPath ) } /${ url } ` ) . replace ( 'index.md' , '' ) . replace ( '.md' , '' )
33
+ return path
34
+ . normalize ( `/docs/${ path . dirname ( githubPath ) } /${ url } ` )
35
+ . replace ( "index.md" , "" )
36
+ . replace ( ".md" , "" ) ;
33
37
}
34
38
}
35
39
@@ -83,16 +87,16 @@ export async function loadMarkdownBySlugArray(slug: string[]) {
83
87
}
84
88
85
89
export const getDocTitle = async ( version : string , slug : string [ ] ) => {
86
- const key = slug . join ( '' )
90
+ const key = slug . join ( "" ) ;
87
91
if ( sidebarMemoryCache . has ( key ) ) {
88
- return sidebarMemoryCache . get ( key )
92
+ return sidebarMemoryCache . get ( key ) ;
89
93
}
90
- const { data, path } = await getDocContentFromSlug ( version , slug )
94
+ const { data, path } = await getDocContentFromSlug ( version , slug ) ;
91
95
const md = Buffer . from ( ( data as any ) . content , "base64" ) . toString ( ) ;
92
- const title = extractHeadingsFromMarkdown ( md , 1 ) ?. [ 0 ]
96
+ const title = extractHeadingsFromMarkdown ( md , 1 ) ?. [ 0 ] ;
93
97
94
- sidebarMemoryCache . set ( key , title || slug . shift ( ) )
95
- return sidebarMemoryCache . get ( key )
98
+ sidebarMemoryCache . set ( key , title || slug . shift ( ) ) ;
99
+ return sidebarMemoryCache . get ( key ) ;
96
100
} ;
97
101
98
102
export const loadV2DocumentationNav = cache ( async ( branch : string ) => {
@@ -103,19 +107,24 @@ export const loadV2DocumentationNav = cache(async (branch: string) => {
103
107
path : "outline.yaml" ,
104
108
ref : branch ,
105
109
} ) ;
106
- const result = Buffer . from ( ( data as any ) . content , "base64" ) ;
107
110
108
- const navData = YAML . parse ( result . toString ( ) ) ;
111
+ if ( ! ( "content" in data ) ) return [ ] ;
112
+
113
+ const result = Buffer . from ( data . content , "base64" ) ;
114
+
115
+ const navData : Chapters = YAML . parse ( result . toString ( ) ) ;
116
+
109
117
const basePath = branch === current ? `/docs` : `/docs/${ branch } ` ;
110
- const nav = await Promise . all (
111
- navData . chapters . map ( async ( part : any ) => ( {
118
+ return Promise . all (
119
+ navData . chapters . map ( async ( part ) => ( {
112
120
title : part . title ,
113
121
basePath : `${ basePath } /${ part . path } ` ,
114
122
links : await Promise . all (
115
123
part . items . map ( async ( item : string ) => ( {
116
- title : (
117
- await getDocTitle ( branch , [ part . path , item === 'index' ? '' : item ] )
118
- ) ,
124
+ title : await getDocTitle ( branch , [
125
+ part . path ,
126
+ item === "index" ? "" : item ,
127
+ ] ) ,
119
128
link :
120
129
item === "index"
121
130
? `${ basePath } /${ part . path } /`
@@ -124,19 +133,30 @@ export const loadV2DocumentationNav = cache(async (branch: string) => {
124
133
) ,
125
134
} ) )
126
135
) ;
127
- return nav ;
128
136
} catch ( error ) {
129
137
console . error ( error ) ;
130
138
}
131
139
return [ ] ;
132
140
} ) ;
133
141
134
- const indexes = [ 'admin' , 'core' , 'create-client' , 'deployment' , 'distribution' , 'extra' , 'schema-generator' ]
135
-
136
- export const getDocContentFromSlug = async ( version : string , slug : string [ ] ) => {
137
- slug = slug . filter ( v => v )
142
+ const indexes = [
143
+ "admin" ,
144
+ "core" ,
145
+ "create-client" ,
146
+ "deployment" ,
147
+ "distribution" ,
148
+ "extra" ,
149
+ "schema-generator" ,
150
+ "client-generator" ,
151
+ ] ;
152
+
153
+ export const getDocContentFromSlug = async (
154
+ version : string ,
155
+ slug : string [ ]
156
+ ) => {
157
+ slug = slug . filter ( ( v ) => v ) ;
138
158
const lastPart = slug . slice ( - 1 ) [ 0 ] ;
139
- const p = slug . join ( "/" ) + ( indexes . includes ( lastPart ) ? ' /index.md' : ' .md' ) ;
159
+ const p = slug . join ( "/" ) + ( indexes . includes ( lastPart ) ? " /index.md" : " .md" ) ;
140
160
141
161
try {
142
162
const { data } = await octokit . rest . repos . getContent ( {
@@ -146,37 +166,43 @@ export const getDocContentFromSlug = async (version: string, slug: string[]) =>
146
166
ref : version ,
147
167
} ) ;
148
168
149
- return { data, path : p }
169
+ return { data, path : p } ;
150
170
} catch ( error ) {
151
- console . error ( ' An error occured while fetching %s' , p )
171
+ console . error ( " An error occured while fetching %s" , p ) ;
152
172
console . error ( error ) ;
153
- return { data : { content : ' error' } , path : p }
173
+ return { data : { content : " error" } , path : p } ;
154
174
}
155
175
} ;
156
176
157
- const codeInside = / \[ c o d e S e l e c t o r \] ( [ \s \S ] + ?) (? = \[ \/ c o d e S e l e c t o r ] ) / gm
158
- const codeBlock = / ` ` ` [ a - z ] ( [ \s \S ] + ?) (? = ` ` ` ) / gm
159
- const codeLanguage = / ` ` ` ( [ a - z ] + ) /
177
+ const codeInside = / \[ c o d e S e l e c t o r \] ( [ \s \S ] + ?) (? = \[ \/ c o d e S e l e c t o r ] ) / gm;
178
+ const codeBlock = / ` ` ` [ a - z ] ( [ \s \S ] + ?) (? = ` ` ` ) / gm;
179
+ const codeLanguage = / ` ` ` ( [ a - z ] + ) / ;
160
180
161
181
function getLang ( block : string ) : string {
162
- const language = block . match ( codeLanguage )
182
+ const language = block . match ( codeLanguage ) ;
163
183
164
184
if ( ! language ?. length ) {
165
- return ' text' ;
185
+ return " text" ;
166
186
}
167
187
168
- return language [ 1 ]
188
+ return language [ 1 ] ;
169
189
}
170
190
171
- export const getHtmlFromGithubContent = async ( { data, path : githubPath } : { data : any , path : string } ) => {
191
+ export const getHtmlFromGithubContent = async ( {
192
+ data,
193
+ path : githubPath ,
194
+ } : {
195
+ data : any ;
196
+ path : string ;
197
+ } ) => {
172
198
const result = Buffer . from ( data . content , "base64" ) . toString ( ) ;
173
199
174
200
marked . setOptions ( { mangle : false , headerIds : false } ) ;
175
201
176
202
const highlighter = await getHighlighter ( {
177
203
themes : [ "github-light" , "one-dark-pro" ] ,
178
204
} ) ;
179
- const languages = highlighter . getLoadedLanguages ( )
205
+ const languages = highlighter . getLoadedLanguages ( ) ;
180
206
181
207
marked . use (
182
208
markedHighlight ( {
@@ -200,54 +226,53 @@ export const getHtmlFromGithubContent = async ({ data, path: githubPath }: { dat
200
226
// this allows the doc to work on github and on our nextjs website
201
227
marked . use ( {
202
228
walkTokens : ( token : any ) => {
203
- if ( ! [ ' link' , ' image' , ' html' ] . includes ( token . type ) ) {
229
+ if ( ! [ " link" , " image" , " html" ] . includes ( token . type ) ) {
204
230
return ;
205
231
}
206
232
207
- if ( token . type === ' html' ) {
208
- const $ = parseHtml ( token . raw )
209
- $ ( 'a' ) . map ( function ( i , elem ) {
210
- const el = $ ( this )
211
- const href = el . attr ( ' href' )
233
+ if ( token . type === " html" ) {
234
+ const $ = parseHtml ( token . raw ) ;
235
+ $ ( "a" ) . map ( function ( i , elem ) {
236
+ const el = $ ( this ) ;
237
+ const href = el . attr ( " href" ) ;
212
238
213
239
if ( href ) {
214
- el . attr ( ' href' , toAbsoluteUrl ( href , githubPath ) )
240
+ el . attr ( " href" , toAbsoluteUrl ( href , githubPath ) ) ;
215
241
}
216
242
217
- return el
218
- } )
219
-
243
+ return el ;
244
+ } ) ;
220
245
221
- $ ( ' img' ) . map ( function ( i , elem ) {
222
- const el = $ ( this )
223
- const src = el . attr ( ' src' )
246
+ $ ( " img" ) . map ( function ( i , elem ) {
247
+ const el = $ ( this ) ;
248
+ const src = el . attr ( " src" ) ;
224
249
225
250
if ( src ) {
226
- el . attr ( ' src' , toAbsoluteUrl ( src , githubPath ) )
251
+ el . attr ( " src" , toAbsoluteUrl ( src , githubPath ) ) ;
227
252
}
228
253
229
- return el
230
- } )
254
+ return el ;
255
+ } ) ;
231
256
232
- token . text = $ . html ( )
257
+ token . text = $ . html ( ) ;
233
258
return ;
234
259
}
235
260
236
- token . href = toAbsoluteUrl ( token . href , githubPath )
237
- }
238
- } )
261
+ token . href = toAbsoluteUrl ( token . href , githubPath ) ;
262
+ } ,
263
+ } ) ;
239
264
240
265
marked . use ( {
241
266
hooks : {
242
267
preprocess : ( markdown : any ) => {
243
- const matches = markdown . match ( codeInside )
268
+ const matches = markdown . match ( codeInside ) ;
244
269
245
270
if ( ! matches ?. length ) {
246
- return markdown
271
+ return markdown ;
247
272
}
248
273
249
274
matches . forEach ( ( m : string ) => {
250
- const blocks = m . match ( codeBlock )
275
+ const blocks = m . match ( codeBlock ) ;
251
276
252
277
if ( ! blocks ) {
253
278
return ;
@@ -256,29 +281,35 @@ export const getHtmlFromGithubContent = async ({ data, path: githubPath }: { dat
256
281
let html = `
257
282
<div class="mb-4 overflow-hidden rounded-2xl bg-gray-100 dark:bg-blue-darkest not-prose">
258
283
<div class="flex flex-wrap -mb-px bg-gray-300/10 dark:bg-blue/20 border-b border-gray-300 dark:border-blue-dark">
259
- `
284
+ ` ;
260
285
261
286
blocks . forEach ( ( block : string , i : number ) => {
262
- const l = getLang ( block )
263
- html += `<div><a key="${ l } " onclick="switchCode(event)" role="button" class="inline-block py-2 px-6 border-b-2 font-semibold text-sm uppercase hover:bg-blue-black/5 dark:hover:bg-blue-black/30 transition-all ${ i === 0 ? 'text-blue dark:text-white border-blue bg-blue-black/5 dark:bg-blue-black/30' : 'text-gray-400 dark:text-blue/60 border-transparent' } ">${ l } </a></div>`
264
- } )
287
+ const l = getLang ( block ) ;
288
+ html += `<div><a key="${ l } " onclick="switchCode(event)" role="button" class="inline-block py-2 px-6 border-b-2 font-semibold text-sm uppercase hover:bg-blue-black/5 dark:hover:bg-blue-black/30 transition-all ${
289
+ i === 0
290
+ ? "text-blue dark:text-white border-blue bg-blue-black/5 dark:bg-blue-black/30"
291
+ : "text-gray-400 dark:text-blue/60 border-transparent"
292
+ } ">${ l } </a></div>`;
293
+ } ) ;
265
294
266
- html += ' </div>'
295
+ html += " </div>" ;
267
296
268
297
blocks . forEach ( ( block : string , i : number ) => {
269
- const l = getLang ( block )
270
- const h = marked . parse ( block + `\n\`\`\`` )
271
- html += `<div key="${ l } " class="p-4 ${ i > 0 ? 'hidden' : '' } ">${ h } </div>`
272
- } )
273
-
274
- html += '</div>'
275
- markdown = markdown . replace ( m , html )
276
- } )
277
-
278
- return markdown . replaceAll ( '[/codeSelector]' , '' )
279
- }
280
- }
281
- } )
298
+ const l = getLang ( block ) ;
299
+ const h = marked . parse ( block + `\n\`\`\`` ) ;
300
+ html += `<div key="${ l } " class="p-4 ${
301
+ i > 0 ? "hidden" : ""
302
+ } ">${ h } </div>`;
303
+ } ) ;
304
+
305
+ html += "</div>" ;
306
+ markdown = markdown . replace ( m , html ) ;
307
+ } ) ;
308
+
309
+ return markdown . replaceAll ( "[/codeSelector]" , "" ) ;
310
+ } ,
311
+ } ,
312
+ } ) ;
282
313
283
314
return marked . parse ( result ) ;
284
- }
315
+ } ;
0 commit comments