|
| 1 | +/* eslint-disable global-require */ |
| 2 | +/* eslint-disable import/no-dynamic-require */ |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | +import matter from 'gray-matter'; |
| 5 | +import ReactMarkdown from 'react-markdown'; |
| 6 | +import remarkGfm from 'remark-gfm'; |
| 7 | +import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; |
| 8 | +import { materialOceanic } from 'react-syntax-highlighter/dist/cjs/styles/prism'; |
| 9 | +import Layout from 'components/Layout'; |
| 10 | +import headerColor from 'helpers/post-header'; |
| 11 | +import styles from 'styles/post.module.scss'; |
| 12 | +import ProgressiveImage from 'components/ProgressiveImage'; |
| 13 | +import isExternalImage from 'helpers/is-external-image'; |
| 14 | +import generateArticleStructuredData from 'helpers/generate-article-structured-data'; |
| 15 | +import generateFaqStructuredData from 'helpers/generate-faq-structured-data'; |
| 16 | + |
| 17 | +export default function BlogPost({ |
| 18 | + siteTitle, |
| 19 | + frontmatter, |
| 20 | + markdownBody, |
| 21 | + date, |
| 22 | + slug, |
| 23 | + image |
| 24 | +}) { |
| 25 | + // eslint-disable-next-line react/jsx-no-useless-fragment |
| 26 | + if (!frontmatter) return <></>; |
| 27 | + |
| 28 | + return ( |
| 29 | + <Layout |
| 30 | + socialMeta={{ |
| 31 | + url: `blog/${slug}`, |
| 32 | + image, |
| 33 | + imageAlt: frontmatter.cover_image_alt, |
| 34 | + type: 'article', |
| 35 | + title: `${frontmatter.title} | ${siteTitle}`, |
| 36 | + description: frontmatter.description |
| 37 | + }} |
| 38 | + breadcrumbs={[ |
| 39 | + { name: 'blog', item: 'blog/' }, |
| 40 | + { name: frontmatter.title, item: `blog/${slug}` } |
| 41 | + ]} |
| 42 | + structured={[ |
| 43 | + generateFaqStructuredData(frontmatter.faq), |
| 44 | + generateArticleStructuredData({ |
| 45 | + title: frontmatter.title, |
| 46 | + slug, |
| 47 | + description: frontmatter.description, |
| 48 | + image, |
| 49 | + date |
| 50 | + }) |
| 51 | + ]} |
| 52 | + > |
| 53 | + <article className={styles.post}> |
| 54 | + {frontmatter.cover_image ? ( |
| 55 | + <ProgressiveImage |
| 56 | + className={styles.post__header__image} |
| 57 | + src={frontmatter.cover_image} |
| 58 | + alt={frontmatter.cover_image_alt} |
| 59 | + /> |
| 60 | + ) : ( |
| 61 | + <div |
| 62 | + className={styles.post__header__image} |
| 63 | + style={{ backgroundColor: headerColor }} |
| 64 | + /> |
| 65 | + )} |
| 66 | + <div className='container'> |
| 67 | + <div className={styles.post__header__title}> |
| 68 | + <h2>{frontmatter.title}</h2> |
| 69 | + <div className={styles.post__date_label}>{date}</div> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + <div className={styles.post__body}> |
| 73 | + <div className='container'> |
| 74 | + <div className={styles.post__intro}>{frontmatter.intro}</div> |
| 75 | + <ReactMarkdown |
| 76 | + remarkPlugins={[remarkGfm]} |
| 77 | + components={{ |
| 78 | + // eslint-disable-next-line react/no-unstable-nested-components |
| 79 | + code({ inline, className, children }) { |
| 80 | + const match = /language-(\w+)/.exec(className || ''); |
| 81 | + |
| 82 | + return !inline && match ? ( |
| 83 | + <SyntaxHighlighter |
| 84 | + style={materialOceanic} |
| 85 | + language={match[1]} |
| 86 | + > |
| 87 | + {String(children).replace(/\n$/, '')} |
| 88 | + </SyntaxHighlighter> |
| 89 | + ) : ( |
| 90 | + <code className={className}>{children}</code> |
| 91 | + ); |
| 92 | + }, |
| 93 | + img: ProgressiveImage |
| 94 | + }} |
| 95 | + > |
| 96 | + {markdownBody} |
| 97 | + </ReactMarkdown> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + </article> |
| 101 | + </Layout> |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +BlogPost.propTypes = { |
| 106 | + siteTitle: PropTypes.string, |
| 107 | + frontmatter: PropTypes.object, |
| 108 | + markdownBody: PropTypes.string, |
| 109 | + date: PropTypes.array, |
| 110 | + slug: PropTypes.string, |
| 111 | + image: PropTypes.string |
| 112 | +}; |
| 113 | + |
| 114 | +export async function getStaticProps({ ...ctx }) { |
| 115 | + const { postname } = ctx.params; |
| 116 | + const content = await import(`../../posts/${postname}.md`); |
| 117 | + const config = await import('../../siteconfig.json'); |
| 118 | + const date = postname.match(/(\d{1,4}([.\--])\d{1,2}([.\--])\d{1,4})/g); |
| 119 | + const data = matter(content.default); |
| 120 | + let image = data.data.cover_image || null; |
| 121 | + |
| 122 | + if (image && !isExternalImage(image)) { |
| 123 | + const src = require(`images/${data.data.cover_image}`); |
| 124 | + |
| 125 | + image = 'https://khendrikse.github.io'.concat(src.src); |
| 126 | + } |
| 127 | + |
| 128 | + return { |
| 129 | + props: { |
| 130 | + siteTitle: config.title, |
| 131 | + frontmatter: data.data, |
| 132 | + markdownBody: data.content, |
| 133 | + date, |
| 134 | + slug: postname, |
| 135 | + image |
| 136 | + } |
| 137 | + }; |
| 138 | +} |
| 139 | + |
| 140 | +export async function getStaticPaths() { |
| 141 | + const blogSlugs = (context => { |
| 142 | + const keys = context.keys(); |
| 143 | + const data = keys.map(key => { |
| 144 | + const slug = key.replace(/^.*[\\/]/, '').slice(0, -3); |
| 145 | + |
| 146 | + return slug; |
| 147 | + }); |
| 148 | + |
| 149 | + return data; |
| 150 | + })(require.context('../../posts', true, /\.\/.*\.md$/)); |
| 151 | + |
| 152 | + const paths = blogSlugs.map(slug => `/blog/${slug}`); |
| 153 | + |
| 154 | + return { |
| 155 | + paths, |
| 156 | + fallback: false |
| 157 | + }; |
| 158 | +} |
0 commit comments