-
Notifications
You must be signed in to change notification settings - Fork 0
feat(examples): add blog example #199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dylnslck
wants to merge
5
commits into
main
Choose a base branch
from
blog-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+537
−4
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app). | ||
|
|
||
| ## Getting Started | ||
|
|
||
| First, run the development server: | ||
|
|
||
| ```bash | ||
| npm run dev | ||
| # or | ||
| yarn dev | ||
| # or | ||
| pnpm dev | ||
| # or | ||
| bun dev | ||
| ``` | ||
|
|
||
| Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. | ||
|
|
||
| You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. | ||
|
|
||
| This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel. | ||
|
|
||
| ## Learn More | ||
|
|
||
| To learn more about Next.js, take a look at the following resources: | ||
|
|
||
| - [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. | ||
| - [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. | ||
|
|
||
| You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome! | ||
|
|
||
| ## Deploy on Vercel | ||
|
|
||
| The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. | ||
|
|
||
| Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import type { NextConfig } from 'next' | ||
|
|
||
| const nextConfig: NextConfig = { | ||
| /* config options here */ | ||
| } | ||
|
|
||
| export default nextConfig |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| "name": "@deepdish/blog", | ||
| "version": "0.0.1", | ||
| "private": true, | ||
| "scripts": { | ||
| "dev": "next dev --turbopack", | ||
| "build": "next build", | ||
| "start": "next start" | ||
| }, | ||
| "dependencies": { | ||
| "@deepdish/cms": "workspace:0.8.0", | ||
| "@deepdish/nextjs": "workspace:0.8.0", | ||
| "@deepdish/resolvers": "workspace:0.13.0", | ||
| "@deepdish/ui": "workspace:0.15.0", | ||
| "next": "15.1.4", | ||
| "react": "catalog:react", | ||
| "react-dom": "catalog:react", | ||
| "valibot": "1.0.0-rc.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@tailwindcss/postcss": "catalog:tailwind", | ||
| "@types/node": "catalog:repo", | ||
| "@types/react": "catalog:react", | ||
| "@types/react-dom": "catalog:react", | ||
| "tailwindcss": "catalog:tailwind", | ||
| "typescript": "catalog:repo" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| const config = { | ||
| plugins: ['@tailwindcss/postcss'], | ||
| } | ||
|
|
||
| export default config |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { BlogPost } from '@/cms' | ||
|
|
||
| export default async function BlogArticle({ | ||
| params, | ||
| }: { | ||
| params: Promise<{ slug: string }> | ||
| }) { | ||
| const { slug } = await params | ||
|
|
||
| return ( | ||
| <div className="h-full overflow-y-auto"> | ||
| <div className="container mx-auto mt-8"> | ||
| <BlogPost deepdish={{ key: slug }} /> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { BlogCard } from '@/cms' | ||
|
|
||
| export default function Blog() { | ||
| return ( | ||
| <div className="h-full overflow-y-auto"> | ||
| <div className="container mx-auto"> | ||
| <h1 className="my-4 text-2xl text-gray-800">Johnny Pizza Blog</h1> | ||
| <p className="my-4 text-gray-600"> | ||
| Check out our cool blog articles and learn more about our pizza! | ||
| </p> | ||
| <div className="flex flex-col gap-4"> | ||
| <BlogCard deepdish={{ collection: '*' }} /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| @import "tailwindcss"; | ||
|
|
||
| :root { | ||
| --background: #ffffff; | ||
| --foreground: #171717; | ||
| } | ||
|
|
||
| @theme inline { | ||
| --color-background: var(--background); | ||
| --color-foreground: var(--foreground); | ||
| --font-sans: var(--font-geist-sans); | ||
| --font-mono: var(--font-geist-mono); | ||
| } | ||
|
|
||
| body { | ||
| background: var(--background); | ||
| color: var(--foreground); | ||
| font-family: Arial, Helvetica, sans-serif; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import type { Metadata } from 'next' | ||
| import { Geist, Geist_Mono } from 'next/font/google' | ||
| import './globals.css' | ||
| import { Navbar } from '@/components/navbar' | ||
|
|
||
| import { DeepDishProvider } from '@/cms' | ||
|
|
||
| const geistSans = Geist({ | ||
| variable: '--font-geist-sans', | ||
| subsets: ['latin'], | ||
| }) | ||
|
|
||
| const geistMono = Geist_Mono({ | ||
| variable: '--font-geist-mono', | ||
| subsets: ['latin'], | ||
| }) | ||
|
|
||
| export const metadata: Metadata = { | ||
| title: 'DeepDish Blog Example', | ||
| description: 'Blog example showcasing the DeepDish CMS', | ||
| } | ||
|
|
||
| export default function RootLayout({ | ||
| children, | ||
| }: Readonly<{ | ||
| children: React.ReactNode | ||
| }>) { | ||
| return ( | ||
| <html lang="en"> | ||
| <body | ||
| className={`${geistSans.variable} ${geistMono.variable} antialiased h-screen flex flex-col`} | ||
| > | ||
| <DeepDishProvider> | ||
| <Navbar /> | ||
| <div className="flex-1 overflow-y-auto">{children}</div> | ||
| </DeepDishProvider> | ||
| </body> | ||
| </html> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { Paragraph } from '@/cms' | ||
| import Link from 'next/link' | ||
|
|
||
| export default function Home() { | ||
| return ( | ||
| <div className="h-full flex items-center justify-center"> | ||
| <div className="max-w-prose text-center flex flex-col gap-4"> | ||
| <Paragraph | ||
| deepdish={{ key: 'home/headline' }} | ||
| className="text-xl text-gray-800 font-bold" | ||
| > | ||
| Welcome to Johnny Pizza | ||
| </Paragraph> | ||
| <Paragraph | ||
| deepdish={{ key: 'home/sub-headline' }} | ||
| className="text-gray-600" | ||
| > | ||
| We pride ourselves on our pizza. We use only the freshest ingredients | ||
| and our team of expert chefs to create the best pizza in town. | ||
| </Paragraph> | ||
| <Link href="/blog">Check out our blog!</Link> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import fs from 'node:fs/promises' | ||
| import { truncate } from '@/utils' | ||
| import { DeepDishProvider as InternalDeepDishProvider } from '@deepdish/cms' | ||
| import { getBaseUrl } from '@deepdish/cms/vercel' | ||
| import { createJsonResolver } from '@deepdish/resolvers/json' | ||
| import { createComponents } from '@deepdish/ui/components' | ||
| import { configure } from '@deepdish/ui/config' | ||
| import type { IntrinsicElement, SetChildren } from '@deepdish/ui/deepdish' | ||
| import Link from 'next/link' | ||
| import * as v from 'valibot' | ||
|
|
||
| // TODO: get local data dir to work | ||
| export const contentPaths = { | ||
| blog: '/tmp/deepdish_blog_blog.json', | ||
| text: '/tmp/deepdish_blog_text.json', | ||
| } as const | ||
|
|
||
| async function initContent() { | ||
| for (const path of Object.values(contentPaths)) { | ||
| const exists = await fs | ||
| .stat(path) | ||
| .then(() => true) | ||
| .catch(() => false) | ||
|
|
||
| if (!exists) { | ||
| await fs.writeFile(path, JSON.stringify({})) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const blogSchema = v.object({ | ||
| title: v.string(), | ||
| author: v.object({ | ||
| name: v.string(), | ||
| email: v.string(), | ||
| }), | ||
| body: v.string(), | ||
| slug: v.string(), | ||
| }) | ||
|
|
||
| const textSchema = v.string() | ||
|
|
||
| const contracts = { | ||
| blog: { | ||
| resolver: createJsonResolver(contentPaths.blog, blogSchema, { | ||
| maxBatchSize: 10, | ||
| }), | ||
| schema: blogSchema, | ||
| }, | ||
| text: { | ||
| resolver: createJsonResolver(contentPaths.text, textSchema, { | ||
| maxBatchSize: 10, | ||
| }), | ||
| schema: textSchema, | ||
| }, | ||
| } | ||
|
|
||
| async function cms() { | ||
| await initContent() | ||
|
|
||
| await configure({ | ||
| contracts, | ||
| logging: { | ||
| enabled: process.env.NODE_ENV === 'development', | ||
| }, | ||
| settings: { | ||
| baseUrl: getBaseUrl({}), | ||
| draft: true, | ||
| }, | ||
| }) | ||
|
|
||
| return createComponents(contracts) | ||
| } | ||
|
|
||
| const components = await cms() | ||
|
|
||
| const Blog = components.blog | ||
| const Text = components.text | ||
|
|
||
| type DeepDishWithoutContract = | ||
| | { | ||
| key: string | ||
| collection?: never | ||
| } | ||
| | { | ||
| collection: string | ||
| } | ||
|
|
||
| type ElementProps<E extends IntrinsicElement, C = undefined> = SetChildren< | ||
| JSX.IntrinsicElements[E], | ||
| C | ||
| > & { | ||
| deepdish: DeepDishWithoutContract | ||
| } | ||
|
|
||
| type ParagraphProps<E extends IntrinsicElement> = ElementProps<E, string> | ||
|
|
||
| export function Paragraph(props: ParagraphProps<'p'>) { | ||
| return ( | ||
| <Text | ||
| deepdish={{ | ||
| ...props.deepdish, | ||
| contract: 'text', | ||
| }} | ||
| fallback={props.children} | ||
| render={async (content) => { | ||
| return <p {...props}>{content}</p> | ||
| }} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| export function BlogCard(props: { | ||
| deepdish: DeepDishWithoutContract | ||
| }) { | ||
| return ( | ||
| <Blog | ||
| deepdish={{ | ||
| ...props.deepdish, | ||
| contract: 'blog', | ||
| }} | ||
| render={async (content) => { | ||
| if (!content) { | ||
| return <div>Blog Card Placeholder</div> | ||
| } | ||
|
|
||
| return ( | ||
| <div className="flex flex-col gap-2 pl-4 border-l-2 border-red-500"> | ||
| <h1 className="text-xl text-gray-800 font-bold">{content.title}</h1> | ||
| <p className="text-sm text-gray-600 italic"> | ||
| Written by {content.author.name} | ||
| </p> | ||
| <p className="text-gray-500">{truncate(content.body, 100)}</p> | ||
| <Link | ||
| className="text-red-500 hover:text-red-400" | ||
| href={`/blog/${content.slug}`} | ||
| > | ||
| Read article | ||
| </Link> | ||
| </div> | ||
| ) | ||
| }} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| export function BlogPost(props: { | ||
| deepdish: DeepDishWithoutContract | ||
| }) { | ||
| return ( | ||
| <Blog | ||
| deepdish={{ | ||
| ...props.deepdish, | ||
| contract: 'blog', | ||
| }} | ||
| render={async (content) => { | ||
| if (!content) { | ||
| return <div>Blog Article Placeholder</div> | ||
| } | ||
|
|
||
| return ( | ||
| <div className="container mx-auto"> | ||
| <h1 className="my-4 text-2xl text-gray-800 font-bold"> | ||
| {content.title} | ||
| </h1> | ||
| <p className="my-4 text-gray-600"> | ||
| Written by {content.author.name} | ||
| </p> | ||
| <p className="my-4 text-gray-500">{content.body}</p> | ||
| </div> | ||
| ) | ||
| }} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| export function DeepDishProvider(props: { | ||
| children: React.ReactNode | ||
| }) { | ||
| return <InternalDeepDishProvider {...props} /> | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is brittle. The
slugproperty of theblogSchemacan ultimately be anything, but it has to match the key exactly AND be URL-safe. It would be nice to support some sort key aliasing or something so that people can have nice slugs that correspond 1-1 with a DeepDish key.