1 | import { match } from "@reach/router/lib/utils"
|
2 | import stripPrefix from "./strip-prefix"
|
3 | import normalizePagePath from "./normalize-page-path"
|
4 |
|
5 | let matchPaths = []
|
6 |
|
7 | const trimPathname = rawPathname => {
|
8 | let pathname = decodeURIComponent(rawPathname)
|
9 |
|
10 | let trimmedPathname = stripPrefix(pathname, __BASE_PATH__)
|
11 |
|
12 | .split(`#`)[0]
|
13 |
|
14 | .split(`?`)[0]
|
15 |
|
16 | return trimmedPathname
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export const setMatchPaths = value => {
|
25 | matchPaths = value
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | export const findMatchPath = rawPathname => {
|
37 | const trimmedPathname = cleanPath(rawPathname)
|
38 |
|
39 | for (const { matchPath, path } of matchPaths) {
|
40 | if (match(matchPath, trimmedPathname)) {
|
41 | return normalizePagePath(path)
|
42 | }
|
43 | }
|
44 |
|
45 | return null
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | export const cleanPath = rawPathname => {
|
56 | const trimmedPathname = trimPathname(rawPathname)
|
57 |
|
58 | let foundPath = trimmedPathname
|
59 | if (foundPath === `/index.html`) {
|
60 | foundPath = `/`
|
61 | }
|
62 |
|
63 | foundPath = normalizePagePath(foundPath)
|
64 |
|
65 | return foundPath
|
66 | }
|