@marvalt/wparser 0.1.15 → 0.1.17

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
@@ -1595,6 +1595,57 @@ function getImageAttributes(block) {
1595
1595
  height: attrs['height'] ? Number(attrs['height']) : undefined,
1596
1596
  };
1597
1597
  }
1598
+ /**
1599
+ * Validate if a Cloudflare URL is complete (has image ID)
1600
+ * A complete Cloudflare URL should have format: https://imagedelivery.net/{account}/{image-id}/
1601
+ */
1602
+ function isValidCloudflareUrl(url) {
1603
+ if (!url)
1604
+ return false;
1605
+ // Check if it's a Cloudflare URL
1606
+ if (!url.includes('imagedelivery.net'))
1607
+ return false;
1608
+ // A complete URL should have at least 3 path segments: /account/image-id/
1609
+ // Incomplete URLs might end with just /account/ or /account
1610
+ const urlObj = new URL(url);
1611
+ const pathSegments = urlObj.pathname.split('/').filter(Boolean);
1612
+ // Should have at least account hash and image ID (2 segments minimum)
1613
+ // But we also accept URLs that end with / (which means they might be complete)
1614
+ // The issue is URLs like: https://imagedelivery.net/ZFArYcvsK9lQ3btUK-x2rA/
1615
+ // This is incomplete - it's missing the image ID
1616
+ return pathSegments.length >= 2;
1617
+ }
1618
+ /**
1619
+ * Extract image URL from block with priority:
1620
+ * 1. Valid cloudflareUrl from attributes
1621
+ * 2. Extract from innerHTML (which should be converted by plugin)
1622
+ * 3. Regular URL attributes
1623
+ *
1624
+ * This handles cases where cloudflareUrl might be incomplete
1625
+ */
1626
+ function extractImageUrlWithFallback(block) {
1627
+ const attrs = block.attributes || {};
1628
+ // Check for cloudflareUrl first (from WordPress plugin)
1629
+ const cloudflareUrl = attrs['cloudflareUrl'];
1630
+ if (cloudflareUrl && isValidCloudflareUrl(cloudflareUrl)) {
1631
+ return cloudflareUrl;
1632
+ }
1633
+ // Try to extract from innerHTML (should be converted by plugin)
1634
+ if (block.innerHTML) {
1635
+ // Extract img src from innerHTML
1636
+ const imgMatch = block.innerHTML.match(/<img[^>]+src=["']([^"']+)["']/i);
1637
+ if (imgMatch && imgMatch[1]) {
1638
+ return imgMatch[1];
1639
+ }
1640
+ // Try background-image in style attribute
1641
+ const bgMatch = block.innerHTML.match(/background-image:\s*url\(["']?([^"')]+)["']?\)/i);
1642
+ if (bgMatch && bgMatch[1]) {
1643
+ return bgMatch[1];
1644
+ }
1645
+ }
1646
+ // Fall back to regular URL attributes
1647
+ return getImageUrl(block);
1648
+ }
1598
1649
 
1599
1650
  /**
1600
1651
  * Style mapping utilities
@@ -1839,36 +1890,26 @@ const Cover = ({ block, children }) => {
1839
1890
  const attrs = block.attributes || {};
1840
1891
  const { url, id, backgroundImage, cloudflareUrl, overlayColor, dimRatio = 0, align = 'full', minHeight, minHeightUnit = 'vh', hasParallax, } = attrs;
1841
1892
  // Get background image URL from various possible sources
1842
- // Priority: cloudflareUrl (from plugin) > url > backgroundImage > innerHTML extraction
1843
- let bgImageUrl = cloudflareUrl;
1893
+ // Use the improved extraction function that handles incomplete cloudflareUrl
1894
+ let bgImageUrl = null;
1895
+ // First, try cloudflareUrl if it's valid
1896
+ if (cloudflareUrl && isValidCloudflareUrl(cloudflareUrl)) {
1897
+ bgImageUrl = cloudflareUrl;
1898
+ }
1899
+ // If not valid or not found, try regular attributes
1844
1900
  if (!bgImageUrl) {
1845
- bgImageUrl = url || backgroundImage || (typeof backgroundImage === 'object' && backgroundImage?.url);
1901
+ bgImageUrl = url || backgroundImage || (typeof backgroundImage === 'object' && backgroundImage?.url) || null;
1846
1902
  }
1847
- // If not found in attributes, try to extract from innerHTML
1848
- if (!bgImageUrl && block.innerHTML) {
1849
- // Try to extract img src from innerHTML
1850
- const imgMatch = block.innerHTML.match(/<img[^>]+src=["']([^"']+)["']/i);
1851
- if (imgMatch && imgMatch[1]) {
1852
- bgImageUrl = imgMatch[1];
1853
- }
1854
- // Try background-image in style attribute
1855
- if (!bgImageUrl) {
1856
- const bgMatch = block.innerHTML.match(/background-image:\s*url\(["']?([^"')]+)["']?\)/i);
1857
- if (bgMatch && bgMatch[1]) {
1858
- bgImageUrl = bgMatch[1];
1859
- }
1860
- }
1903
+ // If still not found, use the fallback extraction (from innerHTML)
1904
+ if (!bgImageUrl) {
1905
+ bgImageUrl = extractImageUrlWithFallback(block);
1861
1906
  }
1862
- // Convert to Cloudflare URL variant if it's a Cloudflare image, otherwise use as-is
1907
+ // Convert to Cloudflare URL variant if it's a Cloudflare image
1863
1908
  if (bgImageUrl) {
1864
1909
  if (isCloudflareImageUrl(bgImageUrl)) {
1865
1910
  // Use full width for cover images
1866
1911
  bgImageUrl = getCloudflareVariantUrl(bgImageUrl, { width: 1920 });
1867
1912
  }
1868
- // If cloudflareUrl was provided, it's already a Cloudflare URL, just add variant
1869
- else if (cloudflareUrl) {
1870
- bgImageUrl = getCloudflareVariantUrl(cloudflareUrl, { width: 1920 });
1871
- }
1872
1913
  }
1873
1914
  // Build alignment classes
1874
1915
  const alignClass = getAlignmentClasses(align);
@@ -1895,44 +1936,42 @@ const Cover = ({ block, children }) => {
1895
1936
  };
1896
1937
  const MediaText = ({ block, children, context }) => {
1897
1938
  const attrs = block.attributes || {};
1898
- const { mediaPosition = 'left', verticalAlignment = 'center', imageFill = false, align = 'wide', } = attrs;
1939
+ const { mediaPosition = 'left', verticalAlignment = 'center', imageFill = false, align, } = attrs;
1899
1940
  // Access innerBlocks to identify media vs content
1900
1941
  const innerBlocks = block.innerBlocks || [];
1901
1942
  // Find media block (image or video)
1902
1943
  let mediaBlockIndex = innerBlocks.findIndex((b) => b.name === 'core/image' || b.name === 'core/video');
1903
- // Check for cloudflareUrl in attributes first (provided by WordPress plugin)
1904
- const cloudflareUrl = attrs['cloudflareUrl'];
1905
- // If no media block found, try to extract from innerHTML
1906
- let imageUrl = null;
1907
- if (mediaBlockIndex === -1 && block.innerHTML) {
1908
- // Extract img src from innerHTML
1909
- const imgMatch = block.innerHTML.match(/<img[^>]+src=["']([^"']+)["']/i);
1910
- if (imgMatch && imgMatch[1]) {
1911
- imageUrl = imgMatch[1];
1912
- }
1913
- }
1914
1944
  // Render children - media-text typically has media as first child, then content
1915
1945
  const childrenArray = React.Children.toArray(children);
1916
1946
  let mediaElement = mediaBlockIndex >= 0 && childrenArray[mediaBlockIndex]
1917
1947
  ? childrenArray[mediaBlockIndex]
1918
1948
  : null;
1919
- // Use cloudflareUrl from attributes if available
1920
- if (!mediaElement && cloudflareUrl) {
1921
- const finalImageUrl = getCloudflareVariantUrl(cloudflareUrl, { width: 1024 });
1922
- mediaElement = (jsxRuntimeExports.jsx("img", { src: finalImageUrl, alt: "", className: "w-full h-auto rounded-lg object-cover" }));
1923
- }
1924
- // If we extracted image URL from innerHTML, render it
1925
- else if (!mediaElement && imageUrl) {
1926
- // Convert to Cloudflare URL if applicable
1927
- const finalImageUrl = isCloudflareImageUrl(imageUrl)
1928
- ? getCloudflareVariantUrl(imageUrl, { width: 1024 })
1929
- : imageUrl;
1930
- mediaElement = (jsxRuntimeExports.jsx("img", { src: finalImageUrl, alt: "", className: "w-full h-auto rounded-lg object-cover" }));
1949
+ // If no media element from innerBlocks, try to extract image URL
1950
+ if (!mediaElement) {
1951
+ const imageUrl = extractImageUrlWithFallback(block);
1952
+ if (imageUrl) {
1953
+ // Convert to Cloudflare variant if it's a Cloudflare URL
1954
+ const finalImageUrl = isCloudflareImageUrl(imageUrl)
1955
+ ? getCloudflareVariantUrl(imageUrl, { width: 1024 })
1956
+ : imageUrl;
1957
+ mediaElement = (jsxRuntimeExports.jsx("img", { src: finalImageUrl, alt: "", className: "w-full h-auto rounded-lg object-cover", loading: "lazy" }));
1958
+ }
1931
1959
  }
1932
1960
  // Content is all other children
1933
1961
  const contentElements = childrenArray.filter((_, index) => index !== mediaBlockIndex);
1934
- // Build alignment classes
1935
- const alignClass = getAlignmentClasses(align) || 'max-w-7xl mx-auto';
1962
+ // Build alignment classes - ensure proper container width
1963
+ // For 'wide', use max-w-7xl; for 'full', use w-full; default to contained
1964
+ let alignClass;
1965
+ if (align === 'full') {
1966
+ alignClass = 'w-full';
1967
+ }
1968
+ else if (align === 'wide') {
1969
+ alignClass = 'max-w-7xl mx-auto';
1970
+ }
1971
+ else {
1972
+ // Default to contained width (not full width)
1973
+ alignClass = 'container mx-auto';
1974
+ }
1936
1975
  // Vertical alignment classes
1937
1976
  const verticalAlignClass = verticalAlignment === 'top' ? 'items-start' :
1938
1977
  verticalAlignment === 'bottom' ? 'items-end' :
@@ -2215,7 +2254,7 @@ class WPErrorBoundary extends React.Component {
2215
2254
 
2216
2255
  /**
2217
2256
  * Extract background image URL from a block
2218
- * Checks various possible sources: url, backgroundImage, innerHTML, featured image
2257
+ * Checks various possible sources: cloudflareUrl, url, backgroundImage, innerHTML, featured image
2219
2258
  */
2220
2259
  function extractBackgroundImage(block, page) {
2221
2260
  const attrs = block.attributes || {};
@@ -2223,34 +2262,17 @@ function extractBackgroundImage(block, page) {
2223
2262
  if (attrs['useFeaturedImage'] === true && page?._embedded?.['wp:featuredmedia']?.[0]?.source_url) {
2224
2263
  return page._embedded['wp:featuredmedia'][0].source_url;
2225
2264
  }
2226
- // Try various attribute keys
2227
- let url = attrs['url'] ||
2228
- attrs['backgroundImage'] ||
2229
- (typeof attrs['backgroundImage'] === 'object' && attrs['backgroundImage']?.url);
2230
- if (typeof url === 'string' && url.trim()) {
2231
- return url.trim();
2232
- }
2233
- // Try to extract from innerHTML if not found in attributes
2234
- if (block.innerHTML) {
2235
- // Try img src from innerHTML
2236
- const imgMatch = block.innerHTML.match(/<img[^>]+src=["']([^"']+)["']/i);
2237
- if (imgMatch && imgMatch[1]) {
2238
- return imgMatch[1];
2239
- }
2240
- // Try background-image in style attribute
2241
- const bgMatch = block.innerHTML.match(/background-image:\s*url\(["']?([^"')]+)["']?\)/i);
2242
- if (bgMatch && bgMatch[1]) {
2243
- return bgMatch[1];
2244
- }
2245
- }
2246
- return null;
2265
+ // Use the improved extraction function that handles incomplete cloudflareUrl
2266
+ // This will check cloudflareUrl first, then innerHTML, then regular attributes
2267
+ return extractImageUrlWithFallback(block);
2247
2268
  }
2248
2269
  /**
2249
2270
  * Extract image URL from a block
2250
2271
  * Returns Cloudflare URL if available, otherwise WordPress URL
2251
2272
  */
2252
2273
  function extractImageUrl(block) {
2253
- return getImageUrl(block);
2274
+ // Use the improved extraction function that handles incomplete cloudflareUrl
2275
+ return extractImageUrlWithFallback(block);
2254
2276
  }
2255
2277
  /**
2256
2278
  * Extract image attributes (url, alt, width, height)
@@ -2653,6 +2675,7 @@ exports.extractFontSize = extractFontSize;
2653
2675
  exports.extractHeadingLevel = extractHeadingLevel;
2654
2676
  exports.extractImageAttributes = extractImageAttributes;
2655
2677
  exports.extractImageUrl = extractImageUrl;
2678
+ exports.extractImageUrlWithFallback = extractImageUrlWithFallback;
2656
2679
  exports.extractMediaPosition = extractMediaPosition;
2657
2680
  exports.extractMinHeight = extractMinHeight;
2658
2681
  exports.extractOverlayColor = extractOverlayColor;
@@ -2677,6 +2700,7 @@ exports.getImageUrl = getImageUrl;
2677
2700
  exports.getSectionSpacingClasses = getSectionSpacingClasses;
2678
2701
  exports.getTextAlignClasses = getTextAlignClasses;
2679
2702
  exports.isCloudflareImageUrl = isCloudflareImageUrl;
2703
+ exports.isValidCloudflareUrl = isValidCloudflareUrl;
2680
2704
  exports.matchesPattern = matchesPattern;
2681
2705
  exports.parseContentPosition = parseContentPosition;
2682
2706
  exports.parseGutenbergBlocks = parseGutenbergBlocks;