@marvalt/wparser 0.1.40 → 0.1.42

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.cjs CHANGED
@@ -2083,13 +2083,45 @@ function extractVideoIframeFromInnerBlocks(block) {
2083
2083
  */
2084
2084
  function extractBackgroundColor(block, context) {
2085
2085
  const attrs = block.attributes || {};
2086
- const wpColorName = attrs['backgroundColor'] || attrs['background'];
2086
+ // Try multiple possible attribute names for background color
2087
+ // WordPress can store it as backgroundColor, background, or in style
2088
+ let wpColorName = attrs['backgroundColor'] ||
2089
+ attrs['background'] ||
2090
+ attrs['backgroundColorSlug'] ||
2091
+ null;
2092
+ // If not found in attributes, check className for WordPress color classes
2093
+ // WordPress uses classes like: has-accent-4-background-color, has-contrast-background-color
2094
+ if (!wpColorName && attrs['className']) {
2095
+ const className = attrs['className'];
2096
+ const colorMatch = className.match(/has-([\w-]+)-background-color/);
2097
+ if (colorMatch && colorMatch[1]) {
2098
+ // Convert kebab-case to slug (e.g., "accent-4" stays "accent-4", "contrast" stays "contrast")
2099
+ wpColorName = colorMatch[1];
2100
+ }
2101
+ }
2102
+ // Debug logging in development
2103
+ if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'development') {
2104
+ if (!wpColorName && context.colorMapper) {
2105
+ console.log('🔍 extractBackgroundColor - No backgroundColor found in block:', {
2106
+ blockName: block.name,
2107
+ hasClassName: !!attrs['className'],
2108
+ className: attrs['className'],
2109
+ hasStyle: !!attrs['style'],
2110
+ availableAttrs: Object.keys(attrs),
2111
+ });
2112
+ }
2113
+ }
2087
2114
  if (!wpColorName || typeof wpColorName !== 'string') {
2088
2115
  return null;
2089
2116
  }
2090
2117
  // Use colorMapper from context if available
2091
2118
  if (context.colorMapper) {
2092
- return context.colorMapper(wpColorName);
2119
+ const result = context.colorMapper(wpColorName);
2120
+ // Debug logging
2121
+ if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'development') {
2122
+ console.log('🎨 extractBackgroundColor - Mapped', wpColorName, '→', result);
2123
+ }
2124
+ return result;
2093
2125
  }
2094
2126
  // Fallback: return null (no background applied)
2095
2127
  return null;
@@ -2104,34 +2136,62 @@ function extractBackgroundColor(block, context) {
2104
2136
  */
2105
2137
  function extractTextColor(block, context) {
2106
2138
  const attrs = block.attributes || {};
2107
- const wpColorName = attrs['textColor'] || attrs['text'];
2139
+ // Try multiple possible attribute names for text color
2140
+ let wpColorName = attrs['textColor'] ||
2141
+ attrs['text'] ||
2142
+ attrs['textColorSlug'] ||
2143
+ null;
2144
+ // If not found in attributes, check className for WordPress color classes
2145
+ // WordPress uses classes like: has-accent-4-color, has-contrast-color
2146
+ // Note: We need to avoid matching has-{color}-background-color, so we check for -color that's not followed by -background
2147
+ if (!wpColorName && attrs['className']) {
2148
+ const className = attrs['className'];
2149
+ // Match: has-{color}-color (text color class, not background-color)
2150
+ // This regex matches "has-accent-4-color" but not "has-accent-4-background-color"
2151
+ const colorMatch = className.match(/has-([\w-]+)-color(?!-background)/);
2152
+ if (colorMatch && colorMatch[1]) {
2153
+ wpColorName = colorMatch[1];
2154
+ }
2155
+ // Debug logging
2156
+ if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'development' && !wpColorName) {
2157
+ // Check if there are any color-related classes
2158
+ const hasColorClass = className.includes('-color');
2159
+ if (hasColorClass && context.colorMapper) {
2160
+ console.log('🔍 extractTextColor - Found color class in className but no match:', {
2161
+ blockName: block.name,
2162
+ className: className,
2163
+ colorClasses: className.split(' ').filter(c => c.includes('-color')),
2164
+ });
2165
+ }
2166
+ }
2167
+ }
2108
2168
  if (!wpColorName || typeof wpColorName !== 'string') {
2109
2169
  return null;
2110
2170
  }
2111
- // Special handling for common WordPress color names when used as text color
2112
- // These mappings take precedence because text color semantics differ from background color
2113
- const textColorMap = {
2114
- 'contrast': 'text-gray-900', // Contrast text is typically dark/black (opposite of contrast background)
2115
- 'base': 'text-white', // Base text on dark backgrounds
2116
- };
2117
- // Check special text color mappings first
2118
- if (textColorMap[wpColorName]) {
2119
- return textColorMap[wpColorName];
2120
- }
2121
2171
  // Use colorMapper from context if available
2122
- // Note: colorMapper might return combined classes like "bg-gray-900 text-white"
2123
- // We'll extract just the text color part
2172
+ // Note: colorMapper returns combined classes like "bg-[#FBFAF3] text-gray-900"
2173
+ // We need to extract just the text color part
2124
2174
  if (context.colorMapper) {
2125
2175
  const mapped = context.colorMapper(wpColorName);
2126
2176
  if (mapped) {
2127
- // If the mapped class includes text color (e.g., "bg-gray-900 text-white"),
2128
- // extract just the text color part
2129
- const textColorMatch = mapped.match(/\btext-\S+/);
2130
- if (textColorMatch) {
2131
- return textColorMatch[0];
2177
+ // Extract text color classes (e.g., "text-white", "text-gray-900")
2178
+ // Match text- classes but not text- in the middle of other classes
2179
+ const textColorMatch = mapped.match(/\btext-[\w-]+/g);
2180
+ if (textColorMatch && textColorMatch.length > 0) {
2181
+ // Return the last text color class (in case there are multiple)
2182
+ const result = textColorMatch[textColorMatch.length - 1];
2183
+ // Debug logging
2184
+ if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'development') {
2185
+ console.log('🎨 extractTextColor - Mapped', wpColorName, '→', result, '(from:', mapped, ')');
2186
+ }
2187
+ return result;
2132
2188
  }
2189
+ // If no text color class found, try to determine from the background color
2190
+ // For theme palette colors, we can infer text color based on brightness
2191
+ // But for now, return null if no text color is in the mapped result
2133
2192
  }
2134
2193
  }
2194
+ // Fallback: return null (no text color applied, will inherit from parent)
2135
2195
  return null;
2136
2196
  }
2137
2197
  /**