forked from solidjs/solid-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.ts
More file actions
38 lines (29 loc) · 1.06 KB
/
helpers.ts
File metadata and controls
38 lines (29 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { useLocation, useMatch } from "@solidjs/router";
import { SUPPORTED_LOCALES } from "./config";
import { useCurrentRouteMetaData } from "~/utils/route-metadata-helper";
export function getLocaleFromPathname(pathname: string) {
return pathname.split("/")[1];
}
export function isValidLocale(
locale: string
): locale is (typeof SUPPORTED_LOCALES)[number] {
// TS is being annoying.
// we are actually narrowing string here.
// @ts-ignore
return SUPPORTED_LOCALES.includes(locale);
}
export function getValidLocaleFromPathname(pathname: string) {
const locale = getLocaleFromPathname(pathname);
return isValidLocale(locale) ? locale : null;
}
export function getEntryFileName() {
const pathname = useLocation().pathname;
const currentRouteMetaData = useCurrentRouteMetaData();
if (currentRouteMetaData.isProjectRoot) {
return `${pathname}/index.mdx`.replace("//", "/");
} else {
// Trim trailing slash
return (pathname.endsWith("/") ? pathname.slice(0, -1) : pathname) + ".mdx";
}
}
export const isExternalURL = (url: string) => /^https?:\/\//.test(url);