@marvalt/wparser 0.1.11 → 0.1.13
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 +35 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +12 -4
- package/dist/index.esm.js +35 -22
- package/dist/index.esm.js.map +1 -1
- package/dist/react/WPContent.d.ts +2 -1
- package/dist/react/WPContent.d.ts.map +1 -1
- package/dist/registry/defaultRegistry.d.ts.map +1 -1
- package/dist/renderer/renderNodes.d.ts +2 -2
- package/dist/renderer/renderNodes.d.ts.map +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/blockExtractors.d.ts +9 -3
- package/dist/utils/blockExtractors.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1382,15 +1382,15 @@ if (process.env.NODE_ENV === 'production') {
|
|
|
1382
1382
|
|
|
1383
1383
|
var jsxRuntimeExports = jsxRuntime.exports;
|
|
1384
1384
|
|
|
1385
|
-
function renderNodes(blocks, registry, options) {
|
|
1386
|
-
return blocks.map((block, index) => renderBlock(block, registry, `b-${index}`, options));
|
|
1385
|
+
function renderNodes(blocks, registry, options, page) {
|
|
1386
|
+
return blocks.map((block, index) => renderBlock(block, registry, `b-${index}`, options, page));
|
|
1387
1387
|
}
|
|
1388
|
-
function renderBlock(block, registry, key, options) {
|
|
1388
|
+
function renderBlock(block, registry, key, options, page) {
|
|
1389
1389
|
const Renderer = registry.renderers[block.name] || registry.fallback;
|
|
1390
1390
|
const children = block.innerBlocks && block.innerBlocks.length
|
|
1391
|
-
? block.innerBlocks.map((child, i) => renderBlock(child, registry, `${key}-${i}`, options))
|
|
1391
|
+
? block.innerBlocks.map((child, i) => renderBlock(child, registry, `${key}-${i}`, options, page))
|
|
1392
1392
|
: undefined;
|
|
1393
|
-
const node = Renderer({ block, children, context: { registry } });
|
|
1393
|
+
const node = Renderer({ block, children, context: { registry, page } });
|
|
1394
1394
|
if (options?.debugWrappers) {
|
|
1395
1395
|
return (jsxRuntimeExports.jsx("div", { "data-block": block.name, className: "wp-block", children: node }, key));
|
|
1396
1396
|
}
|
|
@@ -1734,20 +1734,29 @@ const Paragraph = ({ block, context }) => {
|
|
|
1734
1734
|
const parts = renderTextWithShortcodes(content, context.registry);
|
|
1735
1735
|
// Check if any part is a block-level element (section, div, etc.)
|
|
1736
1736
|
// If so, render without wrapping in <p> to avoid DOM nesting violations
|
|
1737
|
-
const
|
|
1738
|
-
if (React.isValidElement(
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1737
|
+
const isBlockLevelElement = (element) => {
|
|
1738
|
+
if (!React.isValidElement(element)) {
|
|
1739
|
+
return false;
|
|
1740
|
+
}
|
|
1741
|
+
const type = element.type;
|
|
1742
|
+
// Check for block-level HTML elements
|
|
1743
|
+
if (typeof type === 'string' && ['section', 'div', 'article', 'header', 'footer', 'aside', 'nav'].includes(type)) {
|
|
1744
|
+
return true;
|
|
1745
|
+
}
|
|
1746
|
+
// Check if it's React.Fragment - recursively check its children
|
|
1747
|
+
if (type === React.Fragment) {
|
|
1748
|
+
const fragmentProps = element.props;
|
|
1749
|
+
const children = React.Children.toArray(fragmentProps.children);
|
|
1750
|
+
return children.some((child) => isBlockLevelElement(child));
|
|
1751
|
+
}
|
|
1752
|
+
// Check if it's a React component (likely block-level)
|
|
1753
|
+
// Most custom components render block-level content
|
|
1754
|
+
if (typeof type === 'function' || (typeof type === 'object' && type !== null && type !== React.Fragment)) {
|
|
1755
|
+
return true;
|
|
1748
1756
|
}
|
|
1749
1757
|
return false;
|
|
1750
|
-
}
|
|
1758
|
+
};
|
|
1759
|
+
const hasBlockLevelContent = React.Children.toArray(parts).some((part) => isBlockLevelElement(part));
|
|
1751
1760
|
if (hasBlockLevelContent) {
|
|
1752
1761
|
// Render block-level content without <p> wrapper
|
|
1753
1762
|
return jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: parts });
|
|
@@ -2085,7 +2094,7 @@ function createEnhancedRegistry(mappings = [], baseRegistry) {
|
|
|
2085
2094
|
};
|
|
2086
2095
|
}
|
|
2087
2096
|
|
|
2088
|
-
const WPContent = ({ blocks, registry, className }) => {
|
|
2097
|
+
const WPContent = ({ blocks, registry, className, page }) => {
|
|
2089
2098
|
if (!Array.isArray(blocks)) {
|
|
2090
2099
|
if (process.env.NODE_ENV !== 'production') {
|
|
2091
2100
|
// eslint-disable-next-line no-console
|
|
@@ -2097,7 +2106,7 @@ const WPContent = ({ blocks, registry, className }) => {
|
|
|
2097
2106
|
throw new Error('WPContent: registry is required');
|
|
2098
2107
|
}
|
|
2099
2108
|
const ast = parseGutenbergBlocks(blocks);
|
|
2100
|
-
return (jsxRuntimeExports.jsx("div", { className: className, children: renderNodes(ast, registry) }));
|
|
2109
|
+
return (jsxRuntimeExports.jsx("div", { className: className, children: renderNodes(ast, registry, undefined, page) }));
|
|
2101
2110
|
};
|
|
2102
2111
|
|
|
2103
2112
|
/**
|
|
@@ -2118,7 +2127,7 @@ const WPPage = ({ page, registry, className }) => {
|
|
|
2118
2127
|
}
|
|
2119
2128
|
const hasHeroShortcode = React.useMemo(() => detectHeroShortcode(page.blocks), [page.blocks]);
|
|
2120
2129
|
const featured = getFeaturedImage(page);
|
|
2121
|
-
return (jsxRuntimeExports.jsxs("article", { className: className, children: [!hasHeroShortcode && featured && (jsxRuntimeExports.jsx(HeroFromFeatured, { featured: featured, title: page.title?.rendered })), jsxRuntimeExports.jsx(WPContent, { blocks: page.blocks, registry: registry })] }));
|
|
2130
|
+
return (jsxRuntimeExports.jsxs("article", { className: className, children: [!hasHeroShortcode && featured && (jsxRuntimeExports.jsx(HeroFromFeatured, { featured: featured, title: page.title?.rendered })), jsxRuntimeExports.jsx(WPContent, { blocks: page.blocks, registry: registry, page: page })] }));
|
|
2122
2131
|
};
|
|
2123
2132
|
function detectHeroShortcode(blocks) {
|
|
2124
2133
|
for (const block of blocks) {
|
|
@@ -2167,10 +2176,14 @@ class WPErrorBoundary extends React.Component {
|
|
|
2167
2176
|
|
|
2168
2177
|
/**
|
|
2169
2178
|
* Extract background image URL from a block
|
|
2170
|
-
* Checks various possible sources: url, backgroundImage, innerHTML
|
|
2179
|
+
* Checks various possible sources: url, backgroundImage, innerHTML, featured image
|
|
2171
2180
|
*/
|
|
2172
|
-
function extractBackgroundImage(block) {
|
|
2181
|
+
function extractBackgroundImage(block, page) {
|
|
2173
2182
|
const attrs = block.attributes || {};
|
|
2183
|
+
// Check if block uses featured image
|
|
2184
|
+
if (attrs['useFeaturedImage'] === true && page?._embedded?.['wp:featuredmedia']?.[0]?.source_url) {
|
|
2185
|
+
return page._embedded['wp:featuredmedia'][0].source_url;
|
|
2186
|
+
}
|
|
2174
2187
|
// Try various attribute keys
|
|
2175
2188
|
let url = attrs['url'] ||
|
|
2176
2189
|
attrs['backgroundImage'] ||
|