相关性代码调整

This commit is contained in:
2026-06-24 16:52:24 +08:00
parent 5a1bbb0894
commit 5260ca8ea5
29 changed files with 6330 additions and 568 deletions

View File

@@ -6,7 +6,7 @@ import mammoth from "mammoth";
import { MathfieldElement } from 'mathlive';
import 'mathlive/dist/mathlive-static.css';
import 'mathlive/dist/mathlive-fonts.css';
import { importWordDocumentWithMath as parseWordDocumentWithMath, parseHtmlToLatex as convertHtmlToLatex } from '@/utils/wordMathImport';
import { importWordDocumentWithMath as parseWordDocumentWithMath, parseHtmlToLatex as convertHtmlToLatex, postProcessImportedWordHtml, parseImportedHtmlToContentRows, mergeAdjacentBlueTags, normalizeSpacesAroundBlueTags } from '@/utils/wordMathImport';
import api from '../../api/index.js';
import Common from '@/components/common/common'
import Tiff from 'tiff.js';
@@ -1518,6 +1518,9 @@ str = str.replace(regex, function (match, content, offset, fullString) {
if (type == 'table' && tag == 'img' && (attrName === "src" || attrName === "width" || attrName === "height")) {
return attrMatch;
}
if (tag === 'img' && (attrName === 'src' || attrName === 'width' || attrName === 'height' || attrName === 'alt')) {
return attrMatch;
}
return '';
});
@@ -1527,7 +1530,7 @@ str = str.replace(regex, function (match, content, offset, fullString) {
if (type == 'table') {
inputHtml = inputHtml.replace(/<(?!\/?(strong|em|sub|sup|b|i|blue|wmath|img|myfigure|mytable|myh3))[^>]+>/g, ''); // 删除不需要的标签
} else {
inputHtml = inputHtml.replace(/<(?!\/?(strong|em|sub|sup|b|i|blue|wmath|myfigure|mytable|myh3))[^>]+>/g, ''); // 删除不需要的标签
inputHtml = inputHtml.replace(/<(?!\/?(strong|em|sub|sup|b|i|blue|wmath|myfigure|mytable|myh3|img))[^>]+>/g, ''); // 删除不需要的标签
}
@@ -1543,8 +1546,8 @@ str = str.replace(regex, function (match, content, offset, fullString) {
},
importWordDocumentWithMath(file) {
return parseWordDocumentWithMath(file);
importWordDocumentWithMath(file, options) {
return parseWordDocumentWithMath(file, options);
},
parseHtmlToLatex(html) {
@@ -1552,42 +1555,35 @@ str = str.replace(regex, function (match, content, offset, fullString) {
},
cleanAndParseWordContent(content) {
// 1⃣ 解析成 <p> 段落数组
let tempDiv = document.createElement('div');
tempDiv.innerHTML = content; // 解析 HTML 内容
let paragraphs = tempDiv.querySelectorAll("p"); // 选取所有 <p> 作为数据项
const rows = parseImportedHtmlToContentRows(content);
const parsedData = [];
// 2⃣ 将 <p> 内容转换为数组,并处理内容
let parsedData = Array.from(paragraphs).map(p => {
let text = p.innerHTML.trim(); // 获取内容,去除两端空格
text = replaceNegativeSign(text);
rows.forEach((raw) => {
const rawStr = String(raw || '');
if (rawStr && (/wordTableHtml/i.test(rawStr) || /<table[\s>]/i.test(rawStr))) {
parsedData.push(/wordTableHtml/i.test(rawStr) ? rawStr : `<div class="thumbnailTableBox wordTableHtml table_Box">${rawStr}</div>`);
return;
}
if (rawStr === '') {
parsedData.push('');
return;
}
text = this.transformHtmlString(text)
// 3⃣ **正确移除 <o:p>Word 复制的无效标签)**
text = text.replace(/<\/?o:p[^>]*>/g, "");
// 4⃣ **移除所有 style="..."**
text = text.replace(/\s*style="[^"]*"/gi, "");
// 5⃣ **修正标签替换**
text = text.replace(/<strong>/gi, "<b>").replace(/<\/strong>/gi, "</b>");
text = text.replace(/<em>/gi, "<i>").replace(/<\/em>/gi, "</i>");
// 6⃣ **移除空的 span、b、i 标签**
text = text.replace(/<span>\s*<\/span>/gi, "");
text = text.replace(/<b>\s*<\/b>/gi, "");
text = text.replace(/<i>\s*<\/i>/gi, "");
// 7⃣ **确保不移除半个标签(修复匹配规则)**
text = text.replace(/<[^\/>]+>\s*<\/[^>]+>/gi, match => {
return match.trim() === "" ? "" : match;
});
// 8⃣ **返回最终内容**
return text.trim() === "" ? "" : text;
let text = replaceNegativeSign(rawStr.trim());
text = this.transformHtmlString(text);
text = text.replace(/<\/?o:p[^>]*>/g, '');
text = text.replace(/\s*style="[^"]*"/gi, '');
text = text.replace(/<strong>/gi, '<b>').replace(/<\/strong>/gi, '</b>');
text = text.replace(/<em>/gi, '<i>').replace(/<\/em>/gi, '</i>');
text = text.replace(/<span>\s*<\/span>/gi, '');
text = text.replace(/<b>\s*<\/b>/gi, '');
text = text.replace(/<i>\s*<\/i>/gi, '');
text = text.replace(/<[^\/>]+>\s*<\/[^>]+>/gi, (match) => (match.trim() === '' ? '' : match));
text = mergeAdjacentBlueTags(text);
text = normalizeSpacesAroundBlueTags(text);
parsedData.push(text.trim() === '' ? '' : text);
});
return parsedData;
},
@@ -2865,12 +2861,12 @@ str = str.replace(regex, function (match, content, offset, fullString) {
background: 'rgba(0, 0, 0, 0.45)'
})
: null;
parseWordDocumentWithMath(file)
parseWordDocumentWithMath(file, { textOnly: true })
.then((html) => {
if (!html) {
throw new Error('empty content');
}
ed.insertContent(html);
ed.setContent(html);
const body = ed.getBody();
body.querySelectorAll('wmath').forEach((el) => {
let latex = (el.getAttribute('data-latex') || '').trim();