@marvalt/wparser 0.1.6 → 0.1.8

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
@@ -47,6 +47,46 @@ interface ComponentRegistry {
47
47
  /** Fallback renderer for unknown blocks */
48
48
  fallback: BlockRenderer;
49
49
  }
50
+ /**
51
+ * Pattern matching for blocks - allows matching blocks by name and attributes
52
+ */
53
+ interface BlockPattern {
54
+ /** Block name (e.g., 'core/cover', 'core/media-text') */
55
+ name: string;
56
+ /** Optional attribute matching - all specified attributes must match */
57
+ attributes?: Record<string, any>;
58
+ /** Optional nested block patterns for innerBlocks */
59
+ innerBlocks?: BlockPattern[];
60
+ }
61
+ /**
62
+ * Component mapping configuration for enhanced registry
63
+ * Maps block patterns to React components with prop extraction
64
+ */
65
+ interface ComponentMapping<TProps = any> {
66
+ /** Pattern to match against blocks */
67
+ pattern: BlockPattern;
68
+ /** React component to render when pattern matches */
69
+ component: React.ComponentType<TProps>;
70
+ /** Function to extract props from block data */
71
+ extractProps: (block: WordPressBlock, context: RenderContext) => TProps;
72
+ /** Optional wrapper component (e.g., for section spacing) */
73
+ wrapper?: React.ComponentType<{
74
+ children: React.ReactNode;
75
+ block?: WordPressBlock;
76
+ }>;
77
+ /** Priority for matching (higher = more specific, checked first) */
78
+ priority?: number;
79
+ }
80
+ /**
81
+ * Enhanced registry that supports pattern-based component mapping
82
+ * Extends the base ComponentRegistry with pattern matching capabilities
83
+ */
84
+ interface EnhancedRegistry extends ComponentRegistry {
85
+ /** Array of component mappings with patterns */
86
+ mappings: ComponentMapping[];
87
+ /** Find matching component mapping for a block */
88
+ matchBlock: (block: WordPressBlock) => ComponentMapping | null;
89
+ }
50
90
 
51
91
  type WPNode = WordPressBlock;
52
92
  interface ParseOptions {
@@ -60,6 +100,36 @@ declare function renderNodes(blocks: WordPressBlock[], registry: ComponentRegist
60
100
 
61
101
  declare function createDefaultRegistry(): ComponentRegistry;
62
102
 
103
+ /**
104
+ * Create an enhanced registry that supports pattern-based component mapping
105
+ *
106
+ * This combines the default registry (for fallback) with app-specific component mappings.
107
+ * When a block matches a pattern, it uses the mapped component. Otherwise, it falls back
108
+ * to the default renderer.
109
+ *
110
+ * @param mappings - Array of component mappings with patterns
111
+ * @param baseRegistry - Optional base registry (defaults to createDefaultRegistry())
112
+ * @returns Enhanced registry with pattern matching capabilities
113
+ *
114
+ * @example
115
+ * ```ts
116
+ * const mappings: ComponentMapping[] = [
117
+ * {
118
+ * pattern: { name: 'core/cover' },
119
+ * component: HomeHeroSection,
120
+ * extractProps: (block) => ({
121
+ * backgroundImage: extractBackgroundImage(block),
122
+ * title: extractTitle(block),
123
+ * }),
124
+ * wrapper: SectionWrapper,
125
+ * },
126
+ * ];
127
+ *
128
+ * const registry = createEnhancedRegistry(mappings);
129
+ * ```
130
+ */
131
+ declare function createEnhancedRegistry(mappings?: ComponentMapping[], baseRegistry?: ComponentRegistry): EnhancedRegistry;
132
+
63
133
  interface WPContentProps {
64
134
  blocks: WordPressBlock[];
65
135
  registry: ComponentRegistry;
@@ -202,5 +272,161 @@ declare const isCloudflareImageUrl: (url: string | undefined | null) => boolean;
202
272
  */
203
273
  declare const getCloudflareVariantUrl: (url: string, options: CloudflareVariantOptions) => string;
204
274
 
205
- export { WPContent, WPErrorBoundary, WPPage, buildClassName, createDefaultRegistry, extractTextFromHTML, findShortcodes, getAlignmentClasses, getBlockTextContent, getCloudflareVariantUrl, getContainerClasses, getContentSpacingClasses, getFontSizeClasses, getImageAttributes, getImageUrl, getSectionSpacingClasses, getTextAlignClasses, isCloudflareImageUrl, parseGutenbergBlocks, parseShortcodeAttrs, renderNodes, renderTextWithShortcodes };
206
- export type { AuthMode, BlockRenderer, BlockRendererProps, CloudflareVariantOptions, ComponentRegistry, ParseOptions, ParsedShortcode, RenderContext, RenderOptions, ShortcodeAttributes, ShortcodeRenderer, WPContentProps, WPNode, WPPageProps, WordPressBlock, WordPressEmbedded, WordPressFeaturedMedia, WordPressPageMinimal, WordPressTitleField };
275
+ /**
276
+ * Check if a block matches a pattern
277
+ */
278
+ declare function matchesPattern(block: WordPressBlock, pattern: BlockPattern): boolean;
279
+ /**
280
+ * Find the best matching component mapping for a block
281
+ * Returns the mapping with highest priority that matches, or null
282
+ */
283
+ declare function findMatchingMapping(block: WordPressBlock, mappings: ComponentMapping[]): ComponentMapping | null;
284
+
285
+ /**
286
+ * Extract background image URL from a block
287
+ * Checks various possible sources: url, backgroundImage, innerHTML
288
+ */
289
+ declare function extractBackgroundImage(block: WordPressBlock): string | null;
290
+ /**
291
+ * Extract image URL from a block
292
+ * Returns Cloudflare URL if available, otherwise WordPress URL
293
+ */
294
+ declare function extractImageUrl(block: WordPressBlock): string | null;
295
+ /**
296
+ * Extract image attributes (url, alt, width, height)
297
+ */
298
+ declare function extractImageAttributes(block: WordPressBlock): {
299
+ url: string | null;
300
+ alt: string;
301
+ width?: number;
302
+ height?: number;
303
+ };
304
+ /**
305
+ * Extract title/heading text from a block
306
+ */
307
+ declare function extractTitle(block: WordPressBlock): string | null;
308
+ /**
309
+ * Extract content/text from a block
310
+ * Returns React node for rendering
311
+ */
312
+ declare function extractContent(block: WordPressBlock, context: RenderContext): React$1.ReactNode;
313
+ /**
314
+ * Extract media position from media-text block
315
+ */
316
+ declare function extractMediaPosition(block: WordPressBlock): 'left' | 'right';
317
+ /**
318
+ * Extract vertical alignment from block
319
+ */
320
+ declare function extractVerticalAlignment(block: WordPressBlock): 'top' | 'center' | 'bottom';
321
+ /**
322
+ * Extract alignment (full, wide, contained) from block
323
+ */
324
+ declare function extractAlignment(block: WordPressBlock): 'full' | 'wide' | 'contained';
325
+ /**
326
+ * Extract overlay color from cover block
327
+ */
328
+ declare function extractOverlayColor(block: WordPressBlock): string | null;
329
+ /**
330
+ * Extract dim ratio (overlay opacity) from cover block
331
+ */
332
+ declare function extractDimRatio(block: WordPressBlock): number;
333
+ /**
334
+ * Extract min height from block
335
+ */
336
+ declare function extractMinHeight(block: WordPressBlock): {
337
+ value: number;
338
+ unit: string;
339
+ } | null;
340
+ /**
341
+ * Extract heading level from heading block
342
+ */
343
+ declare function extractHeadingLevel(block: WordPressBlock): number;
344
+ /**
345
+ * Extract text alignment from block
346
+ */
347
+ declare function extractTextAlign(block: WordPressBlock): 'left' | 'center' | 'right' | null;
348
+ /**
349
+ * Extract font size from block
350
+ */
351
+ declare function extractFontSize(block: WordPressBlock): string | null;
352
+ /**
353
+ * Convert image URL to Cloudflare variant if it's a Cloudflare URL
354
+ */
355
+ declare function convertImageToCloudflareVariant(url: string | null, options?: {
356
+ width?: number;
357
+ height?: number;
358
+ }): string | null;
359
+ /**
360
+ * Extract title from innerBlocks (finds first heading block)
361
+ */
362
+ declare function extractTitleFromInnerBlocks(block: WordPressBlock): string | null;
363
+ /**
364
+ * Extract subtitle/description from innerBlocks (finds first paragraph block)
365
+ */
366
+ declare function extractSubtitleFromInnerBlocks(block: WordPressBlock): string | null;
367
+ /**
368
+ * Extract buttons from innerBlocks (finds buttons block and extracts button data)
369
+ */
370
+ declare function extractButtonsFromInnerBlocks(block: WordPressBlock): Array<{
371
+ text: string;
372
+ url: string;
373
+ isExternal?: boolean;
374
+ }>;
375
+ /**
376
+ * Extract video iframe HTML from innerBlocks (finds HTML block with iframe)
377
+ */
378
+ declare function extractVideoIframeFromInnerBlocks(block: WordPressBlock): string | null;
379
+
380
+ interface ImageConversionOptions {
381
+ /** Convert to Cloudflare variant if URL is already Cloudflare */
382
+ convertToCloudflare?: boolean;
383
+ /** Default width for Cloudflare variant */
384
+ defaultWidth?: number;
385
+ /** Default height for Cloudflare variant */
386
+ defaultHeight?: number;
387
+ /** Force conversion even if URL is not Cloudflare (not recommended) */
388
+ forceCloudflare?: boolean;
389
+ }
390
+ /**
391
+ * Convert image URL with optional Cloudflare variant transformation
392
+ *
393
+ * @param url - Image URL (WordPress or Cloudflare)
394
+ * @param options - Conversion options
395
+ * @returns Converted URL or original if conversion not applicable
396
+ */
397
+ declare function convertImageUrl(url: string | null | undefined, options?: ImageConversionOptions): string | null;
398
+ /**
399
+ * Batch convert multiple image URLs
400
+ */
401
+ declare function convertImageUrls(urls: (string | null | undefined)[], options?: ImageConversionOptions): (string | null)[];
402
+
403
+ interface SectionWrapperProps {
404
+ children: React$1.ReactNode;
405
+ /** Background color variant */
406
+ background?: 'light' | 'dark' | 'transparent';
407
+ /** Vertical spacing between sections */
408
+ spacing?: 'none' | 'small' | 'medium' | 'large';
409
+ /** Container width */
410
+ container?: 'full' | 'wide' | 'contained';
411
+ /** Additional CSS classes */
412
+ className?: string;
413
+ /** Optional block reference for extracting additional props */
414
+ block?: WordPressBlock;
415
+ }
416
+ /**
417
+ * Generic section wrapper component for consistent spacing and layout
418
+ *
419
+ * Usage in component mappings:
420
+ * ```ts
421
+ * {
422
+ * pattern: { name: 'core/cover' },
423
+ * component: HeroSection,
424
+ * wrapper: SectionWrapper,
425
+ * extractProps: (block) => ({ ... })
426
+ * }
427
+ * ```
428
+ */
429
+ declare const SectionWrapper: React$1.FC<SectionWrapperProps>;
430
+
431
+ 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, extractTextFromHTML, extractTitle, extractTitleFromInnerBlocks, extractVerticalAlignment, extractVideoIframeFromInnerBlocks, findMatchingMapping, findShortcodes, getAlignmentClasses, getBlockTextContent, getCloudflareVariantUrl, getContainerClasses, getContentSpacingClasses, getFontSizeClasses, getImageAttributes, getImageUrl, getSectionSpacingClasses, getTextAlignClasses, isCloudflareImageUrl, matchesPattern, parseGutenbergBlocks, parseShortcodeAttrs, renderNodes, renderTextWithShortcodes };
432
+ export type { AuthMode, BlockPattern, BlockRenderer, BlockRendererProps, CloudflareVariantOptions, ComponentMapping, ComponentRegistry, EnhancedRegistry, ImageConversionOptions, ParseOptions, ParsedShortcode, RenderContext, RenderOptions, SectionWrapperProps, ShortcodeAttributes, ShortcodeRenderer, WPContentProps, WPNode, WPPageProps, WordPressBlock, WordPressEmbedded, WordPressFeaturedMedia, WordPressPageMinimal, WordPressTitleField };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,sBAAsB,CAAC;AACrC,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,0BAA0B,CAAC;AACzC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,sBAAsB,CAAC;AACrC,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,0BAA0B,CAAC;AACzC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,OAAO,EACL,2BAA2B,EAC3B,8BAA8B,EAC9B,6BAA6B,EAC7B,iCAAiC,GAClC,MAAM,yBAAyB,CAAC;AACjC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC"}