feat: add featured image to posts

This commit is contained in:
Felix Schröter 2022-11-15 20:14:27 +01:00
parent df2fd8a520
commit 840621c672
Signed by: felschr
GPG key ID: 671E39E6744C807D
5 changed files with 48 additions and 5 deletions

View file

@ -10,11 +10,12 @@ const pages = [
] ]
type LayoutProps = { type LayoutProps = {
pageTitle: string | ReactNode pageTitle: string
preTitle?: ReactNode
children: ReactNode children: ReactNode
} }
const Layout = ({ pageTitle, children }: LayoutProps) => { const Layout = ({ pageTitle, preTitle, children }: LayoutProps) => {
const theme = useTheme() const theme = useTheme()
return ( return (
@ -58,6 +59,7 @@ const Layout = ({ pageTitle, children }: LayoutProps) => {
</AppBar> </AppBar>
<Container style={{ marginTop: 68.5, marginBottom: 80, minHeight: "100%" }}> <Container style={{ marginTop: 68.5, marginBottom: 80, minHeight: "100%" }}>
{preTitle}
<Typography variant="h1">{pageTitle}</Typography> <Typography variant="h1">{pageTitle}</Typography>
{children} {children}
</Container> </Container>

BIN
src/images/nixos-restic.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 215 KiB

View file

@ -1,8 +1,9 @@
import { ReactNode } from "react" import { ReactNode } from "react"
import { GatsbyLinkProps, graphql, Link, PageProps } from "gatsby" import { GatsbyLinkProps, graphql, Link, PageProps } from "gatsby"
import Layout from "../../components/organisms/Layout" import Layout from "../../components/organisms/Layout"
import { Card, CardContent, Grid, Typography } from "@mui/material" import { Card, CardContent, CardMedia, Grid, Typography } from "@mui/material"
import styled from "@emotion/styled" import styled from "@emotion/styled"
import { GatsbyImage, getImage } from "gatsby-plugin-image"
type PostProps = { type PostProps = {
size?: "normal" | "highlight" size?: "normal" | "highlight"
@ -21,9 +22,23 @@ const Post = ({ size = "normal", post }: PostProps) => {
} }
` `
const featuredImage = getImage(post.frontmatter?.featuredImage?.childImageSharp?.gatsbyImageData!)
return ( return (
<Grid item xs={size === "highlight" ? 12 : 4}> <Grid item xs={size === "highlight" ? 12 : 4}>
<Card> <Card>
{featuredImage && (
<CardMedia
objectFit="contain"
component={(props) => (
<GatsbyImage
image={featuredImage}
alt={post.frontmatter?.featuredImageAlt ?? ""}
{...props}
/>
)}
/>
)}
<CardContent> <CardContent>
<Typography variant="h5" gutterBottom> <Typography variant="h5" gutterBottom>
<PostLink> <PostLink>
@ -71,6 +86,12 @@ export const query = graphql`
frontmatter { frontmatter {
title title
published published
featuredImage {
childImageSharp {
gatsbyImageData(height: 200)
}
}
featuredImageAlt
} }
} }
} }

View file

@ -1,17 +1,22 @@
import React from "react" import React from "react"
import { graphql, Link as GatsbyLink, PageProps } from "gatsby" import { graphql, Link as GatsbyLink, PageProps } from "gatsby"
import { GatsbyImage, getImage } from "gatsby-plugin-image"
import { MDXRenderer } from "gatsby-plugin-mdx" import { MDXRenderer } from "gatsby-plugin-mdx"
import NavigateNextIcon from "@mui/icons-material/NavigateNext" import NavigateNextIcon from "@mui/icons-material/NavigateNext"
import Layout from "../../components/organisms/Layout" import Layout from "../../components/organisms/Layout"
import { defineCustomElements as deckDeckGoHighlightElement } from "@deckdeckgo/highlight-code/dist/loader"; import { defineCustomElements as deckDeckGoHighlightElement } from "@deckdeckgo/highlight-code/dist/loader";
import { Breadcrumbs, Link } from "@mui/material"; import { Breadcrumbs, Link } from "@mui/material";
import { Box } from "@mui/system"
deckDeckGoHighlightElement(); deckDeckGoHighlightElement();
const BlogPost = ({ data: { mdx }, path }: PageProps<Queries.BlogPostQuery>) => { const BlogPost = ({ data: { mdx }, path }: PageProps<Queries.BlogPostQuery>) => {
const featuredImage = getImage(mdx?.frontmatter?.featuredImage?.childImageSharp?.gatsbyImageData!)
return ( return (
<Layout <Layout
pageTitle={ pageTitle={mdx?.frontmatter?.title ?? ""}
preTitle={
<> <>
<Breadcrumbs <Breadcrumbs
separator={<NavigateNextIcon fontSize="small" />} separator={<NavigateNextIcon fontSize="small" />}
@ -34,7 +39,14 @@ const BlogPost = ({ data: { mdx }, path }: PageProps<Queries.BlogPostQuery>) =>
{mdx?.frontmatter?.title} {mdx?.frontmatter?.title}
</Link> </Link>
</Breadcrumbs> </Breadcrumbs>
{mdx?.frontmatter?.title ?? ""} {featuredImage && (
<Box display="flex" justifyContent="center">
<GatsbyImage
image={featuredImage}
alt={mdx?.frontmatter?.featuredImageAlt ?? ""}
/>
</Box>
)}
</> </>
} }
> >
@ -54,6 +66,12 @@ export const query = graphql`
title title
published published
updated updated
featuredImage {
childImageSharp {
gatsbyImageData(width: 800)
}
}
featuredImageAlt
} }
} }
} }

View file

@ -2,6 +2,8 @@
title: Optimised Backups on NixOS with restic & fd title: Optimised Backups on NixOS with restic & fd
published: 2022-08-01 published: 2022-08-01
updated: 2022-08-01 updated: 2022-08-01
featuredImage: ../images/nixos-restic.png
featuredImageAlt: NixOS plus Restic
--- ---
#### UPDATE 2022-07-04 #### UPDATE 2022-07-04