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

Commit

Permalink
build: fix isPost, date fields etc
Browse files Browse the repository at this point in the history
  • Loading branch information
CanRau committed Apr 5, 2020
1 parent f0dee72 commit dbec131
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
9 changes: 6 additions & 3 deletions gatsby/createPages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ const { resolve } = require(`path`)
const {
// isHomePage,
isPage,
isPost,
notIsErrorPage,
prepareSortedTuple,
sortEnglishLast,
} = require(`./helpers`)

module.exports = store => ({ actions, getNodes, graphql }) => {
module.exports = store => ({ actions, getNodes }) => {
const { redirects } = store
const { createPage, createNodeField } = actions

// setup mappings
const allNodes = getNodes()
const Languages = allNodes.filter(x => x.internal.type === `LanguagesAml`)
const PagesAndPosts = allNodes.filter(isPage)
// const PagesAndPosts = allNodes.filter(R.either(isPage, isPost))
const PagesAndPosts = allNodes.filter(node => isPage(node) || isPost(node))

prepareSortedTuple(PagesAndPosts).forEach((group, index) =>
prepareSortedTuple(PagesAndPosts).forEach(group =>
sortEnglishLast(group).forEach((node, _, array) => {
// if (node.frontmatter.slug === `welcome-to-gaiamas-blog`) console.log(node)
const { lang, url, slug } = node.fields
const { layout, shortId, shortlink, oldId, oldSlug } = node.frontmatter

Expand Down
27 changes: 14 additions & 13 deletions gatsby/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ const R = require(`ramda`)
const { shuffle } = require(`lodash`)
const speakingUrl = require(`speakingurl`)

const isPost = R.both(
R.pathEq([`frontmatter`, `published`], true),
R.pathEq([`frontmatter`, `layout`], `BlogPost`)
)
const isPublished = node => node.frontmatter.isPublished === true

const isPageLayout = R.compose(
R.endsWith(`Page`),
R.pathOr(`N/A`, [`frontmatter`, `layout`])
)
const isPost = node =>
node.frontmatter &&
isPublished(node) &&
node.frontmatter.layout === `BlogPost` &&
typeof node.fileAbsolutePath === `string` &&
node.fileAbsolutePath.includes(`gaiama.org_content-feder`)

const isPage = R.both(
R.pathEq([`frontmatter`, `published`], true),
isPageLayout
)
const isPageLayout = node =>
node.frontmatter &&
typeof node.frontmatter.layout === `string` &&
node.frontmatter.layout.endsWith(`Page`)

const isPage = node => isPageLayout(node) && isPublished(node)

const isPageOrPost = R.anyPass([isPage, isPost])
const isPageOrPost = node => isPage(node) || isPost(node)

const getNLast = n => R.compose(R.head, R.takeLast(n))

Expand Down
46 changes: 34 additions & 12 deletions gatsby/onCreateNode.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,47 @@
const moment = require(`moment`)
const { parseISO, format, formatISO } = require(`date-fns`)
const { enUS, de } = require(`date-fns/locale/`)
const { getSlug, getUrl, isPage, isPost } = require(`./helpers`)

module.exports = store => async ({ node, actions }) => {
if (!isPage(node)) return
const dateLocales = {
en: enUS,
de: de,
}

module.exports = store => ({ node, actions }) => {
if (!isPage(node) && !isPost(node)) return
const { createNodeField } = actions
const { date, lang } = node.frontmatter
const nodeDate = moment(date, `Y-MM-DD`)
// 2020-03-31 23:21:33.065829
const nodeDate = moment(date, `Y-MM-DD`, true)
// const nodeDate = isPage(node)
// ? moment(date, `Y-MM-DD`, true)
// : moment(date, `Y-MM-DD HH:mm:ss`, true)

if (!nodeDate.isValid()) {
console.log(
`invalid date`,
node.frontmatter.slug || node.frontmatter.title || node
)
}

const addField = (name, value) =>
createNodeField({
node,
name,
value,
})
const addField = (name, value) => createNodeField({ node, name, value })

addField(`type`, isPost(node) ? `post` : `page`)
addField(`slug`, getSlug(node))
addField(`url`, getUrl(node))
addField(`lang`, lang)
addField(`dateTime`, nodeDate.toISOString())
addField(`dateStr`, nodeDate.format(`Y-MM-DD`))
addField(`dateStrLocalized`, nodeDate.locale(lang).format(`DD MMM Y`))
if (date) {
addField(`dateTime`, nodeDate.toISOString())
addField(`dateStr`, nodeDate.format(`Y-MM-DD`))
addField(`dateStrLocalized`, nodeDate.locale(lang).format(`DD MMM Y`))
} else {
const nodePublished = parseISO(node.frontmatter.published)
addField(`dateTime`, formatISO(nodePublished))
addField(`dateStr`, format(nodePublished, `yyyy-MM-dd`))
addField(
`dateStrLocalized`,
format(nodePublished, `do MMM yyyy`, { locale: dateLocales[lang] })
)
}
}

0 comments on commit dbec131

Please sign in to comment.