1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-28 17:02:04 +03:00

Code cleanup, refactor

Updated to use Str::length for entity descriptions.
Moved function to get first image in page to PageContent class.
This commit is contained in:
James Geiger
2021-02-09 00:16:24 -06:00
parent e458411f91
commit 48587d2c38
7 changed files with 31 additions and 11 deletions

View File

@ -127,17 +127,21 @@ class Page extends BookChild
return $refreshed;
}
/**
* Returns URL to a cover image for the page.
*/
public function getCoverImage(): string
{
$dom = new \DomDocument();
$dom->loadHTML($this->html);
$images = $dom->getElementsByTagName('img');
$default = $this->book->getBookCover();
$firstImage = (new PageContent($this))->fetchFirstImage();
try {
$cover = $images->length > 0 ? $images[0]->getAttribute('src') : $this->book->getBookCover();
} catch (Exception $err) {
$cover = $this->book->getBookCover();
$cover = $firstImage ? $firstImage : $default;
} catch (\Exception $err) {
$cover = $default;
}
return $cover;
}
}