From f12946d581fe66d5269f992db6573bb892286303 Mon Sep 17 00:00:00 2001 From: czemu Date: Sun, 10 Nov 2024 09:39:33 +0100 Subject: [PATCH 1/2] ExportFormatter: Add book description and check for empty book and chapter descriptions in markdown export --- app/Entities/Tools/ExportFormatter.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/Entities/Tools/ExportFormatter.php b/app/Entities/Tools/ExportFormatter.php index beddfe8e6..e85992a9d 100644 --- a/app/Entities/Tools/ExportFormatter.php +++ b/app/Entities/Tools/ExportFormatter.php @@ -315,7 +315,11 @@ class ExportFormatter public function chapterToMarkdown(Chapter $chapter): string { $text = '# ' . $chapter->name . "\n\n"; - $text .= $chapter->description . "\n\n"; + + if (!empty($chapter->description)) { + $text .= $chapter->description . "\n\n"; + } + foreach ($chapter->pages as $page) { $text .= $this->pageToMarkdown($page) . "\n\n"; } @@ -330,6 +334,11 @@ class ExportFormatter { $bookTree = (new BookContents($book))->getTree(false, true); $text = '# ' . $book->name . "\n\n"; + + if (!empty($book->description)) { + $text .= $book->description . "\n\n"; + } + foreach ($bookTree as $bookChild) { if ($bookChild instanceof Chapter) { $text .= $this->chapterToMarkdown($bookChild) . "\n\n"; From 0f9957bc036e40d068dfb0958560485f7456964a Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Mon, 2 Dec 2024 11:46:56 +0000 Subject: [PATCH 2/2] MD Exports: Added HTML description conversion Also updated tests to cover checking description use/conversion. Made during review of #5313 --- app/Entities/Tools/ExportFormatter.php | 12 +++++++----- tests/Entity/ExportTest.php | 12 ++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/Entities/Tools/ExportFormatter.php b/app/Entities/Tools/ExportFormatter.php index e85992a9d..0af68b8db 100644 --- a/app/Entities/Tools/ExportFormatter.php +++ b/app/Entities/Tools/ExportFormatter.php @@ -316,8 +316,9 @@ class ExportFormatter { $text = '# ' . $chapter->name . "\n\n"; - if (!empty($chapter->description)) { - $text .= $chapter->description . "\n\n"; + $description = (new HtmlToMarkdown($chapter->descriptionHtml()))->convert(); + if ($description) { + $text .= $description . "\n\n"; } foreach ($chapter->pages as $page) { @@ -334,9 +335,10 @@ class ExportFormatter { $bookTree = (new BookContents($book))->getTree(false, true); $text = '# ' . $book->name . "\n\n"; - - if (!empty($book->description)) { - $text .= $book->description . "\n\n"; + + $description = (new HtmlToMarkdown($book->descriptionHtml()))->convert(); + if ($description) { + $text .= $description . "\n\n"; } foreach ($bookTree as $bookChild) { diff --git a/tests/Entity/ExportTest.php b/tests/Entity/ExportTest.php index 7aafa3b79..97b1ff1bc 100644 --- a/tests/Entity/ExportTest.php +++ b/tests/Entity/ExportTest.php @@ -417,23 +417,35 @@ class ExportTest extends TestCase public function test_chapter_markdown_export() { $chapter = $this->entities->chapter(); + $chapter->description_html = '

My chapter description

'; + $chapter->save(); $page = $chapter->pages()->first(); + $resp = $this->asEditor()->get($chapter->getUrl('/export/markdown')); $resp->assertSee('# ' . $chapter->name); $resp->assertSee('# ' . $page->name); + $resp->assertSee('My **chapter** description'); } public function test_book_markdown_export() { $book = Book::query()->whereHas('pages')->whereHas('chapters')->first(); + $book->description_html = '

My book description

'; + $book->save(); + $chapter = $book->chapters()->first(); + $chapter->description_html = '

My chapter description

'; + $chapter->save(); + $page = $chapter->pages()->first(); $resp = $this->asEditor()->get($book->getUrl('/export/markdown')); $resp->assertSee('# ' . $book->name); $resp->assertSee('# ' . $chapter->name); $resp->assertSee('# ' . $page->name); + $resp->assertSee('My **book** description'); + $resp->assertSee('My **chapter** description'); } public function test_book_markdown_export_concats_immediate_pages_with_newlines()