@marvalt/wparser 0.1.43 → 0.1.45

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
@@ -2136,6 +2136,7 @@ function extractBackgroundColor(block, context) {
2136
2136
  */
2137
2137
  function extractTextColor(block, context) {
2138
2138
  const attrs = block.attributes || {};
2139
+ const mapper = context.colorMapper;
2139
2140
  // Try multiple possible attribute names for text color
2140
2141
  let wpColorName = attrs['textColor'] ||
2141
2142
  attrs['text'] ||
@@ -2171,17 +2172,23 @@ function extractTextColor(block, context) {
2171
2172
  }
2172
2173
  }
2173
2174
  if (!wpColorName || typeof wpColorName !== 'string') {
2174
- return null;
2175
+ wpColorName = null;
2175
2176
  }
2176
2177
  // Use colorMapper from context if available
2177
2178
  // Note: colorMapper returns combined classes like "bg-[#FBFAF3] text-gray-900"
2178
2179
  // We need to extract just the text color part
2179
- if (context.colorMapper) {
2180
- const mapped = context.colorMapper(wpColorName);
2180
+ if (mapper && wpColorName) {
2181
+ const mapped = mapper(wpColorName);
2181
2182
  if (mapped) {
2182
- // Extract text color classes (e.g., "text-white", "text-gray-900")
2183
- // Match text- classes but not text- in the middle of other classes
2184
- const textColorMatch = mapped.match(/\btext-[\w-]+/g);
2183
+ // If the mapper provided a background arbitrary value, derive a text color class directly from it
2184
+ const bgArbitrary = mapped.match(/\bbg-\[([^\]]+)\]/);
2185
+ if (bgArbitrary && bgArbitrary[1]) {
2186
+ const derivedText = `text-[${bgArbitrary[1]}]`;
2187
+ return derivedText;
2188
+ }
2189
+ // Extract text color classes (e.g., "text-white", "text-gray-900", "text-[#123456]")
2190
+ // Match text- classes including arbitrary values in brackets
2191
+ const textColorMatch = mapped.match(/\btext-(\[[^\]]+\]|[\w-]+)/g);
2185
2192
  if (textColorMatch && textColorMatch.length > 0) {
2186
2193
  // Return the last text color class (in case there are multiple)
2187
2194
  const result = textColorMatch[textColorMatch.length - 1];
@@ -2199,6 +2206,34 @@ function extractTextColor(block, context) {
2199
2206
  // Fallback: return null (no text color applied, will inherit from parent)
2200
2207
  return null;
2201
2208
  }
2209
+ /**
2210
+ * Infer a readable text color class based on a background class string.
2211
+ * Supports bg-[#hex] and bg-[rgb(...)].
2212
+ */
2213
+ function inferTextColorFromBackground(backgroundClass) {
2214
+ if (!backgroundClass)
2215
+ return null;
2216
+ // Try to extract hex color from arbitrary value class bg-[#xxxxxx]
2217
+ const hexMatch = backgroundClass.match(/bg-\[#?([0-9a-fA-F]{6})\]/);
2218
+ if (hexMatch) {
2219
+ const hex = hexMatch[1];
2220
+ const r = parseInt(hex.slice(0, 2), 16);
2221
+ const g = parseInt(hex.slice(2, 4), 16);
2222
+ const b = parseInt(hex.slice(4, 6), 16);
2223
+ const brightness = (r * 299 + g * 587 + b * 114) / 1000;
2224
+ return brightness < 140 ? 'text-white' : 'text-gray-900';
2225
+ }
2226
+ // Try rgb() form
2227
+ const rgbMatch = backgroundClass.match(/bg-\[rgb\((\d+),\s*(\d+),\s*(\d+)\)\]/i);
2228
+ if (rgbMatch) {
2229
+ const r = parseInt(rgbMatch[1], 10);
2230
+ const g = parseInt(rgbMatch[2], 10);
2231
+ const b = parseInt(rgbMatch[3], 10);
2232
+ const brightness = (r * 299 + g * 587 + b * 114) / 1000;
2233
+ return brightness < 140 ? 'text-white' : 'text-gray-900';
2234
+ }
2235
+ return null;
2236
+ }
2202
2237
  /**
2203
2238
  * Extract spacer height from block attributes or innerHTML
2204
2239
  * Returns height in pixels, or null if not found
@@ -2497,6 +2532,8 @@ const Group = ({ block, children, context }) => {
2497
2532
  const layout = attrs['layout'];
2498
2533
  // Extract background color using color mapper
2499
2534
  const backgroundColor = extractBackgroundColor(block, context);
2535
+ // Extract text color (apply on the wrapper so children inherit)
2536
+ const textColor = extractTextColor(block, context) || inferTextColorFromBackground(backgroundColor);
2500
2537
  // Determine if this is a section-level group (has alignment) or content-level
2501
2538
  const isSection = align === 'full' || align === 'wide';
2502
2539
  const containerClass = getContainerClasses(align, layout);
@@ -2510,8 +2547,8 @@ const Group = ({ block, children, context }) => {
2510
2547
  ? 'container'
2511
2548
  : containerClass;
2512
2549
  // Build className with background color if present
2513
- const className = buildClassName(finalContainerClass, spacingClass, backgroundColor // This will be null if no mapping, which is fine
2514
- );
2550
+ const className = buildClassName(finalContainerClass, spacingClass, backgroundColor, // This will be null if no mapping, which is fine
2551
+ textColor);
2515
2552
  return (jsxRuntimeExports.jsx("div", { className: className, children: children }));
2516
2553
  };
2517
2554
  const Columns = ({ block, children }) => {
@@ -2525,9 +2562,10 @@ const Column = ({ block, children, context }) => {
2525
2562
  const width = attrs['width'];
2526
2563
  // Extract background color using color mapper
2527
2564
  const backgroundColor = extractBackgroundColor(block, context);
2565
+ const textColor = extractTextColor(block, context) || inferTextColorFromBackground(backgroundColor);
2528
2566
  // Handle column width (e.g., "50%" becomes flex-basis)
2529
2567
  const style = width ? { flexBasis: width } : undefined;
2530
- return (jsxRuntimeExports.jsx("div", { className: buildClassName('space-y-4 p-6 rounded-lg', backgroundColor), style: style, children: children }));
2568
+ return (jsxRuntimeExports.jsx("div", { className: buildClassName('space-y-4 p-6 rounded-lg', backgroundColor, textColor), style: style, children: children }));
2531
2569
  };
2532
2570
  const Separator = () => jsxRuntimeExports.jsx("hr", { className: "border-gray-200 my-8" });
2533
2571
  const ButtonBlock = ({ block, context }) => {
@@ -3087,6 +3125,7 @@ exports.getImageAttributes = getImageAttributes;
3087
3125
  exports.getImageUrl = getImageUrl;
3088
3126
  exports.getSectionSpacingClasses = getSectionSpacingClasses;
3089
3127
  exports.getTextAlignClasses = getTextAlignClasses;
3128
+ exports.inferTextColorFromBackground = inferTextColorFromBackground;
3090
3129
  exports.isCloudflareImageUrl = isCloudflareImageUrl;
3091
3130
  exports.isValidCloudflareUrl = isValidCloudflareUrl;
3092
3131
  exports.matchesPattern = matchesPattern;