summaryrefslogtreecommitdiff
path: root/src/search.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/search.ts')
-rw-r--r--src/search.ts75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/search.ts b/src/search.ts
new file mode 100644
index 000000000..d9e2fa00c
--- /dev/null
+++ b/src/search.ts
@@ -0,0 +1,75 @@
+import { existsSync } from 'fs';
+
+interface PageLocation {
+ fileURL: URL;
+ snowpackURL: string;
+}
+
+function findAnyPage(candidates: Array<string>, astroRoot: URL): PageLocation | false {
+ for(let candidate of candidates) {
+ const url = new URL(`./pages/${candidate}`, astroRoot);
+ if(existsSync(url)) {
+ return {
+ fileURL: url,
+ snowpackURL: `/_astro/pages/${candidate}.js`
+ };
+ }
+ }
+ return false;
+}
+
+type SearchResult = {
+ statusCode: 200;
+ location: PageLocation;
+ pathname: string;
+} | {
+ statusCode: 301;
+ location: null;
+ pathname: string;
+} | {
+ statusCode: 404;
+};
+
+export function searchForPage(url: URL, astroRoot: URL): SearchResult {
+ const reqPath = decodeURI(url.pathname);
+ const base = reqPath.substr(1);
+
+ // Try to find index.astro/md paths
+ if(reqPath.endsWith('/')) {
+ const candidates = [`${base}index.astro`, `${base}index.md`];
+ const location = findAnyPage(candidates, astroRoot);
+ if(location) {
+ return {
+ statusCode: 200,
+ location,
+ pathname: reqPath
+ };
+ }
+ } else {
+ // Try to find the page by its name.
+ const candidates = [`${base}.astro`, `${base}.md`];
+ let location = findAnyPage(candidates, astroRoot);
+ if(location) {
+ return {
+ statusCode: 200,
+ location,
+ pathname: reqPath
+ };
+ }
+ }
+
+ // Try to find name/index.astro/md
+ const candidates = [`${base}/index.astro`, `${base}/index.md`];
+ const location = findAnyPage(candidates, astroRoot);
+ if(location) {
+ return {
+ statusCode: 301,
+ location: null,
+ pathname: reqPath + '/'
+ };
+ }
+
+ return {
+ statusCode: 404
+ };
+} \ No newline at end of file