Skip to content

Commit 704e28a

Browse files
committed
refactor: replace string concatenation with path utility for URL construction
1 parent 07bd631 commit 704e28a

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

website/src/components/templates/BaseTemplate.tsx

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { html } from "hono/html";
33
import type { FC, PropsWithChildren } from "hono/jsx";
44
import { basePath, originUrl, typstOfficialUrl } from "../../metadata";
55
import type { Page } from "../../types/model";
6+
import { joinPath } from "../../utils/path";
67
import { getTranslationStatus } from "../../utils/translationStatus";
78
import {
89
CaretRightCircleIcon,
@@ -78,41 +79,53 @@ export const BaseTemplate: FC<BaseTemplateProps> = ({
7879
rel="icon"
7980
type="image/png"
8081
sizes="32x32"
81-
href={`${basePath.replace(/\/$/, "")}/favicon.png`}
82+
href={joinPath(basePath, "/favicon.png")}
8283
/>
8384
<link
8485
rel="preload"
85-
href={`${basePath.replace(/\/$/, "")}/fonts/hanken-grotesk/HKGrotesk-Regular.woff2`}
86+
href={joinPath(
87+
basePath,
88+
"/fonts/hanken-grotesk/HKGrotesk-Regular.woff2",
89+
)}
8690
as="font"
8791
type="font/woff2"
8892
crossOrigin="anonymous"
8993
/>
9094
<link
9195
rel="preload"
92-
href={`${basePath.replace(/\/$/, "")}/fonts/hanken-grotesk/HKGrotesk-Bold.woff2`}
96+
href={joinPath(
97+
basePath,
98+
"/fonts/hanken-grotesk/HKGrotesk-Bold.woff2",
99+
)}
93100
as="font"
94101
type="font/woff2"
95102
crossOrigin="anonymous"
96103
/>
97104
<link
98105
rel="preload"
99-
href={`${basePath.replace(/\/$/, "")}/fonts/hanken-grotesk/HKGrotesk-SemiBold.woff2`}
106+
href={joinPath(
107+
basePath,
108+
"/fonts/hanken-grotesk/HKGrotesk-SemiBold.woff2",
109+
)}
100110
as="font"
101111
type="font/woff2"
102112
crossOrigin="anonymous"
103113
/>
104114
<link
105115
rel="preload"
106-
href={`${basePath.replace(/\/$/, "")}/fonts/cascadia-code/CascadiaMono-Regular-Sub.woff2`}
116+
href={joinPath(
117+
basePath,
118+
"/fonts/cascadia-code/CascadiaMono-Regular-Sub.woff2",
119+
)}
107120
as="font"
108121
type="font/woff2"
109122
crossOrigin="anonymous"
110123
/>
111124
<link
112125
href={
113126
import.meta.env.DEV
114-
? `${basePath.replace(/\/$/, "")}/src/globals.css`
115-
: `${basePath.replace(/\/$/, "")}/globals.css`
127+
? joinPath(basePath, "/src/globals.css")
128+
: joinPath(basePath, "/globals.css")
116129
}
117130
rel="stylesheet"
118131
/>
@@ -124,7 +137,7 @@ export const BaseTemplate: FC<BaseTemplateProps> = ({
124137
/* Global font family */
125138
@font-face {
126139
font-family: "HK Grotesk";
127-
src: url("${basePath.replace(/\/$/, "")}/fonts/hanken-grotesk/HKGrotesk-Regular.woff2")
140+
src: url("${joinPath(basePath, "/fonts/hanken-grotesk/HKGrotesk-Regular.woff2")}")
128141
format("woff2");
129142
font-weight: 400;
130143
font-style: normal;
@@ -133,7 +146,7 @@ export const BaseTemplate: FC<BaseTemplateProps> = ({
133146
134147
@font-face {
135148
font-family: "HK Grotesk";
136-
src: url("${basePath.replace(/\/$/, "")}/fonts/hanken-grotesk/HKGrotesk-SemiBold.woff2")
149+
src: url("${joinPath(basePath, "/fonts/hanken-grotesk/HKGrotesk-SemiBold.woff2")}")
137150
format("woff2");
138151
font-weight: 600;
139152
font-style: normal;
@@ -142,15 +155,15 @@ export const BaseTemplate: FC<BaseTemplateProps> = ({
142155
143156
@font-face {
144157
font-family: "HK Grotesk";
145-
src: url("${basePath.replace(/\/$/, "")}/fonts/hanken-grotesk/HKGrotesk-Bold.woff2") format("woff2");
158+
src: url("${joinPath(basePath, "/fonts/hanken-grotesk/HKGrotesk-Bold.woff2")}") format("woff2");
146159
font-weight: 700;
147160
font-style: normal;
148161
font-display: swap;
149162
}
150163
151164
@font-face {
152165
font-family: "Cascadia Mono";
153-
src: url("${basePath.replace(/\/$/, "")}/fonts/cascadia-code/CascadiaMono-Regular-Sub.woff2")
166+
src: url("${joinPath(basePath, "/fonts/cascadia-code/CascadiaMono-Regular-Sub.woff2")}")
154167
format("woff2");
155168
font-weight: 400;
156169
font-style: normal;
@@ -176,7 +189,7 @@ samp {
176189
{import.meta.env.DEV &&
177190
html`
178191
<script>
179-
import("${basePath.replace(/\/$/, "")}/@vite/client")
192+
import("${joinPath(basePath, "/@vite/client")}")
180193
</script>
181194
`}
182195
</head>

website/src/components/ui/common/SearchWindow.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { FC } from "hono/jsx";
22
import { basePath } from "../../../metadata";
3+
import { joinPath } from "../../../utils/path";
34
import { CloseIcon } from "../../icons";
45

56
export const SearchWindow: FC = () => {
@@ -8,12 +9,10 @@ export const SearchWindow: FC = () => {
89
{import.meta.env.PROD && (
910
<>
1011
<link
11-
href={`${basePath.replace(/\/$/, "")}/pagefind/pagefind-ui.css`}
12+
href={joinPath(basePath, "/pagefind/pagefind-ui.css")}
1213
rel="stylesheet"
1314
/>
14-
<script
15-
src={`${basePath.replace(/\/$/, "")}/pagefind/pagefind-ui.js`}
16-
/>
15+
<script src={joinPath(basePath, "/pagefind/pagefind-ui.js")} />
1716
</>
1817
)}
1918
<div class="flex justify-between items-center p-4 border-b border-gray-200 flex-shrink-0">

website/vite.config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ssg from "@hono/vite-ssg";
88
import tailwindcss from "@tailwindcss/vite";
99
import { defineConfig } from "vite";
1010
import { basePath, originUrl } from "./src/metadata";
11+
import { joinPath } from "./src/utils/path";
1112

1213
// Create a symbolic link to the assets generated by typst-docs in ./public/assets/
1314
const assetsDocsPath = resolve(__dirname, "../assets/");
@@ -43,8 +44,8 @@ export default defineConfig({
4344
/^\/assets\/.+/,
4445
/^\/index\.html$/,
4546
// NOTE: @hono/vite-dev-server does not respect the base setting in the Vite configuration.
46-
new RegExp(`^${basePath.replace(/\/$/, "")}/@.+`),
47-
new RegExp(`^${basePath.replace(/\/$/, "")}/node_modules(?:/|$)`),
47+
new RegExp(`^${joinPath(basePath, "@")}`),
48+
new RegExp(`^${joinPath(basePath, "node_modules")}(?:/|$)`),
4849
],
4950
}),
5051
],

0 commit comments

Comments
 (0)