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; return $refreshed;
} }
/**
* Returns URL to a cover image for the page.
*/
public function getCoverImage(): string public function getCoverImage(): string
{ {
$dom = new \DomDocument(); $default = $this->book->getBookCover();
$dom->loadHTML($this->html);
$images = $dom->getElementsByTagName('img'); $firstImage = (new PageContent($this))->fetchFirstImage();
try { try {
$cover = $images->length > 0 ? $images[0]->getAttribute('src') : $this->book->getBookCover(); $cover = $firstImage ? $firstImage : $default;
} catch (Exception $err) { } catch (\Exception $err) {
$cover = $this->book->getBookCover(); $cover = $default;
} }
return $cover; return $cover;
} }
} }

View File

@ -358,4 +358,18 @@ class PageContent
return $html; return $html;
} }
/**
* Retrieve first image in page content and return the source URL.
*/
public function fetchFirstImage(): string
{
$htmlContent = $this->page->html;
$dom = new \DomDocument();
$dom->loadHTML($htmlContent);
$images = $dom->getElementsByTagName('img');
return $images[0]->getAttribute('src');
}
} }

View File

@ -12,7 +12,6 @@
<meta charset="utf-8"> <meta charset="utf-8">
<!-- Social Cards Meta --> <!-- Social Cards Meta -->
<meta property="og:title" content="{{ isset($pageTitle) ? $pageTitle . ' | ' : '' }}{{ setting('app-name') }}"> <meta property="og:title" content="{{ isset($pageTitle) ? $pageTitle . ' | ' : '' }}{{ setting('app-name') }}">
<meta property="og:url" content="{{ url()->current() }}"> <meta property="og:url" content="{{ url()->current() }}">
@stack('social-meta') @stack('social-meta')

View File

@ -7,7 +7,7 @@
@stop @stop
@push('social-meta') @push('social-meta')
<meta property="og:description" content="{{ Str::words($book->description, 50, '...') }}"> <meta property="og:description" content="{{ Str::limit($book->description, 100, '...') }}">
<meta property="og:image" content="{{ $book->getBookCover() }}"> <meta property="og:image" content="{{ $book->getBookCover() }}">
@endpush @endpush

View File

@ -7,7 +7,7 @@
@stop @stop
@push('social-meta') @push('social-meta')
<meta property="og:description" content="{{ Str::words($chapter->description, 50, '...') }}"> <meta property="og:description" content="{{ Str::limit($chapter->description, 100) }}">
<meta property="og:image" content="{{ $chapter->book->getBookCover() }}"> <meta property="og:image" content="{{ $chapter->book->getBookCover() }}">
@endpush @endpush

View File

@ -1,8 +1,11 @@
@extends('tri-layout') @extends('tri-layout')
@push('social-meta') @push('social-meta')
<meta property="og:description" content="{{ Str::words($page->html, 50, '...') }}"> <meta property="og:description" content="{{ Str::limit($page->text, 100, '...') }}">
<meta property="og:image" content="{{ $page->getCoverImage() }}"> <meta property="og:image" content="{{ $page->getCoverImage() }}">
$pageContent->getNavigation($page->html);
@endpush @endpush
@section('body') @section('body')

View File

@ -1,7 +1,7 @@
@extends('tri-layout') @extends('tri-layout')
@push('social-meta') @push('social-meta')
<meta property="og:description" content="{{ Str::words($shelf->description, 50, '...') }}"> <meta property="og:description" content="{{ Str::limit($shelf->description, 100) }}">
<meta property="og:image" content="{{ $shelf->getBookCover() }}"> <meta property="og:image" content="{{ $shelf->getBookCover() }}">
@endpush @endpush