@@ -208,6 +208,122 @@ function combineSitemaps() {
208208 console . log ( `Combined sitemap created successfully with ${ sitemaps . length } URLs.` ) ;
209209}
210210
211+ /**
212+ * Generate redirect files for old URL patterns to new structure
213+ */
214+ function generateRedirects ( ) {
215+ console . log ( 'Generating redirects for old URL structure...' ) ;
216+
217+ // Define the patterns to match and their redirects
218+ const patterns = [
219+ {
220+ oldDir : 'classes' ,
221+ pattern : / ^ E n g i n e \. ( .+ ) \. h t m l $ / ,
222+ newPath : '/engine/classes/$1.html'
223+ } ,
224+ {
225+ oldDir : 'interfaces' ,
226+ pattern : / ^ E n g i n e \. ( .+ ) \. h t m l $ / ,
227+ newPath : '/engine/interfaces/$1.html'
228+ } ,
229+ {
230+ oldDir : 'types' ,
231+ pattern : / ^ E n g i n e \. ( .+ ) \. h t m l $ / ,
232+ newPath : '/engine/types/$1.html'
233+ } ,
234+ {
235+ oldDir : 'modules' ,
236+ pattern : / ^ E n g i n e \. ( .+ ) \. h t m l $ / ,
237+ newPath : '/engine/modules/$1.html'
238+ } ,
239+ {
240+ oldDir : 'functions' ,
241+ pattern : / ^ E n g i n e \. ( .+ ) \. h t m l $ / ,
242+ newPath : '/engine/functions/$1.html'
243+ }
244+ ] ;
245+
246+ // Create all the necessary directories and redirect files
247+ for ( const { oldDir, pattern, newPath } of patterns ) {
248+ const dirPath = path . join ( 'docs' , oldDir ) ;
249+ ensureDir ( dirPath ) ;
250+
251+ // Create a catch-all index.html in the directory for redirecting
252+ const indexPath = path . join ( dirPath , 'index.html' ) ;
253+ const indexContent = `<!DOCTYPE html>
254+ <html>
255+ <head>
256+ <meta charset="utf-8">
257+ <title>Redirecting...</title>
258+ <script>
259+ (function() {
260+ var path = window.location.pathname;
261+ var filename = path.split('/').pop();
262+
263+ if (filename) {
264+ var match = filename.match(${ pattern . toString ( ) } );
265+ if (match && match[1]) {
266+ var newUrl = "${ newPath } ".replace('$1', match[1]);
267+ window.location.href = newUrl;
268+ return;
269+ }
270+ }
271+
272+ // If no match or no filename, redirect to homepage
273+ window.location.href = '/';
274+ })();
275+ </script>
276+ </head>
277+ <body>
278+ <p>Redirecting to the new API reference structure...</p>
279+ </body>
280+ </html>` ;
281+
282+ fs . writeFileSync ( indexPath , indexContent ) ;
283+ console . log ( `Created redirect for /${ oldDir } /* pattern` ) ;
284+ }
285+
286+ // Create a 404 page that attempts to handle redirects as well
287+ const notFoundPath = path . join ( 'docs' , '404.html' ) ;
288+ const notFoundContent = `<!DOCTYPE html>
289+ <html>
290+ <head>
291+ <meta charset="utf-8">
292+ <title>Page Not Found</title>
293+ <script>
294+ (function() {
295+ const path = window.location.pathname;
296+ const segments = path.split('/');
297+ const filename = segments.pop();
298+ const dirType = segments.pop();
299+
300+ // Check if this matches our old URL pattern
301+ if (dirType && filename && ['classes', 'functions', 'interfaces', 'modules', 'types', 'variables'].includes(dirType)) {
302+ const match = filename.match(/^Engine\.(.+)\.html$/);
303+ if (match && match[1]) {
304+ const newUrl = "/engine/" + dirType + "/" + match[1] + ".html";
305+ window.location.href = newUrl;
306+ return;
307+ }
308+ }
309+
310+ // Default fallback - go to homepage
311+ window.location.href = '/';
312+ })();
313+ </script>
314+ </head>
315+ <body>
316+ <h1>Page Not Found</h1>
317+ <p>Redirecting to the new URL structure...</p>
318+ </body>
319+ </html>` ;
320+
321+ fs . writeFileSync ( notFoundPath , notFoundContent ) ;
322+ console . log ( 'Created 404 page with redirection logic' ) ;
323+
324+ console . log ( 'Redirect generation complete.' ) ;
325+ }
326+
211327/**
212328 * Main function to build the documentation
213329 */
@@ -284,6 +400,10 @@ async function buildDocs() {
284400 console . log ( '\nGenerating combined sitemap...' ) ;
285401 combineSitemaps ( ) ;
286402
403+ // Generate redirects for old URLs
404+ console . log ( '\nGenerating redirects for old URL structure...' ) ;
405+ generateRedirects ( ) ;
406+
287407 console . log ( '\nDocumentation build complete. Run "npm run serve" to view it.' ) ;
288408 } catch ( error ) {
289409 console . error ( `\nError: ${ error . message } ` ) ;
0 commit comments