yuque-html-exporter

export yuque document to html.

// ==UserScript==
// @name         yuque-html-exporter
// @namespace    https://github.com/zenonux
// @version      1.5
// @description  export yuque document to html.
// @author       换个头像心好累
// @license      GPL-3.0 License
// @match        *://*.yuque.com/*
// @icon        data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAAsTAAALEwEAmpwYAAAE72lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNi4wLWMwMDMgMTE2LmRkYzdiYzQsIDIwMjEvMDgvMTctMTM6MTg6MzcgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLyIgeG1sbnM6ZGM9Imh0dHA6Ly9wdXJsLm9yZy9kYy9lbGVtZW50cy8xLjEvIiB4bWxuczpwaG90b3Nob3A9Imh0dHA6Ly9ucy5hZG9iZS5jb20vcGhvdG9zaG9wLzEuMC8iIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdEV2dD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlRXZlbnQjIiB4bXA6Q3JlYXRvclRvb2w9IkFkb2JlIFBob3Rvc2hvcCAyMS4yIChXaW5kb3dzKSIgeG1wOkNyZWF0ZURhdGU9IjIwMjMtMDgtMDRUMTU6Mjk6NTQrMDg6MDAiIHhtcDpNb2RpZn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7IhjniFdklOvkTC52Pn3qwij74eLHDiyEPOcrKC0+KhOFK8X1BEd9OPViA3z1Wzl/O/Tj3O544/zP6ZXY/2etGbe5H7PHZpornkKKd4/VWeijS6M1K9FzjTLlpc0rUw+i5mebLKSrrOwMX1s7zmOVIZ+LXiny1ujnPJGh9/+vNn5mcW7p2fnbvTsKfsEomHV/ndYHUTFflMpO6aWKNPCpy/VEbEGpH6jBdHy8TqigepVNJvw777ocPO/sL5X33uDz/52Prha67fcvVncvzG8Pki22uKc8v63LlY3C9H6q4R0V9UkSc29d+btJ5GvLC+zChPGLhe/aJz85S4fJxrRd+pPtFz8S8GZ9ccS0/+8kw0PJf59OJudoH2unw8XXJ/WVU/5YxPBbjTCO804x8ZVqabVUxfzB9GfZYnK7w8OstM1C8SrvrhS/GnCCL6bafyNXXuT/sSP1J9ujTvZoo3wV7j2l/Xr8erPNQJjwTRR8qixXUBuxn0RsGuBdsXabRtnCczz6+d7EUqxBqlCCMnsuxEj6rIM4J8UzX6u0j0kKghKsUn8K8S6hdq/x/jkkKcSKSyLAAAAABJRU5ErkJggg==
// @require    https://cdn.bootcdn.net/ajax/libs/marked/4.3.0/lib/marked.umd.min.js
// @grant      none
// ==/UserScript==

(function () {
  "use strict";

  init();

  function init() {
    const previewPanel = _createPreviewPanel();
    const btn = _createExportButton();
    btn.addEventListener("click", () => {
      const mdDownloadUrl = _getMarkdownFileUrl();
      _parseMarkdownToHtml(mdDownloadUrl).then((docHtml) => {
        _preview(previewPanel, docHtml);
        _copyToClipboard(docHtml).then(() => {
          const parser = new DOMParser();
          const doc = parser.parseFromString(docHtml, "text/html");
          let yuqueLinksCount = _isHasYuqueLink(doc);
          let tableCount = _isHasTableElement(doc);
          let isHasAsterisk = _isHasAsterisk(doc);
          if (yuqueLinksCount || tableCount || isHasAsterisk) {
            _showToast({
              title: "已导出至剪切板",
              message: `检测到
              ${isHasAsterisk ? "加粗异常," : ""}
              ${yuqueLinksCount ? yuqueLinksCount + "个语雀内链," : ""}
              ${tableCount ? tableCount + "个表格," : ""}请手动处理。`,
            });
          } else {
            _showToast({
              title: "已导出至剪切板",
            });
          }
        });
      });
    });
    _toggleElementVisible([btn, previewPanel]);
    _onRouterChange(() => {
      previewPanel.style.border = "none";
      previewPanel.innerHTML = "";
      _toggleElementVisible([btn, previewPanel]);
    });
  }

  function _onRouterChange(handler) {
    history.pushState = _bindEventListener("pushState");
    history.replaceState = _bindEventListener("replaceState");
    window.addEventListener("replaceState", handler);
    window.addEventListener("pushState", handler);
  }

  function _preview(panel, str) {
    panel.style.border = "solid 1px #00b96b";
    panel.innerHTML = str;
  }

  function _parseMarkdownToHtml(url) {
    return window
      .fetch(url)
      .then((res) => res.text())
      .then((html) => {
        const parser = new DOMParser();
        const doc = parser.parseFromString(
          window.marked.parse(_parseMarkdownStrong(html)),
          "text/html"
        );
        const docHtml = _addTableWrapDiv(doc);
        return docHtml;
      });
  }

  function _createPreviewPanel() {
    let rightPanel = document.createElement("div");
    rightPanel.style =
      "position:fixed;top:62px;bottom:10px;right:100px;z-index:100;width:600px;overflow-y:scroll;padding:10px;";
    document.body.append(rightPanel);
    return rightPanel;
  }

  function _getMarkdownFileUrl() {
    return (
      window.location.href +
      "/markdown?attachment=true&latexcode=false&anchor=true&linebreak=true"
    );
  }

  function _createExportButton() {
    let btn = document.createElement("button");
    btn.innerText = "导出html";
    btn.style =
      "position:fixed;top:5px;left:50%;transform:translate(-50%,0);z-index:10000;background:#00b96b;padding:10px 14px;border:none;color:#fff;cursor:pointer;";
    document.body.appendChild(btn);
    return btn;
  }

  function _parseMarkdownStrong(md) {
    // 去除语雀md为"默认加粗如 **"** -> "
    // 为加粗左界符尾部去除空格,右界符去除头部空格,添加尾部空格,避免加粗不生效
    return md
      .replace(/\*\*\s?"\s?\*\*/g, '"')
      .replace(/\*\*[^\*]*\*\*/g, (match) => {
        let text = match.replace(/\*\*/g, "");
        return "**" + text.trim() + "** ";
      });
  }

  function _isHasAsterisk(doc) {
    return /\*\*/g.test(doc.body.innerHTML);
  }

  function _isHasYuqueLink(doc) {
    let aList = doc.querySelectorAll("a");
    let yuqueLinks = aList
      ? Array.from(aList).filter((v) => v.href.includes("yuque.com"))
      : [];
    return yuqueLinks.length;
  }

  function _isHasTableElement(doc) {
    let tableList = doc.querySelectorAll("table");
    return tableList?.length || 0;
  }

  function _addTableWrapDiv(doc) {
    doc.querySelectorAll("table").forEach((child) => {
      const parent = document.createElement("div");
      parent.style.overflowX = "auto";
      child.parentNode.replaceChild(parent, child);
      parent.appendChild(child);
    });
    return doc.body.innerHTML;
  }

  function _toggleElementVisible(elArr) {
    elArr.forEach((el) => {
      if (el) {
        el.style.display = "none";
      }
      setTimeout(() => {
        let isAtDetailPage = document.querySelector(".ne-doc-major-viewer");
        if (!isAtDetailPage) {
          if (el) {
            el.style.display = "none";
          }
        } else {
          if (el) {
            el.style.display = "block";
          }
        }
      }, 2000);
    });
  }

  function _bindEventListener(type) {
    const historyEvent = history[type];
    return function () {
      const newEvent = historyEvent.apply(this, arguments);
      const e = new Event(type);
      e.arguments = arguments;
      window.dispatchEvent(e);
      return newEvent;
    };
  }

  function _showToast(opts) {
    let { title, message } = opts;
    let template = `
    <div>${title}</div>
  `;
    let templateMsg = `
      <div>${title}</div>
      <div style="color:red;">${message}</div>
    `;
    let toast = document.createElement("div");
    toast.innerHTML = message ? templateMsg : template;
    toast.style =
      "position:fixed;top:16%;left:50%;transform:translate(-50%,-24%);z-index:20000;background:#67C23A;padding:30px 45px;border-radius:4px;color:#fff;text-align:center;";
    document.body.appendChild(toast);
    setTimeout(() => {
      document.body.removeChild(toast);
    }, 2000);
  }

  function _copyToClipboard(text) {
    return navigator.clipboard.writeText(text);
  }
})();