Files
klz-cables.com/lib/blog.ts
Marc Mintel a5d77fc69b
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 8s
Build & Deploy / 🧪 QA (push) Failing after 1m13s
Build & Deploy / 🏗️ Build (push) Failing after 5m53s
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🧪 Smoke Test (push) Has been skipped
Build & Deploy / ⚡ Lighthouse (push) Has been skipped
Build & Deploy / ♿ WCAG (push) Has been skipped
Build & Deploy / 🛡️ Quality Gates (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 4s
feat: payload cms
2026-02-24 02:28:48 +01:00

227 lines
6.9 KiB
TypeScript

import { getPayload } from 'payload';
import configPromise from '@payload-config';
import { config } from '@/lib/config';
export function extractExcerpt(content: string): string {
if (!content) return '';
// Remove frontmatter if present (though matter() usually strips it out)
let text = content.replace(/^---[\s\S]*?---/, '');
// Remove MDX component imports and usages
text = text.replace(/<[^>]+>/g, '');
text = text.replace(/^[ \t]*import\s+.*$/gm, '');
text = text.replace(/^[ \t]*export\s+.*$/gm, '');
// Remove markdown headings
text = text.replace(/^#+.*$/gm, '');
// Extract first paragraph or combined lines
const paragraphs = text
.split(/\n\s*\n/)
.filter((p) => p.trim() && !p.trim().startsWith('---') && !p.trim().startsWith('#'));
if (paragraphs.length === 0) return '';
const excerpt = paragraphs[0]
.replace(/[*_`]/g, '') // remove markdown bold/italic/code
.replace(/\[(.*?)\]\(.*?\)/g, '$1') // replace links with their text
.replace(/\s+/g, ' ')
.trim();
return excerpt.length > 200 ? excerpt.slice(0, 197) + '...' : excerpt;
}
export interface PostFrontmatter {
title: string;
date: string;
excerpt?: string;
featuredImage?: string | null;
category?: string;
locale: string;
public?: boolean;
}
export interface PostMdx {
slug: string;
frontmatter: PostFrontmatter;
content: any; // Mapped to Lexical SerializedEditorState
}
export function isPostVisible(post: { frontmatter: { date: string; public?: boolean } }) {
// If explicitly marked as not public, hide in production
if (post.frontmatter.public === false && config.isProduction) {
return false;
}
const postDate = new Date(post.frontmatter.date);
const now = new Date();
return !(postDate > now && config.isProduction);
}
export async function getPostBySlug(slug: string, locale: string): Promise<PostMdx | null> {
const payload = await getPayload({ config: configPromise });
const { docs } = await payload.find({
collection: 'posts',
where: {
slug: { equals: slug },
locale: { equals: locale },
},
draft: process.env.NODE_ENV === 'development',
limit: 1,
});
if (!docs || docs.length === 0) return null;
const doc = docs[0];
return {
slug: doc.slug,
frontmatter: {
title: doc.title,
date: doc.date,
excerpt: doc.excerpt || '',
category: doc.category || '',
locale: doc.locale,
featuredImage:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
: null,
public: doc._status === 'published',
} as PostFrontmatter,
content: doc.content as any, // Native Lexical Editor State
};
}
export async function getAllPosts(locale: string): Promise<PostMdx[]> {
const payload = await getPayload({ config: configPromise });
// Query only published posts (access checks applied automatically by Payload!)
const { docs } = await payload.find({
collection: 'posts',
where: {
locale: {
equals: locale,
},
},
sort: '-date',
draft: process.env.NODE_ENV === 'development', // Includes Drafts if running locally
limit: 100,
});
return docs.map((doc) => {
return {
slug: doc.slug,
frontmatter: {
title: doc.title,
date: doc.date,
excerpt: doc.excerpt || '',
category: doc.category || '',
locale: doc.locale,
featuredImage:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
: null,
} as PostFrontmatter,
// Pass the Lexical content object rather than raw markdown string
content: doc.content as any,
};
});
}
export async function getAllPostsMetadata(locale: string): Promise<Partial<PostMdx>[]> {
const posts = await getAllPosts(locale);
return posts.map((p) => ({
slug: p.slug,
frontmatter: p.frontmatter,
}));
}
export async function getAdjacentPosts(
slug: string,
locale: string,
): Promise<{
prev: PostMdx | null;
next: PostMdx | null;
isPrevRandom?: boolean;
isNextRandom?: boolean;
}> {
const posts = await getAllPosts(locale);
const currentIndex = posts.findIndex((post) => post.slug === slug);
if (currentIndex === -1) {
return { prev: null, next: null };
}
// Posts are sorted by date descending (newest first)
// So "next" post (newer) is at index - 1
// And "previous" post (older) is at index + 1
let next = currentIndex > 0 ? posts[currentIndex - 1] : null;
let prev = currentIndex < posts.length - 1 ? posts[currentIndex + 1] : null;
let isNextRandom = false;
let isPrevRandom = false;
const getRandomPost = (excludeSlugs: string[]) => {
const available = posts.filter((p) => !excludeSlugs.includes(p.slug));
if (available.length === 0) return null;
return available[Math.floor(Math.random() * available.length)];
};
// If there's no next post (we are at the newest post), show a random post instead
if (!next && posts.length > 2) {
next = getRandomPost([slug, prev?.slug].filter(Boolean) as string[]);
isNextRandom = true;
}
// If there's no previous post (we are at the oldest post), show a random post instead
if (!prev && posts.length > 2) {
prev = getRandomPost([slug, next?.slug].filter(Boolean) as string[]);
isPrevRandom = true;
}
return { prev, next, isPrevRandom, isNextRandom };
}
export function getReadingTime(content: string): number {
const wordsPerMinute = 200;
const noOfWords = content.split(/\s/g).length;
const minutes = noOfWords / wordsPerMinute;
return Math.ceil(minutes);
}
export function generateHeadingId(text: string): string {
let id = text.toLowerCase();
id = id.replace(/ä/g, 'ae');
id = id.replace(/ö/g, 'oe');
id = id.replace(/ü/g, 'ue');
id = id.replace(/ß/g, 'ss');
id = id.replace(/[*_`]/g, '');
id = id.replace(/[^\w\s-]/g, '');
id = id
.replace(/\s+/g, '-')
.replace(/-+/g, '-')
.replace(/^-+|-+$/g, '');
return id || 'heading';
}
export function getTextContent(node: any): string {
if (typeof node === 'string') return node;
if (typeof node === 'number') return node.toString();
if (Array.isArray(node)) return node.map(getTextContent).join('');
if (node && typeof node === 'object' && node.props && node.props.children) {
return getTextContent(node.props.children);
}
return '';
}
export function getHeadings(content: string): { id: string; text: string; level: number }[] {
const headingLines = content.split('\n').filter((line) => line.match(/^#{1,3}\s/));
return headingLines.map((line) => {
const level = (line.match(/^#+/)?.[0].length || 0) + 1; // Shift H1->H2, H2->H3, H3->H4
const rawText = line.replace(/^#+\s/, '').trim();
const cleanText = rawText.replace(/[*_`]/g, '');
const id = generateHeadingId(cleanText);
return { id, text: cleanText, level };
});
}