1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2024-11-23 11:22:33 +01: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:
Dan Brown 2021-06-23 20:11:07 +01:00
commit 58fa7679bc
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
7 changed files with 59 additions and 0 deletions

View File

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

View File

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

View File

@ -11,6 +11,12 @@
<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') }}">

View File

@ -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">

View File

@ -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">

View File

@ -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">

View File

@ -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">