From 02cd4f167ca3ee7ab60d0ccead0e371f4ba4542d Mon Sep 17 00:00:00 2001 From: Joost de Valk Date: Wed, 25 Nov 2026 20:31:14 +0100 Subject: [PATCH] Preserve cover spacing and embed links while omitting decorative images --- readme.txt | 3 ++ src/Converter/EmbedConverter.php | 56 ++++++++++++++++++++ src/Converter/ImageConverter.php | 41 +++++++++++++++ src/Converter/MarkdownConverter.php | 4 +- src/Output/ContentRenderer.php | 2 +- tests/conversion.php | 80 +++++++++++++++++++++++++++++ tests/run.php | 1 + 7 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 src/Converter/EmbedConverter.php create mode 100644 src/Converter/ImageConverter.php create mode 100644 tests/conversion.php diff --git a/readme.txt b/readme.txt index 22064dd..4074e75 100644 --- a/readme.txt +++ b/readme.txt @@ -103,6 +103,9 @@ Yes, as an opt-in. With `add_filter( 'markdown_alternate_skip_noindexed', '__ret == Changelog == = Unreleased = +* Fixed cover block images running into the first inner heading +* Omitted aria-hidden and data-URI images from Markdown +* Preserved iframe embeds as links instead of dropping their content * Added Markdown versions of category and tag archives, and of WooCommerce product category, tag and brand archives * Changed frontmatter links to terms of taxonomies that are not served to point to their HTML archive instead of a `.md` URL that returned 404 * Changed frontmatter links to terms to absolute URLs, like the links in the body diff --git a/src/Converter/EmbedConverter.php b/src/Converter/EmbedConverter.php new file mode 100644 index 0000000..4b81d9e --- /dev/null +++ b/src/Converter/EmbedConverter.php @@ -0,0 +1,56 @@ +getAttribute( 'src' ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ); + if ( ! preg_match( '~^(?:https?://|/|\.\.?/)~i', $url ) || preg_match( '/[\x00-\x1f\x7f]/', $url ) ) { + return ''; + } + + // Encode characters that would terminate a Markdown link destination. + $url = str_replace( + array( ' ', '(', ')', '<', '>', '\\' ), + array( '%20', '%28', '%29', '%3C', '%3E', '%5C' ), + $url + ); + $title = trim( (string) preg_replace( '/\s+/u', ' ', html_entity_decode( $element->getAttribute( 'title' ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ) ); + if ( '' === $title ) { + $title = $url; + } + $title = str_replace( + array( '\\', '[', ']', '*', '_', '`', '<', '>' ), + array( '\\\\', '\\[', '\\]', '\\*', '\\_', '\\`', '\\<', '\\>' ), + $title + ); + return '[' . $title . '](' . $url . ")\n\n"; + } + + /** + * Tags converted by this fallback. + * + * @return string[] Supported tags. + */ + public function getSupportedTags(): array { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid -- Library interface. + return array( 'iframe' ); + } +} diff --git a/src/Converter/ImageConverter.php b/src/Converter/ImageConverter.php new file mode 100644 index 0000000..bf52b4b --- /dev/null +++ b/src/Converter/ImageConverter.php @@ -0,0 +1,41 @@ +getAttribute( 'src' ) ), 'data:' ) ) { + return ''; + } + for ( $ancestor = $element; null !== $ancestor; $ancestor = $ancestor->getParent() ) { + if ( 'true' === strtolower( trim( $ancestor->getAttribute( 'aria-hidden' ) ) ) ) { + return ''; + } + } + + $markdown = parent::convert( $element ); + // Cover backgrounds precede a div of inner blocks without a wrapping paragraph. + if ( preg_match( '/(?:^|\s)wp-block-cover__image-background(?:\s|$)/', $element->getAttribute( 'class' ) ) ) { + $markdown .= "\n\n"; + } + return $markdown; + } +} diff --git a/src/Converter/MarkdownConverter.php b/src/Converter/MarkdownConverter.php index d10b1c6..67d5861 100644 --- a/src/Converter/MarkdownConverter.php +++ b/src/Converter/MarkdownConverter.php @@ -34,7 +34,7 @@ public function __construct() { $this->converter = new HtmlConverter([ 'header_style' => 'atx', 'strip_tags' => true, - 'remove_nodes' => 'script style iframe', + 'remove_nodes' => 'script style', 'hard_break' => false, 'list_item_style' => '-', ]); @@ -49,6 +49,8 @@ public function __construct() { // The library already ships table_pipe_escape and table_caption_side // defaults for this converter — only the registration is missing. $this->converter->getEnvironment()->addConverter( new TableConverter() ); + $this->converter->getEnvironment()->addConverter( new ImageConverter() ); + $this->converter->getEnvironment()->addConverter( new EmbedConverter() ); } /** diff --git a/src/Output/ContentRenderer.php b/src/Output/ContentRenderer.php index 550c657..2f79b16 100644 --- a/src/Output/ContentRenderer.php +++ b/src/Output/ContentRenderer.php @@ -26,7 +26,7 @@ class ContentRenderer { * * @var int */ - private const CACHE_SCHEMA = 1; + private const CACHE_SCHEMA = 2; /** * The HTML to Markdown converter. diff --git a/tests/conversion.php b/tests/conversion.php new file mode 100644 index 0000000..38a4eba --- /dev/null +++ b/tests/conversion.php @@ -0,0 +1,80 @@ +Cover

Heading

'; + md_alt_assert_same( "![Cover](https://example.com/cover.jpg)\n\n# Heading", ( new MarkdownConverter() )->convert( $html ) ); +}; + +$md_alt_tests['Visible inline images retain alt text, titles and inline spacing'] = static function () { + $html = '

Before Icon after.

'; + md_alt_assert_same( "Before ![Icon](https://example.com/icon.png \"A title\") after.\n\n![](/image.png)", ( new MarkdownConverter() )->convert( $html ) ); +}; + +$md_alt_tests['Images hidden on themselves or any ancestor are omitted without removing text'] = static function () { + $html = '

Keep text.

Visible

'; + md_alt_assert_same( "Keep text.\n\nKeep this text\n\n![Visible](/visible.png)", ( new MarkdownConverter() )->convert( $html ) ); +}; + +$md_alt_tests['Data URI images are omitted regardless of case or leading whitespace'] = static function () { + foreach ( array( 'data:image/png;base64,AAAA', ' DATA:image/svg+xml;base64,BBBB' ) as $src ) { + md_alt_assert_same( 'Text', ( new MarkdownConverter() )->convert( '

TextInline bytes

' ) ); + } +}; + +$md_alt_tests['Iframe-only embed blocks retain a titled link and their caption'] = static function () { + $html = '
A caption
'; + // Nested block wrappers may contribute additional blank lines. + $output = preg_replace( '/\n{3,}/', "\n\n", ( new MarkdownConverter() )->convert( $html ) ); + md_alt_assert_same( "[Video title](https://example.com/embed/video)\n\nA caption", $output ); +}; + +$md_alt_tests['Untitled embeds keep their URL and remain separated from surrounding text'] = static function () { + md_alt_assert_same( "Before\n\n[https://example.com/video?a=1&b=2](https://example.com/video?a=1&b=2)\n\nAfter", ( new MarkdownConverter() )->convert( '

Before

After

' ) ); + md_alt_assert_contains( '](/local/video)', ( new MarkdownConverter() )->convert( '' ) ); + md_alt_assert_same( '[//example.com/video](//example.com/video)', ( new MarkdownConverter() )->convert( '' ) ); +}; + +$md_alt_tests['Embed attributes cannot introduce executable links or raw HTML'] = static function () { + $converter = new MarkdownConverter(); + foreach ( array( 'javascript:alert(1)', 'data:text/html,bad', 'file:///private/file', 'about:blank', 'vbscript:bad' ) as $src ) { + md_alt_assert_same( '', $converter->convert( '' ) ); + } + md_alt_assert_same( '', $converter->convert( '' ) ); + $output = $converter->convert( '' ); + md_alt_assert_same( '[A \\[title\\] \\](https://example.com/a%28b%29%20c)', $output ); +}; + +$md_alt_tests['Image and iframe code examples remain literal code'] = static function () { + $html = '
<img src="data:image/png;base64,AAAA">\n<iframe src="https://example.com/embed"></iframe>
'; + $output = ( new MarkdownConverter() )->convert( $html ); + md_alt_assert_contains( '', $output ); + md_alt_assert_contains( '', $output ); +}; + +$md_alt_tests['Previously cached conversion output regenerates after the renderer update'] = static function () { + $post = md_alt_test_fixture( 'ordinary' ); + $post->post_content = ''; + set_transient( + 'md_alt_cache_17', + array( + 'markdown' => 'Old output without the embed', + 'modified' => $post->post_modified, + 'version' => '', + 'schema' => 1, + ), + DAY_IN_SECONDS + ); + $renderer = new ContentRenderer(); + $output = $renderer->render( $post ); + md_alt_assert_contains( '[Video](https://example.com/embed)', $output ); + md_alt_assert_same( $output, $renderer->render( $post ) ); + md_alt_assert_same( 1, $GLOBALS['md_alt_test_state']['renders'] ); +}; diff --git a/tests/run.php b/tests/run.php index 99ce7f2..8de5a0b 100644 --- a/tests/run.php +++ b/tests/run.php @@ -450,6 +450,7 @@ static function () { }; require __DIR__ . '/security.php'; +require __DIR__ . '/conversion.php'; $md_alt_failures = 0; foreach ( $md_alt_tests as $md_alt_name => $md_alt_test ) {