12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- import Message from './Message/index.vue'
- import { marked } from "marked";
- async function exportDoc(text:any) {
- const htmlContent = convertMarkdownToHtml(text) as string;
- let title;
- const titleStart = htmlContent.indexOf("<p>");
- const titleEnd = htmlContent.indexOf("</p>");
- if (typeof titleStart == "number" && titleEnd) {
- const data = htmlContent.substring(titleStart + 3, titleEnd);
- if (data.includes("<strong>")) {
- const titleS = data.indexOf("<strong>");
- const titleE = data.indexOf("</strong>");
- title = data.substring(titleS + 8, titleE);
- } else {
- title = data;
- }
- }
- // 将富文本内容拼接为一个完整的html
- const value = `<!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <title>文档</title>
- </head>
- <body>
- ${htmlContent}
- </body>
- </html>`;
- // const title= htmlContent
- const data = new Blob([value]);
- const a = document.createElement("a");
- a.href = window.URL.createObjectURL(data);
- a.setAttribute("download", `${title ? title : "导出内容"}.docx`);
- // a.setAttribute("download", ".docx");
- a.click();
- // 下载后将标签移除
- a.remove();
- }
- // 将 Markdown 转换为 HTML
- function convertMarkdownToHtml(markdown: any) {
- return marked.parse(markdown);
- }
- export { Message,exportDoc }
|