Improved iframe & summary handling in HTML to MD conversion
This commit is contained in:
		
							parent
							
								
									c8a7acb6c7
								
							
						
					
					
						commit
						26af9acc6c
					
				| 
						 | 
					@ -88,6 +88,7 @@ class HtmlToMarkdown
 | 
				
			||||||
        $environment->addConverter(new PreformattedConverter());
 | 
					        $environment->addConverter(new PreformattedConverter());
 | 
				
			||||||
        $environment->addConverter(new TextConverter());
 | 
					        $environment->addConverter(new TextConverter());
 | 
				
			||||||
        $environment->addConverter(new CheckboxConverter());
 | 
					        $environment->addConverter(new CheckboxConverter());
 | 
				
			||||||
 | 
					        $environment->addConverter(new SpacedTagFallbackConverter());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $environment;
 | 
					        return $environment;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,23 @@
 | 
				
			||||||
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace BookStack\Entities\Tools\Markdown;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use League\HTMLToMarkdown\Converter\ConverterInterface;
 | 
				
			||||||
 | 
					use League\HTMLToMarkdown\ElementInterface;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * For certain defined tags, add additional spacing upon the retained HTML content
 | 
				
			||||||
 | 
					 * to separate it out from anything that may be markdown soon afterwards or within.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					class SpacedTagFallbackConverter implements ConverterInterface
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public function convert(ElementInterface $element): string
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return \html_entity_decode($element->getChildrenAsString()) . "\n\n";
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function getSupportedTags(): array
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return ['summary', 'iframe'];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -27,7 +27,7 @@
 | 
				
			||||||
                            </a>
 | 
					                            </a>
 | 
				
			||||||
                        </li>
 | 
					                        </li>
 | 
				
			||||||
                    @endif
 | 
					                    @endif
 | 
				
			||||||
                    <li refs="page-editor@discardDraftWrap" class="{{ $isDraft ? '' : 'hidden' }}">
 | 
					                    <li refs="page-editor@discardDraftWrap" class="{{ $isDraftRevision ? '' : 'hidden' }}">
 | 
				
			||||||
                        <button refs="page-editor@discardDraft" type="button" class="text-neg icon-item">
 | 
					                        <button refs="page-editor@discardDraft" type="button" class="text-neg icon-item">
 | 
				
			||||||
                            @icon('cancel')
 | 
					                            @icon('cancel')
 | 
				
			||||||
                            <div>{{ trans('entities.pages_edit_discard_draft') }}</div>
 | 
					                            <div>{{ trans('entities.pages_edit_discard_draft') }}</div>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -47,6 +47,22 @@ class MarkdownToHtmlTest extends TestCase
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function test_summary_tags_have_newlines_after_to_separate_content()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->assertConversion(
 | 
				
			||||||
 | 
					            '<details><summary>Toggle</summary><p>Test</p></details>',
 | 
				
			||||||
 | 
					            "<details><summary>Toggle</summary>\n\nTest\n\n</details>"
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function test_iframes_tags_have_newlines_after_to_separate_content()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->assertConversion(
 | 
				
			||||||
 | 
					            '<iframe src="https://example.com"></iframe><p>Beans</p>',
 | 
				
			||||||
 | 
					            "<iframe src=\"https://example.com\"></iframe>\n\nBeans"
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected function assertConversion(string $html, string $expectedMarkdown, bool $partialMdMatch = false)
 | 
					    protected function assertConversion(string $html, string $expectedMarkdown, bool $partialMdMatch = false)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $markdown = (new HtmlToMarkdown($html))->convert();
 | 
					        $markdown = (new HtmlToMarkdown($html))->convert();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue