|
|
@@ -143,3 +143,44 @@ export const formatStaticAssetsUrl_Dify = (file_href) => {
|
|
|
return file_href;
|
|
|
}
|
|
|
};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 处理marked解析出表格没有边框问题,加入边框
|
|
|
+ * */
|
|
|
+export const handleTable = (html_content) => {
|
|
|
+ // 第一步:处理 <table> 标签
|
|
|
+ let tablePattern = /<table\b([^>]*)>/g;
|
|
|
+ let tableReplacement = '<table border$1 style="border-collapse: collapse;">';
|
|
|
+ let modified_content = html_content.replace(tablePattern, tableReplacement);
|
|
|
+
|
|
|
+ // 第二步:处理 <th> 和 <td> 标签
|
|
|
+ let cellPattern = /<(th|td)\b([^>]*)>/g;
|
|
|
+ let cellReplacement = (match, tag, attributes) => {
|
|
|
+ // 检查是否已经存在 style 属性
|
|
|
+ let stylePattern = /style\s*=\s*["']([^"']*)["']/i;
|
|
|
+ let styleMatch = attributes.match(stylePattern);
|
|
|
+ let newStyle = 'padding: 5px 10px;';
|
|
|
+
|
|
|
+ if (styleMatch) {
|
|
|
+ // 如果 style 属性已存在,追加 padding 样式
|
|
|
+ let existingStyle = styleMatch;
|
|
|
+ // 避免重复添加 padding
|
|
|
+ if (!existingStyle.includes('padding:')) {
|
|
|
+ newStyle = existingStyle + '; ' + newStyle;
|
|
|
+ } else {
|
|
|
+ newStyle = existingStyle; // 如果 padding 已存在,不修改
|
|
|
+ }
|
|
|
+ // 替换原有的 style 属性
|
|
|
+ attributes = attributes.replace(stylePattern, `style="${newStyle}"`);
|
|
|
+ } else {
|
|
|
+ // 如果 style 属性不存在,直接添加
|
|
|
+ attributes = `style="${newStyle}" ${attributes}`;
|
|
|
+ }
|
|
|
+ // 返回修改后的标签
|
|
|
+ return `<${tag} ${attributes}>`;
|
|
|
+ };
|
|
|
+ // 执行替换
|
|
|
+ modified_content = modified_content.replace(cellPattern, cellReplacement);
|
|
|
+
|
|
|
+ return modified_content;
|
|
|
+};
|