Skip to content

Commit 7cd7a8f

Browse files
author
Taylor Payne
committed
feat: allow customization of external URLs
Create the getExternalLinkUrl function and make it available for use in frontend apps. It checks for a `customExternalUrls` object in the config to see if a custom URL has been provided for a given URL. Use getExternalLinkUrl in the example app for this package. One link is overridden with a custom URL while the other is not.
1 parent 6aebf3d commit 7cd7a8f

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

env.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// Also note that in an actual application this file would be added to .gitignore.
44
const config = {
55
JS_FILE_VAR: 'JS_FILE_VAR_VALUE_FOR_EXAMPLE_APP',
6+
externalLinkUrlOverrides: {
7+
"https://github.com/openedx/docs.openedx.org/": "https://docs.openedx.org/",
8+
}
69
};
710

811
export default config;

example/ExamplePage.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { Link } from 'react-router-dom';
55
import { injectIntl, useIntl } from '@edx/frontend-platform/i18n';
66
import { logInfo } from '@edx/frontend-platform/logging';
77
import { AppContext } from '@edx/frontend-platform/react';
8-
import { ensureConfig, mergeConfig, getConfig } from '@edx/frontend-platform';
8+
import {
9+
ensureConfig, mergeConfig, getConfig, getExternalLinkUrl,
10+
} from '@edx/frontend-platform';
911
/* eslint-enable import/no-extraneous-dependencies */
1012
import messages from './messages';
1113

@@ -49,6 +51,8 @@ function ExamplePage() {
4951
<AuthenticatedUser />
5052
<p>EXAMPLE_VAR env var came through: <strong>{getConfig().EXAMPLE_VAR}</strong></p>
5153
<p>JS_FILE_VAR var came through: <strong>{getConfig().JS_FILE_VAR}</strong></p>
54+
<p>External link to <a href={getExternalLinkUrl('https://github.com/openedx/docs.openedx.org/')}>Open edX docs</a> (customized link).</p>
55+
<p>External link to <a href={getExternalLinkUrl('https://open-edx-proposals.readthedocs.io/en/latest/')}>Open edX OEPs</a> (non-customized link).</p>
5256
<p>Visit <Link to="/authenticated">authenticated page</Link>.</p>
5357
<p>Visit <Link to="/error_example">error page</Link>.</p>
5458
</div>

src/config.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,34 @@ export function ensureConfig(keys, requester = 'unspecified application code') {
307307
});
308308
}
309309

310+
/**
311+
* Get an external link URL based on the URL provided. If the passed in URL is overriden in the
312+
* `externalLinkUrlOverrides` object, it will return the overridden URL. Otherwise, it will return
313+
* the provided URL.
314+
*
315+
*
316+
* @param {string} url - The default URL.
317+
* @returns {string} - The external link URL. Defaults to the input URL if not found in the
318+
* `externalLinkUrlOverrides` object. If the input URL is invalid, '#' is returned.
319+
*
320+
* @example
321+
* import { getExternalLinkUrl } from '@edx/frontend-platform';
322+
*
323+
* <Hyperlink
324+
* destination={getExternalLinkUrl(data.helpLink)}
325+
* target="_blank"
326+
* >
327+
*/
328+
export function getExternalLinkUrl(url) {
329+
// Guard against non-strings or whitespace-only strings
330+
if (typeof url !== 'string' || !url.trim()) {
331+
return '#';
332+
}
333+
334+
const overriddenLinkUrls = getConfig().externalLinkUrlOverrides || {};
335+
return overriddenLinkUrls[url] || url;
336+
}
337+
310338
/**
311339
* An object describing the current application configuration.
312340
*

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export {
3737
setConfig,
3838
mergeConfig,
3939
ensureConfig,
40+
getExternalLinkUrl,
4041
} from './config';
4142
export {
4243
initializeMockApp,

0 commit comments

Comments
 (0)