基于SpringBoot從零構(gòu)建博客網(wǎng)站 - 集成editor.md開發(fā)發(fā)布文章功能

發(fā)布文章功能里面最重要的就是需要集成富文本編輯器赡磅,目前富文本編輯器有很多滞项,例如ueditor埠况,CKEditor、editor.md等纵诞。這里守望博客里面是集成的editor.md,因為editor.md是markdown格式培遵,目前markdown由于簡潔好用浙芙,在各種云筆記、github等中得到了廣泛使用籽腕。

1嗡呼、集成editor.md

editor.md是在github上開源,開源地址為:https://github.com/pandao/editor.md皇耗,下載其發(fā)布的最新版本南窗,即:

001.png

解壓后,將相應(yīng)的文章添加到系統(tǒng)中郎楼,即:


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相關(guān)的引用是提前就引用好的)。

頁面中需要插入富文本編輯器的地方代碼如下:

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

注意標(biāo)簽中有一個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參數(shù)名,是我擴(kuò)展的芙粱,原生的editor.md是沒有這個參數(shù)的祭玉。擴(kuò)展這個參數(shù)的原因是因為editor.md中對于上傳圖片的文件名為editormd-image-file,由于守望博客中對于上傳模塊進(jìn)行統(tǒng)一抽象宅倒,即上傳名稱統(tǒng)一為_uploadFile攘宙,為此就擴(kuò)展了這個參數(shù)進(jìn)行修改上傳圖片的文件名。

  • 對于toolbarIcons參數(shù)的參數(shù)值拐迁,也是我擴(kuò)展的蹭劈,因為原生的editor.md工具欄的種類只有3種,即:full线召、simple铺韧、mini。這樣導(dǎo)致工具欄要么太多了缓淹,要么太少了哈打,所以就再定義一個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料仗、開發(fā)布文章功能

處理文章圖片的UploadGroupLogoHandler,內(nèi)容為:

/**
 * 上傳專欄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 += "/";
            }

            // 根據(jù)當(dāng)前時間構(gòu)建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;
    }
}

加載出發(fā)布文章頁面核心代碼為:

/**
 * 加載出新增文章頁面
 *
 * @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);

    // 構(gòu)建專欄的查詢條件
    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";
}

處理發(fā)布文章的核心代碼為:

/**
 * 新增文章
 *
 * @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 {
        // 接收參數(shù)
        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");

        // 校驗參數(shù)
        if (StringUtils.isEmpty(title) || StringUtils.isEmpty(content) || StringUtils.isEmpty(description)) {
            throw new TipException("缺少必要參數(shù)");
        }

        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("參數(shù)類型錯誤");
        }

        // 去html相關(guān)標(biāo)簽
        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("發(fā)布文章成功");
    } 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;
}

完整的發(fā)布文章頁面如下:


004.png

關(guān)注我

以你最方便的方式關(guān)注我:
微信公眾號:


架構(gòu)與我
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末躏吊,一起剝皮案震驚了整個濱河市氛改,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌比伏,老刑警劉巖胜卤,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異赁项,居然都是意外死亡葛躏,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門悠菜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來紫新,“玉大人,你說我怎么就攤上這事李剖∶⒙剩” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵篙顺,是天一觀的道長偶芍。 經(jīng)常有香客問我充择,道長,這世上最難降的妖魔是什么匪蟀? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任椎麦,我火速辦了婚禮,結(jié)果婚禮上材彪,老公的妹妹穿的比我還像新娘观挎。我一直安慰自己,他們只是感情好段化,可當(dāng)我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布嘁捷。 她就那樣靜靜地躺著,像睡著了一般显熏。 火紅的嫁衣襯著肌膚如雪雄嚣。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天喘蟆,我揣著相機(jī)與錄音缓升,去河邊找鬼。 笑死蕴轨,一個胖子當(dāng)著我的面吹牛港谊,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播橙弱,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼封锉,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了膘螟?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤碾局,失蹤者是張志新(化名)和其女友劉穎荆残,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體净当,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡内斯,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了像啼。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片俘闯。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖忽冻,靈堂內(nèi)的尸體忽然破棺而出真朗,到底是詐尸還是另有隱情,我是刑警寧澤僧诚,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布遮婶,位于F島的核電站蝗碎,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏旗扑。R本人自食惡果不足惜蹦骑,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望臀防。 院中可真熱鬧眠菇,春花似錦、人聲如沸袱衷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽祟昭。三九已至缕坎,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間篡悟,已是汗流浹背谜叹。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留搬葬,地道東北人荷腊。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像急凰,于是被迫代替她去往敵國和親女仰。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 前言 本篇主要是使用Java語言結(jié)合開源editor.md進(jìn)行開發(fā)markdown論壇功能抡锈,具體請看下圖疾忍,如果不是...
    君哥聊編程閱讀 4,028評論 9 12
  • 1 外婆去世已有二十余年一罩,記憶已被時光磨的越發(fā)模糊,怕是一場夢撇簿,卻又真實存在過聂渊。 外婆會講故事,會做很好吃的菜四瘫,會...
    阿梁哥閱讀 761評論 0 3
  • 沒有誰的生活是容易的汉嗽,努力本就是人生常態(tài)。 平庸的人找蜜,總是強(qiáng)調(diào)自己在努力饼暑,然后碌碌無為;成功的人則是默默耕耘,最后...
    鄒建冰閱讀 199評論 0 1