This repository was archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathroutes.js
65 lines (59 loc) · 1.87 KB
/
routes.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import path from "path";
import url from "url";
import { LINK_DESTINATIONS } from "constants/routes";
import {
buildRoute,
buildPathFromFile,
normalizeRoute,
} from "../../buildHelpers/routes";
export { buildRoute, buildPathFromFile, normalizeRoute };
/**
* Check if a link should be treated as relative or not.
* @param {string} href An unprocessed link destination that might be relative.
* Some examples:
* ./something
* ../something
* /something
* something
* https://example.com/something
* This will treat urls like `example.com/something` as relative, but I can't
* find a method to parse URLs without a protocol. This should work currently,
* since all external URLs look to have a protocol, but any incorrect URLs added
* in the future will render as `developers.stellar.org/example.com/something`.
* @return {boolean} Whether it's relative or not
*/
export const isRelativeUrl = (href) => {
const hrefObj = url.parse(href);
if (!href) {
return false;
}
if (hrefObj.host) {
return false;
}
if (hrefObj.pathname?.startsWith("/")) {
return false;
}
return true;
};
export const isHashUrl = (href) => href.startsWith("#");
/**
* resolveRelativeUrl takes a file path and a path relative to it, and returns a
* well-formed URL.
* @param {string} originalFile The original file path that the page was
* generated from. This should end in .mdx.
* @param {string} relativeUrl A relative path.
* @return {string} The final URL, ready for use in an <a> tag.
*/
export const resolveRelativePath = (originalFile, relativeUrl) =>
buildPathFromFile(
path.resolve(normalizeRoute(path.dirname(originalFile)), relativeUrl),
);
export const getLinkTarget = (link) => {
if (link.startsWith("/api")) {
return LINK_DESTINATIONS.api;
}
if (link.startsWith("/docs")) {
return LINK_DESTINATIONS.docs;
}
return LINK_DESTINATIONS.external;
};