remove body from generated contentlayer BlogPosts

This commit is contained in:
Francesco Sansalvadore committed 2025-08-14 16:53:59 +02:00
1 parent f5174e6c4a
commit 3426482002
3 files changed
+37 -9

No files matched your search

-8
View File
@@ -6,21 +6,13 @@ export const BlogPost = defineDocumentType(() => ({
filePathPattern: `**/*.mdx`,
// contentType: 'mdx',
fields: {
author: { type: 'string', required: true },
author_url: { type: 'string', required: false },
author_title: { type: 'string', required: false },
author_image_url: { type: 'string', required: false },
date: { type: 'string', required: true },
description: { type: 'string', required: true },
categories: { type: 'list', of: { type: 'string' }, required: true },
image: { type: 'string', required: false },
launchweek: { type: 'string', required: false },
tags: { type: 'list', of: { type: 'string' }, required: false },
title: { type: 'string', required: true },
thumb: { type: 'string', required: false },
toc_depth: { type: 'number', required: false },
video: { type: 'string', required: false },
youtubeHero: { type: 'string', required: false },
},
computedFields: {
url: {
+1 -1
View File
@@ -12,7 +12,7 @@
"lint": "next lint",
"clean": "rimraf node_modules",
"typecheck": "pnpm run content:build && tsc --noEmit",
"content:build": "contentlayer2 build && node scripts/generateStaticContent.mjs",
"content:build": "contentlayer2 build && node scripts/stripContentlayerBody.mjs && node scripts/generateStaticContent.mjs",
"prettier": "prettier --cache --write \"./{pages,components,lib,stores,styles,tests}/**/*.{ts,tsx,md,js,jsx,json}\"",
"postbuild": "node ./internals/generate-sitemap.mjs && ./../../scripts/upload-static-assets.sh",
"knip": "knip"
@@ -0,0 +1,36 @@
import { promises as fs } from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const blogpostDir = path.join(__dirname, '../.contentlayer/generated/BlogPost')
async function stripBodyFromBlogpostJsonFiles() {
try {
const stat = await fs.stat(blogpostDir).catch(() => null)
if (!stat || !stat.isDirectory()) return
const entries = await fs.readdir(blogpostDir)
const jsonFiles = entries.filter((name) => name.endsWith('.json'))
await Promise.all(
jsonFiles.map(async (fileName) => {
const fullPath = path.join(blogpostDir, fileName)
try {
const raw = await fs.readFile(fullPath, 'utf8')
const data = JSON.parse(raw)
if (data && typeof data === 'object' && 'body' in data) {
delete data.body
await fs.writeFile(fullPath, JSON.stringify(data), 'utf8')
}
} catch {
// Skip files that cannot be processed
}
})
)
} catch {
// Silently ignore to avoid failing the build if optional
}
}
await stripBodyFromBlogpostJsonFiles()