-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #444 from MetaMask/feat/715-allow-web3-in-slug
fix: web3 slug
- Loading branch information
Showing
1 changed file
with
19 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,34 @@ | ||
const { kebabCase } = require('lodash') | ||
|
||
/** | ||
* Custom kebab case function that preserves "web3" | ||
* @param {string} string | ||
* @returns {string} | ||
*/ | ||
function customKebabCase(string) { | ||
// Apply lodash's kebabCase | ||
string = kebabCase(string) | ||
|
||
// Restore "web3" | ||
return string.replace(/web-3/gi, 'web3') | ||
} | ||
|
||
/** | ||
* | ||
* @function | ||
* | ||
* @name getNewsUrl | ||
* | ||
* @param {Object} news | ||
* | ||
* @return {String} final news url | ||
*/ | ||
let getNewsUrl = news => { | ||
let category = 'Uncategorized' | ||
const slug = news?.slug | ||
? kebabCase(news.slug) | ||
: kebabCase(news.title.toLowerCase()) | ||
? customKebabCase(news.slug) | ||
: customKebabCase(news.title.toLowerCase()) | ||
|
||
if (news.categories && news.categories.length) | ||
category = news.categories[0].slug | ||
return `/news/${kebabCase(category)}/${slug}/` | ||
|
||
return `/news/${customKebabCase(category)}/${slug}/` | ||
} | ||
|
||
module.exports.getNewsUrl = getNewsUrl |