Compare commits
No commits in common. "c2a99f61c72dae629a8189a26346af8d98577a5b" and "9f5cc2fcfc4a14525e71d88e0aff513f130f90cd" have entirely different histories.
c2a99f61c7
...
9f5cc2fcfc
7 changed files with 66 additions and 122 deletions
|
@ -4,10 +4,9 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
|
||||||
const { createPage } = actions;
|
const { createPage } = actions;
|
||||||
|
|
||||||
const noteTemplate = path.resolve(`src/templates/note.tsx`);
|
const noteTemplate = path.resolve(`src/templates/note.tsx`);
|
||||||
const result = await graphql(`
|
const result = await graphql(`{
|
||||||
{
|
|
||||||
allMdx(
|
allMdx(
|
||||||
limit: 10
|
limit: 10,
|
||||||
filter: {fileAbsolutePath: {glob: "**/src/notes/*"}}
|
filter: {fileAbsolutePath: {glob: "**/src/notes/*"}}
|
||||||
) {
|
) {
|
||||||
edges {
|
edges {
|
||||||
|
@ -19,40 +18,20 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}`);
|
||||||
`);
|
|
||||||
|
|
||||||
if (result.errors) {
|
if (result.errors) {
|
||||||
reporter.panicOnBuild(`Error while running GraphQL query.`);
|
reporter.panicOnBuild(`Error while running GraphQL query.`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const posts = result.data.allMdx.edges;
|
result.data.allMdx.edges.forEach(({ node }) => {
|
||||||
const postsPerPage = 10;
|
|
||||||
const numPages = Math.ceil(posts.length / postsPerPage);
|
|
||||||
const rootPath = "/notes";
|
|
||||||
|
|
||||||
Array.from({ length: numPages }).forEach((_, i) => {
|
|
||||||
createPage({
|
createPage({
|
||||||
path: rootPath + (i === 0 ? '' : `/${i + 1}`),
|
path: `/notes/${node.frontmatter.path}`,
|
||||||
component: path.resolve("./src/templates/notes.tsx"),
|
|
||||||
context: {
|
|
||||||
limit: postsPerPage,
|
|
||||||
skip: i * postsPerPage,
|
|
||||||
numPages,
|
|
||||||
currentPage: i + 1,
|
|
||||||
rootPath,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
posts.forEach(({ node }) => {
|
|
||||||
createPage({
|
|
||||||
path: `${rootPath}/${node.frontmatter.path}`,
|
|
||||||
component: noteTemplate,
|
component: noteTemplate,
|
||||||
context: {
|
context: {
|
||||||
id: node.id,
|
id: node.id,
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
|
@ -1,17 +1,13 @@
|
||||||
import { PageProps } from 'gatsby';
|
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import Navbar from "../components/navbar";
|
import Navbar from "../components/navbar";
|
||||||
|
|
||||||
export default ({ location }: PageProps): JSX.Element => {
|
export default ({ location }) => {
|
||||||
const messages = [
|
console.log(location);
|
||||||
|
return <>
|
||||||
|
<Navbar />
|
||||||
<p>
|
<p>
|
||||||
In your attempt to search for <code>{location.pathname}</code>, you seem to
|
In your attempt to search for <code>{location.pathname}</code>, you seem to
|
||||||
have gotten lost instead. Fret not, for there is always a path <a href="/">home</a>.
|
have gotten lost instead. Fret not, for there is always a path <a href="/">home</a>.
|
||||||
</p>,
|
</p>
|
||||||
<p>Not all who wander are lost, but you seem to be. Go <a href="/">home</a>?</p>
|
|
||||||
];
|
|
||||||
return <>
|
|
||||||
<Navbar />
|
|
||||||
{messages[Math.floor(Math.random() * messages.length)]}
|
|
||||||
</>;
|
</>;
|
||||||
};
|
};
|
4
src/pages/notes.module.css
Normal file
4
src/pages/notes.module.css
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
.note-title {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
}
|
41
src/pages/notes.tsx
Normal file
41
src/pages/notes.tsx
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
import React from "react";
|
||||||
|
import Navbar from "../components/navbar";
|
||||||
|
import { graphql } from "gatsby";
|
||||||
|
import style from "./notes.module.css";
|
||||||
|
|
||||||
|
export default ({ data }) => {
|
||||||
|
console.log(data);
|
||||||
|
const posts = data.allMdx.edges;
|
||||||
|
return <>
|
||||||
|
<Navbar />
|
||||||
|
{
|
||||||
|
posts.map(({ node }) => {
|
||||||
|
return <article>
|
||||||
|
<div className={style.noteTitle}>
|
||||||
|
<h3><a href={`${node.frontmatter.path}`}>{node.frontmatter.title}</a></h3>
|
||||||
|
<time dateTime={node.frontmatter.date}>{node.frontmatter.date}</time>
|
||||||
|
</div>
|
||||||
|
<p>{node.excerpt}</p>
|
||||||
|
</article>;
|
||||||
|
})
|
||||||
|
}
|
||||||
|
</>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const query = graphql`
|
||||||
|
query IndexQuery {
|
||||||
|
allMdx(sort: {order: ASC, fields: [fileAbsolutePath]}, filter: {fileAbsolutePath: {glob: "**/src/notes/*"}}) {
|
||||||
|
edges {
|
||||||
|
node {
|
||||||
|
excerpt(pruneLength: 250)
|
||||||
|
frontmatter {
|
||||||
|
title
|
||||||
|
date(formatString: "YYYY-MM-DD")
|
||||||
|
path
|
||||||
|
}
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
|
@ -1,12 +0,0 @@
|
||||||
.note-title {
|
|
||||||
display: flex;
|
|
||||||
justify-content: space-between;
|
|
||||||
}
|
|
||||||
|
|
||||||
.prev-page-link {
|
|
||||||
margin-right: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.next-page-link {
|
|
||||||
margin-left: 10px;
|
|
||||||
}
|
|
|
@ -1,64 +0,0 @@
|
||||||
import React from "react";
|
|
||||||
import Navbar from "../components/navbar";
|
|
||||||
import { graphql, PageProps } from "gatsby";
|
|
||||||
import style from "./notes.module.css";
|
|
||||||
|
|
||||||
export default ({ data, pageContext }) => {
|
|
||||||
const posts = data.allMdx.edges;
|
|
||||||
const { currentPage, numPages, rootPath } = pageContext;
|
|
||||||
let prevPageLink = null;
|
|
||||||
|
|
||||||
if (currentPage === 2) {
|
|
||||||
prevPageLink = <a href={rootPath} className={style.prevPageLink}><<</a>;
|
|
||||||
} else if (currentPage !== 1) {
|
|
||||||
prevPageLink = <a href={rootPath + '/' + (currentPage - 1)} className={style.prevPageLink}><<</a>;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<Navbar />
|
|
||||||
{posts.map(({ node }) => {
|
|
||||||
const frontmatter = node.frontmatter
|
|
||||||
return (
|
|
||||||
<article key={frontmatter.title}>
|
|
||||||
<div className={style.noteTitle}>
|
|
||||||
<h3>
|
|
||||||
<a href={`${rootPath + '/' + frontmatter.path}`}>{frontmatter.title}</a>
|
|
||||||
</h3>
|
|
||||||
<time dateTime={frontmatter.date}>{frontmatter.date}</time>
|
|
||||||
</div>
|
|
||||||
<p>{node.excerpt}</p>
|
|
||||||
</article>
|
|
||||||
)
|
|
||||||
})}
|
|
||||||
<p className={style.paginationNav}>
|
|
||||||
{prevPageLink}
|
|
||||||
{currentPage}
|
|
||||||
{currentPage !== numPages && <a href={rootPath + '/' + (currentPage + 1)} className={style.nextPageLink}>>></a>}
|
|
||||||
</p>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const query = graphql`
|
|
||||||
query IndexQuery($skip: Int!, $limit: Int!) {
|
|
||||||
allMdx(
|
|
||||||
sort: { order: DESC, fields: [frontmatter___date] }
|
|
||||||
filter: { fileAbsolutePath: { glob: "**/src/notes/*" } }
|
|
||||||
limit: $limit
|
|
||||||
skip: $skip
|
|
||||||
) {
|
|
||||||
edges {
|
|
||||||
node {
|
|
||||||
excerpt(pruneLength: 250)
|
|
||||||
frontmatter {
|
|
||||||
title
|
|
||||||
date(formatString: "YYYY-MM-DD")
|
|
||||||
path
|
|
||||||
}
|
|
||||||
id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`
|
|
Loading…
Reference in a new issue