提交
This commit is contained in:
@@ -831,26 +831,27 @@ export default {
|
||||
// }
|
||||
|
||||
// },
|
||||
transformHtmlString(inputHtml, type) {
|
||||
|
||||
transformHtmlString(inputHtml, type, options = {}) {
|
||||
const { keepBr = false } = options;
|
||||
if (!keepBr) {
|
||||
inputHtml = inputHtml.replace(/<br\s*\/?>/g, '');
|
||||
}
|
||||
// inputHtml = inputHtml.replace(/(<[^>]+) style="[^"]*"/g, '$1'); // 移除style属性
|
||||
// inputHtml = inputHtml.replace(/(<[^>]+) class="[^"]*"/g, '$1'); // 移除class属性
|
||||
// inputHtml = inputHtml.replace(/<([a-zA-Z0-9]+)[^>]*>/g, '<$1>'); // 删除标签上的所有属性
|
||||
|
||||
inputHtml = inputHtml.replace(/<([a-zA-Z0-9]+)([^>]*)>/g, function (match, tag, attributes) {
|
||||
// 使用正则表达式删除属性(保留 data-latex)
|
||||
let updatedAttributes = attributes.replace(/\s([a-zA-Z0-9-]+)(="[^"]*")?/g, function (attrMatch, attrName) {
|
||||
// 只保留 data-latex 属性,其他属性删除
|
||||
|
||||
if (attrName === "data-latex") {
|
||||
return attrMatch;
|
||||
}
|
||||
if (type == 'table' && tag == 'img' && (attrName === "src" || attrName === "width" || attrName === "height")) {
|
||||
return attrMatch;
|
||||
}
|
||||
return ''; // 删除其他属性
|
||||
return '';
|
||||
});
|
||||
|
||||
// 返回标签,保留 data-latex 属性
|
||||
return `<${tag}${updatedAttributes}>`;
|
||||
});
|
||||
// 2. 删除所有不需要的标签 (除 `strong`, `em`, `sub`, `sup`, `b`, `i` 外的所有标签)
|
||||
@@ -861,14 +862,11 @@ export default {
|
||||
}
|
||||
|
||||
|
||||
// 3. 删除所有 `<br>` 标签
|
||||
inputHtml = inputHtml.replace(/<br\s*\/?>/g, ''); // 删除 <br> 标签
|
||||
// 3. 如果有 `<strong>` 和 `<em>` 标签,去掉内部样式并保留内容
|
||||
// 去掉样式
|
||||
inputHtml = inputHtml.replace(/<span[^>]*>/g, '').replace(/<\/span>/g, ''); // 去除span标签
|
||||
inputHtml = inputHtml.replace(/<strong>/g, '<b>').replace(/<\/strong>/g, '</b>'); // 将 `strong` 替换成 `b`
|
||||
inputHtml = inputHtml.replace(/<em>/g, '<i>').replace(/<\/em>/g, '</i>'); // 将 `em` 替换成 `i`
|
||||
|
||||
// 4. 合并相同标签(如多个连续的 <b> 标签)
|
||||
// 4. 合并相同标签
|
||||
inputHtml = inputHtml.replace(/<b>(.*?)<\/b>\s*<b>/g, '<b>$1'); // 合并连续的 <b> 标签
|
||||
inputHtml = inputHtml.replace(/<i>(.*?)<\/i>\s*<i>/g, '<i>$1'); // 合并连续的 <i> 标签
|
||||
|
||||
|
||||
Reference in New Issue
Block a user