Skip to content

Commit

Permalink
enhancement: auto-detect reversions of redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
nickcernera committed Feb 21, 2025
1 parent 1e3a080 commit 67e8e9d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 20 deletions.
4 changes: 2 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},

Expand Down
File renamed without changes.
42 changes: 39 additions & 3 deletions scripts/track-moves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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}`)
Expand Down Expand Up @@ -71,16 +104,19 @@ 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/') &&
newPath.startsWith('pages/') &&
oldPath.endsWith('.md') &&
newPath.endsWith('.md')
) {
console.log('Found moved file:', oldPath, '->', newPath)
movedFiles.push([oldPath, newPath])
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/generated/pages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
12 changes: 6 additions & 6 deletions src/routes/docs.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
10 changes: 5 additions & 5 deletions src/routing/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
},
Expand Down Expand Up @@ -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"
}
]
},
Expand Down

0 comments on commit 67e8e9d

Please sign in to comment.