Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Rich Text Laravel Package

contentful/rich-text

PHP library for parsing and rendering Contentful Rich Text fields. Parses localized rich text JSON into node objects, resolves linked assets/entries via a link resolver, and renders nodes to output with a simple Renderer API. Requires PHP 7.2+ / 8.0+.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Contentful CMS Integration: The package is optimized for Laravel applications using Contentful as a headless CMS, providing a structured way to parse and render Contentful’s rich-text fields (e.g., richText field type). It aligns with Laravel’s dependency injection and service container patterns, especially when combined with Laravel’s templating engines (Blade, Twig, or Plates).
  • Node-Based Architecture: The library’s node-based parsing and rendering model (e.g., NodeInterface, NodeRendererInterface) is a clean abstraction for handling nested content structures, making it extensible for custom content types (e.g., tables, embeds, or custom blocks).
  • Locale Support: Critical for multilingual Laravel apps, as the parser enforces locale-aware rendering via parseLocalized(), avoiding bugs with embedded assets/entries in mismatched locales (a common pitfall in headless CMS setups).

Integration Feasibility

  • Composer Integration: Zero-friction setup via composer require contentful/rich-text, with autoloader compatibility for Laravel’s PSR-4 standards.
  • Laravel Ecosystem Synergy:
    • Blade/Twig/Plates: Native support for Laravel’s templating engines via TwigExtension/PlatesExtension, enabling seamless rich-text rendering in views.
    • Service Container: The package’s stateless design allows easy registration as a Laravel service provider (e.g., binding Contentful\RichText\Parser to the container).
    • Contentful SDK: Works alongside the contentful/php-sdk for fetching rich-text data from Contentful’s API.
  • Customization Hooks: The NodeRendererInterface enables deep customization (e.g., wrapping headings in Laravel-specific classes, integrating with Laravel’s HTML helpers like Str::limit()).

Technical Risk

Risk Area Mitigation Strategy
Breaking Changes Monitor ParserInterface/RendererInterface for major version bumps (e.g., 4.0.0 dropped PHP7). Use ^4.0 in composer.json to auto-update.
Locale Mismatches Enforce parseLocalized($data, $locale) in all parsers (per AGENTS.md).
Embedded Asset Links Opt-in EmbeddedImage renderer via $renderer->enableEmbeddedImageRenderer(true) to avoid unexpected behavior.
Performance Benchmark parsing/rendering of large rich-text blocks (e.g., 100+ nodes) in Laravel’s request lifecycle.
Template Escaping Use {{ $richText->render() }} in Blade/Twig with `

Key Questions

  1. Contentful Field Mapping:
    • How will rich-text fields be mapped to Laravel models (e.g., via Eloquent accessors or API responses)?
    • Example: return $this->richTextField->parseLocalized($this->richTextField->getData(), app()->getLocale());
  2. Caching Strategy:
    • Should parsed rich-text nodes be cached (e.g., via Laravel’s cache system) to avoid reprocessing identical content?
  3. Fallback Content:
    • How will unsupported nodes (e.g., custom Contentful extensions) be handled? Use CatchAll renderer or throw exceptions?
  4. Testing:
    • Are there existing Laravel feature tests for rich-text rendering (e.g., using Pest or PHPUnit)?
  5. Deployment:
    • Will the package be vendor-locked (e.g., only used in specific modules) or globally required across the app?

Integration Approach

Stack Fit

  • Laravel Core:
    • Service Provider: Register the parser/renderer as singleton bindings in AppServiceProvider:
      public function register(): void
      {
          $this->app->singleton(Contentful\RichText\Parser::class, fn() =>
              new Contentful\RichText\Parser($this->app->make(Contentful\RichText\LinkResolver::class))
          );
          $this->app->singleton(Contentful\RichText\Renderer::class, fn() =>
              (new Contentful\RichText\Renderer())->pushNodeRenderer(new CustomHeadingRenderer())
          );
      }
      
    • Facade: Create a RichText facade for concise syntax:
      use Illuminate\Support\Facades\Facade;
      class RichText extends Facade { protected static function getFacadeAccessor() => 'richText.renderer'; }
      
      Usage: RichText::render($node).
  • Templating:
    • Blade: Use the TwigExtension via a custom Blade directive or inline PHP:
      {!! RichText::render($entry->richTextField) !!}
      
    • Twig/Plates: Leverage built-in extensions for template-based rendering (e.g., rich_text_render_collection).
  • Contentful SDK:
    • Pair with contentful/php-sdk for fetching rich-text data:
      $client = new \Contentful\Client();
      $entry = $client->getEntry($entryId);
      $parsed = app(Contentful\RichText\Parser::class)->parseLocalized($entry->fields->richText, app()->getLocale());
      

Migration Path

  1. Phase 1: Proof of Concept
    • Integrate the package in a single module (e.g., blog posts) using the default renderer.
    • Test parsing/rendering of all Contentful rich-text node types (e.g., headings, lists, embeds).
  2. Phase 2: Customization
    • Implement NodeRendererInterface for custom nodes (e.g., EmbeddedVideo, CalloutBlock).
    • Example: Extend EmbeddedEntryBlock to fetch additional metadata from Contentful.
  3. Phase 3: Templating Integration
    • Replace hardcoded HTML in Blade views with RichText::render().
    • Migrate to Twig/Plates if using those engines (e.g., for microservices).
  4. Phase 4: Optimization
    • Add caching for parsed nodes (e.g., Cache::remember()).
    • Benchmark and optimize for large rich-text blocks (e.g., documentation pages).

Compatibility

Component Compatibility Notes
Laravel Versions Tested with Laravel 9+ (PHP 8.0+). Avoid PHP 7.x due to package’s PHP 8.0+ requirement.
Contentful SDK Ensure contentful/php-sdk version matches the rich-text field structure (e.g., v10+).
Templating Engines Blade: Use {{ !! }} for raw HTML output. Twig/Plates: Use provided extensions.
Custom Content Models Extend NodeRendererInterface for unsupported Contentful field types (e.g., custom blocks).

Sequencing

  1. Setup:
    • Install the package and register bindings in AppServiceProvider.
    • Configure a LinkResolver for resolving embedded assets/entries (e.g., via Contentful’s API client).
  2. Parsing:
    • Parse rich-text data in Laravel’s request lifecycle (e.g., in a FormRequest or controller).
    • Example:
      $node = app(Contentful\RichText\Parser::class)->parseLocalized(
          $entry->fields->richText,
          request()->locale
      );
      
  3. Rendering:
    • Render nodes in views (Blade/Twig/Plates) or API responses.
    • Example (Blade):
      <div class="content">
          {!! RichText::render($node) !!}
      </div>
      
  4. Customization:
    • Implement NodeRendererInterface for custom logic (e.g., analytics tracking, A/B testing).
    • Push custom renderers to the main renderer:
      app(Contentful\RichText\Renderer::class)->pushNodeRenderer(new AnalyticsHeadingRenderer());
      

Operational Impact

Maintenance

  • Dependency Updates:
    • Monitor contentful/rich-text for breaking changes (e.g., locale support in 4.0.0).
    • Use composer why-not contentful/rich-text:4.0.0 to check compatibility.
  • Custom Renderers:
    • Document custom NodeRenderer implementations in a README.md or ADR.
    • Example: Track which renderers are active in a config file (e.g., config/rich-text.php).
  • Deprecations:
    • Replace deprecated methods (e.g., Parser::parse()parseLocalized()) via static analysis tools like PHPStan.

Support

  • Debugging:
    • Use var_dump($node->toArray()) to inspect parsed nodes.
    • Enable CatchAll renderer in development to avoid crashes:
      $renderer->append
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky