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

使用 Markdown 编辑应用说明

目前為 2022-10-03 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();
})();