Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/Entities/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,23 @@ public function forJsonDisplay(): Page
$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;
}

}
14 changes: 14 additions & 0 deletions app/Entities/Tools/PageContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,18 @@ protected function escapeScripts(string $html) : string

return $html;
}

/**
* 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;
}
}
6 changes: 6 additions & 0 deletions resources/views/base.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class="{{ setting()->getForCurrentUser('dark-mode-enabled') ? 'dark-mode ' : ''
<meta name="base-url" content="{{ url('/') }}">
<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')


<!-- Styles and Fonts -->
<link rel="stylesheet" href="{{ versioned_asset('dist/styles.css') }}">
<link rel="stylesheet" media="print" href="{{ versioned_asset('dist/print-styles.css') }}">
Expand Down
5 changes: 5 additions & 0 deletions resources/views/books/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
option:entity-search:entity-type="book"
@stop

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

@section('body')

<div class="mb-s">
Expand Down
5 changes: 5 additions & 0 deletions resources/views/chapters/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
option:entity-search:entity-type="chapter"
@stop

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

@section('body')

<div class="mb-m print-hidden">
Expand Down
5 changes: 5 additions & 0 deletions resources/views/pages/show.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
@extends('tri-layout')

@push('social-meta')
<meta property="og:description" content="{{ Str::limit($page->text, 100, '...') }}">
<meta property="og:image" content="{{ $page->getCoverImage() }}">
@endpush

@section('body')

<div class="mb-m print-hidden">
Expand Down
5 changes: 5 additions & 0 deletions resources/views/shelves/show.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
@extends('tri-layout')

@push('social-meta')
<meta property="og:description" content="{{ Str::limit($shelf->description, 100) }}">
<meta property="og:image" content="{{ $shelf->getBookCover() }}">
@endpush

@section('body')

<div class="mb-s">
Expand Down