Skip to content

Commit bb901c9

Browse files
benbalterCopilot
andcommitted
Fall back to raw HTML when HTML-to-Markdown conversion fails
Catches InvalidArgumentException from league/html-to-markdown's HtmlConverter::convert() in convert_content() and falls back to the post's raw HTML, so a single unparseable post no longer aborts the entire export with 'Jekyll Export failed: Invalid HTML was provided'. Bumps version to 4.0.3. Fixes #400 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8b78549 commit bb901c9

4 files changed

Lines changed: 24 additions & 4 deletions

File tree

docs/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Changelog
22

3+
### 4.0.3
4+
5+
* Catch `InvalidArgumentException` from `league/html-to-markdown` in `convert_content()` and fall back to the post's raw HTML for that single post instead of aborting the entire export with "Jekyll Export failed: Invalid HTML was provided" ([#400](https://github.com/benbalter/wordpress-static-site-exporter/issues/400))
6+
37
### 4.0.2
48

59
* Add shutdown handler to surface fatal errors (memory exhaustion, max execution time) during export with actionable error messages instead of a generic WordPress critical error page

docs/header.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Tags: jekyll, github, github pages, yaml, export, markdown
44
Requires at least: 6.4
55
Tested up to: 6.9
66
Requires PHP: 8.2
7-
Stable tag: 4.0.2
7+
Stable tag: 4.0.3
88
License: GPLv3 or later
99
License URI: http://www.gnu.org/licenses/gpl-3.0.html
1010
GitHub Plugin URI: benbalter/wordpress-to-jekyll-exporter

jekyll-exporter.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Plugin Name: Static Site Exporter
1313
* Plugin URI: https://github.com/benbalter/wordpress-to-jekyll-exporter/
1414
* Description: One-click plugin that converts all posts, pages, taxonomies, metadata, and settings to Markdown and YAML for Jekyll, Hugo, or other static site generators.
15-
* Version: 4.0.2
15+
* Version: 4.0.3
1616
* Author: Ben Balter
1717
* Author URI: https://ben.balter.com
1818
* Text Domain: jekyll-exporter
@@ -377,7 +377,19 @@ public function convert_content( $post ) {
377377
$converter->getEnvironment()->addConverter( new ColspanTableConverter() );
378378
}
379379

380-
$markdown = $converter->convert( $content );
380+
try {
381+
$markdown = $converter->convert( $content );
382+
} catch ( \InvalidArgumentException $e ) {
383+
// Converter rejected the HTML (e.g., empty or unparseable content).
384+
// Fall back to the raw HTML rather than aborting the entire export.
385+
// See https://github.com/benbalter/wordpress-static-site-exporter/issues/400.
386+
$post_id = isset( $post->ID ) ? (int) $post->ID : 0;
387+
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
388+
error_log( sprintf( '[jekyll-export] HTML-to-Markdown conversion failed for post %d: %s. Falling back to raw HTML.', $post_id, $e->getMessage() ) );
389+
$content = apply_filters( 'jekyll_export_html', $content );
390+
$content = apply_filters( 'jekyll_export_content', $content );
391+
return $content;
392+
}
381393

382394
if ( strpos( $markdown, '[]: ' ) !== false ) {
383395
// faulty links; return plain HTML.

readme.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Tags: jekyll, github, github pages, yaml, export, markdown
44
Requires at least: 6.4
55
Tested up to: 6.9
66
Requires PHP: 8.2
7-
Stable tag: 4.0.2
7+
Stable tag: 4.0.3
88
License: GPLv3 or later
99
License URI: http://www.gnu.org/licenses/gpl-3.0.html
1010
GitHub Plugin URI: benbalter/wordpress-to-jekyll-exporter
@@ -272,6 +272,10 @@ The custom post type will be exported as a Jekyll collection. You'll need to ini
272272

273273
== Changelog ==
274274

275+
= 4.0.3 =
276+
277+
* Catch `InvalidArgumentException` from `league/html-to-markdown` in `convert_content()` and fall back to the post's raw HTML for that single post instead of aborting the entire export with "Jekyll Export failed: Invalid HTML was provided" ([#400](https://github.com/benbalter/wordpress-static-site-exporter/issues/400))
278+
275279
= 4.0.2 =
276280

277281
* Add shutdown handler to surface fatal errors (memory exhaustion, max execution time) during export with actionable error messages instead of a generic WordPress critical error page

0 commit comments

Comments
 (0)