@marvalt/wparser 0.1.49 → 0.1.50

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/index.d.ts CHANGED
@@ -493,11 +493,6 @@ declare function extractBackgroundColor(block: WordPressBlock, context: RenderCo
493
493
  * @returns CSS class string (e.g., 'text-white') or null if no mapping
494
494
  */
495
495
  declare function extractTextColor(block: WordPressBlock, context: RenderContext): string | null;
496
- /**
497
- * Infer a readable text color class based on a background class string.
498
- * Supports bg-[#hex] and bg-[rgb(...)].
499
- */
500
- declare function inferTextColorFromBackground(backgroundClass: string | null): string | null;
501
496
  /**
502
497
  * Extract spacer height from block attributes or innerHTML
503
498
  * Returns height in pixels, or null if not found
@@ -555,5 +550,5 @@ interface SectionWrapperProps {
555
550
  */
556
551
  declare const SectionWrapper: React$1.FC<SectionWrapperProps>;
557
552
 
558
- export { SectionWrapper, WPContent, WPErrorBoundary, WPPage, buildClassName, convertImageToCloudflareVariant, convertImageUrl, convertImageUrls, createDefaultRegistry, createEnhancedRegistry, extractAlignment, extractBackgroundColor, extractBackgroundImage, extractButtonsFromInnerBlocks, extractContent, extractDimRatio, extractFontSize, extractHeadingLevel, extractImageAttributes, extractImageUrl, extractImageUrlWithFallback, extractMediaPosition, extractMinHeight, extractOverlayColor, extractSpacerHeight, extractSubtitleFromInnerBlocks, extractTextAlign, extractTextAlignFromInnerBlocks, extractTextColor, extractTextFromHTML, extractTitle, extractTitleFromInnerBlocks, extractVerticalAlignment, extractVideoIframeFromInnerBlocks, findMatchingMapping, findShortcodes, getAlignmentClasses, getBlockTextContent, getCloudflareVariantUrl, getContainerClasses, getContentSpacingClasses, getFontSizeClasses, getImageAttributes, getImageUrl, getSectionSpacingClasses, getTextAlignClasses, inferTextColorFromBackground, isCloudflareImageUrl, isValidCloudflareUrl, matchesPattern, parseContentPosition, parseGutenbergBlocks, parseShortcodeAttrs, renderNodes, renderTextWithShortcodes };
553
+ export { SectionWrapper, WPContent, WPErrorBoundary, WPPage, buildClassName, convertImageToCloudflareVariant, convertImageUrl, convertImageUrls, createDefaultRegistry, createEnhancedRegistry, extractAlignment, extractBackgroundColor, extractBackgroundImage, extractButtonsFromInnerBlocks, extractContent, extractDimRatio, extractFontSize, extractHeadingLevel, extractImageAttributes, extractImageUrl, extractImageUrlWithFallback, extractMediaPosition, extractMinHeight, extractOverlayColor, extractSpacerHeight, extractSubtitleFromInnerBlocks, extractTextAlign, extractTextAlignFromInnerBlocks, extractTextColor, extractTextFromHTML, extractTitle, extractTitleFromInnerBlocks, extractVerticalAlignment, extractVideoIframeFromInnerBlocks, findMatchingMapping, findShortcodes, getAlignmentClasses, getBlockTextContent, getCloudflareVariantUrl, getContainerClasses, getContentSpacingClasses, getFontSizeClasses, getImageAttributes, getImageUrl, getSectionSpacingClasses, getTextAlignClasses, isCloudflareImageUrl, isValidCloudflareUrl, matchesPattern, parseContentPosition, parseGutenbergBlocks, parseShortcodeAttrs, renderNodes, renderTextWithShortcodes };
559
554
  export type { AuthMode, BlockPattern, BlockRenderer, BlockRendererProps, CloudflareVariantOptions, ColorMapper, ComponentMapping, ComponentRegistry, EnhancedRegistry, ImageConversionOptions, ParseOptions, ParsedShortcode, RenderContext, RenderOptions, SectionWrapperProps, ShortcodeAttributes, ShortcodeRenderer, SpacingConfig, WPContentProps, WPNode, WPPageProps, WordPressBlock, WordPressEmbedded, WordPressFeaturedMedia, WordPressPageMinimal, WordPressTitleField };
package/dist/index.esm.js CHANGED
@@ -2081,45 +2081,13 @@ function extractVideoIframeFromInnerBlocks(block) {
2081
2081
  */
2082
2082
  function extractBackgroundColor(block, context) {
2083
2083
  const attrs = block.attributes || {};
2084
- // Try multiple possible attribute names for background color
2085
- // WordPress can store it as backgroundColor, background, or in style
2086
- let wpColorName = attrs['backgroundColor'] ||
2087
- attrs['background'] ||
2088
- attrs['backgroundColorSlug'] ||
2089
- null;
2090
- // If not found in attributes, check className for WordPress color classes
2091
- // WordPress uses classes like: has-accent-4-background-color, has-contrast-background-color
2092
- if (!wpColorName && attrs['className']) {
2093
- const className = attrs['className'];
2094
- const colorMatch = className.match(/has-([\w-]+)-background-color/);
2095
- if (colorMatch && colorMatch[1]) {
2096
- // Convert kebab-case to slug (e.g., "accent-4" stays "accent-4", "contrast" stays "contrast")
2097
- wpColorName = colorMatch[1];
2098
- }
2099
- }
2100
- // Debug logging in development
2101
- if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'development') {
2102
- if (!wpColorName && context.colorMapper) {
2103
- console.log('🔍 extractBackgroundColor - No backgroundColor found in block:', {
2104
- blockName: block.name,
2105
- hasClassName: !!attrs['className'],
2106
- className: attrs['className'],
2107
- hasStyle: !!attrs['style'],
2108
- availableAttrs: Object.keys(attrs),
2109
- });
2110
- }
2111
- }
2084
+ const wpColorName = attrs['backgroundColor'] || attrs['background'];
2112
2085
  if (!wpColorName || typeof wpColorName !== 'string') {
2113
2086
  return null;
2114
2087
  }
2115
2088
  // Use colorMapper from context if available
2116
2089
  if (context.colorMapper) {
2117
- const result = context.colorMapper(wpColorName);
2118
- // Debug logging
2119
- if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'development') {
2120
- console.log('🎨 extractBackgroundColor - Mapped', wpColorName, '→', result);
2121
- }
2122
- return result;
2090
+ return context.colorMapper(wpColorName);
2123
2091
  }
2124
2092
  // Fallback: return null (no background applied)
2125
2093
  return null;
@@ -2134,102 +2102,34 @@ function extractBackgroundColor(block, context) {
2134
2102
  */
2135
2103
  function extractTextColor(block, context) {
2136
2104
  const attrs = block.attributes || {};
2137
- const mapper = context.colorMapper;
2138
- // Try multiple possible attribute names for text color
2139
- let wpColorName = attrs['textColor'] ||
2140
- attrs['text'] ||
2141
- attrs['textColorSlug'] ||
2142
- null;
2143
- // If not found in attributes, check className for WordPress color classes
2144
- // WordPress uses classes like: has-accent-4-color, has-contrast-color
2145
- // Note: We need to avoid matching has-{color}-background-color, so we check for -color that's not followed by -background
2146
- if (!wpColorName && attrs['className']) {
2147
- const className = attrs['className'];
2148
- // Match: has-{color}-color (text color class, not background-color)
2149
- // This regex matches "has-accent-4-color" but not "has-accent-4-background-color"
2150
- const colorMatch = className.match(/has-([\w-]+)-color(?!-background)/);
2151
- if (colorMatch && colorMatch[1]) {
2152
- wpColorName = colorMatch[1];
2153
- }
2154
- // Enhanced debug logging
2155
- if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'development') {
2156
- if (!wpColorName) {
2157
- const hasColorClass = className.includes('-color');
2158
- if (hasColorClass && context.colorMapper) {
2159
- console.log('🔍 extractTextColor - Found color class in className but no match:', {
2160
- blockName: block.name,
2161
- className: className,
2162
- colorClasses: className.split(' ').filter(c => c.includes('-color')),
2163
- allAttrs: Object.keys(attrs),
2164
- });
2165
- }
2166
- }
2167
- else {
2168
- console.log('✅ extractTextColor - Found color in className:', wpColorName);
2169
- }
2170
- }
2171
- }
2105
+ const wpColorName = attrs['textColor'] || attrs['text'];
2172
2106
  if (!wpColorName || typeof wpColorName !== 'string') {
2173
- wpColorName = null;
2107
+ return null;
2108
+ }
2109
+ // Special handling for common WordPress color names when used as text color
2110
+ // These mappings take precedence because text color semantics differ from background color
2111
+ const textColorMap = {
2112
+ 'contrast': 'text-gray-900', // Contrast text is typically dark/black (opposite of contrast background)
2113
+ 'base': 'text-white', // Base text on dark backgrounds
2114
+ };
2115
+ // Check special text color mappings first
2116
+ if (textColorMap[wpColorName]) {
2117
+ return textColorMap[wpColorName];
2174
2118
  }
2175
2119
  // Use colorMapper from context if available
2176
- // Note: colorMapper returns combined classes like "bg-[#FBFAF3] text-gray-900"
2177
- // We need to extract just the text color part
2178
- if (mapper && wpColorName) {
2179
- const mapped = mapper(wpColorName);
2120
+ // Note: colorMapper might return combined classes like "bg-gray-900 text-white"
2121
+ // We'll extract just the text color part
2122
+ if (context.colorMapper) {
2123
+ const mapped = context.colorMapper(wpColorName);
2180
2124
  if (mapped) {
2181
- // If the mapper provided a background arbitrary value, derive a text color class directly from it
2182
- const bgArbitrary = mapped.match(/\bbg-\[([^\]]+)\]/);
2183
- if (bgArbitrary && bgArbitrary[1]) {
2184
- const derivedText = `text-[${bgArbitrary[1]}]`;
2185
- return derivedText;
2125
+ // If the mapped class includes text color (e.g., "bg-gray-900 text-white"),
2126
+ // extract just the text color part
2127
+ const textColorMatch = mapped.match(/\btext-\S+/);
2128
+ if (textColorMatch) {
2129
+ return textColorMatch[0];
2186
2130
  }
2187
- // Extract text color classes (e.g., "text-white", "text-gray-900", "text-[#123456]")
2188
- // Match text- classes including arbitrary values in brackets
2189
- const textColorMatch = mapped.match(/\btext-(\[[^\]]+\]|[\w-]+)/g);
2190
- if (textColorMatch && textColorMatch.length > 0) {
2191
- // Return the last text color class (in case there are multiple)
2192
- const result = textColorMatch[textColorMatch.length - 1];
2193
- // Debug logging
2194
- if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'development') {
2195
- console.log('🎨 extractTextColor - Mapped', wpColorName, '→', result, '(from:', mapped, ')');
2196
- }
2197
- return result;
2198
- }
2199
- // If no text color class found, try to determine from the background color
2200
- // For theme palette colors, we can infer text color based on brightness
2201
- // But for now, return null if no text color is in the mapped result
2202
2131
  }
2203
2132
  }
2204
- // Fallback: return null (no text color applied, will inherit from parent)
2205
- return null;
2206
- }
2207
- /**
2208
- * Infer a readable text color class based on a background class string.
2209
- * Supports bg-[#hex] and bg-[rgb(...)].
2210
- */
2211
- function inferTextColorFromBackground(backgroundClass) {
2212
- if (!backgroundClass)
2213
- return null;
2214
- // Try to extract hex color from arbitrary value class bg-[#xxxxxx]
2215
- const hexMatch = backgroundClass.match(/bg-\[#?([0-9a-fA-F]{6})\]/);
2216
- if (hexMatch) {
2217
- const hex = hexMatch[1];
2218
- const r = parseInt(hex.slice(0, 2), 16);
2219
- const g = parseInt(hex.slice(2, 4), 16);
2220
- const b = parseInt(hex.slice(4, 6), 16);
2221
- const brightness = (r * 299 + g * 587 + b * 114) / 1000;
2222
- return brightness < 140 ? 'text-white' : 'text-gray-900';
2223
- }
2224
- // Try rgb() form
2225
- const rgbMatch = backgroundClass.match(/bg-\[rgb\((\d+),\s*(\d+),\s*(\d+)\)\]/i);
2226
- if (rgbMatch) {
2227
- const r = parseInt(rgbMatch[1], 10);
2228
- const g = parseInt(rgbMatch[2], 10);
2229
- const b = parseInt(rgbMatch[3], 10);
2230
- const brightness = (r * 299 + g * 587 + b * 114) / 1000;
2231
- return brightness < 140 ? 'text-white' : 'text-gray-900';
2232
- }
2233
2133
  return null;
2234
2134
  }
2235
2135
  /**
@@ -2373,9 +2273,6 @@ const Paragraph = ({ block, context }) => {
2373
2273
  const attrs = block.attributes || {};
2374
2274
  const textAlign = getTextAlignClasses(attrs['align']);
2375
2275
  const spacing = getSpacing(context.spacingConfig || context.registry.spacingConfig, 'paragraph', 'my-6');
2376
- // Extract text color if specified
2377
- const textColor = extractTextColor(block, context);
2378
- const textColorClass = textColor || '';
2379
2276
  // Check if innerHTML contains HTML elements (like links, strong, em, etc.)
2380
2277
  const hasHTML = block.innerHTML && /<[a-z][\s\S]*>/i.test(block.innerHTML);
2381
2278
  // Check if content contains shortcodes (check both HTML and text content)
@@ -2411,17 +2308,17 @@ const Paragraph = ({ block, context }) => {
2411
2308
  const hasBlockLevelContent = React.Children.toArray(parts).some((part) => isBlockLevelElement(part));
2412
2309
  if (hasBlockLevelContent) {
2413
2310
  // Render block-level content without <p> wrapper, but add spacing wrapper
2414
- return jsxRuntimeExports.jsx("div", { className: buildClassName(spacing, textColorClass), children: parts });
2311
+ return jsxRuntimeExports.jsx("div", { className: spacing, children: parts });
2415
2312
  }
2416
2313
  // Render shortcode parts inside paragraph (shortcodes are processed as React components)
2417
- return jsxRuntimeExports.jsx("p", { className: buildClassName(spacing, textAlign, textColorClass), children: parts });
2314
+ return jsxRuntimeExports.jsx("p", { className: buildClassName(spacing, textAlign), children: parts });
2418
2315
  }
2419
2316
  // If innerHTML contains HTML elements but no shortcodes, render it directly (preserves links, formatting, etc.)
2420
2317
  if (hasHTML && block.innerHTML) {
2421
- return jsxRuntimeExports.jsx("p", { className: buildClassName(spacing, textAlign, textColorClass), dangerouslySetInnerHTML: { __html: htmlContent } });
2318
+ return jsxRuntimeExports.jsx("p", { className: buildClassName(spacing, textAlign), dangerouslySetInnerHTML: { __html: htmlContent } });
2422
2319
  }
2423
2320
  // No HTML and no shortcodes, just render plain text content
2424
- return jsxRuntimeExports.jsx("p", { className: buildClassName(spacing, textAlign, textColorClass), children: textContent });
2321
+ return jsxRuntimeExports.jsx("p", { className: buildClassName(spacing, textAlign), children: textContent });
2425
2322
  };
2426
2323
  const Heading = ({ block, children, context }) => {
2427
2324
  const attrs = block.attributes || {};
@@ -2434,13 +2331,10 @@ const Heading = ({ block, children, context }) => {
2434
2331
  const sizeClass = fontSize || (level === 1 ? 'text-4xl' : level === 2 ? 'text-3xl' : level === 3 ? 'text-2xl' : 'text-xl');
2435
2332
  // Get spacing from config with improved defaults
2436
2333
  const spacingClass = getHeadingSpacing(context.spacingConfig || context.registry.spacingConfig, level);
2437
- // Extract text color if specified, otherwise fall back to heading var (set by dstyler) or inherit
2334
+ // Extract text color if specified, otherwise inherit from parent
2438
2335
  const textColor = extractTextColor(block, context);
2439
- const isFallbackHeadingColor = !textColor;
2440
- const textColorClass = textColor || 'text-[var(--dstyler-color-heading)]';
2441
- // Inline style ensures heading color wins over inherited parent text classes
2442
- const inlineStyle = isFallbackHeadingColor ? { color: 'var(--dstyler-color-heading)' } : undefined;
2443
- return (jsxRuntimeExports.jsx(Tag, { className: buildClassName('font-bold', textColorClass, sizeClass, textAlign, spacingClass), style: inlineStyle, children: children ?? content }));
2336
+ const textColorClass = textColor || ''; // Don't hardcode - let it inherit from parent
2337
+ return (jsxRuntimeExports.jsx(Tag, { className: buildClassName('font-bold', textColorClass, sizeClass, textAlign, spacingClass), children: children ?? content }));
2444
2338
  };
2445
2339
  const Image = ({ block, context }) => {
2446
2340
  const imageAttrs = getImageAttributes(block);
@@ -2533,8 +2427,6 @@ const Group = ({ block, children, context }) => {
2533
2427
  const layout = attrs['layout'];
2534
2428
  // Extract background color using color mapper
2535
2429
  const backgroundColor = extractBackgroundColor(block, context);
2536
- // Extract text color (apply on the wrapper so children inherit)
2537
- const textColor = extractTextColor(block, context) || inferTextColorFromBackground(backgroundColor);
2538
2430
  // Determine if this is a section-level group (has alignment) or content-level
2539
2431
  const isSection = align === 'full' || align === 'wide';
2540
2432
  const containerClass = getContainerClasses(align, layout);
@@ -2548,8 +2440,8 @@ const Group = ({ block, children, context }) => {
2548
2440
  ? 'container'
2549
2441
  : containerClass;
2550
2442
  // Build className with background color if present
2551
- const className = buildClassName(finalContainerClass, spacingClass, backgroundColor, // This will be null if no mapping, which is fine
2552
- textColor);
2443
+ const className = buildClassName(finalContainerClass, spacingClass, backgroundColor // This will be null if no mapping, which is fine
2444
+ );
2553
2445
  return (jsxRuntimeExports.jsx("div", { className: className, children: children }));
2554
2446
  };
2555
2447
  const Columns = ({ block, children }) => {
@@ -2563,10 +2455,9 @@ const Column = ({ block, children, context }) => {
2563
2455
  const width = attrs['width'];
2564
2456
  // Extract background color using color mapper
2565
2457
  const backgroundColor = extractBackgroundColor(block, context);
2566
- const textColor = extractTextColor(block, context) || inferTextColorFromBackground(backgroundColor);
2567
2458
  // Handle column width (e.g., "50%" becomes flex-basis)
2568
2459
  const style = width ? { flexBasis: width } : undefined;
2569
- return (jsxRuntimeExports.jsx("div", { className: buildClassName('space-y-4 p-6 rounded-lg', backgroundColor, textColor), style: style, children: children }));
2460
+ return (jsxRuntimeExports.jsx("div", { className: buildClassName('space-y-4 p-6 rounded-lg', backgroundColor), style: style, children: children }));
2570
2461
  };
2571
2462
  const Separator = () => jsxRuntimeExports.jsx("hr", { className: "border-gray-200 my-8" });
2572
2463
  const ButtonBlock = ({ block, context }) => {
@@ -3080,5 +2971,5 @@ const SectionWrapper = ({ children, background = 'light', spacing = 'medium', co
3080
2971
  return (jsxRuntimeExports.jsx("section", { className: buildClassName(backgroundClasses[finalBackground] || backgroundClasses.light, spacingClasses[finalSpacing] || spacingClasses.medium, containerClasses[finalContainer] || containerClasses.contained, className), children: children }));
3081
2972
  };
3082
2973
 
3083
- export { SectionWrapper, WPContent, WPErrorBoundary, WPPage, buildClassName, convertImageToCloudflareVariant, convertImageUrl, convertImageUrls, createDefaultRegistry, createEnhancedRegistry, extractAlignment, extractBackgroundColor, extractBackgroundImage, extractButtonsFromInnerBlocks, extractContent, extractDimRatio, extractFontSize, extractHeadingLevel, extractImageAttributes, extractImageUrl, extractImageUrlWithFallback, extractMediaPosition, extractMinHeight, extractOverlayColor, extractSpacerHeight, extractSubtitleFromInnerBlocks, extractTextAlign, extractTextAlignFromInnerBlocks, extractTextColor, extractTextFromHTML, extractTitle, extractTitleFromInnerBlocks, extractVerticalAlignment, extractVideoIframeFromInnerBlocks, findMatchingMapping, findShortcodes, getAlignmentClasses, getBlockTextContent, getCloudflareVariantUrl, getContainerClasses, getContentSpacingClasses, getFontSizeClasses, getImageAttributes, getImageUrl, getSectionSpacingClasses, getTextAlignClasses, inferTextColorFromBackground, isCloudflareImageUrl, isValidCloudflareUrl, matchesPattern, parseContentPosition, parseGutenbergBlocks, parseShortcodeAttrs, renderNodes, renderTextWithShortcodes };
2974
+ export { SectionWrapper, WPContent, WPErrorBoundary, WPPage, buildClassName, convertImageToCloudflareVariant, convertImageUrl, convertImageUrls, createDefaultRegistry, createEnhancedRegistry, extractAlignment, extractBackgroundColor, extractBackgroundImage, extractButtonsFromInnerBlocks, extractContent, extractDimRatio, extractFontSize, extractHeadingLevel, extractImageAttributes, extractImageUrl, extractImageUrlWithFallback, extractMediaPosition, extractMinHeight, extractOverlayColor, extractSpacerHeight, extractSubtitleFromInnerBlocks, extractTextAlign, extractTextAlignFromInnerBlocks, extractTextColor, extractTextFromHTML, extractTitle, extractTitleFromInnerBlocks, extractVerticalAlignment, extractVideoIframeFromInnerBlocks, findMatchingMapping, findShortcodes, getAlignmentClasses, getBlockTextContent, getCloudflareVariantUrl, getContainerClasses, getContentSpacingClasses, getFontSizeClasses, getImageAttributes, getImageUrl, getSectionSpacingClasses, getTextAlignClasses, isCloudflareImageUrl, isValidCloudflareUrl, matchesPattern, parseContentPosition, parseGutenbergBlocks, parseShortcodeAttrs, renderNodes, renderTextWithShortcodes };
3084
2975
  //# sourceMappingURL=index.esm.js.map