word.ts
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { UPLOAD_FILE_MAX_LINE } from "@/app/constant";
import { Document, Packer, Paragraph } from "docx";
import { saveAs } from "file-saver";
import * as mammoth from "mammoth";
export function removeDeepThink(content: string) {
const lines = content.split("\n");
let deepThink: string[] = [];
let j = 0;
let isBreak = false;
for (let i = 0; i < lines.length; i++) {
if (lines[i] === "") {
lines.splice(i, 1); // 删除空行
i--;
continue;
}
if (
lines[i].startsWith(">") ||
lines[i].startsWith("2") ||
lines[i].startsWith("3") ||
lines[i].startsWith("4")
) {
if (j !== 0 && lines[i].startsWith(">")) {
lines[i] = lines[i].substring(1);
}
deepThink.push(lines[i]);
lines.splice(i, 1);
j++;
i--;
} else {
break;
}
}
return lines.join("\n");
}
export function exportWord(content: string) {
content = removeDeepThink(content);
// 简单类型检测示例
const isHTML = (text: string): boolean => {
return (
/<html[\s>]/.test(text) &&
/<\/html>/.test(text) &&
/<head>/.test(text) &&
/<body>/.test(text)
);
};
if (isHTML(content)) {
exportHtmlToWord(content);
} else {
exportMarkdownToWord(content);
}
}
export function exportHtmlToWord(content: string) {
let cleanedContent = content.startsWith("```html")
? content.substring(8)
: content;
if (cleanedContent.endsWith("```")) {
cleanedContent = cleanedContent.substring(0, cleanedContent.length - 4);
}
const blob = new Blob([cleanedContent], { type: "application/msword" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "htmldemo.docx";
// 触发点击事件,开始下载
document.body.appendChild(a);
a.click();
// 下载完成后移除临时链接元素
document.body.removeChild(a);
// 释放 Blob URL 对象
URL.revokeObjectURL(url);
}
function exportMarkdownToWord(content: string) {
// 按换行符拆分内容
const lines = content.split(/\r?\n/);
const paragraphs: Paragraph[] = [];
for (const line of lines) {
// 去除 Markdown 标记(#、*等)
const cleanedLine = line
.replace(/^#+\s*/, "")
.replace(/^\*\*\s*|\*\s*/g, "")
.trim();
// 处理空行
if (cleanedLine === "") {
paragraphs.push(new Paragraph(""));
continue;
}
// 添加文本段落
paragraphs.push(new Paragraph(cleanedLine));
}
// 创建 Word 文档对象
const doc = new Document({
sections: [
{
children: paragraphs,
},
],
});
// 转换为 Blob 并下载
Packer.toBlob(doc)
.then((blob) => {
saveAs(blob, "demo.docx");
})
.catch((error) => {
console.error("导出 Word 失败:", error);
});
}
export async function getWordData(file: File) {
try {
const arrayBuffer = await file.arrayBuffer();
const { value, messages } = await mammoth.extractRawText({ arrayBuffer });
return value.slice(0, UPLOAD_FILE_MAX_LINE);
} catch (error) {
console.error("Error extracting Word content:", error);
throw error;
}
}