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