feat: payload cms
This commit is contained in:
3
.npmrc
Normal file
3
.npmrc
Normal file
@@ -0,0 +1,3 @@
|
||||
@mintel:registry=https://npm.infra.mintel.me/
|
||||
//npm.infra.mintel.me/:_authToken=${NPM_TOKEN}
|
||||
always-auth=true
|
||||
@@ -83,6 +83,9 @@ export default async function BlogPost({ params }: BlogPostProps) {
|
||||
priority
|
||||
className="object-cover"
|
||||
sizes="100vw"
|
||||
style={{
|
||||
objectPosition: `${post.frontmatter.focalX ?? 50}% ${post.frontmatter.focalY ?? 50}%`,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<div className="absolute inset-0 bg-gradient-to-t from-neutral-dark via-neutral-dark/40 to-transparent" />
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { RichText } from '@payloadcms/richtext-lexical/react';
|
||||
import { defaultJSXConverters, RichText } from '@payloadcms/richtext-lexical/react';
|
||||
import type { JSXConverters } from '@payloadcms/richtext-lexical/react';
|
||||
import Image from 'next/image';
|
||||
|
||||
@@ -16,7 +16,9 @@ import Stats from '@/components/blog/Stats';
|
||||
import SplitHeading from '@/components/blog/SplitHeading';
|
||||
import ProductTabs from '@/components/ProductTabs';
|
||||
import ProductTechnicalData from '@/components/ProductTechnicalData';
|
||||
|
||||
const jsxConverters: JSXConverters = {
|
||||
...defaultJSXConverters,
|
||||
blocks: {
|
||||
// Map the custom Payload Blocks created in src/payload/blocks to their React components
|
||||
// Payload Lexical exposes blocks using the 'block-[slug]' pattern
|
||||
@@ -160,7 +162,7 @@ const jsxConverters: JSXConverters = {
|
||||
/>
|
||||
}
|
||||
>
|
||||
<></>
|
||||
{node.fields.content && <RichText data={node.fields.content} converters={jsxConverters} />}
|
||||
</ProductTabs>
|
||||
),
|
||||
},
|
||||
@@ -191,6 +193,9 @@ const jsxConverters: JSXConverters = {
|
||||
height={height}
|
||||
className="w-full object-cover transition-transform duration-700 hover:scale-105"
|
||||
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 75vw, 60vw"
|
||||
style={{
|
||||
objectPosition: `${node?.value?.focalX ?? 50}% ${node?.value?.focalY ?? 50}%`,
|
||||
}}
|
||||
/>
|
||||
{node?.value?.caption && (
|
||||
<figcaption className="p-4 bg-neutral-dark text-white/80 text-sm text-center italic border-t border-white/10">
|
||||
|
||||
@@ -130,12 +130,14 @@ export default function ProductTechnicalData({ data }: ProductTechnicalDataProps
|
||||
<td className="px-3 py-2 text-xs font-bold text-primary sticky left-0 bg-white group-hover:bg-neutral-light/50 z-10 whitespace-nowrap">
|
||||
{row.configuration}
|
||||
</td>
|
||||
{row.cells.map((cell, cellIdx) => (
|
||||
{row.cells.map((cell: any, cellIdx: number) => (
|
||||
<td
|
||||
key={cellIdx}
|
||||
className="px-3 py-2 text-xs text-text-secondary whitespace-nowrap"
|
||||
>
|
||||
{cell}
|
||||
{typeof cell === 'object' && cell !== null && 'value' in cell
|
||||
? cell.value
|
||||
: cell}
|
||||
</td>
|
||||
))}
|
||||
</tr>
|
||||
|
||||
@@ -1,4 +1,16 @@
|
||||
services:
|
||||
# Lightweight proxy: gives Caddy the labels to route klz.localhost → host Mac
|
||||
klz-proxy:
|
||||
image: alpine:latest
|
||||
command: sleep infinity
|
||||
restart: unless-stopped
|
||||
networks:
|
||||
- infra
|
||||
labels:
|
||||
- "caddy=http://${TRAEFIK_HOST:-klz.localhost}"
|
||||
- "caddy.reverse_proxy=host.docker.internal:3100"
|
||||
|
||||
# Full Docker dev (use with `pnpm run dev:docker`)
|
||||
klz-app:
|
||||
build:
|
||||
context: .
|
||||
@@ -17,15 +29,24 @@ services:
|
||||
NEXT_TELEMETRY_DISABLED: "1"
|
||||
POSTGRES_URI: postgres://${DIRECTUS_DB_USER:-payload}:${DIRECTUS_DB_PASSWORD:-120in09oenaoinsd9iaidon}@klz-db:5432/${DIRECTUS_DB_NAME:-payload}
|
||||
PAYLOAD_SECRET: ${DIRECTUS_SECRET:-fallback-secret-for-dev}
|
||||
NODE_OPTIONS: "--max-old-space-size=4096"
|
||||
CI: "true" # Prevents some interactive prompts during install
|
||||
NODE_OPTIONS: "--max-old-space-size=2048"
|
||||
UV_THREADPOOL_SIZE: "4"
|
||||
NPM_TOKEN: ${NPM_TOKEN:-}
|
||||
CI: "true"
|
||||
volumes:
|
||||
- .:/app
|
||||
# Named volumes for persistence & performance (mintel.me standard)
|
||||
- klz_node_modules:/app/node_modules
|
||||
- klz_next_cache:/app/.next
|
||||
- klz_turbo_cache:/app/.turbo
|
||||
- klz_pnpm_store:/pnpm
|
||||
- ~/.npmrc:/root/.npmrc:ro
|
||||
- /app/.git
|
||||
- /app/reference
|
||||
- /app/data
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '4'
|
||||
memory: 4G
|
||||
command: >
|
||||
sh -c "pnpm install && pnpm next dev --turbo --hostname 0.0.0.0"
|
||||
labels:
|
||||
@@ -62,4 +83,5 @@ volumes:
|
||||
external: false
|
||||
klz_node_modules:
|
||||
klz_next_cache:
|
||||
klz_turbo_cache:
|
||||
klz_pnpm_store:
|
||||
|
||||
4874
html-errors-2.json
4874
html-errors-2.json
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
18
lib/blog.ts
18
lib/blog.ts
@@ -32,6 +32,8 @@ export interface PostFrontmatter {
|
||||
date: string;
|
||||
excerpt?: string;
|
||||
featuredImage?: string | null;
|
||||
focalX?: number;
|
||||
focalY?: number;
|
||||
category?: string;
|
||||
locale: string;
|
||||
public?: boolean;
|
||||
@@ -83,6 +85,14 @@ export async function getPostBySlug(slug: string, locale: string): Promise<PostM
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
|
||||
: null,
|
||||
focalX:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalX
|
||||
: 50,
|
||||
focalY:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalY
|
||||
: 50,
|
||||
public: doc._status === 'published',
|
||||
} as PostFrontmatter,
|
||||
content: doc.content as any, // Native Lexical Editor State
|
||||
@@ -117,6 +127,14 @@ export async function getAllPosts(locale: string): Promise<PostMdx[]> {
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
|
||||
: null,
|
||||
focalX:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalX
|
||||
: 50,
|
||||
focalY:
|
||||
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
||||
? doc.featuredImage.focalY
|
||||
: 50,
|
||||
} as PostFrontmatter,
|
||||
// Pass the Lexical content object rather than raw markdown string
|
||||
content: doc.content as any,
|
||||
|
||||
24
lib/mdx.ts
24
lib/mdx.ts
@@ -155,12 +155,23 @@ export async function getAllProducts(locale: string): Promise<ProductMdx[]> {
|
||||
// Fetch ALL products in a single query to avoid N+1 getPayload() calls
|
||||
const payload = await getPayload({ config: configPromise });
|
||||
|
||||
const selectFields = {
|
||||
title: true,
|
||||
slug: true,
|
||||
sku: true,
|
||||
description: true,
|
||||
categories: true,
|
||||
images: true,
|
||||
locale: true,
|
||||
};
|
||||
|
||||
// Get products for this locale
|
||||
const result = await payload.find({
|
||||
collection: 'products',
|
||||
where: { locale: { equals: locale } },
|
||||
depth: 1,
|
||||
pagination: false,
|
||||
select: selectFields,
|
||||
});
|
||||
|
||||
let products: ProductMdx[] = result.docs
|
||||
@@ -174,15 +185,15 @@ export async function getAllProducts(locale: string): Promise<ProductMdx[]> {
|
||||
slug: doc.slug,
|
||||
frontmatter: {
|
||||
title: doc.title,
|
||||
sku: doc.sku,
|
||||
description: doc.description,
|
||||
sku: doc.sku || '',
|
||||
description: doc.description || '',
|
||||
categories: Array.isArray(doc.categories) ? doc.categories.map((c: any) => c.category) : [],
|
||||
images: ((doc.images as any[]) || [])
|
||||
.map((img) => (typeof img === 'string' ? img : img.url))
|
||||
.filter(Boolean),
|
||||
locale: doc.locale,
|
||||
},
|
||||
content: doc.content,
|
||||
content: null, // Avoided loading into memory!
|
||||
}));
|
||||
|
||||
// Also include English fallbacks for slugs not in this locale
|
||||
@@ -193,6 +204,7 @@ export async function getAllProducts(locale: string): Promise<ProductMdx[]> {
|
||||
where: { locale: { equals: 'en' } },
|
||||
depth: 1,
|
||||
pagination: false,
|
||||
select: selectFields,
|
||||
});
|
||||
|
||||
const fallbacks = enResult.docs
|
||||
@@ -207,8 +219,8 @@ export async function getAllProducts(locale: string): Promise<ProductMdx[]> {
|
||||
slug: doc.slug,
|
||||
frontmatter: {
|
||||
title: doc.title,
|
||||
sku: doc.sku,
|
||||
description: doc.description,
|
||||
sku: doc.sku || '',
|
||||
description: doc.description || '',
|
||||
categories: Array.isArray(doc.categories)
|
||||
? doc.categories.map((c: any) => c.category)
|
||||
: [],
|
||||
@@ -218,7 +230,7 @@ export async function getAllProducts(locale: string): Promise<ProductMdx[]> {
|
||||
locale: doc.locale,
|
||||
isFallback: true,
|
||||
},
|
||||
content: doc.content,
|
||||
content: null, // Avoided loading into memory!
|
||||
}));
|
||||
|
||||
products = [...products, ...fallbacks];
|
||||
|
||||
@@ -46,7 +46,7 @@ export class GlitchtipErrorReportingService implements ErrorReportingService {
|
||||
if (!this.sentryPromise) {
|
||||
this.sentryPromise = import('@sentry/nextjs').then((Sentry) => {
|
||||
// Client-side initialization must happen here since sentry.client.config.ts is empty
|
||||
if (typeof window !== 'undefined') {
|
||||
if (typeof window !== 'undefined' && process.env.NODE_ENV === 'production') {
|
||||
Sentry.init({
|
||||
dsn: 'https://public@errors.infra.mintel.me/1',
|
||||
tunnel: '/errors/api/relay',
|
||||
|
||||
2
next-env.d.ts
vendored
2
next-env.d.ts
vendored
@@ -1,6 +1,6 @@
|
||||
/// <reference types="next" />
|
||||
/// <reference types="next/image-types/global" />
|
||||
import "./.next/types/routes.d.ts";
|
||||
import "./.next/dev/types/routes.d.ts";
|
||||
|
||||
// NOTE: This file should not be edited
|
||||
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
|
||||
|
||||
@@ -4,7 +4,7 @@ import { withSentryConfig } from '@sentry/nextjs';
|
||||
|
||||
import { withPayload } from '@payloadcms/next/withPayload';
|
||||
|
||||
console.log('🚀 NEXT CONFIG LOADED FROM:', process.cwd());
|
||||
const isProd = process.env.NODE_ENV === 'production';
|
||||
/** @type {import('next').NextConfig} */
|
||||
const nextConfig = {
|
||||
onDemandEntries: {
|
||||
@@ -22,7 +22,7 @@ const nextConfig = {
|
||||
fullUrl: true,
|
||||
},
|
||||
},
|
||||
output: 'standalone',
|
||||
...(isProd ? { output: 'standalone' } : {}),
|
||||
async headers() {
|
||||
const isProd = process.env.NODE_ENV === 'production';
|
||||
const umamiDomain = new URL(process.env.UMAMI_API_ENDPOINT || 'https://analytics.infra.mintel.me').origin;
|
||||
@@ -430,6 +430,13 @@ const withAnalyzer = withBundleAnalyzer({
|
||||
enabled: process.env.ANALYZE === 'true',
|
||||
});
|
||||
|
||||
export default withPayload(withAnalyzer(withMintelConfig(nextConfig, {
|
||||
hideSourceMaps: true,
|
||||
})));
|
||||
// withMintelConfig internally sets output:'standalone' and wraps with withSentryConfig.
|
||||
// In dev mode, standalone triggers @vercel/nft file tracing on every compile through
|
||||
// VirtioFS, which hammers CPU. Strip it for dev.
|
||||
let resolvedConfig = withPayload(withAnalyzer(withMintelConfig(nextConfig)));
|
||||
|
||||
if (!isProd) {
|
||||
delete resolvedConfig.output;
|
||||
}
|
||||
|
||||
export default resolvedConfig;
|
||||
|
||||
13328
openrouter_models.json
13328
openrouter_models.json
File diff suppressed because it is too large
Load Diff
12
package.json
12
package.json
@@ -91,8 +91,9 @@
|
||||
"vitest": "^4.0.16"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "docker network create infra 2>/dev/null || true && echo \"\\n🚀 Dockerized Environment Starting...\\n\" && COMPOSE_PROJECT_NAME=klz-2026 docker-compose -f docker-compose.dev.yml up klz-app klz-db",
|
||||
"dev:infra": "docker-compose up klz-db klz-gatekeeper",
|
||||
"dev": "COMPOSE_PROJECT_NAME=klz-2026 docker-compose -f docker-compose.dev.yml up -d klz-db klz-proxy && NODE_ENV=development next dev --webpack --port 3100 --hostname 0.0.0.0",
|
||||
"dev:docker": "docker network create infra 2>/dev/null || true && echo \"\\n🚀 Dockerized Environment Starting...\\n\" && COMPOSE_PROJECT_NAME=klz-2026 docker-compose -f docker-compose.dev.yml up klz-app klz-db",
|
||||
"dev:infra": "COMPOSE_PROJECT_NAME=klz-2026 docker-compose -f docker-compose.dev.yml up -d klz-db klz-proxy",
|
||||
"build": "next build",
|
||||
"start": "next start",
|
||||
"lint": "eslint .",
|
||||
@@ -133,6 +134,13 @@
|
||||
},
|
||||
"version": "1.0.1-rc.0",
|
||||
"pnpm": {
|
||||
"onlyBuiltDependencies": [
|
||||
"@parcel/watcher",
|
||||
"@swc/core",
|
||||
"sharp",
|
||||
"esbuild",
|
||||
"unrs-resolver"
|
||||
],
|
||||
"overrides": {
|
||||
"next": "16.1.6",
|
||||
"minimatch": ">=10.2.2"
|
||||
|
||||
@@ -685,6 +685,21 @@ export interface ProductTabsBlock {
|
||||
id?: string | null;
|
||||
}[]
|
||||
| null;
|
||||
content?: {
|
||||
root: {
|
||||
type: string;
|
||||
children: {
|
||||
type: any;
|
||||
version: number;
|
||||
[k: string]: unknown;
|
||||
}[];
|
||||
direction: ('ltr' | 'rtl') | null;
|
||||
format: 'left' | 'start' | 'center' | 'right' | 'end' | 'justify' | '';
|
||||
indent: number;
|
||||
version: number;
|
||||
};
|
||||
[k: string]: unknown;
|
||||
} | null;
|
||||
id?: string | null;
|
||||
blockName?: string | null;
|
||||
blockType: 'productTabs';
|
||||
|
||||
@@ -8,8 +8,11 @@ import { nodemailerAdapter } from '@payloadcms/email-nodemailer';
|
||||
import { BlocksFeature } from '@payloadcms/richtext-lexical';
|
||||
import { payloadBlocks } from './src/payload/blocks/allBlocks';
|
||||
|
||||
// Disable sharp cache to prevent memory leaks in Docker
|
||||
sharp.cache(false);
|
||||
// Only disable sharp cache in production to prevent memory leaks.
|
||||
// In dev, the cache avoids 41s+ re-processing per image through VirtioFS.
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
sharp.cache(false);
|
||||
}
|
||||
|
||||
import { Users } from './src/payload/collections/Users';
|
||||
import { Media } from './src/payload/collections/Media';
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import * as Sentry from '@sentry/nextjs';
|
||||
|
||||
const dsn = process.env.SENTRY_DSN;
|
||||
const isProd = process.env.NODE_ENV === 'production';
|
||||
|
||||
Sentry.init({
|
||||
dsn,
|
||||
enabled: Boolean(dsn),
|
||||
enabled: isProd && Boolean(dsn),
|
||||
|
||||
// Adjust this value in production, or use tracesSampler for greater control
|
||||
tracesSampleRate: 1,
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import * as Sentry from '@sentry/nextjs';
|
||||
|
||||
const dsn = process.env.SENTRY_DSN;
|
||||
const isProd = process.env.NODE_ENV === 'production';
|
||||
|
||||
Sentry.init({
|
||||
dsn,
|
||||
enabled: Boolean(dsn),
|
||||
enabled: isProd && Boolean(dsn),
|
||||
|
||||
// Adjust this value in production, or use tracesSampler for greater control
|
||||
tracesSampleRate: 1,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Block } from 'payload';
|
||||
import { lexicalEditor } from '@payloadcms/richtext-lexical';
|
||||
|
||||
export const ProductTabs: Block = {
|
||||
slug: 'productTabs',
|
||||
@@ -92,5 +93,15 @@ export const ProductTabs: Block = {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'content',
|
||||
type: 'richText',
|
||||
editor: lexicalEditor({
|
||||
features: ({ defaultFeatures }) => [
|
||||
...defaultFeatures,
|
||||
// We don't need blocks inside ProductTabs for now to avoid complexity/recursion
|
||||
],
|
||||
}),
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
@@ -53,13 +53,63 @@ function ensureChildren(parsedNodes: any[]): any[] {
|
||||
return parsedNodes;
|
||||
}
|
||||
|
||||
function parseInlineMarkdown(text: string): any[] {
|
||||
// Simple regex-based inline parser for bold and italic
|
||||
// Matches **bold**, __bold__, *italic*, _italic_
|
||||
const regex = /(\*\*|__|TextNode)(.*?)\1|(\*|_)(.*?)\3/g;
|
||||
const nodes: any[] = [];
|
||||
let lastIndex = 0;
|
||||
let match;
|
||||
|
||||
const createTextNode = (content: string, format = 0) => ({
|
||||
detail: 0,
|
||||
format,
|
||||
mode: 'normal',
|
||||
style: '',
|
||||
text: content,
|
||||
type: 'text',
|
||||
version: 1,
|
||||
});
|
||||
|
||||
const rawMatch = text.matchAll(
|
||||
/(\*\*(.*?)\*\*|__(.*?)__|(?<!\*)\*(?!\*)(.*?)\*|(?<!_)_(?!_)(.*?)_)/g,
|
||||
);
|
||||
|
||||
for (const m of rawMatch) {
|
||||
const offset = m.index!;
|
||||
// Leading plain text
|
||||
if (offset > lastIndex) {
|
||||
nodes.push(createTextNode(text.slice(lastIndex, offset)));
|
||||
}
|
||||
|
||||
const boldContent = m[2] || m[3];
|
||||
const italicContent = m[4] || m[5];
|
||||
|
||||
if (boldContent) {
|
||||
nodes.push(createTextNode(boldContent, 1)); // 1 = Bold
|
||||
} else if (italicContent) {
|
||||
nodes.push(createTextNode(italicContent, 2)); // 2 = Italic
|
||||
}
|
||||
|
||||
lastIndex = offset + m[0].length;
|
||||
}
|
||||
|
||||
// Trailing plain text
|
||||
if (lastIndex < text.length) {
|
||||
nodes.push(createTextNode(text.slice(lastIndex)));
|
||||
}
|
||||
|
||||
return nodes.length > 0 ? nodes : [createTextNode(text)];
|
||||
}
|
||||
|
||||
export function parseMarkdownToLexical(markdown: string): any[] {
|
||||
const textNode = (text: string) => ({
|
||||
const paragraphNode = (text: string) => ({
|
||||
type: 'paragraph',
|
||||
format: '',
|
||||
indent: 0,
|
||||
version: 1,
|
||||
children: [{ mode: 'normal', type: 'text', text, version: 1 }],
|
||||
direction: 'ltr',
|
||||
children: parseInlineMarkdown(text),
|
||||
});
|
||||
|
||||
const nodes: any[] = [];
|
||||
@@ -70,7 +120,6 @@ export function parseMarkdownToLexical(markdown: string): any[] {
|
||||
if (fm) content = content.replace(fm[0], '').trim();
|
||||
|
||||
// 1. EXTRACT MULTILINE WRAPPERS BEFORE CHUNKING
|
||||
// This allows nested newlines inside components without breaking them.
|
||||
const extractBlocks = [
|
||||
{
|
||||
tag: 'HighlightBox',
|
||||
@@ -131,9 +180,66 @@ export function parseMarkdownToLexical(markdown: string): any[] {
|
||||
},
|
||||
}),
|
||||
},
|
||||
{
|
||||
tag: 'ProductTabs',
|
||||
regex: /<ProductTabs([^>]*)>([\s\S]*?)<\/ProductTabs>/g,
|
||||
build: (props: string, inner: string) => {
|
||||
const fullTag = `<ProductTabs ${props}>`;
|
||||
const dataMatch = fullTag.match(/data=\{({[\s\S]*?})\}\s*\/>/);
|
||||
let technicalItems = [];
|
||||
let voltageTables = [];
|
||||
|
||||
if (dataMatch) {
|
||||
try {
|
||||
const parsedData = JSON.parse(dataMatch[1]);
|
||||
technicalItems = parsedData.technicalItems || [];
|
||||
voltageTables = parsedData.voltageTables || [];
|
||||
|
||||
voltageTables.forEach((vt: any) => {
|
||||
vt.rows?.forEach((row: any) => {
|
||||
if (row.cells) {
|
||||
row.cells = row.cells.map((c: any) =>
|
||||
typeof c !== 'object' ? { value: String(c) } : c,
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
} catch (e) {
|
||||
console.warn('Failed to parse ProductTabs JSON data:', e);
|
||||
}
|
||||
}
|
||||
|
||||
return blockNode('productTabs', {
|
||||
technicalItems,
|
||||
voltageTables,
|
||||
content: {
|
||||
root: {
|
||||
type: 'root',
|
||||
format: '',
|
||||
indent: 0,
|
||||
version: 1,
|
||||
direction: 'ltr',
|
||||
children: ensureChildren(parseMarkdownToLexical(inner.trim())),
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
// Placeholder map to temporarily store extracted multi-line blocks
|
||||
function cleanMdxContent(text: string): string {
|
||||
return text
|
||||
.replace(/<section[^>]*>/g, '')
|
||||
.replace(/<\/section>/g, '')
|
||||
.replace(/<h3[^>]*>(.*?)<\/h3>/g, '### $1\n\n')
|
||||
.replace(/<p[^>]*>(.*?)<\/p>/g, '$1\n\n')
|
||||
.replace(/<strong[^>]*>(.*?)<\/strong>/g, '**$1**')
|
||||
.replace(/ /g, ' ')
|
||||
.trim();
|
||||
}
|
||||
|
||||
content = cleanMdxContent(content);
|
||||
|
||||
const placeholders = new Map<string, any>();
|
||||
let placeholderIdx = 0;
|
||||
|
||||
@@ -141,59 +247,22 @@ export function parseMarkdownToLexical(markdown: string): any[] {
|
||||
content = content.replace(block.regex, (match, propsMatch, innerMatch) => {
|
||||
const id = `__BLOCK_PLACEHOLDER_${placeholderIdx++}__`;
|
||||
placeholders.set(id, block.build(propsMatch, innerMatch));
|
||||
return `\n\n${id}\n\n`; // Pad with newlines so it becomes its own chunk
|
||||
return `\n\n${id}\n\n`;
|
||||
});
|
||||
}
|
||||
|
||||
// 2. CHUNK THE REST (Paragraphs, Single-line Components)
|
||||
// 2. CHUNK THE REST
|
||||
const rawChunks = content.split(/\n\s*\n/);
|
||||
|
||||
for (let chunk of rawChunks) {
|
||||
chunk = chunk.trim();
|
||||
if (!chunk) continue;
|
||||
|
||||
// Has Placeholder?
|
||||
if (chunk.startsWith('__BLOCK_PLACEHOLDER_')) {
|
||||
nodes.push(placeholders.get(chunk));
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- Custom Component: ProductTabs ---
|
||||
if (chunk.includes('<ProductTabs')) {
|
||||
const dataMatch = chunk.match(/data=\{({[\s\S]*?})\}\s*\/>/);
|
||||
if (dataMatch) {
|
||||
try {
|
||||
const parsedData = JSON.parse(dataMatch[1]);
|
||||
|
||||
// Normalize String Arrays to Payload Object Arrays { value: "string" }
|
||||
if (parsedData.voltageTables) {
|
||||
parsedData.voltageTables.forEach((vt: any) => {
|
||||
if (vt.rows) {
|
||||
vt.rows.forEach((row: any) => {
|
||||
if (row.cells && Array.isArray(row.cells)) {
|
||||
row.cells = row.cells.map((cell: any) =>
|
||||
typeof cell !== 'object' || cell === null ? { value: String(cell) } : cell,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
nodes.push(
|
||||
blockNode('productTabs', {
|
||||
technicalItems: parsedData.technicalItems || [],
|
||||
voltageTables: parsedData.voltageTables || [],
|
||||
}),
|
||||
);
|
||||
} catch (e: any) {
|
||||
console.warn(`Could not parse JSON payload for ProductTabs:`, e.message);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- Custom Component: StickyNarrative ---
|
||||
if (chunk.includes('<StickyNarrative')) {
|
||||
nodes.push(
|
||||
blockNode('stickyNarrative', {
|
||||
@@ -204,7 +273,6 @@ export function parseMarkdownToLexical(markdown: string): any[] {
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- Custom Component: ComparisonGrid ---
|
||||
if (chunk.includes('<ComparisonGrid')) {
|
||||
nodes.push(
|
||||
blockNode('comparisonGrid', {
|
||||
@@ -217,7 +285,6 @@ export function parseMarkdownToLexical(markdown: string): any[] {
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- Custom Component: VisualLinkPreview ---
|
||||
if (chunk.includes('<VisualLinkPreview')) {
|
||||
nodes.push(
|
||||
blockNode('visualLinkPreview', {
|
||||
@@ -230,7 +297,6 @@ export function parseMarkdownToLexical(markdown: string): any[] {
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- Custom Component: TechnicalGrid ---
|
||||
if (chunk.includes('<TechnicalGrid')) {
|
||||
nodes.push(
|
||||
blockNode('technicalGrid', {
|
||||
@@ -241,7 +307,6 @@ export function parseMarkdownToLexical(markdown: string): any[] {
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- Custom Component: AnimatedImage ---
|
||||
if (chunk.includes('<AnimatedImage')) {
|
||||
const widthMatch = chunk.match(/width=\{?(\d+)\}?/);
|
||||
const heightMatch = chunk.match(/height=\{?(\d+)\}?/);
|
||||
@@ -256,7 +321,6 @@ export function parseMarkdownToLexical(markdown: string): any[] {
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- Custom Component: PowerCTA ---
|
||||
if (chunk.includes('<PowerCTA')) {
|
||||
nodes.push(
|
||||
blockNode('powerCTA', {
|
||||
@@ -266,30 +330,28 @@ export function parseMarkdownToLexical(markdown: string): any[] {
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- Standard Markdown: Headings ---
|
||||
const headingMatch = chunk.match(/^(#{1,6})\s+(.*)/);
|
||||
if (headingMatch) {
|
||||
const level = Math.min(headingMatch[1].length + 1, 6);
|
||||
nodes.push({
|
||||
type: 'heading',
|
||||
tag: `h${headingMatch[1].length}`,
|
||||
tag: `h${level}`,
|
||||
format: '',
|
||||
indent: 0,
|
||||
version: 1,
|
||||
direction: 'ltr',
|
||||
children: [{ mode: 'normal', type: 'text', text: headingMatch[2], version: 1 }],
|
||||
children: parseInlineMarkdown(headingMatch[2]),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- Standard Markdown: Images ---
|
||||
const imageMatch = chunk.match(/^!\[([^\]]*)\]\(([^)]+)\)$/);
|
||||
if (imageMatch) {
|
||||
nodes.push(textNode(chunk));
|
||||
nodes.push(paragraphNode(chunk));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Default: plain text paragraph
|
||||
nodes.push(textNode(chunk));
|
||||
nodes.push(paragraphNode(chunk));
|
||||
}
|
||||
|
||||
return nodes;
|
||||
|
||||
Reference in New Issue
Block a user