Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental: handle redirect from slashed URL #1265

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 19 additions & 33 deletions src/lib/RedirectIntegration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { AstroIntegration } from "astro";
import fs from "fs/promises";
import fs from "node:fs/promises";
import matter from "gray-matter";
import { extname, join, relative } from "path";
import { fileURLToPath } from "url";
import { extname, join, relative } from "node:path";
import { fileURLToPath } from "node:url";
import { glob } from "glob";
import { VFile } from "vfile";

Expand All @@ -14,22 +14,25 @@ export default function redirect(): AstroIntegration {
hooks: {
"astro:config:setup": async ({ updateConfig, config }) => {
const pages = join(fileURLToPath(config.srcDir), "pages");
const paths = await glob("**/*", { cwd: pages, nodir: true, absolute: true });
const paths = await glob("**/*", {
cwd: pages,
nodir: true,
absolute: true,
});
const files = (
await Promise.all(
paths.map(async (path) => {
if (!source.includes(extname(path))) return null;
return readFile(path);
})
)).filter((file) => file !== null);
}),
)
).filter((file) => file !== null);
const redirects = files.flatMap((file) => {
const { redirect_to, redirect_from } = file.data;
const here =
"/" +
relative(pages, file.path).replace(
/(?:index)?\.(?:md|mdx|markdown|astro)$/,
""
);
const here = `/${relative(pages, file.path).replace(
/(?:index)?\.(?:md|mdx|markdown|astro)$/,
"",
)}`;
if (typeof redirect_to === "string") {
return { from: here, to: redirect_to };
}
Expand All @@ -46,7 +49,10 @@ export default function redirect(): AstroIntegration {
});
updateConfig({
redirects: Object.fromEntries(
redirects.map(({ from, to }) => [from, to])
redirects.map(({ from, to }) => [
from.replace(/\/$/, "/index"),
to,
]),
),
});
},
Expand All @@ -59,23 +65,3 @@ async function readFile(path: string) {
const { data } = matter(value);
return new VFile({ path, value, data });
}

function html(to: string) {
return `\
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="refresh" content="0; url=${to}" />
<link rel="canonical" href="${to}" />
</head>
<body>
<h1>Redirecting...</h1>
<p>
If you are not redirected automatically, follow this
<a href="${to}">link</a>.
</p>
</body>
</html>
`;
}
13 changes: 7 additions & 6 deletions src/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { fileURLToPath } from "node:url";
import { join } from "node:path";
import { parse as parsePath } from "path";
import { join, parse as parsePath } from "node:path";

export function getDistFilePath(dir: URL, pathname: string, component: string) {
const base = fileURLToPath(dir);

if (pathname === "/404") {
return join(base, "404.html");
}

if (parsePath(component).name === "index") {
const { name, ext } = parsePath(component);

if (name === "index" && ext && pathname !== component) {
return join(base, pathname, "index.html");
}

return join(base, pathname + ".html");
}
return join(base, `${pathname}.html`);
}
2 changes: 2 additions & 0 deletions src/pages/articles/cosense/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
title: Cosenseの基本的な使い方
redirect_from:
- /articles/scrapbox/
---

## この記事のハイライト
Expand Down
3 changes: 0 additions & 3 deletions src/pages/articles/scrapbox.md

This file was deleted.

2 changes: 2 additions & 0 deletions src/pages/en/articles/cosense/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
title: Basic Use of Cosense
redirect_from:
- /en/articles/scrapbox/
---

## Highlights of This Article
Expand Down
3 changes: 0 additions & 3 deletions src/pages/en/articles/scrapbox.md

This file was deleted.