| 95 | |
| 96 | const REDIRECT_RX = /^Redirect 302 \/(?<component>c.*)\/latest \/\k<component>\/(?<version>.*)$/ |
| 97 | |
| 98 | function versionlessRedirects (text) { |
| 99 | const lines = text.split('\n') |
| 100 | const processed = lines.reduce((accum, line) => { |
| 101 | accum.push(line) |
| 102 | const m = line.match(REDIRECT_RX) |
| 103 | if (m) { |
| 104 | accum.push(`RedirectMatch 302 "^/${m.groups.component}(/?)$" "/${m.groups.component}/${m.groups.version}/"`) |
| 105 | // The first line redirects **/next to **/next/ so the second line does not match. |
| 106 | // Apparently it needs to be a match or it will transform **/next/ to **/next// |
| 107 | accum.push(`RedirectMatch 301 "^/${m.groups.component}/next$" "/${m.groups.component}/next/"`) |
| 108 | accum.push(`RedirectMatch 302 "^/${m.groups.component}/(?![0-9].*|next/)(.+)$" "/${m.groups.component}/${m.groups.version}/$1"`) |
| 109 | // As an alternative, the following line works as long as no file names start with 'next' |
| 110 | // accum.push(`RedirectMatch 302 "^/${m.groups.component}/(?![0-9].*|next)(.+)$" "/${m.groups.component}/${m.groups.version}/$1"`) |
| 111 | } |
| 112 | return accum |
| 113 | }, []) |
| 114 | return processed.join('\n') |
| 115 | } |
| 116 | |
| 117 | // Register the generate-markdown task with lazy loading to avoid requiring |