| 173 | ]) |
| 174 | |
| 175 | export function determineInitialRoutePath(routePath: string) { |
| 176 | const originalRoutePath = |
| 177 | cleanPath( |
| 178 | `/${(cleanPath(routePath) || '').split(SPLIT_REGEX).join('/')}`, |
| 179 | ) || '' |
| 180 | |
| 181 | const parts = routePath.split(SPLIT_REGEX) |
| 182 | |
| 183 | // Escape any characters that in square brackets |
| 184 | // we keep the original path untouched |
| 185 | const escapedParts = parts.map((part) => { |
| 186 | // Check if any disallowed characters are used in brackets |
| 187 | |
| 188 | let match |
| 189 | while ((match = BRACKET_CONTENT_RE.exec(part)) !== null) { |
| 190 | const character = match[1] |
| 191 | if (character === undefined) continue |
| 192 | if (DISALLOWED_ESCAPE_CHARS.has(character)) { |
| 193 | console.error( |
| 194 | `Error: Disallowed character "${character}" found in square brackets in route path "${routePath}".\nYou cannot use any of the following characters in square brackets: ${Array.from( |
| 195 | DISALLOWED_ESCAPE_CHARS, |
| 196 | ).join(', ')}\nPlease remove and/or replace them.`, |
| 197 | ) |
| 198 | process.exit(1) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // Since this split segment is safe at this point, we can |
| 203 | // remove the brackets and replace them with the content inside |
| 204 | return part.replace(BRACKET_CONTENT_RE, '$1') |
| 205 | }) |
| 206 | |
| 207 | // If the syntax for prefix/suffix is different, from the path |
| 208 | // matching internals of router-core, we'd perform those changes here |
| 209 | // on the `escapedParts` array before it is joined back together in |
| 210 | // `final` |
| 211 | |
| 212 | const final = cleanPath(`/${escapedParts.join('/')}`) || '' |
| 213 | |
| 214 | return { |
| 215 | routePath: final, |
| 216 | originalRoutePath, |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Checks if a segment is fully escaped (entirely wrapped in brackets with no nested brackets). |