个人博客系统文章管理代码集成markdown插件

概述

在我实现博客系统的文章管理时,在之前这种操作我都会使用富文本编辑器实现提交,但是因为我再csdn上发表文章的时候有一种编辑器叫markdown编辑器,我平时使用的记笔记软件Typora也是markdown风格,所以我就想在文章管理,以及今后前台发布文章时使用markdown编辑器实现。下面就是我实现markdown的过程。

1、下载editor.md插件
git地址:https://github.com/pandao/editor.md.git
npm安装:npm install editor.md
官网:https://pandao.github.io/editor.md/
2、实现一个简单的markdown
<!--引入 editormd  css核心类-->
<link rel="stylesheet"  href="/markdown/css/editormd.min.css" />

<div id="markdown">
    <textarea style="display: none;" name="markdown-doc" id="markdown-doc"> ### Hello Editor.md !</textarea>
</div>
	<!--引入jq核心类-->	
	<script src="https://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
	<!--引入 editormd js核心类-->	
    <script src="/markdown/editormd.min.js" ></script>
    <script>
    $(function() {
        var editor = editormd("markdown", {
            width  : "100%",
            height : "600px",
            placeholder : "请输入要发布的内容...",
            htmlDecode : true,
            htmlDecode : "style,script,iframe",
            path   : "/markdown/lib/",
            //黑色背景
            previewTheme : "dark",

        });
    });

</script>

页面展示

在这里插入图片描述

3、文章管理的实现

文章管理我设计的还不是很完善,目前我只允许后台修改文章、删除文章、不能发布文章,我准备在前台实现文章的发布,后台的管理员用户,只能对文章修改或删除,可能今后还会增加审核或其他功能。

文章页面展示index.ctp代码:

代码比较简单,我就不做注解了

    <div style="margin-bottom: 5px">
        <span class="layui-breadcrumb">
              <a href="">首页</a>
              <a><cite>文章管理</cite></a>
        </span>
    </div>


    <table class="layui-table">
        <thead>
        <tr>
            <th>编号</th>
            <th>文章发布者</th>
            <th>文章标题</th>
            <th>创建时间</th>
            <th>修改时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach($params as $v): ?>
            <tr>
                <td><?=$v['Article']['id']?></td>
                <td><?=$v['u']['username']?></td>
                <td><?=$v['Article']['title']?></td>
                <td><?= date('Y-m-d H:i:s',$v['Article']['create_time'])?></td>
                <td><?=date('Y-m-d H:i:s',$v['Article']['update_time'])?></td>
                <td>
                    <a class="layui-icon layui-icon-edit"  href="/article/showDell?id=<?=$v['Article']['id']?>' "></a>
                    <a class="layui-icon layui-icon-subtraction"></a>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>

    <?php if($pageCount > 1): ?>
    <nav aria-label="Page navigation">
        <ul class="pagination">
            <li <?php if($page == 1 || !$page): ?> class="disabled" <?php endif; ?> >
            <a <?php if($page == 1 || !$page): ?>  οnclick="return false" <?php endif; ?> href="/auth/index?page=<?= $page-1?>"  aria-label="Previous">
            <span aria-hidden="true"  >&laquo;</span>
            </a>
            </li>
            <?php for($i=0;$i<$pageCount;$i++): ?>
            <li <?php if($page == $i+1): ?> class="active" <?php endif; ?>><a href="/auth/index?page=<?= $i+1 ?>"><?=$i+1?></a></li>
            <?php endfor; ?>
            <li <?php if($page == $pageCount): ?> class="disabled" <?php endif; ?> >
            <a <?php if($page == $pageCount): ?> οnclick="return false" <?php endif; ?>  href="/auth/index?page=<?= $page+1?>" aria-label="Next">
            <span aria-hidden="true">&raquo;</span>
            </a>
            </li>
        </ul>
    </nav>
    <?php endif; ?>

控制器ArticleController.php代码的实现:

<?php
/**
 * Created by PhpStorm.
 * User:  wyq
 * Date: 2021/7/21
 * Time: 10:17
 */

class ArticleController extends AppController
{
    public $components = array('publicFunction');

    /*
     * 显示文章列表
     */
    public function index(){
        $page = $_GET['page'] ? $_GET['page'] : 1;
        $limit = 10;
        $count = $this->Article->count();
        $pageCount = ceil($count / $limit);
        $data = $this->Article->findAll($page,$limit);
        if ($data){
            $this->set(array('params'=>$data,'page'=>$page,'pageCount' => $pageCount));
        }
    }

    /*
     * 显示文章修改页面
     */
    public function showDell(){
        $id = $_GET['id'];
        $data = $this->Article->findOne($id);
        $this->set('params',$data);
    }

    /*
     * 处理文章信息
     */
    public function dell(){
        $post = $_POST;
        $post['content'] = $post['markdown-doc'];
        $post['update_time'] = time();
        $data = $this->Article->dell($post);
        if ($data){
            $this->redirect('/article/index');
        }
    }
}

模型Article.php代码的实现:

<?php
/**
 * Created by PhpStorm.
 * User: wyq
 * Date: 2021/7/21
 * Time: 10:18
 */

class Article extends AppModel
{
    /*
     * 显示全部文章数量
     */
    public function count(){
        $this->setSource('article');
        $count = $this->find('count');
        return $count;
    }

    /*
     * 查询全部文章的信息
     */
    public function findAll($page,$limit){
        $this->setSource('article');
        $data = $this->find('all',array(
            'fields' => array('id','user_id','title','content','create_time','update_time','u.id','u.username'),
            'joins'=> array(
                array(
                    'table' => 'users',
                    'alias' =>'u',
                    'type' => 'left',
                    'conditions' => array('user_id = u.id')
                )
            ),
            'limit' => $limit,
            'page' => $page,
        ));
        return $data;
    }

    /*
     * 查询一篇文章的信息
     */
    public function findOne($id){
        $this->setSource('article');
        $data = $this->find('first',array(
            'conditions'=>array('id'=>$id)
        ));
        return $data;
    }


    /*
     * 文章处理
     */
    public function dell($data){
        $this->setSource('article');
        $res = $this->save($data);
        return $res;
    }
}
4、项目中集成markdown插件

文章详情页面show_dell.ctp

<link rel="stylesheet"  href="/markdown/css/editormd.min.css" />

    <div class="layui-form" >
        <form action="/article/dell" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label" >文章标题</label>
                <div class="layui-input-block">
                    <input type="text" id="id" name="id" value="<?=$params['Article']['id']?>" hidden class="layui-input">
                    <input type="text" id="title" name="title" value="<?=$params['Article']['title']?>" placeholder="请输入文章标题" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">文章发布者</label>
                <div class="layui-input-block">

                    <input type="text" id="user_name" name="user_name" placeholder="请输入文章发布者" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" >文章内容</label>
                <div class="layui-input-block">
                    <div id="markdown">
                        <!--展示文章项目内容-->
                        <textarea style="display: none;" name="markdown-doc" id="markdown-doc"><?=$params['Article']['content']?></textarea>
                        <!--这里存储的是markdown生成的html代码-->
                        <textarea style="display: none;" id="markdown-html-code" name="markdown-html-code" ></textarea>
                    </div>
                </div>
            </div>
            <div class="layui-input-block">
                <div class="layui-inline">
                    <button type="submit" id="submit" class="layui-btn" >发布文章</button>
                </div>
                <div class="layui-inline">
                    <a type="button" href="/article/index" class="layui-btn" >返回</a>
                </div>
            </div>
        </form>
    </div>

    <script src="/markdown/editormd.min.js" ></script>

    <script>
    $(function() {
		//初始化一个markdown编辑器
        var editor = editormd("markdown", {
            width  : "100%",
            height : "600px",
            placeholder : "请输入要发布的内容...",
            //遇到script iframe style 等需要转义,防止xss攻击
            htmlDecode : true,
            htmlDecode : "style,script,iframe",
            path   : "/markdown/lib/",
            //黑色
            previewTheme : "dark",

        });
    });

</script>

页面展示
在这里插入图片描述

总结

总的来说实现改代码不算复杂,但是因为第一次使用该编辑器,并且他的官方文档我也觉得不实惠好理解,所以实现过程中还是遇到了一些困难的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Liblog已更新至V1.2.1版本,源码下载地址:https://github.com/livisky/liblog/archive/1.2.1.tar.gz Liblog最新教程已收录在w3cschool(http://www.w3cschool.cn/liblog/) ,欢迎评论和支持! 一.简介 Liblog是一个简单易用的Markdown博客系统,它是基于开源框架thinkJS(使用 ES6/7 特性开发 Node.js 框架)开发的nodejs项目 需要mysql数据库支持,具有管理后台功能,更新博客分为普通文章markdown文章markdown文章只需要导入你写好的Markdown文件即可。它摆脱了在线编辑器排版困难,无法实时预览的缺点,一切都交给Markdown来完成,一篇博客就是一个Markdown文件。同时也支持评论,代码高亮,分类,标签云,留言板、友情链接、系统设置等常用功能。Liblog提供了不同的主题样式,你可以根据自己的喜好配置,如果你想自己制作博客主题,也是非常容易的。Liblog还支持整站静态网页生成,同时有发布相关的配置,使用nginx做反向代理,动静态资源分离,静态缓存等,使您发布后的博客访问秒开。 二.功能特点 一键导入Markdown文章 文章评论 代码高亮 文章内容分页 系统主题设置 响应式布局,支持手机端访问 良好的SEO 登录/注册/个人中心/个人设置 小型bbs,讨论社区 支持cms标签循环 完善的后台及权限控制 三.安装及其他介绍 安装教程:http://www.jsout.com/page/423.html 前台登录:admin@jsout.com 123456 后台登录:admin 123456 安装Liblog之前,请star并fork项目给作者以鼓励。 如果你认可并支持Liblog,还可通过扫描二维码捐赠Liblog(http://www.jsout.com/donate.html) 四.最新版本Liblogv1.2.1更新 【后台管理】添加友情链接管理 1)友情链接增加独立管理菜单,增加审核、排序、隐藏功能 2)友情链接增删查改 3)系统设置-常规设置的友情链接设置项删除 【后台管理】后台主题设置优化,修复设置错误后台打不开的bug 【个人中心】修复个性签名设置bug,积分算法bug修复-forlong401 【后台管理】添加社区标签管理功能-forlong401 【后台管理】添加后台文章标签排序功能 【个人中心】积分排名榜修复,添加缓存 【后台管理】完善导航管理(新窗口打开、排序等)-斯迈欧 【后台管理】添加系统logo设置-斯迈欧 修复部分已知bug 五.感谢 Liblog的成长需要各位亲们支持!感谢你们使用Liblog,感激你们对Liblog的良好建议与Bug反馈。如果你的博客也是基于Liblog,请告知作者,无偿获取技术指导。 前端汇 http://www.jsout.com 维卡冲突世界 http://www.kixeyes.com Liblog QQ群:256687601 作者邮箱:262248861@qq.com 演示网站:https://www.jsout.com 六.更新日志 详见:https://github.com/livisky/liblog/wiki/updateLog 添加静态资源一键打包功能(html,css,javascript) gulp配置 添加开发和生产运行模式 七.运行程序请选择运行模式 //压缩html,css,js 并生成相应目录 npm run compress //运行开发模式,html,css,js均加载未压缩版本 npm run dev //运行生产模式,html,css,js均加载压缩版本 npm run app //首次运行/更新运行前请先编译项目 npm run compile //线上推荐用pm2来运行(先配置好pm2.json) pm2 start pm2.json 标签:liblog  markdown

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值