Add slugify() util function

This commit is contained in:
Eric Jinks 2018-05-25 11:08:00 +10:00
parent 09c8975884
commit ef7a7fde9a
5 changed files with 20 additions and 8 deletions

View file

@ -1,3 +1,7 @@
## 0.2.6 - 2018-05-25
* Add slugify() util function
## 0.2.5 - 2018-05-25 ## 0.2.5 - 2018-05-25
* Upgrade CMS (1.8.2) * Upgrade CMS (1.8.2)

View file

@ -2,7 +2,6 @@ import React, { Component } from 'react'
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom' import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
import Helmet from 'react-helmet' import Helmet from 'react-helmet'
import _findIndex from 'lodash/findIndex' import _findIndex from 'lodash/findIndex'
import _kebabCase from 'lodash/kebabCase'
import ScrollToTop from './components/ScrollToTop' import ScrollToTop from './components/ScrollToTop'
import Meta from './components/Meta' import Meta from './components/Meta'
@ -18,6 +17,7 @@ import GithubCorner from './components/GithubCorner'
import ServiceWorkerNotifications from './components/ServiceWorkerNotifications' import ServiceWorkerNotifications from './components/ServiceWorkerNotifications'
import AOS from './components/AOS' import AOS from './components/AOS'
import data from './data.json' import data from './data.json'
import { slugify } from './util/url'
import { documentHasTerm, getCollectionTerms } from './util/collection' import { documentHasTerm, getCollectionTerms } from './util/collection'
class App extends Component { class App extends Component {
@ -142,7 +142,7 @@ class App extends Component {
const slug = props.match.params.slug const slug = props.match.params.slug
const singlePostID = _findIndex( const singlePostID = _findIndex(
posts, posts,
item => _kebabCase(item.title) === slug item => slugify(item.title) === slug
) )
const singlePost = posts[singlePostID] const singlePost = posts[singlePostID]
const nextPost = posts[singlePostID + 1] const nextPost = posts[singlePostID + 1]
@ -152,10 +152,10 @@ class App extends Component {
<SinglePost <SinglePost
singlePost={singlePost} singlePost={singlePost}
nextPostURL={ nextPostURL={
nextPost && `/blog/${_kebabCase(nextPost.title)}/` nextPost && `/blog/${slugify(nextPost.title)}/`
} }
prevPostURL={ prevPostURL={
prevPost && `/blog/${_kebabCase(prevPost.title)}/` prevPost && `/blog/${slugify(prevPost.title)}/`
} }
{...props} {...props}
/> />

View file

@ -1,13 +1,13 @@
import React from 'react' import React from 'react'
import { Link } from 'react-router-dom' import { Link } from 'react-router-dom'
import _kebabCase from 'lodash/kebabCase'
import { slugify } from '../util/url'
import BackgroundImage from './BackgroundImage' import BackgroundImage from './BackgroundImage'
import './PostCard.css' import './PostCard.css'
const PostCard = ({ postItem, className = '', ...props }) => ( const PostCard = ({ postItem, className = '', ...props }) => (
<Link <Link
to={`/blog/${_kebabCase(postItem.title)}/`} to={slugify(`/blog/${postItem.title}/`)}
className={`PostCard ${className}`} className={`PostCard ${className}`}
{...props} {...props}
> >

View file

@ -1,7 +1,7 @@
import React from 'react' import React from 'react'
import { NavLink } from 'react-router-dom' import { NavLink } from 'react-router-dom'
import _kebabCase from 'lodash/kebabCase'
import { slugify } from '../util/url'
import './PostCategoriesNav.css' import './PostCategoriesNav.css'
const PostCategoriesNav = ({ categories }) => ( const PostCategoriesNav = ({ categories }) => (
@ -14,7 +14,7 @@ const PostCategoriesNav = ({ categories }) => (
<NavLink <NavLink
className='NavLink' className='NavLink'
key={category.title + index} key={category.title + index}
to={`/blog/category/${_kebabCase(category.title)}/`} to={`/blog/category/${slugify(category.title)}/`}
> >
{category.title} {category.title}
</NavLink> </NavLink>

8
src/util/url.js Normal file
View file

@ -0,0 +1,8 @@
import _kebabCase from 'lodash/kebabCase'
export const slugify = (string = '') =>
// keeps forward slashes
string
.split('/')
.map(_kebabCase)
.join('/')