render-mixins.js 5.0 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1 2 3
import {
  getFilteredTags,
  getLinkTags,
璃白.'s avatar
璃白. 已提交
4
  addLanguageClass,
璃白.'s avatar
璃白. 已提交
5 6 7
  addLinkTarget,
  renderVideo,
  getfilesize
璃白.'s avatar
璃白. 已提交
8 9 10 11 12
} from "@/assets/js/utils";
import marked from "marked";
import DOMPurify from "dompurify";
export default {
  methods: {
璃白.'s avatar
璃白. 已提交
13
    async transferMarkdown(val) {
璃白.'s avatar
璃白. 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
      this.rerender();
      marked.setOptions({
        breaks: true,
        gfm: true,
        langPrefix: "language-",
        highlight: function(code, lang, callback) {
          let html = require("highlight.js").highlightAuto(code).value;
          return html;
        }
      });
      const str = val + "";
      const html = marked(str); // 解析markdown
      const virtualDom = addLanguageClass(html); // 如果没指定语言,添加默认语言
      const cleanHtml = DOMPurify.sanitize(virtualDom.innerHTML, {
        FORBID_TAGS: [
          "style",
          "script",
          "select",
          "option",
          "textarea",
          "form",
          "button"
        ]
      }); // 去除标签
      const filteredTags = getFilteredTags(html, cleanHtml); // 计算是否有标签被过滤
      // 链接转换为卡片
      const { vDom, links } = getLinkTags(this.id, cleanHtml);
璃白.'s avatar
璃白. 已提交
41
      const { callUserList, userHtml } = addLinkTarget(cleanHtml);
璃白.'s avatar
璃白. 已提交
42
      // const videoHtml = await renderVideo(this.id, userHtml);
璃白.'s avatar
璃白. 已提交
43
      this.$emit("callUserList", callUserList);
璃白.'s avatar
璃白. 已提交
44
      this.$emit("getFilteredTags", filteredTags);
璃白.'s avatar
璃白. 已提交
45
      this.$emit("update:html", userHtml);
璃白.'s avatar
璃白. 已提交
46 47 48
      if (links.length) this.$emit("renderLinksHtml", { vDom, links });
    },
    rerender() {
璃白.'s avatar
璃白. 已提交
49
      const _this = this;
璃白.'s avatar
璃白. 已提交
50 51 52 53 54 55 56
      const renderer = {
        image(href, title, text) {
          if (href === null) {
            return text;
          }
          // ![file](...)渲染文件,只可以下载
          if (text === "file") {
璃白.'s avatar
璃白. 已提交
57 58 59 60 61
            const size = title.split(" ").pop();
            const name = title
              .split(" ")
              .slice(0, -1)
              .join(" ");
璃白.'s avatar
璃白. 已提交
62 63 64 65
            return `<div id="md_file_card" class="md_file_card">
              <div class="md_flex_card">
                <span class="md_file_img icon iconfont icon-doc"></span>
                <span class="flex-1">
璃白.'s avatar
璃白. 已提交
66 67
                  <span class="md_file_title">${name}</span>
                  <span class="md_file_desc">${getfilesize(size)}</span>
璃白.'s avatar
璃白. 已提交
68 69
                </span>
                <span class="md_file_controls">
璃白.'s avatar
璃白. 已提交
70
                <a href="${href}" type="file" download="${name}" class="md_file_download icon iconfont icon-xiazai"></a>
璃白.'s avatar
璃白. 已提交
71 72
                </span>
              </div>
璃白.'s avatar
璃白. 已提交
73 74
            </div>`;
          }
璃白.'s avatar
璃白. 已提交
75
          if (text === "video") {
璃白.'s avatar
璃白. 已提交
76
            return `<p class="md_video"><video
璃白.'s avatar
璃白. 已提交
77
                  class="video-js"
璃白.'s avatar
璃白. 已提交
78
                  preload="none"
璃白.'s avatar
璃白. 已提交
79
                  src="${href}"
璃白.'s avatar
璃白. 已提交
80
                   ></video></p>`;
璃白.'s avatar
璃白. 已提交
81
          }
璃白.'s avatar
璃白. 已提交
82
          // ![img](...)渲染图片
璃白.'s avatar
璃白. 已提交
83
          let out = '<p class="md_img_container"><img src="' + href + '" alt="' + text + '"';
璃白.'s avatar
璃白. 已提交
84
          if (title) {
璃白.'s avatar
fix  
璃白. 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97
            const reg_title = /(\%([\u4E00-\u9FA5\w.]+)\s??)/;
            const reg_align = /(\#([a-zA-Z]+)\s??)/;
            const reg_width = /(\=(\d+)\*?\s??)/;
            const reg_height = /([\*|x](\d+)\s??)/;

            if (reg_title.exec(title)) {
              var t = reg_title.exec(title)[2];
              out += ' title="' + t + '"';
            }

            if (reg_align.exec(title)) {
              var a = reg_align.exec(title)[2];
              if (a === "center") {
璃白.'s avatar
璃白. 已提交
98
                out += 'style="display:inline-block;"';
璃白.'s avatar
fix  
璃白. 已提交
99 100 101 102 103 104 105 106 107 108 109
              }
              out += ' align="' + a + '"';
            }
            if (reg_width.exec(title)) {
              var w = reg_width.exec(title)[2];
              out += ' width="' + w + 'px"';
            }
            if (reg_height.exec(title)) {
              var h = reg_height.exec(title)[2];
              out += ' height="' + h + 'px"';
            }
璃白.'s avatar
璃白. 已提交
110
          }
璃白.'s avatar
璃白. 已提交
111
          out += "/></p>";
璃白.'s avatar
璃白. 已提交
112
          return out;
璃白.'s avatar
璃白. 已提交
113
        },
璃白.'s avatar
璃白. 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126
        link(href, title, text) {
          if (href === null) {
            return text;
          }
          let invalidText = "";
          if (href === text) {
            const invalidRule = /[)】}\]]*$/;
            if (href.match(invalidRule)) {
              invalidText = href.match(invalidRule)[0];
              href = href.replace(invalidRule, "");
              text = href;
            }
          }
璃白.'s avatar
璃白. 已提交
127

璃白.'s avatar
璃白. 已提交
128 129 130 131 132 133 134 135 136 137
          let out = '<a href="' + href + '"';
          if (title) {
            out += ' title="' + title + '"';
          }
          out += ">" + text + "</a>";
          if (invalidText) {
            out += invalidText;
          }
          return out;
        },
璃白.'s avatar
璃白. 已提交
138
        text(text) {
璃白.'s avatar
璃白. 已提交
139
          const newText = text.replace(/(\@\S+\s{0,1})/g, function(val) {
璃白.'s avatar
璃白. 已提交
140
            const user = _this.getUserByName(val.slice(1).trim());
璃白.'s avatar
璃白. 已提交
141 142
            return `<a type="user" download data-user="${user &&
              user.username}" href="${(user && user.url) || "javascript:void(0)"}" class="md_call_user">${val}</a>`;
璃白.'s avatar
璃白. 已提交
143
          });
璃白.'s avatar
璃白. 已提交
144
          return newText;
璃白.'s avatar
璃白. 已提交
145 146 147 148 149 150
        }
      };
      marked.use({ renderer });
    }
  }
};