fix: fix page & post issues

This commit is contained in:
Felix Schröter 2022-11-15 22:52:33 +01:00
parent 20cc481ca4
commit 7ceda0bc7a
Signed by: felschr
GPG key ID: 671E39E6744C807D
4 changed files with 71 additions and 13 deletions

61
gatsby-node.ts Normal file
View file

@ -0,0 +1,61 @@
import path from "path";
import type { GatsbyNode } from "gatsby"
export const createPages: GatsbyNode["createPages"] = async ({
graphql,
actions,
/* createNodeId,
createContentDigest, */
}) => {
const { createPage } = actions
const results = await graphql<any>(`
query {
allMdx {
edges {
node {
slug
fields {
source
}
}
}
}
}
`)
const allEdges: any[] = results.data.allMdx.edges
const blogEdges = allEdges.filter(
(edge) => edge.node.fields.source === "posts"
)
const pageEdges = allEdges.filter(
(edge) => edge.node.fields.source === "pages"
)
blogEdges.forEach((edge, index) => {
const previous =
index === blogEdges.length - 1 ? null : blogEdges[index + 1].node
const next = index === 0 ? null : blogEdges[index - 1].node
createPage({
path: `/blog/${edge.node.slug}`,
component: path.resolve("./src/layouts/Post.tsx"),
context: {
slug: edge.node.slug,
previous,
next
}
})
})
pageEdges.forEach((edge, index) => {
createPage({
path: `/${edge.node.slug}`,
component: path.resolve("./src/layouts/Page.tsx"),
context: {
slug: edge.node.slug
}
})
})
}

View file

@ -1,18 +1,15 @@
import React from "react" import * as React from "react"
import { graphql, PageProps } from "gatsby" import { graphql, PageProps } from "gatsby"
import { MDXRenderer } from "gatsby-plugin-mdx" import { MDXRenderer } from "gatsby-plugin-mdx"
import Layout from "../components/organisms/Layout" import Layout from "../components/organisms/Layout"
const Page = ({ data: { mdx }, path }: PageProps<Queries.PageQuery>) => { const Page = ({ data: { mdx }, path }: PageProps<Queries.PageQuery>) => (
console.log("Page...", { path, mdx })
return (
<Layout pageTitle={mdx?.frontmatter?.title ?? ""}> <Layout pageTitle={mdx?.frontmatter?.title ?? ""}>
<MDXRenderer> <MDXRenderer>
{mdx?.body ?? ""} {mdx?.body ?? ""}
</MDXRenderer> </MDXRenderer>
</Layout> </Layout>
) )
}
export const query = graphql` export const query = graphql`
query Page($id: String) { query Page($id: String) {

View file

@ -3,7 +3,7 @@ import { graphql, Link as GatsbyLink, PageProps } from "gatsby"
import { GatsbyImage, getImage } from "gatsby-plugin-image" 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";