fix(path): correct handle URI schemes in joinSegments

In Head component, joinSegments is used to build a socialUrl with
(`https://${baseUrl}`, slug), which causes a slash to be lost in the result.

For example, joinSegments("https://quartz.jzhao.xyz", "layout") will return
"https:/quartz.jzhao.xyz/layout".

The result is then used in og:url and twitter:url in <head><meta>.

This fix tries to preserve each segment, but removes all leading and trailing
slashes. In addition, for the first segment, the leading slash is only
de-duplicated, not fully trimmed.
This commit is contained in:
moexiami 2024-12-30 19:37:35 +08:00
parent 7d4bed64a9
commit 328a1cfb57
No known key found for this signature in database
GPG Key ID: 53E2BDD8BD17882C

View File

@ -185,8 +185,13 @@ export function slugTag(tag: string) {
export function joinSegments(...args: string[]): string {
return args
.filter((segment) => segment !== "")
.map((segment, index) =>
index === 0
? // Deduplicate but not remove leading slashes for first segment
segment.replace(/\/+$/g, "").replace(/^\/\/+/g, "/")
: segment.replace(/^\/+|\/+$/g, ""),
)
.join("/")
.replace(/\/\/+/g, "/")
}
export function getAllSegmentPrefixes(tags: string): string[] {