1+ const fs = require ( 'fs' ) ;
2+ const path = require ( 'path' ) ;
3+ const glob = require ( 'glob' ) ;
4+
5+ const DOCS_DIR = path . join ( __dirname , '..' , 'docs' ) ;
6+ const PUBLIC_MDX_IMAGES = path . join ( __dirname , '..' , 'public' , 'mdx-images' ) ;
7+
8+ function encodeImagePath ( mdxFile , imagePath ) {
9+ // Get the absolute path to the image
10+ const mdxDir = path . dirname ( mdxFile ) ;
11+ const absImagePath = path . resolve ( mdxDir , imagePath ) ;
12+ // Get the path relative to the docs root
13+ let relPath = path . relative ( DOCS_DIR , absImagePath ) ;
14+ // Replace path separators with dashes
15+ relPath = relPath . replace ( / [ \\ / ] / g, '-' ) ;
16+ return relPath ;
17+ }
18+
19+ function ensureDirExists ( dir ) {
20+ if ( ! fs . existsSync ( dir ) ) {
21+ fs . mkdirSync ( dir , { recursive : true } ) ;
22+ }
23+ }
24+
25+ function copyImages ( ) {
26+ ensureDirExists ( PUBLIC_MDX_IMAGES ) ;
27+
28+ // Find all MDX files in docs/
29+ const mdxFiles = glob . sync ( path . join ( DOCS_DIR , '**/*.mdx' ) ) ;
30+ console . log ( `Found ${ mdxFiles . length } MDX files` ) ;
31+
32+ // Match both ./img/ and ../img/ patterns
33+ const imageRegex = / ! \[ [ ^ \] ] * \] \( ( \. \. ? \/ i m g \/ [ ^ " ) ] + ) \) / g;
34+
35+ let copied = 0 ;
36+ mdxFiles . forEach ( mdxFile => {
37+ const content = fs . readFileSync ( mdxFile , 'utf8' ) ;
38+ const matches = [ ...content . matchAll ( imageRegex ) ] ;
39+ for ( const match of matches ) {
40+ const imagePath = match [ 1 ] ;
41+ const encodedName = encodeImagePath ( mdxFile , imagePath ) ;
42+ const src = path . resolve ( path . dirname ( mdxFile ) , imagePath ) ;
43+ const dest = path . join ( PUBLIC_MDX_IMAGES , encodedName ) ;
44+
45+ if ( fs . existsSync ( src ) ) {
46+ // Create the destination directory if it doesn't exist
47+ const destDir = path . dirname ( dest ) ;
48+ if ( ! fs . existsSync ( destDir ) ) {
49+ fs . mkdirSync ( destDir , { recursive : true } ) ;
50+ }
51+ fs . copyFileSync ( src , dest ) ;
52+ copied ++ ;
53+ console . log ( `Copied: ${ src } -> ${ dest } ` ) ;
54+ } else {
55+ console . warn ( `Image not found: ${ src } (referenced in ${ mdxFile } )` ) ;
56+ }
57+ }
58+ } ) ;
59+
60+ // Also copy all images from img directories directly
61+ const imgDirs = glob . sync ( path . join ( DOCS_DIR , '**/img' ) ) ;
62+ console . log ( `\nFound ${ imgDirs . length } img directories:` ) ;
63+ imgDirs . forEach ( dir => console . log ( `- ${ path . relative ( DOCS_DIR , dir ) } ` ) ) ;
64+
65+ imgDirs . forEach ( imgDir => {
66+ const files = fs . readdirSync ( imgDir ) ;
67+ const imageFiles = files . filter ( file => file . match ( / \. ( p n g | j p g | j p e g | g i f ) $ / i) ) ;
68+ console . log ( `\nFound ${ imageFiles . length } images in ${ path . relative ( DOCS_DIR , imgDir ) } :` ) ;
69+ imageFiles . forEach ( file => console . log ( `- ${ file } ` ) ) ;
70+
71+ imageFiles . forEach ( file => {
72+ const src = path . join ( imgDir , file ) ;
73+ // The MDX plugin expects paths like /mdx-images/img-filename.png
74+ const encodedName = `img-${ file } ` ;
75+ const dest = path . join ( PUBLIC_MDX_IMAGES , encodedName ) ;
76+
77+ if ( ! fs . existsSync ( dest ) ) {
78+ fs . copyFileSync ( src , dest ) ;
79+ copied ++ ;
80+ console . log ( `Copied: ${ src } -> ${ dest } ` ) ;
81+ } else {
82+ console . log ( `Skipped (already exists): ${ src } ` ) ;
83+ }
84+ } ) ;
85+ } ) ;
86+
87+ console . log ( `\nTotal images copied: ${ copied } ` ) ;
88+ console . log ( `Images are in: ${ PUBLIC_MDX_IMAGES } ` ) ;
89+ }
90+
91+ copyImages ( ) ;
0 commit comments