@marvalt/wparser 0.1.5 → 0.1.7

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;
@@ -136,6 +206,7 @@ declare function getBlockTextContent(block: WordPressBlock): string;
136
206
  /**
137
207
  * Extract image URL from block attributes
138
208
  * Checks for Cloudflare URLs first, then falls back to regular URLs
209
+ * Also extracts from innerHTML if needed
139
210
  */
140
211
  declare function getImageUrl(block: WordPressBlock): string | null;
141
212
  /**
@@ -201,5 +272,141 @@ declare const isCloudflareImageUrl: (url: string | undefined | null) => boolean;
201
272
  */
202
273
  declare const getCloudflareVariantUrl: (url: string, options: CloudflareVariantOptions) => string;
203
274
 
204
- export { WPContent, WPErrorBoundary, WPPage, buildClassName, createDefaultRegistry, extractTextFromHTML, findShortcodes, getAlignmentClasses, getBlockTextContent, getCloudflareVariantUrl, getContainerClasses, getContentSpacingClasses, getFontSizeClasses, getImageAttributes, getImageUrl, getSectionSpacingClasses, getTextAlignClasses, isCloudflareImageUrl, parseGutenbergBlocks, parseShortcodeAttrs, renderNodes, renderTextWithShortcodes };
205
- 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
+ interface ImageConversionOptions {
361
+ /** Convert to Cloudflare variant if URL is already Cloudflare */
362
+ convertToCloudflare?: boolean;
363
+ /** Default width for Cloudflare variant */
364
+ defaultWidth?: number;
365
+ /** Default height for Cloudflare variant */
366
+ defaultHeight?: number;
367
+ /** Force conversion even if URL is not Cloudflare (not recommended) */
368
+ forceCloudflare?: boolean;
369
+ }
370
+ /**
371
+ * Convert image URL with optional Cloudflare variant transformation
372
+ *
373
+ * @param url - Image URL (WordPress or Cloudflare)
374
+ * @param options - Conversion options
375
+ * @returns Converted URL or original if conversion not applicable
376
+ */
377
+ declare function convertImageUrl(url: string | null | undefined, options?: ImageConversionOptions): string | null;
378
+ /**
379
+ * Batch convert multiple image URLs
380
+ */
381
+ declare function convertImageUrls(urls: (string | null | undefined)[], options?: ImageConversionOptions): (string | null)[];
382
+
383
+ interface SectionWrapperProps {
384
+ children: React$1.ReactNode;
385
+ /** Background color variant */
386
+ background?: 'light' | 'dark' | 'transparent';
387
+ /** Vertical spacing between sections */
388
+ spacing?: 'none' | 'small' | 'medium' | 'large';
389
+ /** Container width */
390
+ container?: 'full' | 'wide' | 'contained';
391
+ /** Additional CSS classes */
392
+ className?: string;
393
+ /** Optional block reference for extracting additional props */
394
+ block?: WordPressBlock;
395
+ }
396
+ /**
397
+ * Generic section wrapper component for consistent spacing and layout
398
+ *
399
+ * Usage in component mappings:
400
+ * ```ts
401
+ * {
402
+ * pattern: { name: 'core/cover' },
403
+ * component: HeroSection,
404
+ * wrapper: SectionWrapper,
405
+ * extractProps: (block) => ({ ... })
406
+ * }
407
+ * ```
408
+ */
409
+ declare const SectionWrapper: React$1.FC<SectionWrapperProps>;
410
+
411
+ export { SectionWrapper, WPContent, WPErrorBoundary, WPPage, buildClassName, convertImageToCloudflareVariant, convertImageUrl, convertImageUrls, createDefaultRegistry, createEnhancedRegistry, extractAlignment, extractBackgroundImage, extractContent, extractDimRatio, extractFontSize, extractHeadingLevel, extractImageAttributes, extractImageUrl, extractMediaPosition, extractMinHeight, extractOverlayColor, extractTextAlign, extractTextFromHTML, extractTitle, extractVerticalAlignment, findMatchingMapping, findShortcodes, getAlignmentClasses, getBlockTextContent, getCloudflareVariantUrl, getContainerClasses, getContentSpacingClasses, getFontSizeClasses, getImageAttributes, getImageUrl, getSectionSpacingClasses, getTextAlignClasses, isCloudflareImageUrl, matchesPattern, parseGutenbergBlocks, parseShortcodeAttrs, renderNodes, renderTextWithShortcodes };
412
+ 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,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC"}