1
+ #! /usr/bin/env node --experimental-strip-types
1
2
import fs from "node:fs/promises" ;
2
3
import path from "node:path" ;
3
4
4
- import { exists , getDirs } from "@magicbell/codegen/fs" ;
5
5
import * as md from "@magicbell/codegen/markdown" ;
6
6
import * as glob from "glob" ;
7
+ import urlJoin from 'url-join' ;
7
8
8
9
function pascalToHyphenCase ( str : string ) : string {
9
10
return str . replace ( / ( [ a - z ] ) ( [ A - Z ] ) / g, "$1-$2" ) . toLowerCase ( ) ;
10
11
}
11
12
12
- function rewriteHref ( url : string ) {
13
- if ( ! url . endsWith ( ".md" ) ) return url ;
14
- return url
15
- . replace ( / ^ d o c u m e n t a t i o n \/ / , "" )
16
- . replace ( / ( [ ^ / ] + ) \. m d $ / , ( _ , fileName ) => pascalToHyphenCase ( fileName ) ) ;
17
- }
18
-
19
13
const root = process . cwd ( ) ;
20
14
const outdir = path . join ( root , "docs-dist" ) ;
21
15
await fs . rm ( outdir , { recursive : true , force : true } ) ;
22
16
23
17
const pkg = JSON . parse ( await fs . readFile ( "package.json" , "utf-8" ) ) ;
18
+ const repoUrl = 'https://github.com/magicbell/magicbell-swift-user-client/blob/main'
19
+ function rewriteHref ( url : string ) {
20
+ if ( url . startsWith ( 'http://' ) || url . startsWith ( 'https://' ) ) return url ;
21
+ console . log ( 'join' , repoUrl , url ) ;
22
+ return urlJoin ( repoUrl , 'documentation' , url ) ;
23
+ }
24
24
25
- // process readmes
25
+ // process readme
26
26
const [ readme ] = glob . sync ( "README.md" , { cwd : root } ) ;
27
27
const rootIndexAst = await md . read ( path . join ( root , readme ) ) ;
28
28
md . removeAllBeforeHeading ( rootIndexAst , "Setup & Configuration" ) ;
29
29
30
- const [ reference ] = glob . sync ( "**/README.md" , {
31
- cwd : path . join ( root , "documentation" ) ,
32
- } ) ;
33
- const referenceAst = await md . read ( path . join ( root , "documentation" , reference ) ) ;
30
+ const [ reference ] = glob . sync ( "documentation/**/README.md" , { cwd : root } ) ;
31
+ const referenceAst = await md . read ( path . join ( root , reference ) ) ;
34
32
md . reIndentHeadings ( referenceAst , 2 ) ;
35
33
// append references to root index
36
34
rootIndexAst . children = rootIndexAst . children . concat ( referenceAst . children ) ;
@@ -39,40 +37,3 @@ md.reIndentHeadings(rootIndexAst, 1);
39
37
md . mapLinks ( rootIndexAst , rewriteHref ) ;
40
38
md . insertFrontMatter ( rootIndexAst , { title : pkg . name } ) ;
41
39
await md . write ( rootIndexAst , path . join ( outdir , "index.mdx" ) ) ;
42
-
43
- // process pages
44
- const docs = glob . sync ( "**/*.md" , {
45
- cwd : path . join ( root , "documentation" ) ,
46
- } ) ;
47
-
48
- for ( const file of docs ) {
49
- if ( file === "README.md" ) continue ;
50
- const ast = await md . read ( path . join ( root , "documentation" , file ) ) ;
51
-
52
- md . reIndentHeadings ( ast , 1 ) ;
53
- md . mapLinks ( ast , rewriteHref ) ;
54
-
55
- let titleComponents = path . parse ( file ) . name . split ( "." ) ;
56
- // Removing noisy namespaces
57
- if ( titleComponents . length > 1 && titleComponents [ 0 ] === "Components" ) {
58
- titleComponents . shift ( ) ;
59
- }
60
- if ( titleComponents . length > 1 && titleComponents [ 0 ] === "Schemas" ) {
61
- titleComponents . shift ( ) ;
62
- }
63
- if ( titleComponents . length > 1 && titleComponents [ 0 ] === "Operations" ) {
64
- titleComponents . shift ( ) ;
65
- }
66
- md . insertFrontMatter ( ast , { title : titleComponents . join ( "." ) } ) ;
67
- md . removeFirstHeading ( ast ) ;
68
-
69
- await md . write ( ast , path . join ( outdir , pascalToHyphenCase ( file ) + "x" ) ) ;
70
- }
71
-
72
- for ( const dir of getDirs ( docs ) ) {
73
- const file = path . join ( outdir , dir , "index.mdx" ) ;
74
- if ( await exists ( file ) ) continue ;
75
- const ast : md . Root = { type : "root" , children : [ ] } ;
76
- md . insertFrontMatter ( ast , { title : path . basename ( dir ) } ) ;
77
- await md . write ( ast , pascalToHyphenCase ( file ) ) ;
78
- }
0 commit comments