Added tasklist support to markdown exporter

This commit is contained in:
Dan Brown 2022-03-22 14:56:51 +00:00
parent ea62fe6004
commit c5aad29c72
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
3 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,28 @@
<?php
namespace BookStack\Entities\Tools\Markdown;
use League\HTMLToMarkdown\Converter\ConverterInterface;
use League\HTMLToMarkdown\ElementInterface;
class CheckboxConverter implements ConverterInterface
{
public function convert(ElementInterface $element): string
{
if (strtolower($element->getAttribute('type')) === 'checkbox') {
$isChecked = $element->getAttribute('checked') === 'checked';
return $isChecked ? ' [x] ' : ' [ ] ';
}
return $element->getValue();
}
/**
* @return string[]
*/
public function getSupportedTags(): array
{
return ['input'];
}
}

View File

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

View File

@ -386,6 +386,18 @@ class ExportTest extends TestCase
$resp->assertSee("# Dogcat\n\n```JavaScript\nvar a = 'cat';\n```\n\nAnother line", false);
}
public function test_page_markdown_export_handles_tasklist_checkboxes()
{
$page = Page::query()->first()->forceFill([
'markdown' => '',
'html' => '<ul><li><input type="checkbox" checked="checked">Item A</li><li><input type="checkbox">Item B</li></ul>',
]);
$page->save();
$resp = $this->asEditor()->get($page->getUrl('/export/markdown'));
$resp->assertSee("- [x] Item A\n- [ ] Item B", false);
}
public function test_chapter_markdown_export()
{
$chapter = Chapter::query()->first();