Skip to content

Commit

Permalink
Add new recipe template
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubkottnauer committed Apr 1, 2021
1 parent 5d2e55a commit cabd8b2
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 7 deletions.
24 changes: 17 additions & 7 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const remarkHTML = require('remark-html')
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions

const recipeTemplate = path.resolve(`src/templates/recipeTemplate.js`)
const recipeTemplate = path.resolve('src/templates/recipeTemplate.js')
const smallRecipesTemplate = path.resolve(
'src/templates/smallRecipesTemplate.js'
)

return graphql(`
{
Expand All @@ -24,6 +27,7 @@ exports.createPages = ({ actions, graphql }) => {
frontmatter {
title
source
layout
}
}
}
Expand All @@ -35,12 +39,16 @@ exports.createPages = ({ actions, graphql }) => {
}

result.data.allMarkdownRemark.edges.forEach(({ node }) => {
const { slug } = node.fields
createPage({
path: node.fields.slug,
component: recipeTemplate,
path: slug,
component:
node.frontmatter.layout === 'smallrecipes'
? smallRecipesTemplate
: recipeTemplate,
context: {
slug: node.fields.slug,
}, // additional data can be passed via context
slug,
},
})
})
})
Expand Down Expand Up @@ -101,11 +109,13 @@ exports.sourceNodes = ({ actions, getNodes, getNode }) => {
node.frontmatter.related instanceof Array
? node.frontmatter.related.map(getRecipeById)
: [getRecipeById(node.frontmatter.related)]

console.log(
node.frontmatter.title,
found.map((x) => x.frontmatter.title)
)
found
.filter((n) => n)
.map((n) => {
// if it's first time for this author init empty array for his books
if (!relatedRecipes[n.id]) {
relatedRecipes[n.id] = []
}
Expand Down
8 changes: 8 additions & 0 deletions recipes/asian-dipping-sauces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Asian Dipping Sauces
tags: sauce
lang: en
layout: smallrecipes
---

test
1 change: 1 addition & 0 deletions recipes/hainanese-chicken.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ title: Hainanese Chicken
tags: chicken
source: 'https://www.youtube.com/watch?v=Vsv8JKI6SwE'
image: hainanese-chicken.jpg
related: asian-dipping-sauces
---

- **1** chicken
Expand Down
189 changes: 189 additions & 0 deletions src/templates/smallRecipesTemplate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import React from 'react'
import { graphql, Link } from 'gatsby'
import { Helmet } from 'react-helmet'
import { remarkForm } from 'gatsby-tinacms-remark'
import { BsFillPeopleFill, BsLink45Deg, BsLink } from 'react-icons/bs'
import Layout from '../components/layout'

function Template({ data }) {
const { markdownRemark } = data
const { frontmatter, html, fields } = markdownRemark

return (
<Layout headerImage={fields.image ? fields.image : undefined}>
<Helmet title={frontmatter.title} />

<div className="recipe">
<h1>{frontmatter.title}</h1>
<div className="recipe-meta">
<div style={{ float: 'left' }}>
{frontmatter.source && (
<div className="section-with-icon">
<BsLink45Deg />
<a
className="text"
target="_blank"
href={frontmatter.source}
rel="noopener noreferrer"
>
original recipe: {frontmatter.source}
</a>
</div>
)}
{frontmatter.serves && (
<div className="section-with-icon">
<BsFillPeopleFill />
<span className="text">{frontmatter.serves}</span>
</div>
)}
{fields.relatedRecipes && (
<div className="section-with-icon">
<BsLink />
<span className="text">
{fields.relatedRecipes
.map((r) => (
<Link key={r.slug} to={r.slug}>
{r.title}
</Link>
))
.reduce((prev, curr) => [prev, ', ', curr])}
</span>
</div>
)}
</div>

<div style={{ float: 'right' }}>
{frontmatter.tags && (
<div>
{frontmatter.tags.split(' ').map((tag) => (
<span className="tag">{tag}</span>
))}
</div>
)}
</div>
</div>
<Recipe text={html} ingredients={fields.ingredients} />
</div>
</Layout>
)
}

export const recipeForm = {
fields: [
{
label: 'Name',
name: 'frontmatter.title',
description:
'Something short but descriptive enougg, e.g. "Chicken Curry", "Sweet and Sour Chicken"',
component: 'text',
required: true,
},
{
label: 'Date',
name: 'rawFrontmatter.date',
component: 'date',
dateFormat: 'YYYY-MM-DD',
timeFormat: false,
description: 'Date when the recipe was created, YYYY-MM-DD',
},
{
name: 'frontmatter.lang',
component: 'select',
label: 'Language',
description: 'Language of the recipe',
options: ['en', 'cz', 'se'],
},
{
label: 'Serves',
name: 'frontmatter.serves',
component: 'text',
},
{
label: 'Related recipes',
name: 'frontmatter.related',
description: 'Space-separated list of filenames of related recipes',
component: 'text',
},
{
label: 'Source',
name: 'frontmatter.source',
component: 'text',
description: 'URL of the original video/article',
},
{
label: 'Tags',
name: 'frontmatter.tags',
component: 'text',
description: 'Space-separated list of tags',
},
{
label: 'Ingredients',
name: 'rawFrontmatter.ingredients',
component: 'markdown',
},
{
label: 'Text',
name: 'rawMarkdownBody',
component: 'markdown',
required: true,
},
],
}

export default remarkForm(Template, recipeForm)

const Recipe = ({ text, ingredients: sourceIngredients }) => {
let recipe = text
let ingredients = sourceIngredients
// Try to split by end of ingredient list
const splitBy = '</ul>'
const idx = text.lastIndexOf(splitBy)

// if individual ingredients aren't specified, extract them from recipe text
if (idx > -1 && !ingredients) {
ingredients = text.substr(0, idx + splitBy.length)
recipe = text.substr(idx + splitBy.length)
}

return (
<div className="recipe-content">
{ingredients && (
<div
className="recipe-ingredients"
dangerouslySetInnerHTML={{ __html: ingredients }}
/>
)}
<div
className="recipe-text"
dangerouslySetInnerHTML={{ __html: recipe }}
/>
</div>
)
}

export const pageQuery = graphql`
query SmallRecipesQuery($slug: String!) {
markdownRemark(fields: { slug: { eq: $slug } }) {
html
frontmatter {
title
serves
source
tags
}
fields {
ingredients
relatedRecipes {
slug
title
}
image {
childImageSharp {
gatsbyImageData(layout: CONSTRAINED)
}
}
}
...TinaRemark
}
}
`

0 comments on commit cabd8b2

Please sign in to comment.