Improve Hook Alterations and Cache Metadata Handling in Markdownify Module

Created on 31 May 2025, about 2 months ago

Problem/Motivation

The Markdownify module's current implementation has several limitations:

  1. There is no dedicated hook to alter the render array of entities before converting them to HTML for Markdown transformation.
  2. Cacheable metadata (BubbleableMetadata) is not optimally handled, leading to potential cache inconsistency issues.
  3. The pipeline for altering rendered HTML lacks flexibility and consistency for extensibility by other modules.

These issues make it challenging for developers to customize the output or ensure proper cache handling for transformed entities.

Steps to reproduce

  1. Attempt to alter the render array of an entity before rendering it to HTML for Markdown transformation using existing hooks.
  2. Inspect cache tags and cache contexts for entities after transformation.
  3. Note the lack of dedicated hooks for modifying the render array and insufficient cache metadata handling.

Proposed resolution

  1. Introduce hook_markdownify_entity_build_alter: A new hook that allows modules to modify the render array of an entity before rendering it to HTML.

  2. Enhance hook_markdownify_entity_html_alter: Add support for BubbleableMetadata to allow for better cache handling during HTML alterations.

  3. Service Updates (MarkdownifyEntityRenderer):

    • Update the toHtml method to capture and merge cacheable metadata during rendering.
    • Add the new hook_markdownify_entity_build_alter for pre-HTML alterations.
    • Ensure renderEntity applies consistent handling for both render arrays and HTML strings, integrating cache metadata.

Remaining tasks

  • [ ] Verify that cache metadata is properly captured and merged.
  • [ ] Document the new hook and update the examples in the markdownify.api.php file.
  • [ ] Review and optimize the integration of BubbleableMetadata in the service class.

User interface changes

None.

API changes

  • Added hook_markdownify_entity_build_alter.
  • Updated hook_markdownify_entity_html_alter to include BubbleableMetadata.
  • Modified the MarkdownifyEntityRenderer service methods (toHtml and renderEntity) to handle cache metadata.

Data model changes

None.

✨ Feature request
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡¨πŸ‡΄Colombia i'mbatman

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024