mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-28 17:02:04 +03:00
Merge branch 'create-content-meta-tags' of https://github.com/james-geiger/BookStack into james-geiger-create-content-meta-tags
This commit is contained in:
@ -138,4 +138,23 @@ class Page extends BookChild
|
||||
$refreshed->html = (new PageContent($refreshed))->render();
|
||||
return $refreshed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns URL to a cover image for the page.
|
||||
*/
|
||||
public function getCoverImage()
|
||||
{
|
||||
//$default = $this->book->getBookCover();
|
||||
$default = url('/logo.png');
|
||||
|
||||
$firstImage = (new PageContent($this))->fetchFirstImage();
|
||||
|
||||
try {
|
||||
$cover = $firstImage ? $firstImage : $default;
|
||||
} catch (\Exception $err) {
|
||||
$cover = $default;
|
||||
}
|
||||
return $cover;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -367,4 +367,18 @@ class PageContent
|
||||
$doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
|
||||
return $doc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve first image in page content and return the source URL.
|
||||
*/
|
||||
public function fetchFirstImage()
|
||||
{
|
||||
$htmlContent = $this->page->html;
|
||||
|
||||
$dom = new \DomDocument();
|
||||
$dom->loadHTML($htmlContent);
|
||||
$images = $dom->getElementsByTagName('img');
|
||||
|
||||
return $images->length > 0 ? $images[0]->getAttribute('src') : null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user