新增面板新增markdown导出

This commit is contained in:
2022-07-28 16:26:10 +08:00
parent f3a6ab23c0
commit 95eb8d8581
4 changed files with 60 additions and 22 deletions

View File

@@ -4,11 +4,13 @@ declare(strict_types=1);
namespace app\admin\controller; namespace app\admin\controller;
use app\common\tools\PostBlock;
use app\model\Category; use app\model\Category;
use app\model\Post as ModelPost; use app\model\Post as ModelPost;
use app\model\PostCategory; use app\model\PostCategory;
use app\model\PostTag; use app\model\PostTag;
use app\model\Tag; use app\model\Tag;
use League\HTMLToMarkdown\HtmlConverter;
use think\facade\Cache; use think\facade\Cache;
use think\facade\Route; use think\facade\Route;
use think\facade\View; use think\facade\View;
@@ -91,6 +93,17 @@ class Post extends Common
return $this->success('添加成功', url('index', ['type' => $this->request->param('type')])); 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]);
}
/** /**
* 显示指定的资源 * 显示指定的资源
* *

View File

@@ -5,7 +5,9 @@ declare(strict_types=1);
namespace app\model; namespace app\model;
use app\common\model\Base; use app\common\model\Base;
use app\common\tools\PostBlock;
use app\common\tools\PostShow; use app\common\tools\PostShow;
use League\HTMLToMarkdown\HtmlConverter;
use think\facade\Cache; use think\facade\Cache;
use think\facade\Request; use think\facade\Request;
use think\Model; use think\Model;
@@ -56,9 +58,6 @@ class Post extends Base
public function getCommentCountAttr() public function getCommentCountAttr()
{ {
return PostComment::getPostCommentsCount($this->getData('id')); return PostComment::getPostCommentsCount($this->getData('id'));
} }
@@ -143,15 +142,26 @@ class Post extends Base
return strtotime($value); return strtotime($value);
} }
public function setContentAttr($value)
{
return json_encode($value);
}
public function setContentHtmlAttr($value) public function setContentHtmlAttr($value)
{ {
return trim($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() public function getContentHtmlShowAttr()
{ {
@@ -160,11 +170,6 @@ class Post extends Base
return PostShow::handleCopyright($content); return PostShow::handleCopyright($content);
} }
public function getContentAttr($value)
{
return json_decode($value, true);
}
public function getPosterAttr($value) public function getPosterAttr($value)
{ {
if (empty($value)) { if (empty($value)) {

View File

@@ -29,7 +29,8 @@
"ulthon/user_hub_client": "^1.0", "ulthon/user_hub_client": "^1.0",
"thepixeldeveloper/sitemap": "^5.1", "thepixeldeveloper/sitemap": "^5.1",
"jaeger/querylist": "^4.2", "jaeger/querylist": "^4.2",
"jaeger/phpquery-single": "^1.1" "jaeger/phpquery-single": "^1.1",
"league/html-to-markdown": "^5.1"
}, },
"require-dev": { "require-dev": {
"symfony/var-dumper": "^4.2" "symfony/var-dumper": "^4.2"

View File

@@ -63,6 +63,9 @@
{:\\app\\common\\tools\\PostBlock::copyright($post)} {:\\app\\common\\tools\\PostBlock::copyright($post)}
</article> </article>
</div> </div>
<div style="max-width: 715px;">
<textarea class="layui-textarea" autoHeight="true" id="post-markdown" placeholder="同步markdown内容" autocomplete="off">{$post->content_markdown|raw}</textarea>
</div>
<div class="options-box layui-btn-container"> <div class="options-box layui-btn-container">
<a class="copy-option layui-btn layui-btn-sm" target="_blank" href="{$post.read_url}">打开原文</a> <a class="copy-option layui-btn layui-btn-sm" target="_blank" href="{$post.read_url}">打开原文</a>
@@ -79,6 +82,7 @@
<div class="layui-btn layui-btn-sm resetcode"> <div class="layui-btn layui-btn-sm resetcode">
还原代码 还原代码
</div> </div>
<a class=" layui-btn layui-btn-sm select-markdown" href="#post-markdown">选中markdown</a>
</div> </div>
<script> <script>
@@ -110,18 +114,17 @@
$('#post-content').find('pre').show().each(function (index, elem) { $('#post-content').find('pre').show().each(function (index, elem) {
$(elem).removeClass('tobase64') $(elem).removeClass('tobase64')
}) })
}) })
}) })
$('.copy-option').focusout(function (e) { $('.copy-option').focusout(function (e) {
copyContent($(this).attr('href')) copyContent($(this).attr('href'))
}) })
$('.select-markdown').click(function () {
$('#post-markdown').select()
})
$('.build-copyright').click(function () { $('.build-copyright').click(function () {
var copyElem = $('.copyright').find('p').last(); var copyElem = $('.copyright').find('p').last();
@@ -167,6 +170,22 @@
selection.addRange(range); selection.addRange(range);
} }
$(function () {
$.fn.autoHeight = function () {
function autoHeight(elem) {
elem.style.height = 'auto';
elem.style.height = elem.scrollHeight + 20 + 'px';
}
this.each(function () {
autoHeight(this);
$(this).on('keyup', function () {
autoHeight(this);
});
});
}
$('textarea[autoHeight]').autoHeight();
})
</script> </script>
</body> </body>