cms-renderer 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/block-renderer.js +281 -4
- package/dist/lib/block-renderer.js.map +1 -1
- package/dist/lib/cms-api.js +34 -3
- package/dist/lib/cms-api.js.map +1 -1
- package/dist/lib/data-utils.js +7 -4
- package/dist/lib/data-utils.js.map +1 -1
- package/dist/lib/markdown-utils.js +10 -5
- package/dist/lib/markdown-utils.js.map +1 -1
- package/dist/lib/renderer.js +321 -9
- package/dist/lib/renderer.js.map +1 -1
- package/dist/lib/result.js +96 -18
- package/dist/lib/result.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-6QR5B5KQ.js +0 -287
- package/dist/chunk-6QR5B5KQ.js.map +0 -1
- package/dist/chunk-HVKFEZBT.js +0 -116
- package/dist/chunk-HVKFEZBT.js.map +0 -1
- package/dist/chunk-JHKDRASN.js +0 -39
- package/dist/chunk-JHKDRASN.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../lib/markdown-utils.ts"],"sourcesContent":["/**\n * Markdown Parsing Utilities\n *\n * Three implementations showing the progression from naive to production-ready:\n * - v1 (Naive): Direct call, crashes on bad input\n * - v2 (Defensive): Try-catch with null fallback\n * - v3 (Robust): Result type, validation, sanitization\n *\n * The robust version (v3) is exported as the default `parseMarkdown`.\n */\n\nimport type { MDTree } from 'md4w';\nimport { mdToJSON } from 'md4w';\n\nimport { err, ok, type Result } from './result';\n\n// -----------------------------------------------------------------------------\n// Types\n// -----------------------------------------------------------------------------\n\n/**\n * Error codes for markdown parsing failures.\n */\nexport const ParseErrorCode = {\n INVALID_INPUT: 'INVALID_INPUT',\n EMPTY_CONTENT: 'EMPTY_CONTENT',\n PARSE_FAILED: 'PARSE_FAILED',\n CONTENT_TOO_LONG: 'CONTENT_TOO_LONG',\n NESTED_TOO_DEEP: 'NESTED_TOO_DEEP',\n} as const;\n\nexport type ParseErrorCode = (typeof ParseErrorCode)[keyof typeof ParseErrorCode];\n\n/**\n * Structured error for markdown parsing failures.\n */\nexport interface ParseError {\n code: ParseErrorCode;\n message: string;\n cause?: Error;\n}\n\n/**\n * Options for robust markdown parsing.\n */\nexport interface ParseOptions {\n /**\n * Maximum content length in characters.\n * @default 500000 (500KB of text, ~100K words)\n */\n maxLength?: number;\n\n /**\n * Whether to sanitize XSS attempts in the output.\n * @default true\n */\n sanitize?: boolean;\n\n /**\n * Whether to allow empty content.\n * @default false\n */\n allowEmpty?: boolean;\n}\n\n// -----------------------------------------------------------------------------\n// Constants\n// -----------------------------------------------------------------------------\n\nconst DEFAULT_MAX_LENGTH = 500_000; // 500KB of text\n\n/**\n * Patterns that indicate potential XSS attempts in markdown.\n * These are checked in raw content before parsing.\n */\nconst XSS_PATTERNS = [\n /<script\\b/i,\n /javascript:/i,\n /on\\w+\\s*=/i, // onclick=, onerror=, etc.\n /data:/i, // data: URIs can be dangerous\n /vbscript:/i,\n] as const;\n\n// -----------------------------------------------------------------------------\n// V1: Naive Implementation\n// -----------------------------------------------------------------------------\n\n/**\n * Naive markdown parser - crashes on bad input.\n *\n * DO NOT USE IN PRODUCTION. This is for demonstration only.\n *\n * Problems:\n * - No input validation\n * - No error handling\n * - Will crash the entire app on malformed input\n * - No protection against XSS\n * - No content limits\n *\n * @example\n * ```ts\n * // This crashes if content is null, undefined, or malformed\n * const ast = parseMarkdownV1(userInput);\n * ```\n */\nexport function parseMarkdownV1(content: string): MDTree {\n return mdToJSON(content);\n}\n\n// -----------------------------------------------------------------------------\n// V2: Defensive Implementation\n// -----------------------------------------------------------------------------\n\n/**\n * Defensive markdown parser - catches errors but loses context.\n *\n * Better than v1 but still problematic:\n * - Returns null on any error (loses error details)\n * - Caller can't distinguish between empty content and parse failure\n * - Still no XSS protection\n * - Still no content limits\n *\n * @example\n * ```ts\n * const ast = parseMarkdownV2(userInput);\n * if (!ast) {\n * // What went wrong? We don't know.\n * return <ErrorFallback />;\n * }\n * ```\n */\nexport function parseMarkdownV2(content: string): MDTree | null {\n try {\n // Basic null check\n if (content == null) {\n return null;\n }\n return mdToJSON(content);\n } catch {\n return null;\n }\n}\n\n// -----------------------------------------------------------------------------\n// V3: Robust Implementation\n// -----------------------------------------------------------------------------\n\n/**\n * Creates a ParseError with the given code and message.\n */\nfunction createParseError(code: ParseErrorCode, message: string, cause?: Error): ParseError {\n return { code, message, cause };\n}\n\n/**\n * Checks content for potential XSS patterns.\n * Returns the first matched pattern or null if clean.\n */\nfunction detectXssPattern(content: string): RegExp | null {\n for (const pattern of XSS_PATTERNS) {\n if (pattern.test(content)) {\n return pattern;\n }\n }\n return null;\n}\n\n/**\n * Sanitizes content by removing dangerous patterns.\n * This is a basic sanitizer; production should use DOMPurify.\n */\nfunction sanitizeContent(content: string): string {\n let sanitized = content;\n\n // Remove script tags\n sanitized = sanitized.replace(/<script\\b[^<]*(?:(?!<\\/script>)<[^<]*)*<\\/script>/gi, '');\n\n // Remove javascript: and vbscript: URLs\n sanitized = sanitized.replace(/javascript:/gi, 'removed:');\n sanitized = sanitized.replace(/vbscript:/gi, 'removed:');\n\n // Remove event handlers (onclick, onerror, etc.)\n sanitized = sanitized.replace(/\\bon\\w+\\s*=\\s*[\"'][^\"']*[\"']/gi, '');\n sanitized = sanitized.replace(/\\bon\\w+\\s*=\\s*\\S+/gi, '');\n\n return sanitized;\n}\n\n/**\n * Robust markdown parser - production-ready with full error context.\n *\n * Features:\n * - Input validation with specific error codes\n * - Result type for explicit error handling\n * - XSS pattern detection and optional sanitization\n * - Content length limits\n * - Preserves error context for debugging\n *\n * @param content - Markdown string to parse\n * @param options - Parsing options\n * @returns Result containing the AST or a structured error\n *\n * @example\n * ```ts\n * const result = parseMarkdownV3(userInput);\n *\n * if (!result.ok) {\n * switch (result.error.code) {\n * case 'INVALID_INPUT':\n * return <InvalidInputError message={result.error.message} />;\n * case 'CONTENT_TOO_LONG':\n * return <ContentTooLongError />;\n * case 'PARSE_FAILED':\n * console.error('Parse error:', result.error.cause);\n * return <ParseError />;\n * default:\n * return <GenericError />;\n * }\n * }\n *\n * return <MarkdownRenderer ast={result.value} />;\n * ```\n */\nexport function parseMarkdownV3(\n content: string,\n options: ParseOptions = {}\n): Result<MDTree, ParseError> {\n const { maxLength = DEFAULT_MAX_LENGTH, sanitize = true, allowEmpty = false } = options;\n\n // 1. Validate input type\n if (content === null || content === undefined) {\n return err(\n createParseError(\n ParseErrorCode.INVALID_INPUT,\n `Content must be a string, received ${content === null ? 'null' : 'undefined'}`\n )\n );\n }\n\n if (typeof content !== 'string') {\n return err(\n createParseError(\n ParseErrorCode.INVALID_INPUT,\n `Content must be a string, received ${typeof content}`\n )\n );\n }\n\n // 2. Handle empty content\n const trimmed = content.trim();\n if (trimmed.length === 0 && !allowEmpty) {\n return err(\n createParseError(ParseErrorCode.EMPTY_CONTENT, 'Content is empty or contains only whitespace')\n );\n }\n\n // 3. Check content length\n if (content.length > maxLength) {\n return err(\n createParseError(\n ParseErrorCode.CONTENT_TOO_LONG,\n `Content exceeds maximum length of ${maxLength.toLocaleString()} characters ` +\n `(received ${content.length.toLocaleString()})`\n )\n );\n }\n\n // 4. Check for XSS patterns\n const xssPattern = detectXssPattern(content);\n if (xssPattern) {\n if (process.env.NODE_ENV === 'development') {\n console.warn(\n `[parseMarkdownV3] XSS pattern detected: ${xssPattern.toString()}`,\n sanitize ? 'Content will be sanitized.' : 'Sanitization is disabled!'\n );\n }\n }\n\n // 5. Optionally sanitize content\n const processedContent = sanitize ? sanitizeContent(content) : content;\n\n // 6. Parse markdown with error handling\n try {\n const ast = mdToJSON(processedContent);\n return ok(ast);\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return err(\n createParseError(\n ParseErrorCode.PARSE_FAILED,\n `Failed to parse markdown: ${cause.message}`,\n cause\n )\n );\n }\n}\n\n// -----------------------------------------------------------------------------\n// Default Export\n// -----------------------------------------------------------------------------\n\n/**\n * Production-ready markdown parser.\n *\n * This is an alias for `parseMarkdownV3` - the robust implementation\n * with validation, sanitization, and Result-based error handling.\n *\n * @example\n * ```ts\n * import { parseMarkdown } from '@/lib/markdown-utils';\n *\n * const result = parseMarkdown(content);\n * if (!result.ok) {\n * // Handle error with full context\n * console.error(`[${result.error.code}] ${result.error.message}`);\n * return null;\n * }\n * return result.value;\n * ```\n */\nexport const parseMarkdown = parseMarkdownV3;\n\n// -----------------------------------------------------------------------------\n// Utility Functions\n// -----------------------------------------------------------------------------\n\n/**\n * Checks if content is safe markdown (no XSS patterns detected).\n *\n * @param content - Markdown string to check\n * @returns true if no XSS patterns detected\n */\nexport function isSafeMarkdown(content: string): boolean {\n return detectXssPattern(content) === null;\n}\n\n/**\n * Estimates the word count of markdown content.\n * Useful for content length limits and reading time estimates.\n *\n * @param content - Markdown string to count\n * @returns Estimated word count\n */\nexport function estimateWordCount(content: string): number {\n // Remove markdown syntax for more accurate count\n const plainText = content\n .replace(/#+\\s/g, '') // Remove headings\n .replace(/\\*\\*|__|~~|`/g, '') // Remove formatting\n .replace(/\\[([^\\]]+)\\]\\([^)]+\\)/g, '$1') // Convert links to text\n .replace(/!\\[([^\\]]*)\\]\\([^)]+\\)/g, '') // Remove images\n .replace(/```[\\s\\S]*?```/g, '') // Remove code blocks\n .replace(/`[^`]+`/g, ''); // Remove inline code\n\n const words = plainText.trim().split(/\\s+/).filter(Boolean);\n return words.length;\n}\n\n/**\n * Estimates reading time for markdown content.\n *\n * @param content - Markdown string\n * @param wordsPerMinute - Reading speed (default 200 WPM)\n * @returns Reading time in minutes\n */\nexport function estimateReadingTime(content: string, wordsPerMinute = 200): number {\n const wordCount = estimateWordCount(content);\n return Math.ceil(wordCount / wordsPerMinute);\n}\n"],"mappings":";;;;;;AAYA,SAAS,gBAAgB;AAWlB,IAAM,iBAAiB;AAAA,EAC5B,eAAe;AAAA,EACf,eAAe;AAAA,EACf,cAAc;AAAA,EACd,kBAAkB;AAAA,EAClB,iBAAiB;AACnB;AAwCA,IAAM,qBAAqB;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B,iBAAiB,KAAa;AACjF,QAAM,YAAY,kBAAkB,OAAO;AAC3C,SAAO,KAAK,KAAK,YAAY,cAAc;AAC7C;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../lib/markdown-utils.ts","../../lib/result.ts"],"sourcesContent":["/**\n * Markdown Parsing Utilities\n *\n * Three implementations showing the progression from naive to production-ready:\n * - v1 (Naive): Direct call, crashes on bad input\n * - v2 (Defensive): Try-catch with null fallback\n * - v3 (Robust): Result type, validation, sanitization\n *\n * The robust version (v3) is exported as the default `parseMarkdown`.\n */\n\nimport type { MDTree } from 'md4w';\nimport { mdToJSON } from 'md4w';\n\nimport { err, ok, type Result } from './result';\n\n// -----------------------------------------------------------------------------\n// Types\n// -----------------------------------------------------------------------------\n\n/**\n * Error codes for markdown parsing failures.\n */\nexport const ParseErrorCode = {\n INVALID_INPUT: 'INVALID_INPUT',\n EMPTY_CONTENT: 'EMPTY_CONTENT',\n PARSE_FAILED: 'PARSE_FAILED',\n CONTENT_TOO_LONG: 'CONTENT_TOO_LONG',\n NESTED_TOO_DEEP: 'NESTED_TOO_DEEP',\n} as const;\n\nexport type ParseErrorCode = (typeof ParseErrorCode)[keyof typeof ParseErrorCode];\n\n/**\n * Structured error for markdown parsing failures.\n */\nexport interface ParseError {\n code: ParseErrorCode;\n message: string;\n cause?: Error;\n}\n\n/**\n * Options for robust markdown parsing.\n */\nexport interface ParseOptions {\n /**\n * Maximum content length in characters.\n * @default 500000 (500KB of text, ~100K words)\n */\n maxLength?: number;\n\n /**\n * Whether to sanitize XSS attempts in the output.\n * @default true\n */\n sanitize?: boolean;\n\n /**\n * Whether to allow empty content.\n * @default false\n */\n allowEmpty?: boolean;\n}\n\n// -----------------------------------------------------------------------------\n// Constants\n// -----------------------------------------------------------------------------\n\nconst DEFAULT_MAX_LENGTH = 500_000; // 500KB of text\n\n/**\n * Patterns that indicate potential XSS attempts in markdown.\n * These are checked in raw content before parsing.\n */\nconst XSS_PATTERNS = [\n /<script\\b/i,\n /javascript:/i,\n /on\\w+\\s*=/i, // onclick=, onerror=, etc.\n /data:/i, // data: URIs can be dangerous\n /vbscript:/i,\n] as const;\n\n// -----------------------------------------------------------------------------\n// V1: Naive Implementation\n// -----------------------------------------------------------------------------\n\n/**\n * Naive markdown parser - crashes on bad input.\n *\n * DO NOT USE IN PRODUCTION. This is for demonstration only.\n *\n * Problems:\n * - No input validation\n * - No error handling\n * - Will crash the entire app on malformed input\n * - No protection against XSS\n * - No content limits\n *\n * @example\n * ```ts\n * // This crashes if content is null, undefined, or malformed\n * const ast = parseMarkdownV1(userInput);\n * ```\n */\nexport function parseMarkdownV1(content: string): MDTree {\n return mdToJSON(content);\n}\n\n// -----------------------------------------------------------------------------\n// V2: Defensive Implementation\n// -----------------------------------------------------------------------------\n\n/**\n * Defensive markdown parser - catches errors but loses context.\n *\n * Better than v1 but still problematic:\n * - Returns null on any error (loses error details)\n * - Caller can't distinguish between empty content and parse failure\n * - Still no XSS protection\n * - Still no content limits\n *\n * @example\n * ```ts\n * const ast = parseMarkdownV2(userInput);\n * if (!ast) {\n * // What went wrong? We don't know.\n * return <ErrorFallback />;\n * }\n * ```\n */\nexport function parseMarkdownV2(content: string): MDTree | null {\n try {\n // Basic null check\n if (content == null) {\n return null;\n }\n return mdToJSON(content);\n } catch {\n return null;\n }\n}\n\n// -----------------------------------------------------------------------------\n// V3: Robust Implementation\n// -----------------------------------------------------------------------------\n\n/**\n * Creates a ParseError with the given code and message.\n */\nfunction createParseError(code: ParseErrorCode, message: string, cause?: Error): ParseError {\n return { code, message, cause };\n}\n\n/**\n * Checks content for potential XSS patterns.\n * Returns the first matched pattern or null if clean.\n */\nfunction detectXssPattern(content: string): RegExp | null {\n for (const pattern of XSS_PATTERNS) {\n if (pattern.test(content)) {\n return pattern;\n }\n }\n return null;\n}\n\n/**\n * Sanitizes content by removing dangerous patterns.\n * This is a basic sanitizer; production should use DOMPurify.\n */\nfunction sanitizeContent(content: string): string {\n let sanitized = content;\n\n // Remove script tags\n sanitized = sanitized.replace(/<script\\b[^<]*(?:(?!<\\/script>)<[^<]*)*<\\/script>/gi, '');\n\n // Remove javascript: and vbscript: URLs\n sanitized = sanitized.replace(/javascript:/gi, 'removed:');\n sanitized = sanitized.replace(/vbscript:/gi, 'removed:');\n\n // Remove event handlers (onclick, onerror, etc.)\n sanitized = sanitized.replace(/\\bon\\w+\\s*=\\s*[\"'][^\"']*[\"']/gi, '');\n sanitized = sanitized.replace(/\\bon\\w+\\s*=\\s*\\S+/gi, '');\n\n return sanitized;\n}\n\n/**\n * Robust markdown parser - production-ready with full error context.\n *\n * Features:\n * - Input validation with specific error codes\n * - Result type for explicit error handling\n * - XSS pattern detection and optional sanitization\n * - Content length limits\n * - Preserves error context for debugging\n *\n * @param content - Markdown string to parse\n * @param options - Parsing options\n * @returns Result containing the AST or a structured error\n *\n * @example\n * ```ts\n * const result = parseMarkdownV3(userInput);\n *\n * if (!result.ok) {\n * switch (result.error.code) {\n * case 'INVALID_INPUT':\n * return <InvalidInputError message={result.error.message} />;\n * case 'CONTENT_TOO_LONG':\n * return <ContentTooLongError />;\n * case 'PARSE_FAILED':\n * console.error('Parse error:', result.error.cause);\n * return <ParseError />;\n * default:\n * return <GenericError />;\n * }\n * }\n *\n * return <MarkdownRenderer ast={result.value} />;\n * ```\n */\nexport function parseMarkdownV3(\n content: string,\n options: ParseOptions = {}\n): Result<MDTree, ParseError> {\n const { maxLength = DEFAULT_MAX_LENGTH, sanitize = true, allowEmpty = false } = options;\n\n // 1. Validate input type\n if (content === null || content === undefined) {\n return err(\n createParseError(\n ParseErrorCode.INVALID_INPUT,\n `Content must be a string, received ${content === null ? 'null' : 'undefined'}`\n )\n );\n }\n\n if (typeof content !== 'string') {\n return err(\n createParseError(\n ParseErrorCode.INVALID_INPUT,\n `Content must be a string, received ${typeof content}`\n )\n );\n }\n\n // 2. Handle empty content\n const trimmed = content.trim();\n if (trimmed.length === 0 && !allowEmpty) {\n return err(\n createParseError(ParseErrorCode.EMPTY_CONTENT, 'Content is empty or contains only whitespace')\n );\n }\n\n // 3. Check content length\n if (content.length > maxLength) {\n return err(\n createParseError(\n ParseErrorCode.CONTENT_TOO_LONG,\n `Content exceeds maximum length of ${maxLength.toLocaleString()} characters ` +\n `(received ${content.length.toLocaleString()})`\n )\n );\n }\n\n // 4. Check for XSS patterns\n const xssPattern = detectXssPattern(content);\n if (xssPattern) {\n if (process.env.NODE_ENV === 'development') {\n console.warn(\n `[parseMarkdownV3] XSS pattern detected: ${xssPattern.toString()}`,\n sanitize ? 'Content will be sanitized.' : 'Sanitization is disabled!'\n );\n }\n }\n\n // 5. Optionally sanitize content\n const processedContent = sanitize ? sanitizeContent(content) : content;\n\n // 6. Parse markdown with error handling\n try {\n const ast = mdToJSON(processedContent);\n return ok(ast);\n } catch (error) {\n const cause = error instanceof Error ? error : new Error(String(error));\n return err(\n createParseError(\n ParseErrorCode.PARSE_FAILED,\n `Failed to parse markdown: ${cause.message}`,\n cause\n )\n );\n }\n}\n\n// -----------------------------------------------------------------------------\n// Default Export\n// -----------------------------------------------------------------------------\n\n/**\n * Production-ready markdown parser.\n *\n * This is an alias for `parseMarkdownV3` - the robust implementation\n * with validation, sanitization, and Result-based error handling.\n *\n * @example\n * ```ts\n * import { parseMarkdown } from '@/lib/markdown-utils';\n *\n * const result = parseMarkdown(content);\n * if (!result.ok) {\n * // Handle error with full context\n * console.error(`[${result.error.code}] ${result.error.message}`);\n * return null;\n * }\n * return result.value;\n * ```\n */\nexport const parseMarkdown = parseMarkdownV3;\n\n// -----------------------------------------------------------------------------\n// Utility Functions\n// -----------------------------------------------------------------------------\n\n/**\n * Checks if content is safe markdown (no XSS patterns detected).\n *\n * @param content - Markdown string to check\n * @returns true if no XSS patterns detected\n */\nexport function isSafeMarkdown(content: string): boolean {\n return detectXssPattern(content) === null;\n}\n\n/**\n * Estimates the word count of markdown content.\n * Useful for content length limits and reading time estimates.\n *\n * @param content - Markdown string to count\n * @returns Estimated word count\n */\nexport function estimateWordCount(content: string): number {\n // Remove markdown syntax for more accurate count\n const plainText = content\n .replace(/#+\\s/g, '') // Remove headings\n .replace(/\\*\\*|__|~~|`/g, '') // Remove formatting\n .replace(/\\[([^\\]]+)\\]\\([^)]+\\)/g, '$1') // Convert links to text\n .replace(/!\\[([^\\]]*)\\]\\([^)]+\\)/g, '') // Remove images\n .replace(/```[\\s\\S]*?```/g, '') // Remove code blocks\n .replace(/`[^`]+`/g, ''); // Remove inline code\n\n const words = plainText.trim().split(/\\s+/).filter(Boolean);\n return words.length;\n}\n\n/**\n * Estimates reading time for markdown content.\n *\n * @param content - Markdown string\n * @param wordsPerMinute - Reading speed (default 200 WPM)\n * @returns Reading time in minutes\n */\nexport function estimateReadingTime(content: string, wordsPerMinute = 200): number {\n const wordCount = estimateWordCount(content);\n return Math.ceil(wordCount / wordsPerMinute);\n}\n","/**\n * Result Type Utilities\n *\n * Go-style error handling with discriminated union types.\n * Provides type-safe success/error handling without exceptions.\n *\n * @example\n * ```ts\n * const [err, data] = await handle(async () => {\n * const response = await fetch(url);\n * if (!response.ok) throw new Error(`HTTP ${response.status}`);\n * return response.json();\n * });\n *\n * if (err) {\n * console.error('Failed:', err.message);\n * return { error: err.message };\n * }\n * return { data };\n * ```\n */\n\n// -----------------------------------------------------------------------------\n// Result Type\n// -----------------------------------------------------------------------------\n\n/**\n * A discriminated union representing either success or failure.\n *\n * @template T - The success value type\n * @template E - The error type (defaults to Error)\n *\n * @example\n * ```ts\n * function divide(a: number, b: number): Result<number, string> {\n * if (b === 0) return err('Division by zero');\n * return ok(a / b);\n * }\n *\n * const result = divide(10, 2);\n * if (isOk(result)) {\n * console.log('Result:', result.value); // 5\n * } else {\n * console.error('Error:', result.error);\n * }\n * ```\n */\nexport type Result<T, E = Error> = { ok: true; value: T } | { ok: false; error: E };\n\n// -----------------------------------------------------------------------------\n// Constructors\n// -----------------------------------------------------------------------------\n\n/**\n * Creates a success Result.\n *\n * @param value - The success value\n * @returns A success Result containing the value\n *\n * @example\n * ```ts\n * const result = ok(42);\n * // { ok: true, value: 42 }\n * ```\n */\nexport function ok<T>(value: T): Result<T, never> {\n return { ok: true, value };\n}\n\n/**\n * Creates a failure Result.\n *\n * @param error - The error value\n * @returns A failure Result containing the error\n *\n * @example\n * ```ts\n * const result = err(new Error('Something went wrong'));\n * // { ok: false, error: Error('Something went wrong') }\n * ```\n */\nexport function err<E>(error: E): Result<never, E> {\n return { ok: false, error };\n}\n\n// -----------------------------------------------------------------------------\n// Type Guards\n// -----------------------------------------------------------------------------\n\n/**\n * Type guard to check if a Result is successful.\n *\n * @param result - The Result to check\n * @returns true if the Result is a success\n *\n * @example\n * ```ts\n * const result = fetchData();\n * if (isOk(result)) {\n * // TypeScript knows result.value exists here\n * console.log(result.value);\n * }\n * ```\n */\nexport function isOk<T, E>(result: Result<T, E>): result is { ok: true; value: T } {\n return result.ok === true;\n}\n\n/**\n * Type guard to check if a Result is a failure.\n *\n * @param result - The Result to check\n * @returns true if the Result is a failure\n *\n * @example\n * ```ts\n * const result = fetchData();\n * if (isErr(result)) {\n * // TypeScript knows result.error exists here\n * console.error(result.error);\n * }\n * ```\n */\nexport function isErr<T, E>(result: Result<T, E>): result is { ok: false; error: E } {\n return result.ok === false;\n}\n\n// -----------------------------------------------------------------------------\n// Extractors\n// -----------------------------------------------------------------------------\n\n/**\n * Extracts the value from a Result, throwing if it's an error.\n *\n * @param result - The Result to unwrap\n * @returns The success value\n * @throws The error if Result is a failure\n *\n * @example\n * ```ts\n * const result = ok(42);\n * const value = unwrap(result); // 42\n *\n * const errorResult = err(new Error('fail'));\n * const value2 = unwrap(errorResult); // throws Error('fail')\n * ```\n */\nexport function unwrap<T, E>(result: Result<T, E>): T {\n if (isOk(result)) {\n return result.value;\n }\n throw result.error;\n}\n\n/**\n * Extracts the value from a Result, returning a default on error.\n *\n * @param result - The Result to unwrap\n * @param defaultValue - The value to return if Result is an error\n * @returns The success value or the default value\n *\n * @example\n * ```ts\n * const result = err(new Error('fail'));\n * const value = unwrapOr(result, 0); // 0\n *\n * const okResult = ok(42);\n * const value2 = unwrapOr(okResult, 0); // 42\n * ```\n */\nexport function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T {\n if (isOk(result)) {\n return result.value;\n }\n return defaultValue;\n}\n\n/**\n * Extracts the value from a Result, computing a default on error.\n *\n * @param result - The Result to unwrap\n * @param fn - Function to compute the default value from the error\n * @returns The success value or the computed default\n *\n * @example\n * ```ts\n * const result = err(new Error('not found'));\n * const value = unwrapOrElse(result, (e) => {\n * console.error('Error:', e.message);\n * return [];\n * });\n * ```\n */\nexport function unwrapOrElse<T, E>(result: Result<T, E>, fn: (error: E) => T): T {\n if (isOk(result)) {\n return result.value;\n }\n return fn(result.error);\n}\n\n// -----------------------------------------------------------------------------\n// Transformers\n// -----------------------------------------------------------------------------\n\n/**\n * Maps a successful Result's value.\n *\n * @param result - The Result to map\n * @param fn - Function to transform the value\n * @returns A new Result with the transformed value, or the original error\n *\n * @example\n * ```ts\n * const result = ok(5);\n * const doubled = map(result, (n) => n * 2); // ok(10)\n *\n * const errorResult = err('fail');\n * const still = map(errorResult, (n) => n * 2); // err('fail')\n * ```\n */\nexport function map<T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E> {\n if (isOk(result)) {\n return ok(fn(result.value));\n }\n return result;\n}\n\n/**\n * Maps a failed Result's error.\n *\n * @param result - The Result to map\n * @param fn - Function to transform the error\n * @returns A new Result with the transformed error, or the original value\n *\n * @example\n * ```ts\n * const result = err('not found');\n * const mapped = mapErr(result, (e) => new Error(e)); // err(Error('not found'))\n * ```\n */\nexport function mapErr<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F> {\n if (isErr(result)) {\n return err(fn(result.error));\n }\n return result;\n}\n\n/**\n * Chains Result-returning operations.\n *\n * @param result - The Result to chain from\n * @param fn - Function that returns a new Result\n * @returns The chained Result\n *\n * @example\n * ```ts\n * function parse(input: string): Result<number, string> {\n * const n = parseInt(input, 10);\n * return isNaN(n) ? err('not a number') : ok(n);\n * }\n *\n * function double(n: number): Result<number, string> {\n * return ok(n * 2);\n * }\n *\n * const result = flatMap(parse('5'), double); // ok(10)\n * const fail = flatMap(parse('abc'), double); // err('not a number')\n * ```\n */\nexport function flatMap<T, U, E>(\n result: Result<T, E>,\n fn: (value: T) => Result<U, E>\n): Result<U, E> {\n if (isOk(result)) {\n return fn(result.value);\n }\n return result;\n}\n\n// -----------------------------------------------------------------------------\n// Async Helpers\n// -----------------------------------------------------------------------------\n\n/**\n * Wraps a Promise in a Result type.\n *\n * @param promise - The Promise to wrap\n * @returns A Promise that resolves to a Result\n *\n * @example\n * ```ts\n * const result = await fromPromise(fetch('/api/data'));\n * if (isErr(result)) {\n * console.error('Fetch failed:', result.error);\n * return;\n * }\n * const response = result.value;\n * ```\n */\nexport async function fromPromise<T>(promise: Promise<T>): Promise<Result<T, Error>> {\n try {\n const value = await promise;\n return ok(value);\n } catch (error) {\n return err(error instanceof Error ? error : new Error(String(error)));\n }\n}\n\n/**\n * Wraps a throwing function in a Result type.\n *\n * @param fn - The function to wrap\n * @returns A Result containing the return value or the thrown error\n *\n * @example\n * ```ts\n * const result = tryCatch(() => JSON.parse(input));\n * if (isErr(result)) {\n * console.error('Invalid JSON:', result.error);\n * return null;\n * }\n * return result.value;\n * ```\n */\nexport function tryCatch<T>(fn: () => T): Result<T, Error> {\n try {\n return ok(fn());\n } catch (error) {\n return err(error instanceof Error ? error : new Error(String(error)));\n }\n}\n\n/**\n * Wraps an async function in a Result type.\n *\n * @param fn - The async function to wrap\n * @returns A Promise that resolves to a Result\n *\n * @example\n * ```ts\n * const result = await tryCatchAsync(async () => {\n * const response = await fetch('/api/data');\n * if (!response.ok) throw new Error(`HTTP ${response.status}`);\n * return response.json();\n * });\n * ```\n */\nexport async function tryCatchAsync<T>(fn: () => Promise<T>): Promise<Result<T, Error>> {\n try {\n const value = await fn();\n return ok(value);\n } catch (error) {\n return err(error instanceof Error ? error : new Error(String(error)));\n }\n}\n\n// -----------------------------------------------------------------------------\n// Tuple Helpers (Go-style)\n// -----------------------------------------------------------------------------\n\n/**\n * Go-style tuple for error handling: [error, value]\n *\n * @example\n * ```ts\n * const [err, data] = await handle(fetchData);\n * if (err) {\n * console.error(err);\n * return;\n * }\n * console.log(data);\n * ```\n */\nexport type GoTuple<T, E = Error> = [E, undefined] | [undefined, T];\n\n/**\n * Converts a Result to a Go-style tuple.\n *\n * @param result - The Result to convert\n * @returns A tuple of [error, value]\n *\n * @example\n * ```ts\n * const result = await fetchData();\n * const [err, data] = toTuple(result);\n * ```\n */\nexport function toTuple<T, E>(result: Result<T, E>): GoTuple<T, E> {\n if (isOk(result)) {\n return [undefined, result.value];\n }\n return [result.error, undefined];\n}\n\n/**\n * Wraps an async function and returns a Go-style tuple.\n *\n * @param fn - The async function to wrap\n * @returns A Promise that resolves to [error, value] tuple\n *\n * @example\n * ```ts\n * const [err, data] = await handle(async () => {\n * const res = await fetch('/api/users');\n * if (!res.ok) throw new Error(`HTTP ${res.status}`);\n * return res.json();\n * });\n *\n * if (err) {\n * console.error('Failed to fetch users:', err.message);\n * return [];\n * }\n * return data;\n * ```\n */\nexport async function handle<T>(fn: () => Promise<T>): Promise<GoTuple<T, Error>> {\n try {\n const value = await fn();\n return [undefined, value];\n } catch (error) {\n const err = error instanceof Error ? error : new Error(String(error));\n return [err, undefined];\n }\n}\n\n/**\n * Wraps a synchronous function and returns a Go-style tuple.\n *\n * @param fn - The function to wrap\n * @returns A tuple of [error, value]\n *\n * @example\n * ```ts\n * const [err, parsed] = handleSync(() => JSON.parse(input));\n * if (err) {\n * console.error('Invalid JSON:', err.message);\n * return null;\n * }\n * return parsed;\n * ```\n */\nexport function handleSync<T>(fn: () => T): GoTuple<T, Error> {\n try {\n const value = fn();\n return [undefined, value];\n } catch (error) {\n const err = error instanceof Error ? error : new Error(String(error));\n return [err, undefined];\n }\n}\n"],"mappings":";AAYA,SAAS,gBAAgB;;;ACqDlB,SAAS,GAAM,OAA4B;AAChD,SAAO,EAAE,IAAI,M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AE,EAC3B,QAAQ,0BAA0B,IAAI,EACtC,QAAQ,2BAA2B,EAAE,EACrC,QAAQ,mBAAmB,EAAE,EAC7B,QAAQ,YAAY,EAAE;AAEzB,QAAM,QAAQ,UAAU,KAAK,EAAE,MAAM,KAAK,EAAE,OAAO,OAAO;AAC1D,SAAO,MAAM;AACf;AASO,SAAS,oBAAoB,SAAiB,iBAAiB,KAAa;AACjF,QAAM,YAAY,kBAAkB,OAAO;AAC3C,SAAO,KAAK,KAAK,YAAY,cAAc;AAC7C;","names":[]}
|
package/dist/lib/renderer.js
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BlockRenderer
|
|
3
|
-
} from "../chunk-6QR5B5KQ.js";
|
|
4
|
-
import {
|
|
5
|
-
getCmsClient
|
|
6
|
-
} from "../chunk-JHKDRASN.js";
|
|
7
|
-
|
|
8
1
|
// ../../packages/cms-schema/src/blocks/article.ts
|
|
9
2
|
function normalizeArticleContent(payload) {
|
|
10
3
|
if (!payload || typeof payload !== "object") {
|
|
@@ -248,7 +241,326 @@ function isValidBlockSchemaName(name) {
|
|
|
248
241
|
// lib/renderer.tsx
|
|
249
242
|
import { unstable_noStore } from "next/cache";
|
|
250
243
|
import { notFound } from "next/navigation";
|
|
251
|
-
|
|
244
|
+
|
|
245
|
+
// lib/block-renderer.tsx
|
|
246
|
+
import React from "react";
|
|
247
|
+
import { BlockToolbar } from "./block-toolbar.js";
|
|
248
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
249
|
+
function walkReactNode(node, visitors, ctx = {}) {
|
|
250
|
+
const path = ctx.path ?? [];
|
|
251
|
+
if (node == null || typeof node === "boolean") return node;
|
|
252
|
+
if (typeof node === "string" || typeof node === "number") {
|
|
253
|
+
const value = String(node);
|
|
254
|
+
return visitors.onText ? visitors.onText({ value, path, parentType: ctx.parentType, key: ctx.key }) : node;
|
|
255
|
+
}
|
|
256
|
+
if (Array.isArray(node)) {
|
|
257
|
+
return node.map((child, i) => {
|
|
258
|
+
const childKey = child?.key ?? null;
|
|
259
|
+
const result = walkReactNode(child, visitors, {
|
|
260
|
+
path: [...path, i],
|
|
261
|
+
parentType: ctx.parentType,
|
|
262
|
+
key: childKey
|
|
263
|
+
});
|
|
264
|
+
if (React.isValidElement(result) && result.key == null) {
|
|
265
|
+
return React.cloneElement(result, { key: childKey ?? `arr-${path.join("-")}-${i}` });
|
|
266
|
+
}
|
|
267
|
+
return result;
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
if (React.isValidElement(node)) {
|
|
271
|
+
const el = node;
|
|
272
|
+
const elProps = el.props;
|
|
273
|
+
const hasChildren = elProps && "children" in elProps;
|
|
274
|
+
const nextChildren = hasChildren ? React.Children.map(elProps.children, (child, i) => {
|
|
275
|
+
const childKey = child?.key ?? null;
|
|
276
|
+
const result = walkReactNode(child, visitors, {
|
|
277
|
+
path: [...path, "children", i],
|
|
278
|
+
parentType: el.type,
|
|
279
|
+
key: childKey
|
|
280
|
+
});
|
|
281
|
+
if (React.isValidElement(result) && result.key == null) {
|
|
282
|
+
return React.cloneElement(result, { key: childKey ?? `child-${path.join("-")}-${i}` });
|
|
283
|
+
}
|
|
284
|
+
return result;
|
|
285
|
+
}) : elProps?.children;
|
|
286
|
+
const cloned = hasChildren ? React.cloneElement(el, void 0, nextChildren) : el;
|
|
287
|
+
return visitors.onElement ? visitors.onElement({ element: cloned, path }) : cloned;
|
|
288
|
+
}
|
|
289
|
+
return node;
|
|
290
|
+
}
|
|
291
|
+
function extractContentValues(content, basePath = []) {
|
|
292
|
+
const map = /* @__PURE__ */ new Map();
|
|
293
|
+
function walk(obj, path) {
|
|
294
|
+
if (typeof obj === "string" && obj.trim() !== "") {
|
|
295
|
+
const contentPath = path.join(".");
|
|
296
|
+
const existing = map.get(obj) || [];
|
|
297
|
+
existing.push({ contentPath, value: obj });
|
|
298
|
+
map.set(obj, existing);
|
|
299
|
+
} else if (Array.isArray(obj)) {
|
|
300
|
+
for (let index = 0; index < obj.length; index++) {
|
|
301
|
+
walk(obj[index], [...path, String(index)]);
|
|
302
|
+
}
|
|
303
|
+
} else if (obj && typeof obj === "object") {
|
|
304
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
305
|
+
walk(value, [...path, key]);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
walk(content, basePath);
|
|
310
|
+
return map;
|
|
311
|
+
}
|
|
312
|
+
function renderToWalkableTree(node, keyPrefix = "") {
|
|
313
|
+
if (node == null || typeof node === "boolean") return node;
|
|
314
|
+
if (typeof node === "string" || typeof node === "number") return node;
|
|
315
|
+
if (Array.isArray(node)) {
|
|
316
|
+
return node.map((child, i) => {
|
|
317
|
+
const result = renderToWalkableTree(child, `${keyPrefix}${i}-`);
|
|
318
|
+
if (React.isValidElement(result) && result.key == null) {
|
|
319
|
+
const existingKey = child?.key;
|
|
320
|
+
return React.cloneElement(result, { key: existingKey ?? `${keyPrefix}${i}` });
|
|
321
|
+
}
|
|
322
|
+
return result;
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
if (React.isValidElement(node)) {
|
|
326
|
+
const el = node;
|
|
327
|
+
const elProps = el.props;
|
|
328
|
+
if (typeof el.type === "function") {
|
|
329
|
+
try {
|
|
330
|
+
const rendered = el.type(el.props);
|
|
331
|
+
return renderToWalkableTree(rendered, keyPrefix);
|
|
332
|
+
} catch {
|
|
333
|
+
return node;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if (elProps && "children" in elProps) {
|
|
337
|
+
const newChildren = renderToWalkableTree(elProps.children, keyPrefix);
|
|
338
|
+
return React.cloneElement(el, void 0, newChildren);
|
|
339
|
+
}
|
|
340
|
+
return node;
|
|
341
|
+
}
|
|
342
|
+
return node;
|
|
343
|
+
}
|
|
344
|
+
function BlockRenderer({ block, registry, disableEditable }) {
|
|
345
|
+
const Component = registry[block.type];
|
|
346
|
+
if (!Component) {
|
|
347
|
+
if (process.env.NODE_ENV === "development") {
|
|
348
|
+
console.warn(`[BlockRenderer] Unknown block type: ${block.type}`);
|
|
349
|
+
}
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
const component = /* @__PURE__ */ jsx(Component, { content: block.content });
|
|
353
|
+
if (disableEditable) {
|
|
354
|
+
return component;
|
|
355
|
+
}
|
|
356
|
+
const renderedTree = renderToWalkableTree(component);
|
|
357
|
+
const contentValueMap = extractContentValues(block.content);
|
|
358
|
+
const usedPaths = /* @__PURE__ */ new Set();
|
|
359
|
+
let isRoot = true;
|
|
360
|
+
const wrappedComponent = walkReactNode(renderedTree, {
|
|
361
|
+
onText: ({ value, key, path }) => {
|
|
362
|
+
const matches = contentValueMap.get(value);
|
|
363
|
+
if (!matches || matches.length === 0) {
|
|
364
|
+
return value;
|
|
365
|
+
}
|
|
366
|
+
const match = matches.find((m) => !usedPaths.has(m.contentPath)) ?? matches[0];
|
|
367
|
+
if (!match) return value;
|
|
368
|
+
usedPaths.add(match.contentPath);
|
|
369
|
+
const spanKey = key ?? `${block.id}-${match.contentPath}-${path.join("-")}`;
|
|
370
|
+
return /* @__PURE__ */ jsx(
|
|
371
|
+
"span",
|
|
372
|
+
{
|
|
373
|
+
"data-cms-editable": true,
|
|
374
|
+
"data-block-id": block.id,
|
|
375
|
+
"data-block-type": block.type,
|
|
376
|
+
"data-content-path": match.contentPath,
|
|
377
|
+
children: value
|
|
378
|
+
},
|
|
379
|
+
spanKey
|
|
380
|
+
);
|
|
381
|
+
},
|
|
382
|
+
onElement: ({ element, path }) => {
|
|
383
|
+
if (isRoot && path.length === 0) {
|
|
384
|
+
isRoot = false;
|
|
385
|
+
const elProps = element.props;
|
|
386
|
+
const existingChildren = elProps?.children;
|
|
387
|
+
return React.cloneElement(
|
|
388
|
+
element,
|
|
389
|
+
{
|
|
390
|
+
"data-cms-block": true,
|
|
391
|
+
"data-block-id": block.id,
|
|
392
|
+
"data-block-type": block.type
|
|
393
|
+
},
|
|
394
|
+
existingChildren,
|
|
395
|
+
/* @__PURE__ */ jsx(BlockToolbar, { blockId: block.id }, "cms-toolbar")
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
return element;
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
return /* @__PURE__ */ jsxs("div", { style: { display: "contents" }, children: [
|
|
402
|
+
/* @__PURE__ */ jsx("style", { children: `
|
|
403
|
+
[data-cms-block] {
|
|
404
|
+
position: relative;
|
|
405
|
+
}
|
|
406
|
+
[data-cms-block]:hover {
|
|
407
|
+
outline: 2px solid #3b82f6;
|
|
408
|
+
outline-offset: 4px;
|
|
409
|
+
}
|
|
410
|
+
[data-cms-editable] {
|
|
411
|
+
cursor: pointer;
|
|
412
|
+
border-radius: 2px;
|
|
413
|
+
}
|
|
414
|
+
[data-cms-editable]:hover {
|
|
415
|
+
outline: 2px solid #3b82f6;
|
|
416
|
+
outline-offset: 2px;
|
|
417
|
+
}
|
|
418
|
+
.cms-block-toolbar {
|
|
419
|
+
position: absolute;
|
|
420
|
+
bottom: -16px;
|
|
421
|
+
left: 50%;
|
|
422
|
+
transform: translateX(-50%);
|
|
423
|
+
display: flex;
|
|
424
|
+
gap: 4px;
|
|
425
|
+
background: #1f2937;
|
|
426
|
+
border-radius: 6px;
|
|
427
|
+
padding: 4px;
|
|
428
|
+
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
|
|
429
|
+
opacity: 0;
|
|
430
|
+
pointer-events: none;
|
|
431
|
+
transition: opacity 0.15s ease;
|
|
432
|
+
z-index: 1000;
|
|
433
|
+
}
|
|
434
|
+
[data-cms-block]:hover .cms-block-toolbar {
|
|
435
|
+
opacity: 1;
|
|
436
|
+
pointer-events: auto;
|
|
437
|
+
}
|
|
438
|
+
.cms-block-toolbar button {
|
|
439
|
+
display: flex;
|
|
440
|
+
align-items: center;
|
|
441
|
+
justify-content: center;
|
|
442
|
+
width: 28px;
|
|
443
|
+
height: 28px;
|
|
444
|
+
border: none;
|
|
445
|
+
background: transparent;
|
|
446
|
+
color: #9ca3af;
|
|
447
|
+
border-radius: 4px;
|
|
448
|
+
cursor: pointer;
|
|
449
|
+
transition: background 0.15s ease, color 0.15s ease;
|
|
450
|
+
}
|
|
451
|
+
.cms-block-toolbar button:hover {
|
|
452
|
+
background: #374151;
|
|
453
|
+
color: #fff;
|
|
454
|
+
}
|
|
455
|
+
.cms-block-toolbar button.delete:hover {
|
|
456
|
+
background: #dc2626;
|
|
457
|
+
color: #fff;
|
|
458
|
+
}
|
|
459
|
+
.cms-block-toolbar button:disabled {
|
|
460
|
+
opacity: 0.4;
|
|
461
|
+
cursor: not-allowed;
|
|
462
|
+
}
|
|
463
|
+
.cms-block-toolbar button:disabled:hover {
|
|
464
|
+
background: transparent;
|
|
465
|
+
color: #9ca3af;
|
|
466
|
+
}
|
|
467
|
+
.cms-block-toolbar svg {
|
|
468
|
+
width: 16px;
|
|
469
|
+
height: 16px;
|
|
470
|
+
}
|
|
471
|
+
` }),
|
|
472
|
+
/* @__PURE__ */ jsx(
|
|
473
|
+
"script",
|
|
474
|
+
{
|
|
475
|
+
dangerouslySetInnerHTML: {
|
|
476
|
+
__html: `
|
|
477
|
+
(function() {
|
|
478
|
+
if (window.__cmsEditableInitialized) return;
|
|
479
|
+
window.__cmsEditableInitialized = true;
|
|
480
|
+
|
|
481
|
+
document.addEventListener('click', function(e) {
|
|
482
|
+
// Ignore toolbar clicks
|
|
483
|
+
if (e.target.closest('.cms-block-toolbar')) {
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// Check for editable text click first (more specific)
|
|
488
|
+
var editableTarget = e.target.closest('[data-cms-editable]');
|
|
489
|
+
if (editableTarget) {
|
|
490
|
+
var message = {
|
|
491
|
+
type: 'cms-editable-click',
|
|
492
|
+
blockId: editableTarget.getAttribute('data-block-id'),
|
|
493
|
+
blockType: editableTarget.getAttribute('data-block-type'),
|
|
494
|
+
contentPath: editableTarget.getAttribute('data-content-path')
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
if (window.parent && window.parent !== window) {
|
|
498
|
+
window.parent.postMessage(message, '*');
|
|
499
|
+
}
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// Check for block-level click
|
|
504
|
+
var blockTarget = e.target.closest('[data-cms-block]');
|
|
505
|
+
if (blockTarget) {
|
|
506
|
+
var message = {
|
|
507
|
+
type: 'cms-editable-click',
|
|
508
|
+
blockId: blockTarget.getAttribute('data-block-id'),
|
|
509
|
+
blockType: blockTarget.getAttribute('data-block-type'),
|
|
510
|
+
contentPath: null
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
if (window.parent && window.parent !== window) {
|
|
514
|
+
window.parent.postMessage(message, '*');
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
})();
|
|
519
|
+
`
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
),
|
|
523
|
+
wrappedComponent
|
|
524
|
+
] }, block.id);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// lib/cms-api.ts
|
|
528
|
+
import { createTRPCClient, httpBatchLink } from "@trpc/client";
|
|
529
|
+
import superjson from "superjson";
|
|
530
|
+
function getCmsApiUrl(cmsUrl) {
|
|
531
|
+
return `${cmsUrl}/api/trpc`;
|
|
532
|
+
}
|
|
533
|
+
function createFetchWithApiKey(apiKey) {
|
|
534
|
+
return async (url, options) => {
|
|
535
|
+
let finalUrl = url;
|
|
536
|
+
if (apiKey) {
|
|
537
|
+
const urlObj = new URL(url.toString());
|
|
538
|
+
urlObj.searchParams.set("api_key", apiKey);
|
|
539
|
+
finalUrl = urlObj.toString();
|
|
540
|
+
}
|
|
541
|
+
const response = await fetch(finalUrl, options);
|
|
542
|
+
return response;
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
function createCmsClient(options) {
|
|
546
|
+
const url = getCmsApiUrl(options.cmsUrl);
|
|
547
|
+
console.log("[CMS API] Creating client with URL:", url);
|
|
548
|
+
return createTRPCClient({
|
|
549
|
+
links: [
|
|
550
|
+
httpBatchLink({
|
|
551
|
+
url,
|
|
552
|
+
transformer: superjson,
|
|
553
|
+
fetch: createFetchWithApiKey(options.apiKey)
|
|
554
|
+
})
|
|
555
|
+
]
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
function getCmsClient(options) {
|
|
559
|
+
return createCmsClient(options);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// lib/renderer.tsx
|
|
563
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
252
564
|
function getWebsiteId(providedWebsiteId) {
|
|
253
565
|
const websiteId = providedWebsiteId ?? process.env.NEXT_PUBLIC_WEBSITE_ID ?? process.env.WEBSITE_ID ?? process.env.CMS_WEBSITE_ID;
|
|
254
566
|
if (!websiteId) {
|
|
@@ -347,7 +659,7 @@ async function ParametricRoutePage({
|
|
|
347
659
|
content
|
|
348
660
|
});
|
|
349
661
|
}
|
|
350
|
-
return /* @__PURE__ */
|
|
662
|
+
return /* @__PURE__ */ jsx2("main", { children: blocks.map((block) => /* @__PURE__ */ jsx2(
|
|
351
663
|
BlockRenderer,
|
|
352
664
|
{
|
|
353
665
|
block,
|