From ff801fb32abb646438327dbcbe1433348002f51c Mon Sep 17 00:00:00 2001 From: Konstantinos Leimonis Date: Sun, 6 Jun 2021 19:49:11 +0100 Subject: [PATCH 01/84] [WIP] Add cms notion integration example - index page --- examples/cms-notion/.env.local.example | 2 + examples/cms-notion/.gitignore | 34 ++++ examples/cms-notion/README.md | 172 ++++++++++++++++++ examples/cms-notion/components/alert.js | 42 +++++ examples/cms-notion/components/avatar.js | 12 ++ examples/cms-notion/components/container.js | 3 + examples/cms-notion/components/cover-image.js | 25 +++ examples/cms-notion/components/date.js | 7 + examples/cms-notion/components/footer.js | 30 +++ examples/cms-notion/components/header.js | 12 ++ examples/cms-notion/components/hero-post.js | 44 +++++ examples/cms-notion/components/intro.js | 28 +++ examples/cms-notion/components/layout.js | 16 ++ .../components/markdown-styles.module.css | 18 ++ examples/cms-notion/components/meta.js | 42 +++++ .../cms-notion/components/more-stories.js | 24 +++ examples/cms-notion/components/post-body.js | 13 ++ examples/cms-notion/components/post-header.js | 27 +++ .../cms-notion/components/post-preview.js | 39 ++++ examples/cms-notion/components/post-title.js | 7 + .../components/section-separator.js | 3 + examples/cms-notion/lib/api.js | 157 ++++++++++++++++ examples/cms-notion/lib/constants.js | 5 + examples/cms-notion/package.json | 23 +++ examples/cms-notion/pages/_app.js | 7 + examples/cms-notion/pages/_document.js | 15 ++ examples/cms-notion/pages/api/exit-preview.js | 8 + examples/cms-notion/pages/api/posts.js | 57 ++++++ examples/cms-notion/pages/api/preview.js | 41 +++++ examples/cms-notion/pages/index.js | 44 +++++ examples/cms-notion/pages/posts/[slug].js | 73 ++++++++ examples/cms-notion/postcss.config.js | 19 ++ .../0f5a110c-7e12-4bc3-8f5e-b207c29eeedb.jpeg | Bin 0 -> 190878 bytes .../538d7d3a-6efb-48cd-84fd-6da8700a12a8.jpeg | Bin 0 -> 59229 bytes .../b73b3510-6d65-44ae-8986-3d5c47294780.jpeg | Bin 0 -> 118218 bytes .../public/favicon/android-chrome-192x192.png | Bin 0 -> 4795 bytes .../public/favicon/android-chrome-512x512.png | Bin 0 -> 14640 bytes .../public/favicon/apple-touch-icon.png | Bin 0 -> 1327 bytes .../public/favicon/browserconfig.xml | 9 + .../public/favicon/favicon-16x16.png | Bin 0 -> 595 bytes .../public/favicon/favicon-32x32.png | Bin 0 -> 880 bytes .../cms-notion/public/favicon/favicon.ico | Bin 0 -> 15086 bytes .../public/favicon/mstile-150x150.png | Bin 0 -> 3567 bytes .../public/favicon/safari-pinned-tab.svg | 33 ++++ .../public/favicon/site.webmanifest | 19 ++ examples/cms-notion/styles/index.css | 5 + examples/cms-notion/tailwind.config.js | 32 ++++ examples/cms-notion/types/author.json | 18 ++ examples/cms-notion/types/post.json | 52 ++++++ 49 files changed, 1217 insertions(+) create mode 100644 examples/cms-notion/.env.local.example create mode 100644 examples/cms-notion/.gitignore create mode 100644 examples/cms-notion/README.md create mode 100644 examples/cms-notion/components/alert.js create mode 100644 examples/cms-notion/components/avatar.js create mode 100644 examples/cms-notion/components/container.js create mode 100644 examples/cms-notion/components/cover-image.js create mode 100644 examples/cms-notion/components/date.js create mode 100644 examples/cms-notion/components/footer.js create mode 100644 examples/cms-notion/components/header.js create mode 100644 examples/cms-notion/components/hero-post.js create mode 100644 examples/cms-notion/components/intro.js create mode 100644 examples/cms-notion/components/layout.js create mode 100644 examples/cms-notion/components/markdown-styles.module.css create mode 100644 examples/cms-notion/components/meta.js create mode 100644 examples/cms-notion/components/more-stories.js create mode 100644 examples/cms-notion/components/post-body.js create mode 100644 examples/cms-notion/components/post-header.js create mode 100644 examples/cms-notion/components/post-preview.js create mode 100644 examples/cms-notion/components/post-title.js create mode 100644 examples/cms-notion/components/section-separator.js create mode 100644 examples/cms-notion/lib/api.js create mode 100644 examples/cms-notion/lib/constants.js create mode 100644 examples/cms-notion/package.json create mode 100644 examples/cms-notion/pages/_app.js create mode 100644 examples/cms-notion/pages/_document.js create mode 100644 examples/cms-notion/pages/api/exit-preview.js create mode 100644 examples/cms-notion/pages/api/posts.js create mode 100644 examples/cms-notion/pages/api/preview.js create mode 100644 examples/cms-notion/pages/index.js create mode 100644 examples/cms-notion/pages/posts/[slug].js create mode 100644 examples/cms-notion/postcss.config.js create mode 100644 examples/cms-notion/public/cover-images/0f5a110c-7e12-4bc3-8f5e-b207c29eeedb.jpeg create mode 100644 examples/cms-notion/public/cover-images/538d7d3a-6efb-48cd-84fd-6da8700a12a8.jpeg create mode 100644 examples/cms-notion/public/cover-images/b73b3510-6d65-44ae-8986-3d5c47294780.jpeg create mode 100644 examples/cms-notion/public/favicon/android-chrome-192x192.png create mode 100644 examples/cms-notion/public/favicon/android-chrome-512x512.png create mode 100644 examples/cms-notion/public/favicon/apple-touch-icon.png create mode 100644 examples/cms-notion/public/favicon/browserconfig.xml create mode 100644 examples/cms-notion/public/favicon/favicon-16x16.png create mode 100644 examples/cms-notion/public/favicon/favicon-32x32.png create mode 100644 examples/cms-notion/public/favicon/favicon.ico create mode 100644 examples/cms-notion/public/favicon/mstile-150x150.png create mode 100644 examples/cms-notion/public/favicon/safari-pinned-tab.svg create mode 100644 examples/cms-notion/public/favicon/site.webmanifest create mode 100644 examples/cms-notion/styles/index.css create mode 100644 examples/cms-notion/tailwind.config.js create mode 100644 examples/cms-notion/types/author.json create mode 100644 examples/cms-notion/types/post.json diff --git a/examples/cms-notion/.env.local.example b/examples/cms-notion/.env.local.example new file mode 100644 index 0000000000000..51f6650723fb5 --- /dev/null +++ b/examples/cms-notion/.env.local.example @@ -0,0 +1,2 @@ +NOTION_API_TOKEN= +NOTION_DATABASE_ID= \ No newline at end of file diff --git a/examples/cms-notion/.gitignore b/examples/cms-notion/.gitignore new file mode 100644 index 0000000000000..1437c53f70bc2 --- /dev/null +++ b/examples/cms-notion/.gitignore @@ -0,0 +1,34 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# next.js +/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# local env files +.env.local +.env.development.local +.env.test.local +.env.production.local + +# vercel +.vercel diff --git a/examples/cms-notion/README.md b/examples/cms-notion/README.md new file mode 100644 index 0000000000000..bf5755fd92860 --- /dev/null +++ b/examples/cms-notion/README.md @@ -0,0 +1,172 @@ +# A statically generated blog example using Next.js and Prismic + +This example showcases Next.js's [Static Generation](https://nextjs.org/docs/basic-features/pages) feature using [Prismic](https://prismic.io/) as the data source. + +## Demo + +### [https://next-blog-prismic.vercel.app/](https://next-blog-prismic.vercel.app/) + +## Deploy your own + +Once you have access to [the environment variables you'll need](#step-5-set-up-environment-variables), deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example): + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/cms-prismic&project-name=cms-prismic&repository-name=cms-prismic&env=PRISMIC_API_TOKEN,PRISMIC_REPOSITORY_NAME&envDescription=Required%20to%20connect%20the%20app%20with%20Prismic&envLink=https://vercel.link/cms-prismic-env) + +### Related examples + +- [WordPress](/examples/cms-wordpress) +- [DatoCMS](/examples/cms-datocms) +- [Sanity](/examples/cms-sanity) +- [TakeShape](/examples/cms-takeshape) +- [Contentful](/examples/cms-contentful) +- [Strapi](/examples/cms-strapi) +- [Agility CMS](/examples/cms-agilitycms) +- [Cosmic](/examples/cms-cosmic) +- [ButterCMS](/examples/cms-buttercms) +- [Storyblok](/examples/cms-storyblok) +- [Kontent](/examples/cms-kontent) +- [Ghost](/examples/cms-ghost) +- [Blog Starter](/examples/blog-starter) + +## How to use + +Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example: + +```bash +npx create-next-app --example cms-prismic cms-prismic-app +# or +yarn create next-app --example cms-prismic cms-prismic-app +``` + +## Configuration + +### Step 1. Create an account and a repository on Prismic + +First, [create an account on Prismic](https://prismic.io/). + +After creating an account, create a **repository** from the [dashboard](https://prismic.io/dashboard/) and assign to it any name of your liking. + +### Step 2. Create an `author` type + +From the repository page, create a new **custom type**: + +- The name should be `author`. + +Next, add these fields (you don't have to modify the settings): + +- `name` - **Key Text** field +- `picture` - **Image** field + +Alternatively, you can copy the JSON in [`types/author.json`](types/author.json), then click on **JSON editor** and paste it there. + +Save the type and continue. + +### Step 3. Create a `post` type + +From the repository page, create a new **custom type**: + +- The name should be `post`. + +Next, add these fields (you don't have to modify the settings unless specified): + +- `title` - **Title** field +- `content` - **Rich Text** field +- `excerpt` - **Key Text** field +- `coverimage` - **Image** field +- `date` - **Date** field +- `author` - **Content relationship** field, you may also add `author` to the **Constraint to custom type** option to only accept documents from the `author` type. +- `slug` - **UID** field. + +Alternatively, you can copy the JSON in [`types/post.json`](types/post.json), then click on **JSON editor** and paste it there. + +Save the type and continue. + +### Step 4. Populate Content + +Go to the **Content** page, it's in the menu at the top left, then click on **Create new** and select the **author** type: + +- You just need **1 author document**. +- Use dummy data for the text. +- For the image, you can download one from [Unsplash](https://unsplash.com/). + +Next, select **Post** and create a new document. + +- We recommend creating at least **2 Post documents**. +- Use dummy data for the text. +- You can write markdown for the **content** field. +- For images, you can download them from [Unsplash](https://unsplash.com/). +- Pick the **author** you created earlier. + +**Important:** For each document, you need to click **Publish** after saving. If not, the document will be in the draft state. + +### Step 5. Set up environment variables + +Follow the instructions in [this post](https://intercom.help/prismicio/en/articles/1036153-generating-an-access-token) to generate a new access token. + +Next, copy the `.env.local.example` file in this directory to `.env.local` (which will be ignored by Git): + +```bash +cp .env.local.example .env.local +``` + +Then set each variable on `.env.local`: + +- `PRISMIC_API_TOKEN` should be the **Permanent access token** you just created +- `PRISMIC_REPOSITORY_NAME` is the name of your repository (the one in the URL) +- `PRISMIC_REPOSITORY_LOCALE` is the locale of your repository. Defaults to `en-us` + +Your `.env.local` file should look like this: + +```bash +PRISMIC_API_TOKEN=... +PRISMIC_REPOSITORY_NAME=... +PRISMIC_REPOSITORY_LOCALE=... +``` + +Make sure the locale matches your settings in the Prismic dashboard. + +### Step 6. Run Next.js in development mode + +```bash +npm install +npm run dev + +# or + +yarn install +yarn dev +``` + +Your blog should be up and running on [http://localhost:3000](http://localhost:3000)! If it doesn't work, post on [GitHub discussions](https://github.com/vercel/next.js/discussions). + +### Step 7. Try preview mode + +On your repository page, go to **Settings**, click on **Previews** and then **Create a New Preview** for development, fill the form like so: + +- **Site Name**: may be anything, like `development` +- **Domain of Your Application**: `http://localhost:3000` +- **Link Resolver**: `/api/preview` + +Once saved, go to one of the posts you've created and: + +- **Update the title**. For example, you can add `[Draft]` in front of the title. +- Click **Save**, but **DO NOT** click **Publish**. By doing this, the post will be in draft state. +- Right next to the **Publish** button you should see the **Preview** button, displayed with an eye icon. Click on it! + +You should now be able to see the updated title. To exit preview mode, you can click on **Click here to exit preview mode** at the top of the page. + +### Step 8. Deploy on Vercel + +You can deploy this app to the cloud with [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). + +#### Deploy Your Local Project + +To deploy your local project to Vercel, push it to GitHub/GitLab/Bitbucket and [import to Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example). + +**Important**: When you import your project on Vercel, make sure to click on **Environment Variables** and set them to match your `.env.local` file. + +#### Deploy from Our Template + +Alternatively, you can deploy using our template by clicking on the Deploy button below. + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/cms-prismic&project-name=cms-prismic&repository-name=cms-prismic&env=PRISMIC_API_TOKEN,PRISMIC_REPOSITORY_NAME&envDescription=Required%20to%20connect%20the%20app%20with%20Prismic&envLink=https://vercel.link/cms-prismic-env) diff --git a/examples/cms-notion/components/alert.js b/examples/cms-notion/components/alert.js new file mode 100644 index 0000000000000..2994d59fbd321 --- /dev/null +++ b/examples/cms-notion/components/alert.js @@ -0,0 +1,42 @@ +import Container from './container' +import cn from 'classnames' +import { EXAMPLE_PATH } from '../lib/constants' + +export default function Alert({ preview }) { + return ( +
+ +
+ {preview ? ( + <> + This is page is a preview.{' '} + + Click here + {' '} + to exit preview mode. + + ) : ( + <> + The source code for this blog is{' '} + + available on GitHub + + . + + )} +
+
+
+ ) +} diff --git a/examples/cms-notion/components/avatar.js b/examples/cms-notion/components/avatar.js new file mode 100644 index 0000000000000..50847843ff60d --- /dev/null +++ b/examples/cms-notion/components/avatar.js @@ -0,0 +1,12 @@ +export default function Avatar({ name, picture }) { + return ( +
+ {name[0].text} +
{name}
+
+ ) +} diff --git a/examples/cms-notion/components/container.js b/examples/cms-notion/components/container.js new file mode 100644 index 0000000000000..fc1c29dfb0747 --- /dev/null +++ b/examples/cms-notion/components/container.js @@ -0,0 +1,3 @@ +export default function Container({ children }) { + return
{children}
+} diff --git a/examples/cms-notion/components/cover-image.js b/examples/cms-notion/components/cover-image.js new file mode 100644 index 0000000000000..9f52f4e51c199 --- /dev/null +++ b/examples/cms-notion/components/cover-image.js @@ -0,0 +1,25 @@ +import cn from 'classnames' +import Link from 'next/link' + +export default function CoverImage({ title, url, slug }) { + const image = ( + {`Cover + ) + return ( +
+ {slug ? ( + + {image} + + ) : ( + image + )} +
+ ) +} diff --git a/examples/cms-notion/components/date.js b/examples/cms-notion/components/date.js new file mode 100644 index 0000000000000..c77dbb23fad83 --- /dev/null +++ b/examples/cms-notion/components/date.js @@ -0,0 +1,7 @@ +import { format } from 'date-fns' +// import { Date as PrismicDate } from 'prismic-reactjs' + +export default function Date({ dateString }) { + // const date = PrismicDate(dateString) + return +} diff --git a/examples/cms-notion/components/footer.js b/examples/cms-notion/components/footer.js new file mode 100644 index 0000000000000..da9eed88ec263 --- /dev/null +++ b/examples/cms-notion/components/footer.js @@ -0,0 +1,30 @@ +import Container from './container' +import { EXAMPLE_PATH } from '../lib/constants' + +export default function Footer() { + return ( + + ) +} diff --git a/examples/cms-notion/components/header.js b/examples/cms-notion/components/header.js new file mode 100644 index 0000000000000..562e7e3eebb6a --- /dev/null +++ b/examples/cms-notion/components/header.js @@ -0,0 +1,12 @@ +import Link from 'next/link' + +export default function Header() { + return ( +

+ + Blog + + . +

+ ) +} diff --git a/examples/cms-notion/components/hero-post.js b/examples/cms-notion/components/hero-post.js new file mode 100644 index 0000000000000..4a218ee32c691 --- /dev/null +++ b/examples/cms-notion/components/hero-post.js @@ -0,0 +1,44 @@ +import Link from 'next/link' +// import { RichText } from 'prismic-reactjs' +import Avatar from '../components/avatar' +import Date from '../components/date' +import CoverImage from '../components/cover-image' + +export default function HeroPost({ + title, + coverImageUrl, + date, + excerpt, + author, + slug, +}) { + return ( +
+
+ +
+
+
+

+ + + {title} + + +

+
+ +
+
+
+

{excerpt}

+ {author && } +
+
+
+ ) +} diff --git a/examples/cms-notion/components/intro.js b/examples/cms-notion/components/intro.js new file mode 100644 index 0000000000000..5931b3c5961bd --- /dev/null +++ b/examples/cms-notion/components/intro.js @@ -0,0 +1,28 @@ +import { CMS_NAME, CMS_URL } from '../lib/constants' + +export default function Intro() { + return ( +
+

+ Blog. +

+

+ A statically generated blog example using{' '} + + Next.js + {' '} + and{' '} + + {CMS_NAME} + + . +

+
+ ) +} diff --git a/examples/cms-notion/components/layout.js b/examples/cms-notion/components/layout.js new file mode 100644 index 0000000000000..99d95353131e0 --- /dev/null +++ b/examples/cms-notion/components/layout.js @@ -0,0 +1,16 @@ +import Alert from '../components/alert' +import Footer from '../components/footer' +import Meta from '../components/meta' + +export default function Layout({ preview, children }) { + return ( + <> + +
+ +
{children}
+
+