Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
feat(slug): add speakingurl, auto generate slug & new url field
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `slug` will now only contain the slugified title (no path)
add `url` field, constructed of language, possible `/blog/` prefix and the `slug`
  • Loading branch information
CanRau committed Oct 6, 2018
1 parent a325b75 commit b8cd7f5
Show file tree
Hide file tree
Showing 19 changed files with 115 additions and 40 deletions.
55 changes: 36 additions & 19 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { resolve, join } = require(`path`)
const { writeFileSync } = require(`fs`)
const { homepage } = require(`./package.json`)
const moment = require(`moment`)
const { compareAsc, parse: parseDate } = require(`date-fns`)
const mkDir = require(`make-dir`)
const { Feed } = require(`feed`)
const { shuffle } = require(`lodash`)
const R = require(`ramda`)
const speakingUrl = require(`speakingurl`)
const { homepage } = require(`./package.json`)
const { redirects } = require(`./redirects.js`)
// const dotenv = require(`dotenv`).config({
Expand Down Expand Up @@ -60,7 +60,26 @@ const prepareSortedTuple = R.compose(
sortByDirectory
)
const sortEnglishLast = R.sortBy(R.path([`frontmatter`, `lang`]))
const notIsErrorPage = node => node.frontmatter.is !== `ErrorPage`
const notIsErrorPage = node => node.frontmatter.layout !== `ErrorPage`
const isHomePage = node => node.frontmatter.layout === `HomePage`
// TODO: maybe use slugify?
const getSlug = node =>
speakingUrl(node.frontmatter.slug || node.frontmatter.title, {
lang: node.frontmatter.lang,
})
const getUrl = node =>
[
``,
node.frontmatter.lang,
isPost(node) ? `blog` : null,
isHomePage(node) ? null : getSlug(node),
``,
]
.filter(x => x !== null)
.join(`/`)
// `/${node.frontmatter.lang}${isPost(node) && `/blog`}/${speakingUrl(node.frontmatter.title, {
// node.frontmatter.lang,
// })}`

exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions
Expand All @@ -74,7 +93,9 @@ exports.onCreateNode = ({ node, actions }) => {
})

addNodeField(`type`, isPost(node) ? `post` : `page`)
addNodeField(`slug`, node.frontmatter.slug)
// strip language from slug field
addNodeField(`slug`, getSlug(node))
addNodeField(`url`, getUrl(node))
addNodeField(`lang`, node.frontmatter.lang)
addNodeField(`dateTime`, theMoment.toISOString())
addNodeField(`dateStr`, theMoment.format(`Y-MM-DD`))
Expand All @@ -97,28 +118,24 @@ exports.createPages = async ({ actions, getNodes, graphql }) => {

prepareSortedTuple(PagesAndPosts).forEach((group, index) =>
sortEnglishLast(group).forEach((node, _, array) => {
const { lang, slug } = node.fields
const { lang, url, slug } = node.fields
const { layout, shortId, shortlink, oldId, oldSlug } = node.frontmatter

createPage({
path: node.fields.slug,
component: resolve(`./src/templates/${layout}.js`),
context: {
slug: node.fields.slug,
lang: node.fields.lang,
},
})
console.log(`${slug} => ${url}`)

const component = resolve(`./src/templates/${layout}.js`)
const context = { url, slug, lang }

createPage({ component, path: url, context })

// create root 404
if (layout === `404` && node.fields.lang === `en`) {
slug === `404` &&
lang === `en` &&
createPage({
component,
path: `/404`,
component: resolve(`./src/templates/${layout}.js`),
context: {
slug: node.fields.slug,
lang: node.fields.lang,
},
context,
})
}

createNodeField({
node,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"rehype-react": "^3.0.3",
"slash": "^2.0.0",
"slugify": "^1.2.9",
"speakingurl": "^14.0.1",
"typeface-amatic-sc": "^0.0.54",
"typeface-quicksand": "^0.0.54",
"typography": "^0.16.17",
Expand Down
4 changes: 2 additions & 2 deletions src/components/ArticlePreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { colors } from '@/theme'

const ArticlePreview = ({ article, isVisible, ...props }) => (
<article css={articleStyles.article(isVisible)} {...props}>
<Link to={article.fields.slug}>
<Link to={article.fields.url}>
{article.frontmatter.cover && (
<Img fluid={article.frontmatter.cover.childImageSharp.fluid} />
)}
Expand All @@ -23,7 +23,7 @@ const ArticlePreview = ({ article, isVisible, ...props }) => (
<footer css={articleStyles.footer}>
<div css={articleStyles.footerInner}>
<time css={articleStyles.time}>{article.fields.dateStrLocalized}</time>
<Link to={article.fields.slug}>{`read more`}</Link>
<Link to={article.fields.url}>{`read more`}</Link>
</div>
</footer>
</article>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Footer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ const Footer = ({
{legal.length &&
legal.map(({ node }) => (
<Link
to={node.frontmatter.slug}
key={node.frontmatter.slug}
to={node.fields.url}
key={node.fields.url}
css={{ marginRight: `.5rem` }}
>
{node.frontmatter.title}
Expand Down
8 changes: 5 additions & 3 deletions src/components/Header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ class Header extends Component {
<div css={style.headerTop}>
<div css={style.topInner}>
<div css={style.headerBrand}>
<Link to={homepage.slug} css={style.headerLink}>
<Link to={homepage.fields.url} css={style.headerLink}>
<h2>
{homepage.header.title}
{homepage.header.subtitle && ` - ${homepage.header.subtitle}`}
{homepage.frontmatter.header.title}
{homepage.frontmatter.header.subtitle &&
` - ${homepage.frontmatter.header.subtitle}`}
</h2>
</Link>
</div>

{/* TODO: remember lang in local FORAGE? to show correct in 404 */}
<nav css={style.headerMeta}>
{meta.map((link, index) => (
<div
Expand Down
11 changes: 9 additions & 2 deletions src/components/MainLayout/fragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ export const Fragments = graphql`
) {
edges {
node {
fields {
url
}
frontmatter {
title
titleShort
menu
slug
lang
#icon
}
}
}
Expand All @@ -106,6 +108,9 @@ export const Fragments = graphql`
homepage: javascriptFrontmatter(
frontmatter: { lang: { eq: $lang }, layout: { eq: "HomePage" } }
) {
fields {
url
}
frontmatter {
lang
title
Expand All @@ -124,9 +129,11 @@ export const Fragments = graphql`
) {
edges {
node {
fields {
url
}
frontmatter {
lang
slug
title
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/components/MainLayout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ const generateMainMenu = items =>
.map(({ node }) => ({
title: node.frontmatter.title,
titleShort: node.frontmatter.titleShort,
to: node.frontmatter.slug,
to: node.fields.url,
}))

const generateMetaMenu = ({ translations, getLang, menuItems }) =>
translations
.map(x => ({
...getLang(x.frontmatter.lang),
to: x.frontmatter.slug,
to: x.fields.url,
}))
.concat(
menuItems
Expand All @@ -64,7 +64,7 @@ const generateMetaMenu = ({ translations, getLang, menuItems }) =>
title: node.frontmatter.title,
titleShort: node.frontmatter.titleShort,
icon: node.frontmatter.icon,
to: node.frontmatter.slug,
to: node.fields.url,
}))
)

Expand All @@ -76,6 +76,9 @@ const getTranslations = page =>
[
...(page.fields.translations || []),
{
fields: {
url: page.fields.url,
},
frontmatter: {
lang: page.frontmatter.lang,
slug: page.frontmatter.slug,
Expand Down Expand Up @@ -216,7 +219,7 @@ class MainLayout extends Component {
<meta property="og:site_name" content={site.siteMetadata.title} />
<meta
property="og:url"
content={`${site.siteMetadata.siteUrl}${page.frontmatter.slug}`}
content={`${site.siteMetadata.siteUrl}${page.fields.url}`}
/>
<meta
property="og:locale"
Expand Down Expand Up @@ -289,7 +292,7 @@ class MainLayout extends Component {
/>
)} */}
<html lang={lang} />
{/* <body className={slugify(page.frontmatter.slug)} /> */}
{/* <body className={slugify(page.fields.url)} /> */}
</Helmet>

<a href="#main" css={screenReaderAndFocusable}>
Expand All @@ -304,7 +307,7 @@ class MainLayout extends Component {
)}

<Header
homepage={homepage.frontmatter}
homepage={homepage}
meta={metaMenu}
menu={mainMenu}
logo={SiteMeta.frontmatter.assets.logo.image}
Expand Down
6 changes: 3 additions & 3 deletions src/components/Newsticker/NewstickerItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ const NewstickerItem = ({ item, readmoreLabel, layout, ...props }) => {
}}
>
{coverImage && (
<Link to={item.frontmatter.slug} css={styles.imageWrapper(layout)}>
<Link to={item.fields.url} css={styles.imageWrapper(layout)}>
<Img fluid={coverImage} css={styles.image} />
</Link>
)}

<div>
<h2 css={styles.title(layout)}>
<Link to={item.frontmatter.slug}>{item.frontmatter.title}</Link>
<Link to={item.fields.url}>{item.frontmatter.title}</Link>
</h2>

<p css={styles.excerpt}>
{item.excerpt || item.frontmatter.summary}

{readmoreLabel && (
<Link to={item.frontmatter.slug} css={styles.readmoreLink}>
<Link to={item.fields.url} css={styles.readmoreLink}>
{readmoreLabel}
</Link>
)}
Expand Down
1 change: 1 addition & 0 deletions src/components/Newsticker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export const fragment = graphql`
html
fields {
lang
url
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/templates/AboutPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ export const query = graphql`
page: javascriptFrontmatter(frontmatter: { slug: { eq: $slug } }) {
fields {
url
translations {
fields {
url
}
frontmatter {
title
lang
Expand Down
7 changes: 6 additions & 1 deletion src/templates/BlogPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const BlogPage = props => {
</Link>

{filter && (
<Link to={props.data.page.frontmatter.slug}>
<Link to={props.data.page.fields.url}>
{props.data.page.frontmatter.sortLabels.all}
</Link>
)}
Expand Down Expand Up @@ -225,7 +225,11 @@ export const query = graphql`
page: javascriptFrontmatter(frontmatter: { slug: { eq: $slug } }) {
fields {
url
translations {
fields {
url
}
frontmatter {
title
lang
Expand Down Expand Up @@ -299,6 +303,7 @@ export const query = graphql`
}
}
fields {
url
slug
lang
dateTime
Expand Down
7 changes: 7 additions & 0 deletions src/templates/BlogPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ export const query = graphql`
dateTime
dateStr
dateStrLocalized
url
translations {
fields {
url
}
frontmatter {
title
lang
Expand All @@ -243,6 +247,9 @@ export const query = graphql`
}
suggested {
excerpt(pruneLength: 135)
fields {
url
}
frontmatter {
id
title
Expand Down
4 changes: 4 additions & 0 deletions src/templates/ContactPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ export const query = graphql`
page: javascriptFrontmatter(frontmatter: { slug: { eq: $slug } }) {
fields {
url
translations {
fields {
url
}
frontmatter {
title
lang
Expand Down
4 changes: 4 additions & 0 deletions src/templates/DonatePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ export const query = graphql`
page: javascriptFrontmatter(frontmatter: { slug: { eq: $slug } }) {
fields {
url
translations {
fields {
url
}
frontmatter {
title
lang
Expand Down
4 changes: 4 additions & 0 deletions src/templates/GalleryPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@

// page: javascriptFrontmatter(frontmatter: { slug: { eq: $slug } }) {
// fields {
// url
// translations {
// fields {
// url
// }
// frontmatter {
// title
// lang
Expand Down
Loading

0 comments on commit b8cd7f5

Please sign in to comment.