基于SpringBoot从零构建博客网站 - 集成editor.md开发发布文章功能

发布文章功能里面最重要的就是需要集成富文本编辑器,目前富文本编辑器有很多,例如ueditor,CKEditor、editor.md等。这里守望博客里面是集成的editor.md,因为editor.md是markdown格式,目前markdown由于简洁好用,在各种云笔记、github等中得到了广泛使用。

1、集成editor.md

editor.md是在github上开源,开源地址为:https://github.com/pandao/editor.md,下载其发布的最新版本,即:

001.png

解压后,将相应的文章添加到系统中,即:


002.png

将这些docs、examples、tests文件夹是删除了的,因为这些文件夹里面的文件是不需要用到的。

页面中需要引用的文件如下:

<link href="${rc.contextPath}/static/plugins/editor/css/editormd.min.css" rel="stylesheet">
<script src="${rc.contextPath}/static/plugins/editor/editormd.min.js" type="text/javascript"></script>

页面中只需要引入editor.md中的editormd.min.css和editormd.min.js文件(注意:对于jquery相关的引用是提前就引用好的)。

页面中需要插入富文本编辑器的地方代码如下:

<div class="form-group">
    <div class="col-sm-12" id="article-editormd">
        <textarea style="display:none;"></textarea>
    </div>
</div>

注意标签中有一个id值为article-editormd,后面初始化富文本编辑器时,需要用到。

初始化富文本编辑器的代码如下:

var editor;
$(function () {
    editor = editormd("article-editormd", {
        width: "100%",
        height: 640,
        placeholder: '',
        syncScrolling: "single",
        path: "${rc.contextPath}/static/plugins/editor/lib/",
        saveHTMLToTextarea: true,
        imageUpload: true,
        imageFormats: ["jpg", "jpeg", "gif", "png", "bmp"],
        imageUploadURL: "${rc.contextPath}/upload?_distType=_articleImg",
        imageUploadFileName: "_uploadFile",
        toolbarIcons: "sw"
    });
});

注意:

  • 其中imageUploadFileName参数名,是我扩展的,原生的editor.md是没有这个参数的。扩展这个参数的原因是因为editor.md中对于上传图片的文件名为editormd-image-file,由于守望博客中对于上传模块进行统一抽象,即上传名称统一为_uploadFile,为此就扩展了这个参数进行修改上传图片的文件名。

  • 对于toolbarIcons参数的参数值,也是我扩展的,因为原生的editor.md工具栏的种类只有3种,即:full、simple、mini。这样导致工具栏要么太多了,要么太少了,所以就再定义一个sw,里面工具就是自己想要的工具,即:

    sw : [
        "undo", "redo", "|",
        "bold", "del", "italic", "quote", "|",
        "h1", "h2", "h3", "h4", "h5", "h6", "|",
        "list-ul", "list-ol", "hr", "|",
        "link", "image", "code", "preformatted-text", "code-block", "table", "html-entities", "|",
        "watch", "preview", "clear", "search"
    ]
    

最终富文本编辑器页面效果如下:


003.png

2、开发布文章功能

处理文章图片的UploadGroupLogoHandler,内容为:

/**
 * 上传专栏Logo处理类
 *
 * @author lzj
 * @since 1.0
 * @date [2019-07-23]
 */
@Slf4j
@Component("_groupLogo")
public class UploadGroupLogoHandler implements IUploadHandler {

    @Resource(name = "configCache")
    private ICache<Config> configCache;

    @Override
    public Object upload(MultipartFile file, String distType, String userId) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            // 获取图片的原始名称
            String originalName = file.getOriginalFilename();

            // 判断图片的类型
            if (!(originalName.endsWith(".jpg") || originalName.endsWith(".JPG") || originalName.endsWith(".png") || originalName.endsWith(".PNG") || originalName.endsWith(".gif") || originalName.endsWith(".GIF") || originalName.endsWith(".jpeg") || originalName.endsWith(".JPEG"))) {
                throw new TipException("您上传的图片类型有误,请上传格式为jpg、png或gif");
            }

            // 获取图片的大小
            long fileSize = file.getSize();

            // 图片大小不能超过2M, 2M = 2 * 1024 * 1024B = 2097152B
            if (fileSize > 2097152L) {
                throw new TipException("您上传的图片超过2M");
            }

            Config config = configCache.get(Config.CONFIG_IMG_GROUP_LOGO_PATH);
            // 保存头像的根目录
            String basePath = config.getConfigValue();
            if (!(basePath.endsWith("/") || basePath.endsWith("\\"))) {
                basePath += "/";
            }

            // 根据当前时间构建yyyyMM的文件夹,建立到月的文件夹
            String dateDirName = DateUtil.date2Str(new Date(), DateUtil.YEAR_MONTH_FORMAT);
            basePath += dateDirName;

            File imageDir = new File(basePath);
            if (!imageDir.exists()) {
                imageDir.mkdirs();
            }

            String fileNewName = IdGenarator.guid() + originalName.substring(originalName.lastIndexOf("."));
            FileUtil.copy(file.getInputStream(), new FileOutputStream(new File(imageDir, fileNewName)));

            result.put("url", dateDirName + "/" + fileNewName);
            result.put("msg", "上传成功");
        } catch (TipException e) {
            result.put("url", "");
            result.put("msg", e.getMessage());
        } catch (Exception e) {
            log.error("上传失败", e);
            result.put("url", "");
            result.put("msg", "上传失败");
        }
        return result;
    }

    @Override
    public void download(String fileId, HttpServletResponse response) throws Exception {
    }

    @Override
    public Object list(String distType, String userId) throws Exception {
        return null;
    }
}

加载出发布文章页面核心代码为:

/**
 * 加载出新增文章页面
 *
 * @param model
 * @param request
 * @param session
 * @return
 */
@RequestMapping(value = "/user/article/add", method = RequestMethod.GET)
public String add(Model model, HttpServletRequest request, HttpSession session) {
    // 获取登录信息
    User tempUser = (User) session.getAttribute(Const.SESSION_USER);
    String userId = tempUser.getUserId();

    // 获取用户信息
    User user = userService.getById(userId);

    // 构建专栏的查询条件
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("creator", user.getUserId());
    params.put("status", Group.STATUS_SUCCESS);

    List<Group> groups = groupService.list(new QueryWrapper<Group>().allEq(params).orderByDesc("createTime"));

    model.addAttribute("user", user);
    model.addAttribute("groups", groups);
    return Const.BASE_INDEX_PAGE + "blog/article/add";
}

处理发布文章的核心代码为:

/**
 * 新增文章
 *
 * @param request
 * @param session
 * @return
 */
@RequestMapping(value = "/user/article/add", method = RequestMethod.POST)
@ResponseBody
public Result add(HttpServletRequest request, HttpSession session) {
    Result result = new Result();
    try {
        // 接收参数
        String groupId = request.getParameter("groupId");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String tag = request.getParameter("tag");
        String description = request.getParameter("description");
        String typeStr = request.getParameter("type");
        String canTopStr = request.getParameter("canTop");
        String canCommentStr = request.getParameter("canComment");

        // 校验参数
        if (StringUtils.isEmpty(title) || StringUtils.isEmpty(content) || StringUtils.isEmpty(description)) {
            throw new TipException("缺少必要参数");
        }

        int type = 0;
        int canTop = 0;
        int canComment = 1;
        try {
            type = Integer.parseInt(typeStr);
            canTop = Integer.parseInt(canTopStr);
            canComment = Integer.parseInt(canCommentStr);
        } catch (Exception e) {
            throw new TipException("参数类型错误");
        }

        // 去html相关标签
        description = StringUtil.replaceHtmlTags(description);

        // 客户端ip
        String ip = HttpUtil.getIpAddr(request);

        // 获取session中的用户信息
        User tempUser = (User) session.getAttribute(Const.SESSION_USER);
        String userId = tempUser.getUserId();

        // 封装文章信息
        Article article = new Article();
        article.setArticleId(IdGenarator.guid());
        article.setGroupId(groupId);
        article.setTitle(title);
        article.setContent(content);
        article.setDescription(description);
        article.setType(type);
        article.setCanTop(canTop);
        article.setCanComment(canComment);
        article.setViewCount(0L);
        article.setGoodNum(0L);
        article.setBadNum(0L);
        article.setCreateTime(new Date());
        article.setCreateIp(ip);
        article.setUserId(userId);

        // 保存文章
        articleService.create(article, tag);

        result.setCode(Result.CODE_SUCCESS);
        result.setMsg("发布文章成功");
    } catch (TipException e) {
        result.setCode(Result.CODE_EXCEPTION);
        result.setMsg(e.getMessage());
    } catch (Exception e) {
        log.error("新增文章失败", e);
        result.setCode(Result.CODE_EXCEPTION);
        result.setMsg("新增文章失败");
    }
    return result;
}

完整的发布文章页面如下:


004.png

关注我

以你最方便的方式关注我:
微信公众号:


架构与我
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容

  • 前言 本篇主要是使用Java语言结合开源editor.md进行开发markdown论坛功能,具体请看下图,如果不是...
    君哥聊编程阅读 3,870评论 9 12
  • 1 外婆去世已有二十余年,记忆已被时光磨的越发模糊,怕是一场梦,却又真实存在过。 外婆会讲故事,会做很好吃的菜,会...
    阿梁哥阅读 596评论 0 3
  • 没有谁的生活是容易的,努力本就是人生常态。 平庸的人,总是强调自己在努力,然后碌碌无为;成功的人则是默默耕耘,最后...
    邹建冰阅读 196评论 0 1