diff --git a/app/admin/controller/Post.php b/app/admin/controller/Post.php index c0b262e..f5119c8 100644 --- a/app/admin/controller/Post.php +++ b/app/admin/controller/Post.php @@ -4,11 +4,13 @@ declare(strict_types=1); namespace app\admin\controller; +use app\common\tools\PostBlock; use app\model\Category; use app\model\Post as ModelPost; use app\model\PostCategory; use app\model\PostTag; use app\model\Tag; +use League\HTMLToMarkdown\HtmlConverter; use think\facade\Cache; use think\facade\Route; use think\facade\View; @@ -29,7 +31,7 @@ class Post extends Common ->where('type', $this->request->param('type', 1)) ->order('id desc') ->paginate([ - 'query' =>$this->request->get() + 'query' => $this->request->get() ]); View::assign('list', $list); @@ -91,6 +93,17 @@ class Post extends Common return $this->success('添加成功', url('index', ['type' => $this->request->param('type')])); } + public function convertHtml2Markdown() + { + $content = $this->request->post('content'); + + $converter = new HtmlConverter(); + + $markdown = $converter->convert($content); + + return json_message(['markdown' => $markdown]); + } + /** * 显示指定的资源 * diff --git a/app/model/Post.php b/app/model/Post.php index d39c1a3..8a9364d 100644 --- a/app/model/Post.php +++ b/app/model/Post.php @@ -5,7 +5,9 @@ declare(strict_types=1); namespace app\model; use app\common\model\Base; +use app\common\tools\PostBlock; use app\common\tools\PostShow; +use League\HTMLToMarkdown\HtmlConverter; use think\facade\Cache; use think\facade\Request; use think\Model; @@ -56,9 +58,6 @@ class Post extends Base public function getCommentCountAttr() { - - - return PostComment::getPostCommentsCount($this->getData('id')); } @@ -143,15 +142,26 @@ class Post extends Base return strtotime($value); } - public function setContentAttr($value) - { - return json_encode($value); - } public function setContentHtmlAttr($value) { return trim($value); } + public function getContentMarkdownAttr() + { + $content_html = $this->getAttr('content_html'); + + + $content_html .= PostBlock::copyright($this); + + + $converter = new HtmlConverter(array('strip_tags' => true)); + + $markdown = $converter->convert($content_html); + + return $markdown; + } + public function getContentHtmlShowAttr() { @@ -160,11 +170,6 @@ class Post extends Base return PostShow::handleCopyright($content); } - public function getContentAttr($value) - { - return json_decode($value, true); - } - public function getPosterAttr($value) { if (empty($value)) { diff --git a/composer.json b/composer.json index b72f291..db6cc28 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,8 @@ "ulthon/user_hub_client": "^1.0", "thepixeldeveloper/sitemap": "^5.1", "jaeger/querylist": "^4.2", - "jaeger/phpquery-single": "^1.1" + "jaeger/phpquery-single": "^1.1", + "league/html-to-markdown": "^5.1" }, "require-dev": { "symfony/var-dumper": "^4.2" diff --git a/view/admin/post/output.html b/view/admin/post/output.html index 37d340b..a871a7a 100644 --- a/view/admin/post/output.html +++ b/view/admin/post/output.html @@ -63,6 +63,9 @@ {:\\app\\common\\tools\\PostBlock::copyright($post)} +