1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-07-01 06:02:08 +02:00
blog/src/templates/post.jsx

125 lines
2.7 KiB
React
Raw Normal View History

2018-07-17 23:33:55 +02:00
import React from 'react'
import PropTypes from 'prop-types'
import Helmet from 'react-helmet'
import { graphql } from 'gatsby'
2018-07-18 00:24:11 +02:00
import Layout from '../components/Layout'
import Image from '../components/atoms/Image'
2018-07-19 16:49:20 +02:00
import PostTitle from '../components/atoms/PostTitle'
2018-07-19 02:22:01 +02:00
import PostMeta from '../components/molecules/PostMeta'
2018-07-18 23:04:31 +02:00
import styles from './Post.module.scss'
2018-07-17 23:33:55 +02:00
2018-07-20 15:52:17 +02:00
const separator = '<!-- more -->'
// Extract lead paragraph from content
// Grab everything before more tag, or just first paragraph
const PostLead = ({ post }) => {
let lead
const content = post.html
if (post.frontmatter.type === 'post') {
if (content.includes(separator)) {
lead = content.split(separator)[0]
} else {
lead = content.split('\n')[0]
}
} else {
return null
}
return (
<div
className={styles.hentry__lead}
dangerouslySetInnerHTML={{ __html: lead }}
/>
)
}
// Remove lead paragraph from content
const PostContent = ({ post }) => {
let content
content = post.html
if (post.frontmatter.type === 'post') {
if (content.includes(separator)) {
content = content.split(separator)[1]
} else {
const lead = content.split('\n')[0]
content = content.replace(lead, '')
}
}
return (
<div className="content" dangerouslySetInnerHTML={{ __html: content }} />
)
}
const Post = ({ data, location }) => {
2018-07-17 23:33:55 +02:00
const { markdownRemark: post } = data
2018-07-19 02:22:01 +02:00
const { contentYaml: meta } = data
2018-07-19 16:49:20 +02:00
const { title, image, type, linkurl } = post.frontmatter
2018-07-17 23:33:55 +02:00
return (
2018-07-18 00:24:11 +02:00
<Layout location={location}>
2018-07-17 23:33:55 +02:00
<Helmet title={title} />
2018-07-18 23:04:31 +02:00
<article className={styles.hentry}>
2018-07-19 16:49:20 +02:00
<PostTitle type={type} linkurl={linkurl} title={title} />
2018-07-18 23:04:31 +02:00
{image && (
<figure className={styles.hentry__teaser}>
<Image fluid={image.childImageSharp.fluid} alt={title} />
</figure>
)}
2018-07-19 16:49:20 +02:00
2018-07-17 23:33:55 +02:00
<div
2018-07-18 23:04:31 +02:00
className={styles.hentry__content}
2018-07-17 23:33:55 +02:00
dangerouslySetInnerHTML={{ __html: post.html }}
/>
2018-07-19 02:22:01 +02:00
<PostMeta post={post} meta={meta} />
2018-07-18 00:24:11 +02:00
</article>
</Layout>
2018-07-17 23:33:55 +02:00
)
}
Post.propTypes = {
2018-07-20 15:52:17 +02:00
data: PropTypes.object.isRequired,
location: PropTypes.object.isRequired
2018-07-17 23:33:55 +02:00
}
export default Post
export const pageQuery = graphql`
query BlogPostByPath($slug: String!) {
markdownRemark(fields: { slug: { eq: $slug } }) {
html
2018-07-19 23:04:41 +02:00
excerpt
2018-07-17 23:33:55 +02:00
frontmatter {
2018-07-19 16:49:20 +02:00
type
2018-07-17 23:33:55 +02:00
title
image {
childImageSharp {
...ImageFluid
}
}
2018-07-19 02:22:01 +02:00
author
updated
category
tags
2018-07-19 16:49:20 +02:00
linkurl
2018-07-17 23:33:55 +02:00
}
fields {
slug
date(formatString: "MMMM DD, YYYY")
}
}
2018-07-19 02:22:01 +02:00
contentYaml {
author {
uri
}
}
2018-07-17 23:33:55 +02:00
}
`