Skip to content

Commit abed4ea

Browse files
committed
Exports: Updated plaintext export to use new converter
1 parent c7d3775 commit abed4ea

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

app/Exports/ExportFormatter.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use BookStack\Uploads\ImageService;
1212
use BookStack\Util\CspService;
1313
use BookStack\Util\HtmlDocument;
14+
use BookStack\Util\HtmlToPlainText;
1415
use DOMElement;
1516
use Exception;
1617
use Throwable;
@@ -242,32 +243,21 @@ protected function containHtml(string $htmlContent): string
242243

243244
/**
244245
* Converts the page contents into simple plain text.
245-
* This method filters any bad looking content to provide a nice final output.
246+
* We re-generate the plain text from HTML at this point, post-page-content rendering.
246247
*/
247248
public function pageToPlainText(Page $page, bool $pageRendered = false, bool $fromParent = false): string
248249
{
249250
$html = $pageRendered ? $page->html : (new PageContent($page))->render();
250-
// Add proceeding spaces before tags so spaces remain between
251-
// text within elements after stripping tags.
252-
$html = str_replace('<', " <", $html);
253-
$text = trim(strip_tags($html));
254-
// Replace multiple spaces with single spaces
255-
$text = preg_replace('/ {2,}/', ' ', $text);
256-
// Reduce multiple horrid whitespace characters.
257-
$text = preg_replace('/(\x0A|\xA0|\x0A|\r|\n){2,}/su', "\n\n", $text);
258-
$text = html_entity_decode($text);
259-
// Add title
260-
$text = $page->name . ($fromParent ? "\n" : "\n\n") . $text;
261-
262-
return $text;
251+
$contentText = (new HtmlToPlainText())->convert($html);
252+
return $page->name . ($fromParent ? "\n" : "\n\n") . $contentText;
263253
}
264254

265255
/**
266256
* Convert a chapter into a plain text string.
267257
*/
268258
public function chapterToPlainText(Chapter $chapter): string
269259
{
270-
$text = $chapter->name . "\n" . $chapter->description;
260+
$text = $chapter->name . "\n" . $chapter->descriptionInfo()->getPlain();
271261
$text = trim($text) . "\n\n";
272262

273263
$parts = [];

tests/Exports/TextExportTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function test_book_text_export_format()
5252
$resp = $this->asEditor()->get($entities['book']->getUrl('/export/plaintext'));
5353

5454
$expected = "Export Book\nThis is a book with stuff to export\n\nExport chapter\nA test chapter to be exported\nIt has loads of info within\n\n";
55-
$expected .= "My wonderful page!\nMy great page Full of great stuff";
55+
$expected .= "My wonderful page!\nMy great page\nFull of great stuff";
5656
$resp->assertSee($expected);
5757
}
5858

@@ -82,7 +82,7 @@ public function test_chapter_text_export_format()
8282
$resp = $this->asEditor()->get($entities['book']->getUrl('/export/plaintext'));
8383

8484
$expected = "Export chapter\nA test chapter to be exported\nIt has loads of info within\n\n";
85-
$expected .= "My wonderful page!\nMy great page Full of great stuff";
85+
$expected .= "My wonderful page!\nMy great page\nFull of great stuff";
8686
$resp->assertSee($expected);
8787
}
8888
}

0 commit comments

Comments
 (0)