diff --git a/gatsby-node.ts b/gatsby-node.ts index 76d5954d..85b1e806 100644 --- a/gatsby-node.ts +++ b/gatsby-node.ts @@ -5,6 +5,7 @@ */ import type { GatsbyNode } from "gatsby" import { characterKeys, characterInfos } from "./src/constants" +import path from "path" export const sourceNodes: GatsbyNode["sourceNodes"] = async ({ actions, @@ -30,3 +31,53 @@ export const sourceNodes: GatsbyNode["sourceNodes"] = async ({ actions.createNode(node) }) } + +export const createPages: GatsbyNode["createPages"] = async ({ + actions, + graphql +}) => { + const { createPage } = actions + + // ニュースページ + const newsPostTemplate = path.resolve("src/templates/newsPost.tsx") + + const result = await graphql(` + query NewsPages { + allMarkdownRemark(filter: { fileAbsolutePath: { regex: "/news/" } }) { + edges { + node { + frontmatter { + slug + } + } + } + } + } + `) + + if (result.errors) { + throw result.errors + } + + const data = result.data as { + allMarkdownRemark: { + edges: { + node: { + frontmatter: { + slug: string + } + } + }[] + } + } + + data.allMarkdownRemark.edges.forEach(({ node }) => { + createPage({ + path: `/news/${node.frontmatter.slug}/`, + component: newsPostTemplate, + context: { + slug: node.frontmatter.slug, + }, + }) + }) +} diff --git a/src/components/page.tsx b/src/components/page.tsx index f0ca299f..e1f79094 100644 --- a/src/components/page.tsx +++ b/src/components/page.tsx @@ -102,6 +102,10 @@ export const Page: React.FC<{ 変更履歴 + {/* TODO: リリース時にコメントアウトを外す + + ニュース + */} + {/* TODO: リリース時にコメントアウトを外す +
  • + + ニュース + +
  • */}
  • { + const data = useStaticQuery(graphql` + query IndexPage { + allMarkdownRemark ( + filter: {fileAbsolutePath: {regex: "/news/"}} + sort: {frontmatter: {date: DESC}} + ) { + edges { + node { + html + frontmatter { + title + slug + date(formatString: "YYYY/MM/DD") + } + } + } + } + } + `); + + return ( + + +
    +
    +

    ニュース

    + {data.allMarkdownRemark.edges.map((edge) => ( +
    + + {edge.node.frontmatter!.title} + +

    {edge.node.frontmatter!.date}

    +
    + ))} +
    +
    +
    + ) +} + +export default NewsIndex diff --git a/src/templates/newsPost.tsx b/src/templates/newsPost.tsx new file mode 100644 index 00000000..dc3ad385 --- /dev/null +++ b/src/templates/newsPost.tsx @@ -0,0 +1,45 @@ +import React from "react" +import "../components/layout.scss" +import { Page } from "../components/page" +import Seo from "../components/seo" +import { graphql } from "gatsby" + +const NewsPost = ({ data }) => { + const { markdownRemark } = data; + const { frontmatter, html } = markdownRemark; + + return ( + + +
    +
    +

    {frontmatter.title}

    +

    {frontmatter.date}

    +
    +
    +
    +
    + ) +} + +export const query = graphql` + query($slug: String!) { + markdownRemark(frontmatter: { slug: { eq: $slug } }) { + html + frontmatter { + slug + title + date(formatString: "YYYY/MM/DD") + } + } + } +`; + +export default NewsPost