From 67e8e9dc4e580c8dd7002a9e2fc8cecf21805c1e Mon Sep 17 00:00:00 2001 From: Nick Cernera Date: Fri, 21 Feb 2025 17:12:08 -0500 Subject: [PATCH] enhancement: auto-detect reversions of redirects --- next.config.js | 4 +- .../04-agent-api-reference.md | 0 scripts/track-moves.ts | 42 +++++++++++++++++-- src/generated/pages.json | 8 ++-- src/routes/docs.generated.ts | 12 +++--- src/routing/navigation.ts | 10 ++--- 6 files changed, 56 insertions(+), 20 deletions(-) rename pages/{02-getting-started => 01-overview}/04-agent-api-reference.md (100%) diff --git a/next.config.js b/next.config.js index ed9b00fb..52fa7e0f 100644 --- a/next.config.js +++ b/next.config.js @@ -27,8 +27,8 @@ const nextConfig = { }, async redirects() { return [ { - source: '/overview/agent-api-reference', - destination: '/getting-started/agent-api-reference', + source: '/getting-started/agent-api-reference', + destination: '/overview/agent-api-reference', permanent: true, }, diff --git a/pages/02-getting-started/04-agent-api-reference.md b/pages/01-overview/04-agent-api-reference.md similarity index 100% rename from pages/02-getting-started/04-agent-api-reference.md rename to pages/01-overview/04-agent-api-reference.md diff --git a/scripts/track-moves.ts b/scripts/track-moves.ts index 214dfdad..74b2b22f 100644 --- a/scripts/track-moves.ts +++ b/scripts/track-moves.ts @@ -11,6 +11,26 @@ function stripNumberedPrefixes(path: string): string { .join('/') } +function removeRedirectFromConfig(source: string) { + let content = fs.readFileSync(CONFIG_FILE, 'utf-8') + + // Find the redirect entry + const redirectRegex = new RegExp( + `\\s*\\{\\s*source:\\s*'${source}',[^}]+\\},?\\n?`, + 'g' + ) + + // Remove the redirect + content = content.replace(redirectRegex, '') + + // Clean up any double newlines created by the removal + content = content.replace(/\n\n\n+/g, '\n\n') + + // Write back to the file + fs.writeFileSync(CONFIG_FILE, content) + console.log(`Removed redirect for: ${source}`) +} + function addRedirectToConfig(oldPath: string, newPath: string) { // Read the current next.config.js let content = fs.readFileSync(CONFIG_FILE, 'utf-8') @@ -30,6 +50,19 @@ function addRedirectToConfig(oldPath: string, newPath: string) { .replace(/\/index$/, '') ) + // Check if this is a file returning to its original location + // by looking for a redirect where this file's new location was the source + const returningFileRegex = new RegExp( + `source:\\s*'${newUrl}',[^}]+destination:\\s*'${oldUrl}'` + ) + + if (content.match(returningFileRegex)) { + console.log(`File returning to original location: ${newUrl} -> ${oldUrl}`) + removeRedirectFromConfig(newUrl) + + return + } + // Check if redirect already exists if (content.includes(`source: '${oldUrl}'`)) { console.log(`Redirect already exists for: ${oldUrl}`) @@ -71,9 +104,11 @@ function getMovedFiles(): Array<[string, string]> { const movedFiles: Array<[string, string]> = [] gitStatus.split('\n').forEach((line) => { - // R = renamed file - if (line.startsWith('R ')) { - const [_, oldPath, newPath] = line.trim().split(/\s+/) + // R = renamed/moved file + if (line.startsWith('R ') || line.startsWith(' R')) { + // Git status format for renames is: R old-path -> new-path + const [_, paths] = line.trim().split(/\s+(.+)/) + const [oldPath, newPath] = paths.split(' -> ') if ( oldPath.startsWith('pages/') && @@ -81,6 +116,7 @@ function getMovedFiles(): Array<[string, string]> { oldPath.endsWith('.md') && newPath.endsWith('.md') ) { + console.log('Found moved file:', oldPath, '->', newPath) movedFiles.push([oldPath, newPath]) } } diff --git a/src/generated/pages.json b/src/generated/pages.json index bb65fdcf..fde9a2ec 100644 --- a/src/generated/pages.json +++ b/src/generated/pages.json @@ -11,6 +11,10 @@ "path": "/overview/management-api-reference", "lastmod": "2025-02-21T20:28:52.000Z" }, + { + "path": "/overview/agent-api-reference", + "lastmod": "2025-02-21T22:08:20.000Z" + }, { "path": "/getting-started/first-steps/cli-quickstart", "lastmod": "2025-02-20T19:05:59.000Z" @@ -75,10 +79,6 @@ "path": "/getting-started/advanced-config", "lastmod": "2025-02-20T19:05:59.000Z" }, - { - "path": "/getting-started/agent-api-reference", - "lastmod": "2025-02-21T22:02:03.611Z" - }, { "path": "/plural-features/continuous-deployment/deployment-operator", "lastmod": "2025-02-20T19:05:59.000Z" diff --git a/src/routes/docs.generated.ts b/src/routes/docs.generated.ts index 19ff6a97..97c6fd0e 100644 --- a/src/routes/docs.generated.ts +++ b/src/routes/docs.generated.ts @@ -24,6 +24,12 @@ export const docRoutes: DocRouteMap = { id: 'overview_management_api_reference', }, + overview_agent_api_reference: { + path: '/01-overview/04-agent-api-reference', + title: 'Agent API Reference', + id: 'overview_agent_api_reference', + }, + getting_started_first_steps: { path: '/02-getting-started/01-first-steps', title: 'First steps', @@ -120,12 +126,6 @@ export const docRoutes: DocRouteMap = { id: 'getting_started_advanced_config_private_ca', }, - overview_agent_api_reference: { - path: '/02-getting-started/04-agent-api-reference', - title: 'Agent API Reference', - id: 'overview_agent_api_reference', - }, - plural_features_continuous_deployment: { path: '/03-plural-features/01-continuous-deployment', title: 'Continuous deployment', diff --git a/src/routing/navigation.ts b/src/routing/navigation.ts index 34298a46..ba65ba79 100644 --- a/src/routing/navigation.ts +++ b/src/routing/navigation.ts @@ -23,6 +23,11 @@ export const docNavigation: NavMenu = [ "title": "Management API Reference", "href": "/overview/management-api-reference", "sortPath": "/01-overview/03-management-api-reference" + }, + { + "title": "Agent API Reference", + "href": "/overview/agent-api-reference", + "sortPath": "/01-overview/04-agent-api-reference" } ] }, @@ -112,11 +117,6 @@ export const docNavigation: NavMenu = [ "sortPath": "/02-getting-started/03-advanced-config/03-private-ca" } ] - }, - { - "title": "Agent API Reference", - "href": "/getting-started/agent-api-reference", - "sortPath": "/02-getting-started/04-agent-api-reference" } ] },