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

Function extract

src/resolution/frameworks/astro.ts:104–159  ·  view source on GitHub ↗
(filePath: string, content: string)

Source from the content-addressed store, hash-verified

102 },
103
104 extract(filePath: string, content: string) {
105 const nodes: Node[] = [];
106 const references: UnresolvedRef[] = [];
107 const now = Date.now();
108
109 // Normalize to forward slashes
110 const normalized = filePath.replace(/\\/g, '/');
111
112 // Astro file-based routing lives under src/pages/ — .astro files are
113 // pages, .ts/.js files are API endpoints. (.md/.mdx pages exist too but
114 // aren't indexed as source.) Underscore-prefixed segments are excluded
115 // from routing by Astro.
116 const pagesMatch = /(?:^|\/)src\/pages\//.exec(normalized);
117 if (pagesMatch && /\.(astro|ts|js|mjs)$/.test(normalized)) {
118 const afterPages = normalized.substring(pagesMatch.index + pagesMatch[0].length);
119 const base = afterPages.split('/').pop() || '';
120
121 // Underscore-prefixed segments are excluded from routing by Astro;
122 // a stray `*.config.*` in a pages dir is never a route.
123 if (
124 !afterPages.split('/').some((segment) => segment.startsWith('_')) &&
125 !/\.config\.[a-z]+$/.test(base)
126 ) {
127 const routePath = filePathToAstroRoute(afterPages);
128 const isPage = normalized.endsWith('.astro');
129 const language = isPage ? 'astro' : 'typescript';
130 const route: Node = {
131 id: `route:${filePath}:${routePath}:1`,
132 kind: 'route',
133 name: routePath,
134 qualifiedName: `${filePath}::route:${routePath}`,
135 filePath,
136 startLine: 1,
137 endLine: 1,
138 startColumn: 0,
139 endColumn: 0,
140 language,
141 updatedAt: now,
142 };
143 nodes.push(route);
144 if (isPage) {
145 references.push(pageComponentRef(route, '.astro', 'astro'));
146 } else {
147 // An endpoint is served by the verbs it exports: `export const GET:
148 // APIRoute = …`, `export async function POST(…)`.
149 for (const m of content.matchAll(ENDPOINT_EXPORT)) {
150 const verb = m[1] ?? m[2]!;
151 const line = content.slice(0, m.index).split('\n').length;
152 references.push({ fromNodeId: route.id, referenceName: verb, referenceKind: 'references', line, column: 0, filePath, language, candidates: [verb] });
153 }
154 }
155 }
156 }
157
158 return { nodes, references };
159 },
160};
161

Callers

nothing calls this directly

Calls 4

pageComponentRefFunction · 0.90
filePathToAstroRouteFunction · 0.85
pushMethod · 0.80
execMethod · 0.65

Tested by

no test coverage detected