This commit is contained in:
2025-07-17 17:01:17 +08:00
parent 087d1dd641
commit 2e94ede31b

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