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;
}
}

View file

@ -358,4 +358,18 @@ class PageContent
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">
<!-- Social Cards Meta -->
<meta property="og:title" content="{{ isset($pageTitle) ? $pageTitle . ' | ' : '' }}{{ setting('app-name') }}">
<meta property="og:url" content="{{ url()->current() }}">
@stack('social-meta')

View file

@ -7,7 +7,7 @@
@stop
@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() }}">
@endpush

View file

@ -7,7 +7,7 @@
@stop
@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() }}">
@endpush

View file

@ -1,8 +1,11 @@
@extends('tri-layout')
@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() }}">
$pageContent->getNavigation($page->html);
@endpush
@section('body')

View file

@ -1,7 +1,7 @@
@extends('tri-layout')
@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() }}">
@endpush