1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-06-28 16:48:00 +02:00

category & tag archives

This commit is contained in:
Matthias Kretschmann 2018-07-19 23:04:41 +02:00
parent f3f5f3381f
commit fe761f9f5a
Signed by: m
GPG Key ID: 606EEEF3C479A91F
7 changed files with 141 additions and 40 deletions

View File

@ -33,6 +33,7 @@ module.exports = {
{ {
resolve: 'gatsby-transformer-remark', resolve: 'gatsby-transformer-remark',
options: { options: {
excerpt_separator: '<!-- more -->',
plugins: [ plugins: [
{ {
resolve: 'gatsby-remark-images', resolve: 'gatsby-remark-images',
@ -44,6 +45,12 @@ module.exports = {
backgroundColor: '#dfe8ef' backgroundColor: '#dfe8ef'
} }
}, },
{
resolve: 'gatsby-remark-copy-linked-files',
options: {
destinationDir: 'media'
}
},
'gatsby-remark-prismjs', 'gatsby-remark-prismjs',
'gatsby-remark-smartypants', 'gatsby-remark-smartypants',
'gatsby-remark-autolink-headers' 'gatsby-remark-autolink-headers'
@ -60,6 +67,7 @@ module.exports = {
'gatsby-transformer-yaml', 'gatsby-transformer-yaml',
'gatsby-transformer-sharp', 'gatsby-transformer-sharp',
'gatsby-plugin-sharp', 'gatsby-plugin-sharp',
'gatsby-plugin-sitemap' 'gatsby-plugin-sitemap',
'gatsby-plugin-catch-links'
] ]
} }

View File

@ -47,9 +47,7 @@ exports.createPages = ({ graphql, actions }) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const postTemplate = path.resolve('src/templates/Post.jsx') const postTemplate = path.resolve('src/templates/Post.jsx')
// const indexTemplate = path.resolve('src/templates/index.jsx') const archiveTemplate = path.resolve('src/templates/Archive.jsx')
// const tagTemplate = path.resolve('src/templates/tag.jsx')
// const categoryTemplate = path.resolve('src/templates/category.jsx')
resolve( resolve(
graphql( graphql(
@ -65,6 +63,10 @@ exports.createPages = ({ graphql, actions }) => {
slug slug
date date
} }
frontmatter {
category
tags
}
} }
} }
} }
@ -90,46 +92,46 @@ exports.createPages = ({ graphql, actions }) => {
}) })
}) })
// const tagSet = new Set() // Category & Tag Pages
// const tagMap = new Map() const tagSet = new Set()
// const categorySet = new Set() const tagMap = new Map()
const categorySet = new Set()
// posts.forEach(post => { posts.forEach(post => {
// if (post.node.frontmatter.tags) { if (post.node.frontmatter.tags) {
// post.node.frontmatter.tags.forEach(tag => { post.node.frontmatter.tags.forEach(tag => {
// tagSet.add(tag) tagSet.add(tag)
// const array = tagMap.has(tag) ? tagMap.get(tag) : [] const array = tagMap.has(tag) ? tagMap.get(tag) : []
// array.push(post) array.push(post)
// tagMap.set(tag, array) tagMap.set(tag, array)
// }) })
// } }
// if (post.node.frontmatter.category) { if (post.node.frontmatter.category) {
// categorySet.add(post.node.frontmatter.category) categorySet.add(post.node.frontmatter.category)
// } }
// }) })
// const tagList = Array.from(tagSet) const tagList = Array.from(tagSet)
// tagList.forEach(tag => { tagList.forEach(tag => {
// // Creates tag pages createPage({
// createPage({ path: `/tag/${tag}/`,
// path: `/tags/${tag}/`, component: archiveTemplate,
// component: tagTemplate, context: { tag }
// context: { tag } })
// }) })
// })
// const categoryList = Array.from(categorySet) const categoryList = Array.from(categorySet)
// categoryList.forEach(category => { categoryList.forEach(category => {
// createPage({ createPage({
// path: `/categories/${category}/`, path: `/${category}/`,
// component: categoryTemplate, component: archiveTemplate,
// context: { category } context: { category }
// }) })
// }) })
resolve() resolve()
}) })

View File

@ -25,12 +25,14 @@
"exif-js": "^2.3.0", "exif-js": "^2.3.0",
"gatsby": "^2.0.0-beta.47", "gatsby": "^2.0.0-beta.47",
"gatsby-image": "^2.0.0-beta.6", "gatsby-image": "^2.0.0-beta.6",
"gatsby-plugin-catch-links": "^1.0.24",
"gatsby-plugin-matomo": "^0.4.1", "gatsby-plugin-matomo": "^0.4.1",
"gatsby-plugin-react-helmet": "^3.0.0-beta.3", "gatsby-plugin-react-helmet": "^3.0.0-beta.3",
"gatsby-plugin-sass": "^2.0.0-beta.5", "gatsby-plugin-sass": "^2.0.0-beta.5",
"gatsby-plugin-sharp": "^2.0.0-beta.5", "gatsby-plugin-sharp": "^2.0.0-beta.5",
"gatsby-plugin-sitemap": "^2.0.0-beta.2", "gatsby-plugin-sitemap": "^2.0.0-beta.2",
"gatsby-remark-autolink-headers": "^2.0.0-beta.3", "gatsby-remark-autolink-headers": "^2.0.0-beta.3",
"gatsby-remark-copy-linked-files": "^1.5.37",
"gatsby-remark-images": "^2.0.1-beta.7", "gatsby-remark-images": "^2.0.1-beta.7",
"gatsby-remark-prismjs": "^3.0.0-beta.3", "gatsby-remark-prismjs": "^3.0.0-beta.3",
"gatsby-remark-smartypants": "^2.0.0-beta.2", "gatsby-remark-smartypants": "^2.0.0-beta.2",
@ -49,13 +51,14 @@
"react-dom": "^16.4.1", "react-dom": "^16.4.1",
"react-helmet": "^5.2.0", "react-helmet": "^5.2.0",
"react-time": "^4.3.0", "react-time": "^4.3.0",
"slugify": "^1.3.0",
"vex-js": "^4.1.0" "vex-js": "^4.1.0"
}, },
"devDependencies": { "devDependencies": {
"babel-eslint": "^8.2.6", "babel-eslint": "^8.2.6",
"eslint": "^5.1.0", "eslint": "^5.1.0",
"eslint-config-prettier": "^2.9.0", "eslint-config-prettier": "^2.9.0",
"eslint-loader": "^2.0.0", "eslint-loader": "^2.1.0",
"eslint-plugin-graphql": "^2.1.1", "eslint-plugin-graphql": "^2.1.1",
"eslint-plugin-prettier": "^2.6.2", "eslint-plugin-prettier": "^2.6.2",
"eslint-plugin-react": "^7.10.0", "eslint-plugin-react": "^7.10.0",

View File

@ -1,6 +1,7 @@
import React from 'react' import React from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import Time from 'react-time' import Time from 'react-time'
import slugify from 'slugify'
import PostLinkActions from '../atoms/PostLinkActions' import PostLinkActions from '../atoms/PostLinkActions'
import styles from './PostMeta.module.scss' import styles from './PostMeta.module.scss'
@ -33,7 +34,7 @@ const PostMeta = ({ post, meta }) => {
{category && ( {category && (
<div className={styles.categories}> <div className={styles.categories}>
<a className={styles.category} href="/"> <a className={styles.category} href={`/${slugify(category)}/`}>
{category} {category}
</a> </a>
</div> </div>
@ -42,7 +43,7 @@ const PostMeta = ({ post, meta }) => {
{tags && ( {tags && (
<div className={styles.tags}> <div className={styles.tags}>
{tags.map(tag => ( {tags.map(tag => (
<a key={tag} className={styles.tag} href={`/tag/${tag}/`}> <a key={tag} className={styles.tag} href={`/tag/${slugify(tag)}/`}>
{tag} {tag}
</a> </a>
))} ))}

78
src/templates/Archive.jsx Normal file
View File

@ -0,0 +1,78 @@
import React from 'react'
import PropTypes from 'prop-types'
import { Link, graphql } from 'gatsby'
import Layout from '../components/Layout'
import styles from './Archive.module.scss'
const Archive = ({ data, pageContext }) => {
let posts
let archiveTitle
if (pageContext.category) {
posts = data.category.edges
archiveTitle = pageContext.category
} else {
posts = data.tag.edges
archiveTitle = pageContext.tag
}
const Posts = posts.map(post => (
<li key={post.node.id}>
<Link to={post.node.fields.slug}>{post.node.frontmatter.title}</Link>
</li>
))
return (
<Layout location={location}>
<h1 className={styles.archiveTitle}>{archiveTitle}</h1>
<ul>{Posts}</ul>
</Layout>
)
}
Archive.propTypes = {
data: PropTypes.object.isRequired,
pageContext: PropTypes.object.isRequired
}
export default Archive
export const archiveQuery = graphql`
query($category: String, $tag: String) {
category: allMarkdownRemark(
filter: { frontmatter: { category: { eq: $category } } }
sort: { order: DESC, fields: [fields___date] }
) {
edges {
node {
id
frontmatter {
title
}
fields {
slug
date(formatString: "MMMM DD, YYYY")
}
}
}
}
tag: allMarkdownRemark(
filter: { frontmatter: { tags: { eq: $tag } } }
sort: { order: DESC, fields: [fields___date] }
) {
edges {
node {
id
frontmatter {
title
}
fields {
slug
date(formatString: "MMMM DD, YYYY")
}
}
}
}
}
`

View File

@ -0,0 +1,8 @@
@import 'variables';
@import 'mixins';
.archiveTitle {
font-size: $font-size-h3;
@include heading-band();
}

View File

@ -47,6 +47,7 @@ export const pageQuery = graphql`
query BlogPostByPath($slug: String!) { query BlogPostByPath($slug: String!) {
markdownRemark(fields: { slug: { eq: $slug } }) { markdownRemark(fields: { slug: { eq: $slug } }) {
html html
excerpt
frontmatter { frontmatter {
type type
title title