Skip to content

Commit cef21d5

Browse files
committed
repo url handling cleanup
1 parent 9d33db3 commit cef21d5

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

src/core/path.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,21 @@ export function pathWithForwardSlashes(path: string) {
160160
}
161161

162162
export function ensureTrailingSlash(path: string) {
163-
if (!path.endsWith("/")) {
163+
if (path && !path.endsWith("/")) {
164164
return path + "/";
165165
} else {
166166
return path;
167167
}
168168
}
169169

170+
export function removeTrailingSlash(path: string) {
171+
if (path && path.endsWith("/")) {
172+
return path.slice(0, path.length - 1);
173+
} else {
174+
return path;
175+
}
176+
}
177+
170178
export function resolveGlobs(
171179
root: string,
172180
globs: string[],

src/project/types/book/book-config.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
repoUrlIcon,
6363
websiteConfigActions,
6464
websiteProjectConfig,
65+
websiteRepoInfoFromUrl,
6566
} from "../website/website-config.ts";
6667

6768
import { kSidebarLogo } from "../website/website-navigation.ts";
@@ -236,14 +237,21 @@ export async function bookProjectConfig(
236237

237238
function siteRepoUrl(site: Metadata) {
238239
const repoUrl = site[kSiteRepoUrl] as string;
240+
const branch = site[kSiteRepoBranch] || "main";
239241
if (site[kSiteRepoSubdir]) {
240242
const subdir = ensureTrailingSlash(site[kSiteRepoSubdir] as string);
241-
const branch = site[kSiteRepoBranch] || "main";
242243
return pathWithForwardSlashes(
243244
join(repoUrl, `tree/${branch}/${subdir}`),
244245
);
245246
} else {
246-
return repoUrl;
247+
const repoInfo = websiteRepoInfoFromUrl(repoUrl);
248+
if (repoInfo.path) {
249+
return pathWithForwardSlashes(
250+
join(repoInfo.baseUrl, `tree/${branch}`, repoInfo.path),
251+
);
252+
} else {
253+
return repoInfo.baseUrl;
254+
}
247255
}
248256
}
249257

src/project/types/website/website-config.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ import {
3737
kSiteUrl,
3838
kWebsite,
3939
} from "./website-constants.ts";
40-
import { ensureTrailingSlash } from "../../../core/path.ts";
40+
import {
41+
ensureTrailingSlash,
42+
removeTrailingSlash,
43+
} from "../../../core/path.ts";
4144
type WebsiteConfigKey =
4245
| "title"
4346
| "image"
@@ -194,25 +197,28 @@ export function websiteRepoInfo(
194197
path: ensureTrailingSlash(repoSubdir),
195198
};
196199
} else {
197-
// extract into base and path
198-
const match = repoUrl.match(/(https?:\/\/(?:[^\/]+\/){3})(.*)/);
199-
if (match) {
200-
return {
201-
baseUrl: match[1],
202-
path: ensureTrailingSlash(match[2]) || "",
203-
};
204-
} else {
205-
return {
206-
baseUrl: repoUrl,
207-
path: "",
208-
};
209-
}
200+
return websiteRepoInfoFromUrl(repoUrl);
210201
}
211202
} else {
212203
return undefined;
213204
}
214205
}
215206

207+
export function websiteRepoInfoFromUrl(repoUrl: string) {
208+
const match = repoUrl.match(/(https?:\/\/(?:[^\/]+\/){3})(.*)/);
209+
if (match) {
210+
return {
211+
baseUrl: match[1],
212+
path: ensureTrailingSlash(match[2]) || "",
213+
};
214+
} else {
215+
return {
216+
baseUrl: repoUrl,
217+
path: "",
218+
};
219+
}
220+
}
221+
216222
export function websiteRepoBranch(project?: ProjectConfig): string {
217223
return websiteConfigString(kSiteRepoBranch, project) || "main";
218224
}

0 commit comments

Comments
 (0)