@marvalt/wparser 0.1.67 → 0.1.69

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
@@ -1602,14 +1602,23 @@ function getBlockTextContent(block) {
1602
1602
  */
1603
1603
  function getImageUrl(block) {
1604
1604
  const attrs = block.attributes || {};
1605
- // Check various possible URL attributes
1606
- let url = attrs['url'] ||
1607
- attrs['src'] ||
1608
- attrs['imageUrl'] ||
1609
- attrs['mediaUrl'] ||
1610
- attrs['backgroundImage'];
1611
- if (typeof url === 'string' && url.trim()) {
1612
- return url.trim();
1605
+ // Check various possible URL attributes - prioritize Cloudflare URLs
1606
+ const possibleUrls = [
1607
+ attrs['url'],
1608
+ attrs['src'],
1609
+ attrs['imageUrl'],
1610
+ attrs['mediaUrl'],
1611
+ attrs['backgroundImage']
1612
+ ].filter((url) => typeof url === 'string' && url.trim() !== '');
1613
+ // If any URL is a Cloudflare URL, return it
1614
+ for (const url of possibleUrls) {
1615
+ if (isValidCloudflareUrl(url)) {
1616
+ return url.trim();
1617
+ }
1618
+ }
1619
+ // If no Cloudflare URL found, return first available URL
1620
+ if (possibleUrls.length > 0) {
1621
+ return possibleUrls[0].trim();
1613
1622
  }
1614
1623
  // Try to extract from innerHTML if it's an img tag
1615
1624
  if (block.innerHTML) {
@@ -1628,10 +1637,12 @@ function getImageUrl(block) {
1628
1637
  }
1629
1638
  /**
1630
1639
  * Extract image attributes (alt, width, height, alignment) from block
1640
+ * Prioritizes Cloudflare URLs over WordPress URLs
1631
1641
  */
1632
1642
  function getImageAttributes(block) {
1633
1643
  const attrs = block.attributes || {};
1634
- const url = getImageUrl(block);
1644
+ // Use extractImageUrlWithFallback to prioritize Cloudflare URLs
1645
+ const url = extractImageUrlWithFallback(block);
1635
1646
  // Extract width - can be number or string like "640px"
1636
1647
  let width;
1637
1648
  const widthAttr = attrs['width'];
@@ -1711,24 +1722,46 @@ function extractImageUrlWithFallback(block) {
1711
1722
  const attrs = block.attributes || {};
1712
1723
  // Check for cloudflareUrl first (from WordPress plugin)
1713
1724
  const cloudflareUrl = attrs['cloudflareUrl'];
1714
- if (cloudflareUrl && isValidCloudflareUrl(cloudflareUrl)) {
1715
- return cloudflareUrl;
1725
+ if (cloudflareUrl) {
1726
+ // If cloudflareUrl exists, validate it
1727
+ if (isValidCloudflareUrl(cloudflareUrl)) {
1728
+ return cloudflareUrl;
1729
+ }
1730
+ // If cloudflareUrl exists but is invalid, log for debugging (in development)
1731
+ if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'development') {
1732
+ console.warn('extractImageUrlWithFallback: cloudflareUrl exists but is invalid:', cloudflareUrl);
1733
+ }
1716
1734
  }
1717
1735
  // Try to extract from innerHTML (should be converted by plugin)
1718
1736
  if (block.innerHTML) {
1719
1737
  // Extract img src from innerHTML
1720
1738
  const imgMatch = block.innerHTML.match(/<img[^>]+src=["']([^"']+)["']/i);
1721
1739
  if (imgMatch && imgMatch[1]) {
1722
- return imgMatch[1];
1740
+ const innerHtmlUrl = imgMatch[1];
1741
+ // If innerHTML has a Cloudflare URL, use it (plugin converted it)
1742
+ if (isValidCloudflareUrl(innerHtmlUrl)) {
1743
+ return innerHtmlUrl;
1744
+ }
1745
+ // If innerHTML has WordPress URL, continue to check regular attributes
1723
1746
  }
1724
1747
  // Try background-image in style attribute
1725
1748
  const bgMatch = block.innerHTML.match(/background-image:\s*url\(["']?([^"')]+)["']?\)/i);
1726
1749
  if (bgMatch && bgMatch[1]) {
1727
- return bgMatch[1];
1750
+ const bgUrl = bgMatch[1];
1751
+ // If background-image has a Cloudflare URL, use it
1752
+ if (isValidCloudflareUrl(bgUrl)) {
1753
+ return bgUrl;
1754
+ }
1728
1755
  }
1729
1756
  }
1730
1757
  // Fall back to regular URL attributes
1731
- return getImageUrl(block);
1758
+ const regularUrl = getImageUrl(block);
1759
+ // If regular URL is a Cloudflare URL, use it
1760
+ if (regularUrl && isValidCloudflareUrl(regularUrl)) {
1761
+ return regularUrl;
1762
+ }
1763
+ // Last resort: return regular URL (WordPress URL as fallback)
1764
+ return regularUrl;
1732
1765
  }
1733
1766
 
1734
1767
  /**