Skip to content

Commit

Permalink
refactor(content): migrate to content-collections (#235)
Browse files Browse the repository at this point in the history
* refactor(content): migrate to content-collections

* refactor: make reusable collection definition

* refactor: adjust some field to be optional
  • Loading branch information
sozonome authored Jul 14, 2024
1 parent 3608511 commit ceec54c
Show file tree
Hide file tree
Showing 41 changed files with 505 additions and 321 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ sitemap*.xml

# Turbo
.turbo

# Contents
.content-collections
122 changes: 122 additions & 0 deletions content-collections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { defineCollection, defineConfig } from '@content-collections/core';
import readingTime from 'reading-time';

const projects = defineCollection({
name: 'projects',
directory: 'content/projects',
include: '**/*.md',
schema: (z) => ({
title: z.string(),
description: z.string(),
published: z.boolean().catch(true),
/** will be shown at home */
highlight: z.boolean().optional(),
/** shown at project list frontpage */
featured: z.boolean().optional(),
sznmApps: z.boolean().optional(),
stacks: z.array(z.string()).optional(),
date: z.string(),
projectLink: z.string().optional(),
repoLink: z.string().optional(),
appStoreLink: z.string().optional(),
playStoreLink: z.string().optional(),
icon: z.string().optional(),
thumbnail: z.string().optional(),
thumbnailDark: z.string().optional(),
projectType: z.string().optional(),
id: z.string().optional(),
}),
transform: (doc) => ({
...doc,
id: doc._meta.fileName.replace(/\.md$|\.mdx$/, ''),
}),
});

const posts = defineCollection({
name: 'posts',
directory: 'content/posts',
include: '**/*.md',
schema: (z) => ({
title: z.string(),
date: z.string(),
cover: z.string(),
thumbnail: z.string().optional(),
description: z.string().optional(),
legacyID: z.string().optional(),
published: z.boolean().catch(true),
tags: z.array(z.string()),
readTime: z
.object({
text: z.string(),
minutes: z.string(),
time: z.string(),
words: z.string(),
})
.optional(),
cover_image: z.string().optional(),
id: z.string().optional(),
}),
transform: (doc) => ({
...doc,
readTime: Object(readingTime(doc.content)),
id: doc._meta.fileName.replace(/\.md$|\.mdx$/, ''),
}),
});

type NoteCollectionDefinitionParams = {
name: string;
directory: string;
};

const noteCollectionDefinition = ({
name,
directory,
}: NoteCollectionDefinitionParams) =>
defineCollection({
name,
directory,
include: '**/*.md',
schema: (z) => ({
title: z.string(),
description: z.string(),
published: z.boolean().catch(true),
date: z.string(),
tags: z.array(z.string()),
id: z.string().optional(),
}),
transform: (doc) => ({
...doc,
id: doc._meta.fileName.replace(/\.md$|\.mdx$/, ''),
}),
});

const notes = noteCollectionDefinition({
name: 'notes',
directory: 'content/notes',
});

const todayILearns = noteCollectionDefinition({
name: 'todayILearns',
directory: 'content/til',
});

const testimonies = defineCollection({
name: 'testimonies',
directory: 'content/testimonies',
include: '**/*.md',
schema: (z) => ({
name: z.string(),
title: z.string(),
year: z.string(),
linkedin: z.string().optional(),
id: z.string().optional(),
}),
transform: (doc) => ({
...doc,
id: doc._meta.fileName.replace(/\.md$|\.mdx$/, ''),
}),
});

export default defineConfig({
collections: [projects, posts, notes, todayILearns, testimonies],
});
7 changes: 6 additions & 1 deletion knip.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type { KnipConfig } from 'knip';

const config: KnipConfig = {
entry: ['src/app/layout.tsx', 'src/middleware.ts', 'vitest.*'],
entry: [
'src/app/layout.tsx',
'src/middleware.ts',
'vitest.*',
'content-collections.ts',
],
project: ['src/**/*.{ts,tsx,js,jsx}'],
};

Expand Down
122 changes: 63 additions & 59 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
});
const { withContentCollections } = require('@content-collections/next');
const million = require('million/compiler');

/**
Expand Down Expand Up @@ -65,65 +66,68 @@ const portfolioZoneUrl = process.env.PORTFOLIO_ZONE_URL ?? '';
const portfolioDataPrefix = `/_next/data/${process.env.PORTFOLIO_BUILD_ID}`;

/** @type {import('next').NextConfig} */
module.exports = million.next(
withBundleAnalyzer({
reactStrictMode: true,
headers: async () => {
return [
{
source: '/',
headers: securityHeaders,
},
{
source: '/:path*',
headers: securityHeaders,
},
];
},
eslint: {
dirs: ['src'],
},
transpilePackages: ['lodash-es'],
experimental: {
scrollRestoration: true,
},
redirects: () => {
return [
{
source: '/snippets/:id',
destination: '/notes/:id',
permanent: false,
},
];
},
rewrites: () => ({
beforeFiles: [
{
source: '/portfolio',
destination: `${portfolioZoneUrl}/portfolio`,
},
{
source: '/portfolio/:path*',
destination: `${portfolioZoneUrl}/portfolio/:path*`,
},
{
source: `${portfolioDataPrefix}/portfolio.json`,
destination: `${portfolioZoneUrl}${portfolioDataPrefix}/portfolio.json`,
},
{
source: `${portfolioDataPrefix}/portfolio/:path*`,
destination: `${portfolioZoneUrl}${portfolioDataPrefix}/portfolio/:path*`,
},
{
source: '/api/portfolio/:path*',
destination: `${portfolioZoneUrl}/api/portfolio/:path*`,
},
{
source: '/assets/portfolio/:path*',
destination: `${portfolioZoneUrl}/assets/portfolio/:path*`,
},
],
}),
const nextConfig = {
reactStrictMode: true,
headers: async () => {
return [
{
source: '/',
headers: securityHeaders,
},
{
source: '/:path*',
headers: securityHeaders,
},
];
},
eslint: {
dirs: ['src'],
},
transpilePackages: ['lodash-es'],
experimental: {
scrollRestoration: true,
},
redirects: () => {
return [
{
source: '/snippets/:id',
destination: '/notes/:id',
permanent: false,
},
];
},
rewrites: () => ({
beforeFiles: [
{
source: '/portfolio',
destination: `${portfolioZoneUrl}/portfolio`,
},
{
source: '/portfolio/:path*',
destination: `${portfolioZoneUrl}/portfolio/:path*`,
},
{
source: `${portfolioDataPrefix}/portfolio.json`,
destination: `${portfolioZoneUrl}${portfolioDataPrefix}/portfolio.json`,
},
{
source: `${portfolioDataPrefix}/portfolio/:path*`,
destination: `${portfolioZoneUrl}${portfolioDataPrefix}/portfolio/:path*`,
},
{
source: '/api/portfolio/:path*',
destination: `${portfolioZoneUrl}/api/portfolio/:path*`,
},
{
source: '/assets/portfolio/:path*',
destination: `${portfolioZoneUrl}/assets/portfolio/:path*`,
},
],
}),
};

/** @type {import('next').NextConfig} */
module.exports = million.next(
withBundleAnalyzer(withContentCollections(nextConfig)),
{ auto: true },
);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"@vercel/speed-insights": "^1.0.12",
"cmdk": "^1.0.0",
"framer-motion": "^11.2.11",
"gray-matter": "^4.0.3",
"lodash-es": "^4.17.21",
"million": "3.1.11",
"next": "14.2.4",
Expand All @@ -70,6 +69,8 @@
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "19.2.2",
"@commitlint/cz-commitlint": "19.2.0",
"@content-collections/core": "^0.6.2",
"@content-collections/next": "^0.2.0",
"@next/bundle-analyzer": "^14.2.4",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.14.5",
Expand Down
Loading

0 comments on commit ceec54c

Please sign in to comment.