Skip to content

Commit

Permalink
Strip <p> tag in meta description, leave it in the card
Browse files Browse the repository at this point in the history
  • Loading branch information
kutaloweb committed Feb 10, 2019
1 parent db0e500 commit 412f48e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
10 changes: 7 additions & 3 deletions app/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Page extends Model
*
* @var array
*/
protected $appends = ['stripped_body'];
protected $appends = ['totally_stripped_body'];

/**
* Get created at in a human readable format.
Expand All @@ -61,9 +61,13 @@ public function getUpdatedAtAttribute()
*
* @return string
*/
public function getStrippedBodyAttribute()
public function getTotallyStrippedBodyAttribute()
{
return strip_tags($this->attributes['body']);
$string = $this->attributes['body'];
$doubleSpace = strip_tags(str_replace('<', ' <', $string));
$singleSpace = str_replace(' ', ' ', $doubleSpace);

return trim($singleSpace);
}

/**
Expand Down
24 changes: 21 additions & 3 deletions app/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Post extends Model
*
* @var array
*/
protected $appends = ['stripped_body'];
protected $appends = ['stripped_body', 'totally_stripped_body'];

/**
* Get the user that owns the post.
Expand Down Expand Up @@ -84,13 +84,31 @@ public function getUpdatedAtAttribute()
}

/**
* Get body with the stripped HTML tags.
* Get body with the stripped HTML tags allowing only <p> tag.
*
* @return string
*/
public function getStrippedBodyAttribute()
{
return strip_tags($this->attributes['body'], '<p>');
$string = $this->attributes['body'];
$doubleSpace = strip_tags(str_replace('<', ' <', $string), '<p>');
$singleSpace = str_replace(' ', ' ', $doubleSpace);

return trim($singleSpace);
}

/**
* Get body with the stripped HTML tags.
*
* @return string
*/
public function getTotallyStrippedBodyAttribute()
{
$string = $this->attributes['body'];
$doubleSpace = strip_tags(str_replace('<', ' <', $string));
$singleSpace = str_replace(' ', ' ', $doubleSpace);

return trim($singleSpace);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions resources/assets/js/views/page/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@
return {
title: `${this.documentTitle}`,
meta: [
{name: 'description', content: this.page ? this.limitWords(this.page.stripped_body) : ''},
{name: 'description', content: this.page ? this.limitWords(this.page.totally_stripped_body) : ''},
{name: 'twitter:card', content: 'summary_large_image'},
{name: 'twitter:title', content: this.page ? this.page.title : ''},
{name: 'twitter:description', content: this.page ? this.limitWords(this.page.stripped_body) : ''},
{name: 'twitter:description', content: this.page ? this.limitWords(this.page.totally_stripped_body) : ''},
{name: 'twitter:image', content: `${this.getConfig('app_url')}/uploads/images/cover-default.png`},
{property: 'og:type', content: 'website'},
{property: 'og:site_name', content: this.getConfig('company_name')},
{property: 'og:url', content: this.page ? `${this.getConfig('app_url')}/${this.page.slug}` : ''},
{property: 'og:title', content: this.page ? this.page.title : ''},
{property: 'og:description', content: this.page ? this.limitWords(this.page.stripped_body) : ''},
{property: 'og:description', content: this.page ? this.limitWords(this.page.totally_stripped_body) : ''},
{property: 'og:image', content: `${this.getConfig('app_url')}/uploads/images/cover-default.png`}
]
}
Expand Down
6 changes: 3 additions & 3 deletions resources/assets/js/views/post/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@
return {
title: `${this.documentTitle}`,
meta: [
{name: 'description', content: this.post ? this.limitWords(this.post.stripped_body) : ''},
{name: 'description', content: this.post ? this.limitWords(this.post.totally_stripped_body) : ''},
{name: 'twitter:card', content: 'summary_large_image'},
{name: 'twitter:title', content: this.post ? this.post.title : ''},
{name: 'twitter:description', content: this.post ? this.limitWords(this.post.stripped_body) : ''},
{name: 'twitter:description', content: this.post ? this.limitWords(this.post.totally_stripped_body) : ''},
{name: 'twitter:image', content: this.post ? `${this.getConfig('app_url')}/${this.post.cover}` : ''},
{property: 'og:type', content: 'website'},
{property: 'og:site_name', content: this.getConfig('company_name')},
{property: 'og:url', content: this.post ? `${this.getConfig('app_url')}/${this.categorySlug}/${this.post.slug}` : ''},
{property: 'og:title', content: this.post ? this.post.title : ''},
{property: 'og:description', content: this.post ? this.limitWords(this.post.stripped_body) : ''},
{property: 'og:description', content: this.post ? this.limitWords(this.post.totally_stripped_body) : ''},
{property: 'og:image', content: this.post ? `${this.getConfig('app_url')}/${this.post.cover}` : ''}
]
}
Expand Down

0 comments on commit 412f48e

Please sign in to comment.