Add (jsonld)

This commit is contained in:
2023-09-26 00:09:51 +08:00
parent 2c12b8857d
commit 8da977daeb
23 changed files with 238 additions and 85 deletions

View File

@@ -9,6 +9,7 @@
use Artesaos\SEOTools\Facades\SEOMeta;
use GrahamCampbell\Markdown\Facades\Markdown;
use Illuminate\Http\Request;
use JsonLd\Context;
use Symfony\Component\DomCrawler\Crawler;
class FrontPostController extends Controller
@@ -25,11 +26,36 @@ public function index(Request $request, $slug)
//dd($content);
$content = $this->injectBootstrapClasses($content);
$content = $this->injectTableOfContents($content);
$content = $this->injectFeaturedImage($post, $content);
$content = $this->injectPublishDateAndAuthor($post, $content);
$content = $this->injectTableOfContents($content);
$post_description = $post->excerpt.' '.$post->title.' by EchoScoop.';
// Generate breadcrumb data
$breadcrumbs = collect([
['name' => 'Home', 'url' => route('front.home')],
]);
if ($post->category) {
foreach ($post->category->ancestors as $ancestor) {
$breadcrumbs->push([
'name' => $ancestor->name,
'url' => route('front.category', $ancestor->slug),
]);
}
$breadcrumbs->push([
'name' => $post->category->name,
'url' => route('front.category', $post->category->slug),
]);
}
$breadcrumbs->push([
'name' => $post->title,
'url' => url()->current(), // The current page URL; the breadcrumb is not clickable
]);
SEOMeta::setTitle($post->title, false);
SEOMeta::setDescription($post_description);
SEOMeta::addMeta('article:published_time', $post->published_at->format('Y-m-d'), 'property');
@@ -68,7 +94,21 @@ public function index(Request $request, $slug)
->addValue('description', $post_description)
->addValue('articleBody', trim(preg_replace('/\s\s+/', ' ', strip_tags($content))));
return view('front.single_post', compact('post', 'content'));
// breadcrumb json ld
$listItems = [];
foreach ($breadcrumbs as $index => $breadcrumb) {
$listItems[] = [
'name' => $breadcrumb['name'],
'url' => $breadcrumb['url'],
];
}
$breadcrumb_context = Context::create('breadcrumb_list', [
'itemListElement' => $listItems,
]);
return view('front.single_post', compact('post', 'content', 'breadcrumbs', 'breadcrumb_context'));
}
private function injectBootstrapClasses($content)
@@ -77,7 +117,7 @@ private function injectBootstrapClasses($content)
// Handle Headings
$crawler->filter('h1')->each(function (Crawler $node) {
$node->getNode(0)->setAttribute('class', trim($node->attr('class').' display-6 fw-bolder mt-3 mb-4'));
$node->getNode(0)->setAttribute('class', trim($node->attr('class').' display-6 fw-bolder mt-3 mb-2'));
});
$crawler->filter('h2')->each(function (Crawler $node) {
@@ -171,6 +211,20 @@ private function injectTableOfContents($html)
return $updatedHtml;
}
private function injectPublishDateAndAuthor($post, $content)
{
$publishedAtFormatted = $post->published_at->format('F j, Y');
$authorName = $post->author->name;
// Create the HTML structure for publish date and author
$publishInfo = "<div class=\"mb-4\"><span class=\"text-secondary small\">Published on {$publishedAtFormatted} by {$authorName}</span></div>";
// Inject the publish date and author information after the `h1` tag
$content = preg_replace('/(<\/h1>)/', '$1'.$publishInfo, $content, 1);
return $content;
}
private function injectFeaturedImage($post, $content)
{
if (! empty($post->featured_image)) {