1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2024-10-30 07:32:39 +01:00

Improved iframe & summary handling in HTML to MD conversion

This commit is contained in:
Dan Brown 2022-04-29 14:58:28 +01:00
parent c8a7acb6c7
commit 26af9acc6c
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
4 changed files with 41 additions and 1 deletions

View File

@ -88,6 +88,7 @@ class HtmlToMarkdown
$environment->addConverter(new PreformattedConverter());
$environment->addConverter(new TextConverter());
$environment->addConverter(new CheckboxConverter());
$environment->addConverter(new SpacedTagFallbackConverter());
return $environment;
}

View File

@ -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'];
}
}

View File

@ -27,7 +27,7 @@
</a>
</li>
@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">
@icon('cancel')
<div>{{ trans('entities.pages_edit_discard_draft') }}</div>

View File

@ -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)
{
$markdown = (new HtmlToMarkdown($html))->convert();