This commit is contained in:
2025-07-17 17:01:17 +08:00
parent 47caddad60
commit 0725cd779f

View File

@@ -176,33 +176,26 @@
}, 300); }, 300);
}); });
// 英文截断处理 // 英文截断处理
$('#rendered-content').css({ // 目标容器:富文本内容所在的元素
'word-wrap': 'break-word', const contentContainer = $('#rendered-content');
'overflow-wrap': 'break-word',
'word-break': 'normal',
'white-space': 'normal',
});
// 处理函数 // 遍历所有包含文本内容的标签p、h1-h6、strong等
function processTextNodes($element) { contentContainer.find('*').each(function() {
$element.contents().each(function() { const $element = $(this);
if (this.nodeType === 3) { // 文本节点 const htmlContent = $element.html();
let $parent = $(this).parent();
let cleanHtml = $parent.html()
.replace(/<[^>]+>/g, '') // 移除所有标签
.replace(/\s/g, ''); // 移除所有空白
if (cleanHtml !== '&nbsp;') { // 条件1排除内容仅为一个&nbsp;的标签(如<p>&nbsp;</p>
$(this).replaceWith(this.nodeValue.replace(/&nbsp;/g, ' ')); if (htmlContent.trim() === '&nbsp;') {
return; // 不处理,继续下一个元素
} }
} else if (this.nodeType === 1) { // 元素节点
processTextNodes($(this)); // 条件2检查是否包含&nbsp;且有实际文本内容
if (htmlContent.includes('&nbsp;')) {
// 将所有&nbsp;替换为普通空格(有效占位符,支持单词完整换行)
const newContent = htmlContent.replace(/&nbsp;/g, ' ');
$element.html(newContent);
} }
}); });
}
// 执行处理
processTextNodes($('#rendered-content'));
}); });
</script> </script>
{/block} {/block}