├── __mocks__ ├── styleMock.js └── fileMock.js ├── .eslintrc.json ├── jest.setup.js ├── public ├── favicon.ico └── images │ └── huper-by-joshua-earle-lWYUA42UmL8-unsplash.jpg ├── content └── posts │ ├── hello.md │ ├── hello-again-2.md │ ├── hello-again.md │ ├── html.md │ ├── markdown.md │ ├── code-highlight.md │ └── with-image.md ├── pages ├── about.js ├── posts │ └── [slug].js ├── _document.js ├── index.js ├── _app.js └── archive │ └── [page].js ├── .github ├── dependabot.yml └── workflows │ ├── test-n-build.yml │ └── update-packages.yml ├── lib ├── date.js ├── __tests__ │ └── date.test.js ├── gtag.js └── content-loader.js ├── .gitignore ├── components ├── __tests__ │ ├── Layout.test.js │ ├── Pager.test.js │ ├── Markdown.test.js │ └── __snapshots__ │ │ ├── Pager.test.js.snap │ │ ├── Markdown.test.js.snap │ │ └── Layout.test.js.snap ├── Pager.js ├── Markdown.js └── Layout.js ├── __tests__ ├── index.test.js ├── __snapshots__ │ ├── index.test.js.snap │ └── posts_slug.test.js.snap └── posts_slug.test.js ├── LICENSE ├── package.json ├── jest.config.js └── README.md /__mocks__/styleMock.js: -------------------------------------------------------------------------------- 1 | module.exports = {}; 2 | -------------------------------------------------------------------------------- /__mocks__/fileMock.js: -------------------------------------------------------------------------------- 1 | (module.exports = "test-file-stub") 2 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "next/core-web-vitals" 3 | } 4 | -------------------------------------------------------------------------------- /jest.setup.js: -------------------------------------------------------------------------------- 1 | import '@testing-library/jest-dom/extend-expect' 2 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gh640/nextjs-blog-example-ja/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /content/posts/hello.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: ブログを始めます 3 | published: 2020-07-12 4 | --- 5 | 6 | これからブログを始めます。 7 | -------------------------------------------------------------------------------- /pages/about.js: -------------------------------------------------------------------------------- 1 | const About = () => { 2 | return
ブログを書いています。
3 | } 4 | 5 | export default About 6 | -------------------------------------------------------------------------------- /content/posts/hello-again-2.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: ブログをまた再開します 3 | published: 2028-03-21 4 | --- 5 | 6 | バタバタしており久しぶりの投稿になってしまいました。 7 | -------------------------------------------------------------------------------- /content/posts/hello-again.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: ブログを再開します 3 | published: 2024-05-27 4 | --- 5 | 6 | 開始したとたんに間が空いてしまいましたが、再開します。 7 | 8 | - 閑さや 9 | - 岩にしみ入る 10 | - 蝉の声 11 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: github-actions 4 | directory: / 5 | schedule: 6 | interval: weekly 7 | target-branch: main 8 | -------------------------------------------------------------------------------- /public/images/huper-by-joshua-earle-lWYUA42UmL8-unsplash.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gh640/nextjs-blog-example-ja/HEAD/public/images/huper-by-joshua-earle-lWYUA42UmL8-unsplash.jpg -------------------------------------------------------------------------------- /lib/date.js: -------------------------------------------------------------------------------- 1 | const formatDate = (date) => { 2 | if (!(date instanceof Date)) { 3 | return '' 4 | } 5 | 6 | let year = date.getFullYear() 7 | let month = (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1) 8 | let day = (date.getDate() < 10 ? '0' : '') + date.getDate() 9 | 10 | return `${year}/${month}/${day}` 11 | } 12 | 13 | export { formatDate } 14 | -------------------------------------------------------------------------------- /content/posts/html.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: HTML を試す投稿 3 | published: 2020-07-23 4 | --- 5 | 6 | 10 | 11 |

最初の見出し

12 | 13 |

ふたつめの見出し

14 | 15 |
16 | 絶対に開けて見てはいけません 17 | 見ないでと言ったのに見てしまいましたね〜 18 |
19 | -------------------------------------------------------------------------------- /content/posts/markdown.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Markdown を試す投稿 3 | published: 2020-07-15 4 | --- 5 | 6 | 見出し: 7 | 8 | # h1 9 | 10 | ## h2 11 | 12 | ### h3 13 | 14 | #### h4 15 | 16 | ##### h5 17 | 18 | ###### h6 19 | 20 | テーブル: 21 | 22 | | 西 | 東 | 23 | | --- | --- | 24 | | 西山 | 東山 | 25 | 26 | 順序付きリスト: 27 | 28 | 1. うんだもこら 29 | 2. いけなもんや 30 | 3. あたいがどんの 31 | 4. ちゃわんなんだ 32 | 5. ひにひにさんども 33 | 6. あろもんせば 34 | 7. きれいなもんごわんさ 35 | -------------------------------------------------------------------------------- /content/posts/code-highlight.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: コードをきれいに見えるようにしました 3 | published: 2038-03-21 4 | --- 5 | 6 | サンプルです。 7 | 8 | `[slug].js`: 9 | 10 | ```js 11 | /** 12 | * ページコンポーネントで使用する値を用意する 13 | */ 14 | export async function getStaticProps({ params }) { 15 | const content = await readContentFile({ fs, slug: params.slug }) 16 | 17 | return { 18 | props: { 19 | ...content 20 | } 21 | } 22 | } 23 | ``` 24 | -------------------------------------------------------------------------------- /content/posts/with-image.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 画像付きの投稿 3 | published: 2020-07-13 4 | --- 5 | 6 | ![サンプル画像](/images/huper-by-joshua-earle-lWYUA42UmL8-unsplash.jpg) 7 | 8 | Photo by Huper by Joshua Earle on Unsplash 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | 21 | # debug 22 | npm-debug.log* 23 | yarn-debug.log* 24 | yarn-error.log* 25 | 26 | # local env files 27 | .env.local 28 | .env.development.local 29 | .env.test.local 30 | .env.production.local 31 | -------------------------------------------------------------------------------- /lib/__tests__/date.test.js: -------------------------------------------------------------------------------- 1 | import { formatDate } from "../date.js" 2 | 3 | describe("formatDate", () => { 4 | it("returns empty string for data other than Date", () => { 5 | expect(formatDate({})).toBe("") 6 | }) 7 | 8 | it("returns collect format with single-digit month", () => { 9 | expect(formatDate(new Date(2018, 4, 7))).toBe("2018/05/07") 10 | }) 11 | 12 | it("returns collect format with double-digit-month", () => { 13 | expect(formatDate(new Date(2018, 11, 27))).toBe("2018/12/27") 14 | }) 15 | }) 16 | -------------------------------------------------------------------------------- /lib/gtag.js: -------------------------------------------------------------------------------- 1 | // See: https://github.com/zeit/next.js/blob/v9.3.1/examples/with-google-analytics/lib/gtag.js 2 | const GA_TRACKING_ID = process.env.GA_TRACKING_ID 3 | 4 | // https://developers.google.com/analytics/devguides/collection/gtagjs/pages 5 | const pageview = (url) => { 6 | window.gtag('config', GA_TRACKING_ID, { 7 | page_path: url, 8 | }) 9 | } 10 | 11 | // https://developers.google.com/analytics/devguides/collection/gtagjs/events 12 | const event = ({ action, category, label, value }) => { 13 | window.gtag('event', action, { 14 | event_category: category, 15 | event_label: label, 16 | value: value, 17 | }) 18 | } 19 | 20 | export { GA_TRACKING_ID, pageview, event } 21 | -------------------------------------------------------------------------------- /components/__tests__/Layout.test.js: -------------------------------------------------------------------------------- 1 | import Head from "next/head" 2 | // import Link from "next/link" 3 | import renderer from "react-test-renderer" 4 | 5 | import Layout from "../Layout.js" 6 | 7 | jest.mock("next/head") 8 | jest.mock("next/link", () => { 9 | const MockedLink = ({ children, ...rest }) => {children} 10 | return MockedLink 11 | }) 12 | 13 | describe("Layout", () => { 14 | Head.mockImplementation(({ children }) => children) 15 | 16 | it("renders collectly with title", () => { 17 | const tree = renderer 18 | .create() 19 | .toJSON() 20 | expect(tree).toMatchSnapshot() 21 | }) 22 | 23 | it("renders collectly with children", () => { 24 | const tree = renderer 25 | .create( 26 |
27 | Hello World 28 |
29 |
) 30 | .toJSON() 31 | expect(tree).toMatchSnapshot() 32 | }) 33 | }) 34 | -------------------------------------------------------------------------------- /.github/workflows/test-n-build.yml: -------------------------------------------------------------------------------- 1 | name: "テストとビルド" 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | 9 | env: 10 | NODE_VERSION: '18' 11 | 12 | jobs: 13 | jest: 14 | name: Run tests with jest 15 | runs-on: ubuntu-latest 16 | steps: 17 | - name: Checkout 18 | uses: actions/checkout@v3 19 | - name: Use Node.js 20 | uses: actions/setup-node@v3 21 | with: 22 | node-version: ${{ env.NODE_VERSION }} 23 | cache: 'npm' 24 | - name: Install packages 25 | run: npm ci 26 | - name: Run tests 27 | run: npm test 28 | - name: Run Linter 29 | run: npm run lint 30 | - name: Build 31 | run: | 32 | npm run build 33 | npm run export 34 | - name: Archive artifacts 35 | uses: actions/upload-artifact@v3 36 | with: 37 | name: out 38 | path: out 39 | -------------------------------------------------------------------------------- /components/Pager.js: -------------------------------------------------------------------------------- 1 | import Link from "next/link" 2 | 3 | const Pager = (props) => { 4 | const { total, page, perPage, hrefCallback } = props 5 | 6 | const prevPage = page > 1 ? page - 1 : null 7 | let nextPage = null 8 | if (page < Math.ceil(total / perPage)) { 9 | nextPage = page + 1 10 | } 11 | 12 | return ( 13 |
14 | 15 | {prevPage ? ( 16 | {prevPage} 17 | ) : null} 18 | 19 | {page} 20 | 21 | {nextPage ? ( 22 | {nextPage} 23 | ) : null} 24 | 25 | 26 | 38 |
39 | ) 40 | } 41 | 42 | export default Pager 43 | -------------------------------------------------------------------------------- /__tests__/index.test.js: -------------------------------------------------------------------------------- 1 | import Link from "next/link" 2 | import renderer from "react-test-renderer" 3 | 4 | import Layout from "../components/Layout" 5 | import Home from "../pages/index.js" 6 | 7 | jest.mock("next/link", () => { 8 | return ({ children, ...attrs }) => { 9 | return {children} 10 | } 11 | }) 12 | jest.mock("../components/Layout") 13 | 14 | describe("Home", () => { 15 | Layout.mockImplementation(({ children }) => children) 16 | 17 | it("renders correctly without posts nor archive", () => { 18 | const tree = renderer 19 | .create() 20 | .toJSON() 21 | expect(tree).toMatchSnapshot() 22 | }) 23 | 24 | it("renders correctly with posts and archive", () => { 25 | const posts = [ 26 | { 27 | title: "かきくけこ", 28 | slug: "kakikukeko", 29 | published: "2020/08/27", 30 | }, 31 | { 32 | title: "あいうえお", 33 | slug: "aiueo", 34 | published: "2018/05/27", 35 | }, 36 | ] 37 | const tree = renderer 38 | .create() 39 | .toJSON() 40 | expect(tree).toMatchSnapshot() 41 | }) 42 | }) 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 GOTO Hayato 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /pages/posts/[slug].js: -------------------------------------------------------------------------------- 1 | import fs from "fs" 2 | import path from "path" 3 | 4 | import Layout from "../../components/Layout" 5 | import Markdown from "../../components/Markdown" 6 | import { listContentFiles, readContentFile } from "../../lib/content-loader" 7 | 8 | export default function Post(params) { 9 | return ( 10 | 11 |
12 | {params.published} 13 |
14 |
15 | {params.content} 16 |
17 |
18 | ) 19 | } 20 | 21 | /** 22 | * ページコンポーネントで使用する値を用意する 23 | */ 24 | export async function getStaticProps({ params }) { 25 | const content = await readContentFile({ fs, slug: params.slug }) 26 | 27 | return { 28 | props: { 29 | ...content 30 | } 31 | } 32 | } 33 | 34 | /** 35 | * 有効な URL パラメータを全件返す 36 | */ 37 | export async function getStaticPaths() { 38 | const paths = listContentFiles({ fs }) 39 | .map((filename) => ({ 40 | params: { 41 | slug: path.parse(filename).name, 42 | } 43 | })) 44 | 45 | return { paths, fallback: false } 46 | } 47 | -------------------------------------------------------------------------------- /components/__tests__/Pager.test.js: -------------------------------------------------------------------------------- 1 | import renderer from "react-test-renderer" 2 | 3 | import Pager from "../Pager.js" 4 | 5 | jest.mock("next/link", () => { 6 | const MockedLink = ({ children, ...rest }) => {children} 7 | return MockedLink 8 | }) 9 | 10 | describe("Pager", () => { 11 | it("renders collectly with first page", () => { 12 | const tree = renderer 13 | .create( `/archive/${page}`} 18 | />) 19 | .toJSON() 20 | expect(tree).toMatchSnapshot() 21 | }) 22 | 23 | it("renders collectly with second page", () => { 24 | const tree = renderer 25 | .create( `/archive/${page}`} 30 | />) 31 | .toJSON() 32 | expect(tree).toMatchSnapshot() 33 | }) 34 | 35 | it("renders collectly with last page", () => { 36 | const tree = renderer 37 | .create( `/archive/${page}`} 42 | />) 43 | .toJSON() 44 | expect(tree).toMatchSnapshot() 45 | }) 46 | }) 47 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nextjs-blog-example-ja", 3 | "version": "13.0.0", 4 | "private": true, 5 | "scripts": { 6 | "dev": "next dev", 7 | "build": "next build", 8 | "start": "next start", 9 | "lint": "next lint", 10 | "export": "next export", 11 | "test": "jest" 12 | }, 13 | "dependencies": { 14 | "@next/font": "^13.1.6", 15 | "gray-matter": "^4.0.2", 16 | "next": "^13.1.6", 17 | "react": "^18.2.0", 18 | "react-dom": "^18.2.0", 19 | "react-markdown": "^8.0.5", 20 | "react-syntax-highlighter": "^15.5.0", 21 | "rehype-raw": "^6.1.0", 22 | "remark-gfm": "^3.0.1" 23 | }, 24 | "devDependencies": { 25 | "@babel/core": "^7.14.5", 26 | "@babel/preset-env": "^7.14.5", 27 | "@babel/preset-react": "^7.14.5", 28 | "@testing-library/jest-dom": "^5.14.1", 29 | "@testing-library/react": "^13.4.0", 30 | "babel-jest": "^28.1.3", 31 | "eslint": "8.33.0", 32 | "eslint-config-next": "13.1.6", 33 | "identity-obj-proxy": "^3.0.0", 34 | "jest": "^28.1.3", 35 | "jest-environment-jsdom": "^28.1.3", 36 | "react-test-renderer": "^18.2.0" 37 | }, 38 | "engines": { 39 | "node": ">=18.0.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /components/__tests__/Markdown.test.js: -------------------------------------------------------------------------------- 1 | import renderer from "react-test-renderer" 2 | 3 | import Markdown from "../Markdown.js" 4 | 5 | jest.mock("next/link", () => { 6 | const MockedLink = ({ children, ...rest }) => {children} 7 | return MockedLink 8 | }) 9 | 10 | describe("Markdown", () => { 11 | it("renders collectly with simple markdown", () => { 12 | const tree = renderer 13 | .create({` 14 | # 見出し 1 15 | 16 | # 見出し 2 17 | 18 | - これは 19 | - リスト 20 | - です 21 | 22 | テーブル: 23 | 24 | | 西 | 東 | 25 | | --- | --- | 26 | | 西山 | 東山 | 27 | 28 | `}) 29 | .toJSON() 30 | expect(tree).toMatchSnapshot() 31 | }) 32 | 33 | it("renders collectly with html", () => { 34 | const tree = renderer 35 | .create({` 36 |

テーブルサンプル

37 |
38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 |
カラム 1カラム 2
1.11.2
2.12.2
56 |
57 | `}
) 58 | .toJSON() 59 | expect(tree).toMatchSnapshot() 60 | }) 61 | 62 | }) 63 | -------------------------------------------------------------------------------- /pages/_document.js: -------------------------------------------------------------------------------- 1 | import Document, { Html, Head, Main, NextScript } from 'next/document' 2 | 3 | import { GA_TRACKING_ID } from '../lib/gtag' 4 | 5 | class MyDocument extends Document { 6 | render() { 7 | return ( 8 | 9 | 10 | { /* gtag / Google Analytics を利用する */} 11 | { 12 | GA_TRACKING_ID &&