Skip to content
Permalink
Browse files

Add HtmlPurifier, Markdown, Cut/Hidecut

  • Loading branch information...
dignityinside committed Jun 14, 2019
1 parent 04d62c6 commit 7dd7e2b1e9ce267a7422e14836bcabeb9433084c
@@ -0,0 +1,59 @@
<?php
namespace App\Helpers;
/**
* HtmlPurifier provides an ability to clean up HTML from any harmful code.
*
* Basic usage is the following:
*
* ```php
* echo HtmlPurifier::process($html);
* ```
*
* For more details please refer to [HTMLPurifier documentation](http://htmlpurifier.org/).
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
class HtmlPurifier
{
/**
* Passes markup through HTMLPurifier making it safe to output to end user.
*
* @param string $content The HTML content to purify
* @param array|\Closure|null $config The config to use for HtmlPurifier.
*
* @return string the purified HTML content.
*/
public static function process($content, $config = null)
{
$configInstance = \HTMLPurifier_Config::create($config instanceof \Closure ? null : $config);
$configInstance->autoFinalize = false;
$purifier = \HTMLPurifier::instance($configInstance);
$purifier->config->set('Cache.SerializerPath', config('cache.stores.file.path'));
$purifier->config->set('Cache.SerializerPermissions', 0775);
static::configure($configInstance);
if ($config instanceof \Closure) {
call_user_func($config, $configInstance);
}
return $purifier->purify($content);
}
/**
* Allow the extended HtmlPurifier class to set some default config options.
* @param \HTMLPurifier_Config $config
*/
protected static function configure($config)
{
}
}
@@ -0,0 +1,26 @@
<?php
namespace App\Helpers;
use \cebe\markdown\GithubMarkdown;
class Markdown
{
/**
* Converts markdown into HTML.
*
* @param string $markdown the markdown text to parse
*
* @return string the parsed HTML output
*/
public static function process($markdown)
{
$parser = new GithubMarkdown();
$parser->enableNewlines = true;
$parser->keepListStartNumber = true;
$parser->html5 = true;
return $parser->parse($markdown);
}
}
@@ -0,0 +1,43 @@
<?php
namespace App\Helpers;
/**
* Text helper
*
* @author Alexander Schilling <dignityinside@protonmail.com>
*/
class Text
{
/**
* Cuts text after [cut] mark
*
* @param string $text
* @param string $moreLink
*
* @author Alexander Makarov <sam@rmcreative.ru>
*
* @return string
*/
public static function cut(string $text, ?string $moreLink = null)
{
$text = explode('[cut]', $text, 2);
return empty($text[1]) ? $text[0] : $moreLink === null ? $text[0] : $text[0] . "\n$moreLink";
}
/**
* Removes [cut] mark from the text
*
* @param string $text
*
* @author Alexander Makarov <sam@rmcreative.ru>
*
* @return string
*/
public static function hideCut(string $text): string
{
return str_replace('[cut]', '', $text);
}
}
@@ -5,6 +5,7 @@
use App\Page;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use \App\Helpers\HtmlPurifier;
/**
* Class PageObserver
@@ -27,6 +28,8 @@ public function creating(Page $page)
}
$page->owner_id = Auth::id();
$page->content = HtmlPurifier::process($page->content);
}
/**
@@ -38,6 +41,8 @@ public function updating(Page $page)
if (empty($page->slug)) {
$page->slug = $this->generateUniqueSlug($page);
}
$page->content = HtmlPurifier::process($page->content);
}
/**
@@ -2,6 +2,8 @@
namespace App;
use App\Helpers\Markdown;
use App\Helpers\Text;
use Illuminate\Database\Eloquent\Model;
/**
@@ -13,6 +15,7 @@
*/
class Page extends Model
{
/**
* Allowed fields
*
@@ -60,4 +63,34 @@ public function getRouteKeyName(): string
{
return 'slug';
}
/**
* Returns intro content
*
* @return string
*/
public function getIntroContent()
{
return Text::cut($this->getHtmlContent());
}
/**
* Returns full content
*
* @return string
*/
public function getFullContent(): string
{
return Text::hideCut($this->getHtmlContent());
}
/**
* Returns html content
*
* @return string
*/
private function getHtmlContent(): string
{
return Markdown::process($this->content);
}
}
@@ -9,6 +9,8 @@
"license": "MIT",
"require": {
"php": "^7.1.3",
"ezyang/htmlpurifier": "^4.10",
"cebe/markdown": "~1.0.0 | ~1.1.0 | ~1.2.0",
"fideloper/proxy": "^4.0",
"laravel/framework": "5.8.*",
"laravel/tinker": "^1.0"

Some generated files are not rendered by default. Learn more.

0 comments on commit 7dd7e2b

Please sign in to comment.
You can’t perform that action at this time.