From b26bf69adda2fe034106fd48a373404e199a81b4 Mon Sep 17 00:00:00 2001 From: Louis Jeckel Date: Mon, 14 Sep 2020 16:34:01 +0200 Subject: [PATCH] html2text --- .idea/lettre-pharma.iml | 1 + .idea/php.xml | 1 + app/Models/HasMetaTags.php | 11 ++++++++++- app/Models/LaboArticle.php | 3 ++- composer.json | 1 + composer.lock | 39 +++++++++++++++++++++++++++++++++++++- 6 files changed, 53 insertions(+), 3 deletions(-) diff --git a/.idea/lettre-pharma.iml b/.idea/lettre-pharma.iml index a117e04..0937141 100644 --- a/.idea/lettre-pharma.iml +++ b/.idea/lettre-pharma.iml @@ -17,6 +17,7 @@ + diff --git a/.idea/php.xml b/.idea/php.xml index fdcbde4..b0b2f0f 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -208,6 +208,7 @@ + diff --git a/app/Models/HasMetaTags.php b/app/Models/HasMetaTags.php index 0572f59..a0e2331 100644 --- a/app/Models/HasMetaTags.php +++ b/app/Models/HasMetaTags.php @@ -10,7 +10,7 @@ trait HasMetaTags { return [ 'title' => $this->getMetaTitle(), - 'description' => strip_tags($this->getMetaDescription()), + 'description' => $this->getCleanMetaDescription(), 'image' => $this->getMetaImage(), 'url' => $this->getMetaUrl(), ]; @@ -22,6 +22,15 @@ trait HasMetaTags return $this->title; } + /** + * @return string + */ + final protected function getCleanMetaDescription(): string + { + $str = (new \Html2Text\Html2Text($this->getMetaDescription()))->getText(); + return preg_replace("/\r\n|\r|\n/", ' ', $str); + } + protected function getMetaDescription(): string { return $this->chapo; diff --git a/app/Models/LaboArticle.php b/app/Models/LaboArticle.php index 10d0176..2b9784f 100644 --- a/app/Models/LaboArticle.php +++ b/app/Models/LaboArticle.php @@ -56,9 +56,10 @@ class LaboArticle extends Model implements Sortable, PillBoxContract ]; + public function getPreviewAttribute(): string { - $line = strip_tags($this->content); + $line = (new \Html2Text\Html2Text($this->content))->getText(); if (preg_match('/^.{1,200}\b/s', $line, $match)) { $line = trim($match[0]) . "…"; diff --git a/composer.json b/composer.json index 23b53f1..b29e909 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "fideloper/proxy": "^4.2", "fruitcake/laravel-cors": "^1.0", "guzzlehttp/guzzle": "^6.3", + "html2text/html2text": "^4.3", "itsgoingd/clockwork": "^4.1", "judev/php-htmltruncator": "^1.2", "kriswallsmith/buzz": "^1.1", diff --git a/composer.lock b/composer.lock index 238f80c..e3b5740 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "70d746984d1fe596d469cdd574622e82", + "content-hash": "3a16aca0cab60a6ab491bc8a8516253e", "packages": [ { "name": "algolia/algoliasearch-client-php", @@ -2074,6 +2074,43 @@ ], "time": "2019-07-01T23:21:34+00:00" }, + { + "name": "html2text/html2text", + "version": "4.3.1", + "source": { + "type": "git", + "url": "https://github.com/mtibben/html2text.git", + "reference": "61ad68e934066a6f8df29a3d23a6460536d0855c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mtibben/html2text/zipball/61ad68e934066a6f8df29a3d23a6460536d0855c", + "reference": "61ad68e934066a6f8df29a3d23a6460536d0855c", + "shasum": "" + }, + "require-dev": { + "phpunit/phpunit": "~4" + }, + "suggest": { + "ext-mbstring": "For best performance", + "symfony/polyfill-mbstring": "If you can't install ext-mbstring" + }, + "type": "library", + "autoload": { + "psr-4": { + "Html2Text\\": [ + "src/", + "test/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-2.0-or-later" + ], + "description": "Converts HTML to formatted plain text", + "time": "2020-04-16T23:44:31+00:00" + }, { "name": "http-interop/http-factory-guzzle", "version": "1.0.0", -- 2.39.5