Skip to content

Commit 0d1f46d

Browse files
feat(lib/dev-hub-url): allow overriding dev hub & doc base URLs
- Rename `generateDevHubHref` to `getDevHubUrl` and move to `src/lib/dev-hub-url.js` - Rename `generateDocsHref` to `getDocUrl` and move to `src/lib/dev-hub-url.js` - Expose public `setDevHubBaseUrl` so that consumers of this project may change the base URL used for dev hub & doc links. Fixes #1579
1 parent ce16294 commit 0d1f46d

File tree

2 files changed

+43
-42
lines changed

2 files changed

+43
-42
lines changed

src/lib/dev-hub-url.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* If you change the base URL here, you should probably also change it in places where it's still hard coded:
3+
* - README.md,
4+
* - CONTRIBUTING.md,
5+
* - sandbox/index.html,
6+
* - test/utils.test.js.
7+
*/
8+
let devHubBaseUrl = 'https://www.clever.cloud/developers';
9+
10+
/** @param {string} value */
11+
export function setDevHubBaseUrl(value) {
12+
devHubBaseUrl = value.replace(/\/$/, '');
13+
}
14+
15+
/**
16+
* Rely on this helper for every reference to the docs website
17+
*
18+
* @param {string} [path]
19+
* @returns {string} href
20+
*/
21+
export function getDocUrl(path = '') {
22+
const docsBaseUrl = devHubBaseUrl + '/doc';
23+
24+
if (path === '') {
25+
return docsBaseUrl;
26+
}
27+
28+
return path.startsWith('/') ? `${docsBaseUrl}${path}` : `${docsBaseUrl}/${path}`;
29+
}
30+
31+
/**
32+
* Rely on this helper for every reference to the developer hub website
33+
*
34+
* @param {string} path
35+
* @returns {string}
36+
*/
37+
export function getDevHubUrl(path = '') {
38+
if (path === '') {
39+
return devHubBaseUrl;
40+
}
41+
42+
return path.startsWith('/') ? `${devHubBaseUrl}${path}` : `${devHubBaseUrl}/${path}`;
43+
}

src/lib/utils.js

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
/**
2-
* If you change the base URL here, you should probably also change it in places where it's still hard coded:
3-
* - README.md,
4-
* - CONTRIBUTING.md,
5-
* - sandbox/index.html,
6-
* - test/utils.test.js.
7-
*/
8-
const CC_DEV_HUB_BASE_URL = 'https://www.clever.cloud/developers/';
9-
101
/**
112
* @param {Record<string, any>} a
123
* @param {Record<string, any>} b
@@ -256,39 +247,6 @@ export function sleep(delay) {
256247
});
257248
}
258249

259-
/**
260-
* Rely on this helper for every reference to the Clever Cloud docs website
261-
*
262-
* @param {string} [path]
263-
* @returns {string} href
264-
*/
265-
export function generateDocsHref(path = '') {
266-
/**
267-
* Ensure "/doc" is appended after the base URL, then append the provided path
268-
* If you change the base URL here, you should probably also change it in places where it's still hard coded:
269-
* - README.md,
270-
* - CONTRIBUTING.md,
271-
* - sandbox/index.html,
272-
* - test/utils.test.js.
273-
*/
274-
const CC_DOCS_BASE_URL = CC_DEV_HUB_BASE_URL.replace(/\/$/, '') + '/doc/';
275-
// if a '/' is present at the beginning, the path is considered absolute and it is appended right after the origin
276-
// we want the path to always be appended after the existing path of the base URL so we remove the first '/' so that it's considered relative
277-
return new URL(path.replace(/^\//, ''), CC_DOCS_BASE_URL).href;
278-
}
279-
280-
/**
281-
* Rely on this helper for every reference to the Clever Cloud developer hub website
282-
*
283-
* @param {string} [path]
284-
* @returns {string} href
285-
*/
286-
export function generateDevHubHref(path = '') {
287-
// if a '/' is present at the beginning, the path is considered absolute and it is appended right after the origin
288-
// we want the path to always be appended after the existing path of the base URL so we remove the first '/' so that it's considered relative
289-
return new URL(path.replace(/^\//, ''), CC_DEV_HUB_BASE_URL).href;
290-
}
291-
292250
/**
293251
* Checks if a given DOM element is currently visible within a given container's bounds.
294252
*

0 commit comments

Comments
 (0)