「Z-Blog」使用 Markdown 编辑应用介绍

使用 Markdown 编辑应用说明

当前为 2022-10-03 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         「Z-Blog」使用 Markdown 编辑应用介绍
// @namespace    https://www.wdssmq.com/
// @version      0.1.1
// @author       wdssmq
// @description  使用 Markdown 编辑应用说明
// @null     ----------------------------
// @contributionURL    https://github.com/wdssmq#%E4%BA%8C%E7%BB%B4%E7%A0%81
// @contributionAmount 5.93
// @null     ----------------------------
// @link     https://github.com/wdssmq/userscript
// @link     https://afdian.net/@wdssmq
// @link     https://greasyfork.org/zh-CN/users/6865-wdssmq
// @null     ----------------------------
// @match        https://app.zblogcn.com/zb_system/admin/edit.php?*
// @require      https://unpkg.com/turndown/dist/turndown.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/marked/4.1.1/marked.min.js
// @grant        none
// ==/UserScript==

/* jshint esversion: 8 */
/* globals editor_api marked TurndownService sContent */
/* eslint no-useless-escape: 0 */
(function () {
  "use strict";

  const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

  let fnPost;

  function fnPostEvet() {
    fnPost = $("#btnPost")[0].onclick;
    // console.log(fnPost);
    $("#btnPost").after("<input class=\"button\" style=\"width:180px;height:38px;\" type=\"submit\" value=\"提交\" id=\"btnPost2\"\">").hide();
    $("#btnPost2")[0].onclick = function () {
      let oBody = editor_api.editor.content.get();
      // console.log(oBody);
      if (oBody.indexOf("isMarkdown")>-1){
        oBody = oBody.replace(/<\/p>[<>\/pbr\n\s]+<p>/g,"\n\n");
        oBody = oBody.replace(/<p>|isMarkdown|<\/p>/g,"\n\n");
        // console.log(oBody);
        // console.log(marked(oBody));
        editor_api.editor.content.put(marked(oBody));
        return fnPost();
      }
    };
  }

  function fnMain() {
    console.log("start");
    const turndownService = new TurndownService({ headingStyle: "atx" });
    const oBody = editor_api.editor.content.get();
    fnPostEvet();
    if (oBody.indexOf("markdown-here-wrapper") > -1) {
      return;
    }
    if (oBody.indexOf("isMarkdown") > -1) {
      return;
    }
    // let markdown = turndownService.turndown("<h1>Hello world!</h1>");
    // console.log(markdown);
    let markdown = turndownService.turndown(oBody);
    console.log(markdown);
    const arrHtml = ["<p>", "", "</p>","<p>isMarkdown</p>"];
    arrHtml[1] = markdown.replace(/\n\n/g, "</p><p></p><p>");
    editor_api.editor.content.put(arrHtml.join(""));
  }

  async function fnStart() {
    if (!editor_api || $("#editor_content").length == 0 || sContent == "") {
      await sleep(3000);
      fnStart();
    } else {
      fnMain();
    }
  }
  fnStart();
})();