index.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import Message from './Message/index.vue'
  2. import { marked } from "marked";
  3. async function exportDoc(text:any) {
  4. const htmlContent = convertMarkdownToHtml(text) as string;
  5. let title;
  6. const titleStart = htmlContent.indexOf("<p>");
  7. const titleEnd = htmlContent.indexOf("</p>");
  8. if (typeof titleStart == "number" && titleEnd) {
  9. const data = htmlContent.substring(titleStart + 3, titleEnd);
  10. if (data.includes("<strong>")) {
  11. const titleS = data.indexOf("<strong>");
  12. const titleE = data.indexOf("</strong>");
  13. title = data.substring(titleS + 8, titleE);
  14. } else {
  15. title = data;
  16. }
  17. }
  18. // 将富文本内容拼接为一个完整的html
  19. const value = `<!DOCTYPE html>
  20. <html lang="zh-CN">
  21. <head>
  22. <meta charset="UTF-8">
  23. <title>文档</title>
  24. </head>
  25. <body>
  26. ${htmlContent}
  27. </body>
  28. </html>`;
  29. // const title= htmlContent
  30. const data = new Blob([value]);
  31. const a = document.createElement("a");
  32. a.href = window.URL.createObjectURL(data);
  33. a.setAttribute("download", `${title ? title : "导出内容"}.docx`);
  34. // a.setAttribute("download", ".docx");
  35. a.click();
  36. // 下载后将标签移除
  37. a.remove();
  38. }
  39. // 将 Markdown 转换为 HTML
  40. function convertMarkdownToHtml(markdown: any) {
  41. return marked.parse(markdown);
  42. }
  43. export { Message,exportDoc }