Skip to content

Update ContentRenderer.php#18

Open
mathetos wants to merge 1 commit intoProgressPlanner:mainfrom
mathetos:stability-bugfix-no-16
Open

Update ContentRenderer.php#18
mathetos wants to merge 1 commit intoProgressPlanner:mainfrom
mathetos:stability-bugfix-no-16

Conversation

@mathetos
Copy link

Fixes #16

When the HTML-to-Markdown converter throws, the code now falls back to wp_strip_all_tags() plus html_entity_decode() instead of causing a fatal error.

Context

When a post or page contains HTML that causes the League HTML-to-Markdown converter to throw (e.g., malformed or edge-case markup), the plugin previously did not catch the exception. This resulted in a PHP fatal error and a 500 response instead of returning markdown or a safe fallback. This PR adds a try/catch around the conversion call and uses a plain-text fallback so .md URLs always return content instead of failing.

Summary

This PR can be summarized in the following changelog entry:

  • Bug fix: Gracefully handle HTML-to-Markdown conversion failures. When the League converter throws, fall back to wp_strip_all_tags() and html_entity_decode() instead of causing a fatal error. Ensures .md URLs always return output.

Relevant technical choices:

  • Try/catch around convert(): Wraps the converter call in a \Throwable catch so both Exception and Error are handled.
  • Fallback behavior: Uses wp_strip_all_tags() for the HTML → plain text fallback and html_entity_decode() to decode entities, mirroring the behavior used for code blocks elsewhere in the renderer.
  • No filter added: This PR only adds the bug fix. A separate PR or issue can add a filter for customizing the fallback if desired.

Test instructions

  1. Create or edit a post with content that can trigger a converter exception (e.g., unusual or malformed HTML, or content known to break the converter).
  2. Visit the post’s .md URL (e.g. https://yoursite.com/post-slug.md).
  3. Expected: The page returns markdown or plain text instead of a 500 or fatal error.
  4. Confirm normal posts still convert to markdown correctly.
  5. Confirm code blocks and other supported content still render as expected.

Relevant test scenarios

  • Changes should be tested with the browser console open
  • Changes should be tested on different posts/pages/taxonomies/custom post types/custom taxonomies — Use at least one post and one page to verify behavior.
  • Changes should be tested on different editors (Block/Classic/Elementor/other)
  • Changes should be tested on different browsers
  • Changes should be tested on multisite

UI changes

  • This PR changes the UI in the plugin. I have added the 'UI change' label to this PR.

Documentation

  • I have written documentation for this change.

Quality assurance

  • I have tested this code to the best of my abilities.
  • I have added unit tests to verify the code works as intended.
  • I have checked that the base branch is correctly set.

Fixes #16

Fixes ProgressPlanner#16

When the HTML-to-Markdown converter throws, the code now falls back to wp_strip_all_tags() plus html_entity_decode() instead of causing a fatal error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stability and fallbacks with uncaught exception exists in HTML

1 participant

Comments