-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathhref.ts
More file actions
23 lines (22 loc) · 818 Bytes
/
Copy pathhref.ts
File metadata and controls
23 lines (22 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* Prefix a root-absolute path (starting with `/`) with the configured base
* path so internal links work when the site is served from a subpath
* (e.g. GitHub Pages project sites). External URLs and fragments pass through.
*/
export function withBase(path: string): string {
if (!path.startsWith("/")) {
return path;
}
return `${import.meta.env.BASE_URL}${path}`.replaceAll("//", "/");
}
/**
* Strip the leading version segment from a doc id, so the remaining path
* can be re-rooted under a different version (e.g. pointing the current
* page at `/docs/latest/...` or building the upstream edit path).
*
* "v5.9.x/Reference/Errors" → "Reference/Errors"
* "latest" → ""
*/
export function afterVersion(docId: string): string {
return docId.replace(/^[^/]+(\/|$)/, "");
}