11import fs from 'node:fs/promises' ;
22import path from 'node:path' ;
33
4- import { exists , getDirs } from '@magicbell/codegen/fs' ;
54import * as md from '@magicbell/codegen/markdown' ;
65import * as glob from 'glob' ;
7-
8- function pascalToHyphenCase ( str : string ) : string {
9- return str . replace ( / ( [ a - z ] ) ( [ A - Z ] ) / g, '$1-$2' ) . toLowerCase ( ) ;
10- }
11-
12- function rewriteHref ( url : string ) {
13- if ( ! url . endsWith ( '.md' ) ) return url ;
14- return url . replace ( / ^ d o c u m e n t a t i o n \/ / , '' ) . replace ( / ( [ ^ / ] + ) \. m d $ / , ( _ , fileName ) => pascalToHyphenCase ( fileName ) ) ;
15- }
6+ import urlJoin from 'url-join' ;
167
178const root = process . cwd ( ) ;
189const outdir = path . join ( root , 'docs-dist' ) ;
1910await fs . rm ( outdir , { recursive : true , force : true } ) ;
2011
2112const pkg = JSON . parse ( await fs . readFile ( 'package.json' , 'utf-8' ) ) ;
13+ const repoUrl = 'https://github.com/magicbell/magicbell-java-user-client/blob/main'
14+
15+ function rewriteHref ( url : string ) {
16+ if ( url . startsWith ( 'http://' ) || url . startsWith ( 'https://' ) ) return url ;
17+ return urlJoin ( repoUrl , url ) ;
18+ }
2219
2320// process readme
2421const [ readme ] = glob . sync ( 'README.md' , { cwd : root } ) ;
@@ -29,29 +26,3 @@ md.mapLinks(readmeAst, rewriteHref);
2926md . insertFrontMatter ( readmeAst , { title : pkg . name } ) ;
3027
3128await md . write ( readmeAst , path . join ( outdir , 'index.mdx' ) ) ;
32-
33- // process pages
34- const docs = glob . sync ( '**/*.md' , {
35- cwd : path . join ( root , 'documentation' ) ,
36- } ) ;
37-
38- for ( const file of docs ) {
39- const ast = await md . read ( path . join ( root , 'documentation' , file ) ) ;
40-
41- md . reIndentHeadings ( ast , 1 ) ;
42- md . mapLinks ( ast , rewriteHref ) ;
43-
44- const title = md . getTitle ( ast ) ;
45- md . insertFrontMatter ( ast , { title } ) ;
46- md . removeFirstHeading ( ast ) ;
47-
48- await md . write ( ast , path . join ( outdir , pascalToHyphenCase ( file ) + 'x' ) ) ;
49- }
50-
51- for ( const dir of getDirs ( docs ) ) {
52- const file = path . join ( outdir , dir , 'index.mdx' ) ;
53- if ( await exists ( file ) ) continue ;
54- const ast : md . Root = { type : 'root' , children : [ ] } ;
55- md . insertFrontMatter ( ast , { title : path . basename ( dir ) } ) ;
56- await md . write ( ast , file ) ;
57- }
0 commit comments