Create Open Graph meta tags for book/page/chapter/shelf

This commit is contained in:
James Geiger 2020-12-21 23:20:13 -06:00
parent 4b36df08a8
commit e458411f91
6 changed files with 41 additions and 0 deletions

View File

@ -126,4 +126,18 @@ class Page extends BookChild
$refreshed->html = (new PageContent($refreshed))->render(); $refreshed->html = (new PageContent($refreshed))->render();
return $refreshed; return $refreshed;
} }
public function getCoverImage(): string
{
$dom = new \DomDocument();
$dom->loadHTML($this->html);
$images = $dom->getElementsByTagName('img');
try {
$cover = $images->length > 0 ? $images[0]->getAttribute('src') : $this->book->getBookCover();
} catch (Exception $err) {
$cover = $this->book->getBookCover();
}
return $cover;
}
} }

View File

@ -11,6 +11,13 @@
<meta name="base-url" content="{{ url('/') }}"> <meta name="base-url" content="{{ url('/') }}">
<meta charset="utf-8"> <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 --> <!-- Styles and Fonts -->
<link rel="stylesheet" href="{{ versioned_asset('dist/styles.css') }}"> <link rel="stylesheet" href="{{ versioned_asset('dist/styles.css') }}">
<link rel="stylesheet" media="print" href="{{ versioned_asset('dist/print-styles.css') }}"> <link rel="stylesheet" media="print" href="{{ versioned_asset('dist/print-styles.css') }}">

View File

@ -6,6 +6,11 @@
option:entity-search:entity-type="book" option:entity-search:entity-type="book"
@stop @stop
@push('social-meta')
<meta property="og:description" content="{{ Str::words($book->description, 50, '...') }}">
<meta property="og:image" content="{{ $book->getBookCover() }}">
@endpush
@section('body') @section('body')
<div class="mb-s"> <div class="mb-s">

View File

@ -6,6 +6,11 @@
option:entity-search:entity-type="chapter" option:entity-search:entity-type="chapter"
@stop @stop
@push('social-meta')
<meta property="og:description" content="{{ Str::words($chapter->description, 50, '...') }}">
<meta property="og:image" content="{{ $chapter->book->getBookCover() }}">
@endpush
@section('body') @section('body')
<div class="mb-m print-hidden"> <div class="mb-m print-hidden">

View File

@ -1,5 +1,10 @@
@extends('tri-layout') @extends('tri-layout')
@push('social-meta')
<meta property="og:description" content="{{ Str::words($page->html, 50, '...') }}">
<meta property="og:image" content="{{ $page->getCoverImage() }}">
@endpush
@section('body') @section('body')
<div class="mb-m print-hidden"> <div class="mb-m print-hidden">

View File

@ -1,5 +1,10 @@
@extends('tri-layout') @extends('tri-layout')
@push('social-meta')
<meta property="og:description" content="{{ Str::words($shelf->description, 50, '...') }}">
<meta property="og:image" content="{{ $shelf->getBookCover() }}">
@endpush
@section('body') @section('body')
<div class="mb-s"> <div class="mb-s">