Skip to content

Commit f783b24

Browse files
committed
refactor: add utility to remove base path
1 parent 4ff9368 commit f783b24

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

website/src/components/templates/BaseTemplate.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { html } from "hono/html";
22
import type { FC, PropsWithChildren } from "hono/jsx";
33
import { basePath, originUrl, typstOfficialDocsUrl } from "../../metadata";
44
import type { Page } from "../../types/model";
5-
import { joinPath } from "../../utils/path";
5+
import { joinPath, removeBasePath } from "../../utils/path";
66
import { getTranslationStatus } from "../../utils/translationStatus";
77
import {
88
CaretRightCircleIcon,
@@ -46,7 +46,10 @@ export const BaseTemplate: FC<BaseTemplateProps> = ({
4646
const translationStatus = getTranslationStatus(route);
4747
const absoluteRouteUrl = new URL(route, originUrl).toString();
4848
const faviconUrl = new URL(`${basePath}/favicon.png`, originUrl).toString();
49-
const typstOfficialRouteUrl = joinPath(typstOfficialDocsUrl, route.slice(basePath.length - (basePath.endsWith("/") ? 1 : 0)));
49+
const typstOfficialRouteUrl = joinPath(
50+
typstOfficialDocsUrl,
51+
removeBasePath(basePath, route),
52+
);
5053
return (
5154
<html lang="ja" class="scroll-pt-24">
5255
<head>

website/src/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { basePath } from "./metadata";
1313
import type { Body, Page } from "./types/model";
1414
import { flattenDocs } from "./utils/flattenDocs";
1515
import { isPageOfKind } from "./utils/isPageOfKind";
16+
import { removeBasePath } from "./utils/path";
1617
import { registerRoutes } from "./utils/translationStatus";
1718

1819
// typst-docsが生成したドキュメント
@@ -55,7 +56,7 @@ The 'basePath' must match the 'base' value used in typst-docs.
5556
Please ensure both this site and typst-docs are configured with the same base path.`,
5657
);
5758
}
58-
route = route.slice(basePath.length - (basePath.endsWith("/") ? 1 : 0));
59+
route = removeBasePath(basePath, route);
5960
app.get(route, (c) => {
6061
if (isPageOfKind(page, "html")) {
6162
return c.html(<HtmlTemplate page={page} {...commonProps} />);

website/src/utils/path.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "vitest";
2-
import { applyBasePath, joinPath } from "./path";
2+
import { applyBasePath, joinPath, removeBasePath } from "./path";
33

44
describe("joinPath", () => {
55
it("should join base and path with single slash", () => {
@@ -45,3 +45,24 @@ describe("applyBasePath", () => {
4545
expect(applyBasePath("", "/foo")).toBe("/foo");
4646
});
4747
});
48+
49+
describe("removeBasePath", () => {
50+
it("should remove basePath with trailing slash", () => {
51+
expect(removeBasePath("/docs/", "/docs/foo/bar")).toBe("/foo/bar");
52+
expect(removeBasePath("/base/", "/base/foo")).toBe("/foo");
53+
});
54+
it("should remove basePath without trailing slash", () => {
55+
expect(removeBasePath("/docs", "/docs/foo/bar")).toBe("/foo/bar");
56+
expect(removeBasePath("/base", "/base/foo")).toBe("/foo");
57+
});
58+
it("should return route unchanged if it does not start with basePath", () => {
59+
expect(removeBasePath("/docs", "/other/foo")).toBe("/other/foo");
60+
expect(removeBasePath("/base", "/docs/foo")).toBe("/docs/foo");
61+
});
62+
it("should handle root basePath", () => {
63+
expect(removeBasePath("/", "/foo/bar")).toBe("/foo/bar");
64+
});
65+
it("should handle empty basePath", () => {
66+
expect(removeBasePath("", "/foo/bar")).toBe("/foo/bar");
67+
});
68+
});

website/src/utils/path.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,22 @@ export const applyBasePath = (basePath: string, path: string): string => {
4646
const isRelative = !path.startsWith("/");
4747
return isRelative ? path : joinPath(basePath, path);
4848
};
49+
50+
/**
51+
* Removes the basePath prefix from a route string.
52+
*
53+
* @param basePath - The base path to remove.
54+
* @param route - The route string to process.
55+
* @returns The route string with basePath removed from the start.
56+
*
57+
* @example
58+
* ```ts
59+
* removeBasePath("/docs/", "/docs/foo/bar") -> "/foo/bar"
60+
* removeBasePath("/docs", "/docs/foo/bar") -> "/foo/bar"
61+
* ```
62+
*/
63+
export const removeBasePath = (basePath: string, route: string): string => {
64+
if (!route.startsWith(basePath)) return route;
65+
const offset = basePath.length - (basePath.endsWith("/") ? 1 : 0);
66+
return route.slice(offset);
67+
};

0 commit comments

Comments
 (0)