MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / filePathToAstroRoute

Function filePathToAstroRoute

src/resolution/frameworks/astro.ts:206–221  ·  view source on GitHub ↗

* Convert a path under src/pages/ to an Astro route path. * * blog/[slug].astro -> /blog/:slug * blog/[...path].astro -> /blog/*path * api/posts.ts -> /api/posts * index.astro -> /

(afterPages: string)

Source from the content-addressed store, hash-verified

204 * index.astro -> /
205 */
206function filePathToAstroRoute(afterPages: string): string {
207 // Remove the extension
208 const withoutExt = afterPages.replace(/\.(astro|ts|js|mjs)$/, '');
209
210 // index files map to their parent path (index -> /, blog/index -> /blog)
211 const withoutIndex = withoutExt.replace(/(^|\/)index$/, '$1').replace(/\/$/, '');
212
213 // Convert Astro param syntax
214 const route = '/' + withoutIndex
215 .replace(/\[\.\.\.([^\]]+)\]/g, '*$1') // [...rest] -> *rest (catch-all)
216 .replace(/\[([^\]]+)\]/g, ':$1'); // [param] -> :param
217
218 if (route === '/') return '/';
219 // Remove trailing slash
220 return route.replace(/\/$/, '');
221}

Callers 1

extractFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected