render-mixins.js 5.5 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1 2 3
import {
  getFilteredTags,
  getLinkTags,
璃白.'s avatar
璃白. 已提交
4
  addLanguageClass,
郭维嘉 已提交
5
  formatElements,
6
  linkTypeSpiltStr,
璃白.'s avatar
璃白. 已提交
7
  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);
郭维嘉 已提交
41
      const { callUserList, userHtml } = formatElements(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 84 85 86 87 88
          let out =
            '<p class="md_img_container"><img src="' +
            href +
            '" alt="' +
            text +
            '"';
璃白.'s avatar
璃白. 已提交
89
          if (title) {
璃白.'s avatar
fix  
璃白. 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102
            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
璃白. 已提交
103
                out += 'style="display:inline-block;"';
璃白.'s avatar
fix  
璃白. 已提交
104 105 106 107 108 109 110 111 112 113 114
              }
              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
璃白. 已提交
115
          }
璃白.'s avatar
璃白. 已提交
116
          out += "/></p>";
璃白.'s avatar
璃白. 已提交
117
          return out;
璃白.'s avatar
璃白. 已提交
118
        },
璃白.'s avatar
璃白. 已提交
119
        link(href, title, text) {
璃白.'s avatar
璃白. 已提交
120
          if (!href && !title) return "";
璃白.'s avatar
璃白. 已提交
121 122 123
          if (href === null) {
            return text;
          }
124
          const linkTypeArr = href.split(linkTypeSpiltStr);
璃白.'s avatar
璃白. 已提交
125 126
          const linkType = linkTypeArr[1];
          href = linkTypeArr[0];
127
          text = text.split(linkTypeSpiltStr)[0];
璃白.'s avatar
璃白. 已提交
128 129 130 131 132 133 134 135 136
          let invalidText = "";
          if (href === text) {
            const invalidRule = /[)】}\]]*$/;
            if (href.match(invalidRule)) {
              invalidText = href.match(invalidRule)[0];
              href = href.replace(invalidRule, "");
              text = href;
            }
          }
璃白.'s avatar
璃白. 已提交
137

璃白.'s avatar
璃白. 已提交
138 139 140 141
          let out = '<a href="' + href + '"';
          if (title) {
            out += ' title="' + title + '"';
          }
璃白.'s avatar
璃白. 已提交
142 143 144
          if (linkType) {
            out += ' data-type="' + linkType + '"';
          }
璃白.'s avatar
璃白. 已提交
145 146 147 148 149 150
          out += ">" + text + "</a>";
          if (invalidText) {
            out += invalidText;
          }
          return out;
        },
璃白.'s avatar
璃白. 已提交
151
        text(text) {
璃白.'s avatar
璃白. 已提交
152 153 154 155 156 157 158 159 160
          const newText = text
            .replace(/(\@\S+\s{0,1})/g, function(val) {
              const user = _this.getUserByName(val.slice(1).trim());
              return `<a type="user" download data-user="${user &&
                user.username}" href="${(user && user.url) || "javascript:void(0)"}" class="md_call_user">${val}</a>`;
            })
            .replace(/^\s{2,3}(.+)/, function(val) {
              return `<span style="display:inline-block;text-indent:2em;">${val}</span>`;
            });
璃白.'s avatar
璃白. 已提交
161
          return newText;
璃白.'s avatar
璃白. 已提交
162 163 164 165 166 167
        }
      };
      marked.use({ renderer });
    }
  }
};