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