Skip to content

Commit 9b23d76

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.
1 parent 9f92281 commit 9b23d76

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/config.js

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

310+
/**
311+
* Get a custom link for an external URL, if it exists. If it does not exist, the original URL
312+
* is returned. Will look for a custom URL mapping in the `customExternalUrls` object in the config.
313+
*
314+
*
315+
* @param {string} url - The default URL.
316+
* @returns {string} - The external link URL. Defaults to the input URL if not found in the
317+
* custom external URLs. If the input URL is invalid, a warning is logged and '#' is returned.
318+
*
319+
* @example
320+
* import { getExternalLinkUrl } from '@edx/frontend-platform';
321+
*
322+
* <Hyperlink
323+
* destination={getExternalLinkUrl(data.helpLink)}
324+
* target="_blank"
325+
* >
326+
*/
327+
export function getExternalLinkUrl(url) {
328+
// Guard against non-strings or whitespace-only strings
329+
if (typeof url !== 'string' || !url.trim()) {
330+
return '#';
331+
}
332+
333+
const customUrls = getConfig().customExternalUrls || {};
334+
return customUrls[url] || url;
335+
}
336+
310337
/**
311338
* An object describing the current application configuration.
312339
*

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)