MCPcopy Create free account
hub / github.com/Feather-2/Burner-X / segmentBlockIntoSubBlocks

Function segmentBlockIntoSubBlocks

js/processing/sub_block_segmenter.js:8–237  ·  view source on GitHub ↗

* 将块级元素内容按标点分割成子块 (span.sub-block)。 * @param {HTMLElement} blockElement - 要分割的块级元素 (如 p, h1-h6)。 * @param {string|number} parentBlockIndex - 父块的索引。

(blockElement, parentBlockIndex, force = false)

Source from the content-addressed store, hash-verified

6 * @param {string|number} parentBlockIndex - 父块的索引。
7 */
8 function segmentBlockIntoSubBlocks(blockElement, parentBlockIndex, force = false) {
9 // 性能埋点:分块开始
10 performance.mark('subBlock-start');
11 // 调试开关:本文件的检测/一致性类日志统一受控
12 const __SUBBLOCK_DEBUG__ = (function(){
13 try {
14 return !!(window && (window.ENABLE_SUBBLOCK_DEBUG || localStorage.getItem('ENABLE_SUBBLOCK_DEBUG') === 'true'));
15 } catch { return false; }
16 })();
17
18 // ===== 新增:分割前检测 =====
19 const preSubBlocks = Array.from(blockElement.querySelectorAll('.sub-block'));
20 if (__SUBBLOCK_DEBUG__ && preSubBlocks.length > 0) {
21 console.warn(`[SubBlockSegmenter][检测] 分割前已存在 ${preSubBlocks.length} 个 .sub-block,内容摘要:`, preSubBlocks.map(sb => (sb.textContent || '').substring(0, 30)));
22 }
23 const preTextContent = blockElement.textContent;
24
25 // ===== 新增:详细日志 =====
26 // console.log(`[SubBlockSegmenter] 开始分块 #${parentBlockIndex}, 元素类型: ${blockElement.tagName}, 内容前20字符: "${(blockElement.textContent || '').substring(0, 20)}..."`);
27
28 // 检查是否已经有子块,如果有则记录它们
29 const existingSubBlocks = blockElement.querySelectorAll('.sub-block');
30 if (existingSubBlocks.length > 0) {
31 // console.log(`[SubBlockSegmenter] 警告: 块 #${parentBlockIndex} 已有 ${existingSubBlocks.length} 个子块,这些将被重新生成`);
32 // console.log(`[SubBlockSegmenter] 现有子块ID列表:`, Array.from(existingSubBlocks).map(sb => sb.dataset.subBlockId));
33 }
34
35 // 优化:只有当文本足够长且包含中文或英文句读符号才进行分块
36 const rawText = (blockElement.textContent || '').trim();
37 const containsCnPeriod = rawText.indexOf('。') !== -1;
38 const containsEnPunct = /[\.!?;:]/.test(rawText);
39 // console.log(`[SubBlockSegmenter] 块 #${parentBlockIndex} 文本长度: ${rawText.length}, CN句号: ${containsCnPeriod}, EN标点: ${containsEnPunct}`);
40
41 // ===== 新增:公式感知检测 =====
42 const hasFormula = checkForFormulas(blockElement, rawText);
43 if (__SUBBLOCK_DEBUG__ && hasFormula) {
44 console.log(`[SubBlockSegmenter] 块 #${parentBlockIndex} 包含数学公式,启用公式感知分割`);
45 }
46
47 // 修改分割条件:包含公式的块使用不同的分割策略
48 // 仅当文本很短且也不含中英文句读时才跳过;
49 // 只要存在句读符,即使很短也执行分块以支持精确高亮。
50 if (!force && !hasFormula && (rawText.length < 80 && (!containsCnPeriod && !containsEnPunct))) {
51 // 跳过分割
52 // console.log(`[SubBlockSegmenter] 块 #${parentBlockIndex} 不满足分块条件,跳过分块`);
53 performance.mark('subBlock-end');
54 performance.measure('subBlockSegmentSkipping', 'subBlock-start', 'subBlock-end');
55 return;
56 }
57
58 // 如果块元素本身是表格,或者其内部有表格,则不进行分割处理
59 if (blockElement.tagName === 'TABLE' || blockElement.querySelector('table')) {
60 // console.log(`[SubBlockSegmenter] 块 #${parentBlockIndex} 是表格或包含表格,跳过分块`);
61 return; // 直接返回,不修改表格内容
62 }
63
64 // ===== 新增:检测 Markdown 表格语法 =====
65 // 检测表格分隔符行:|---|---|... 或 |:---|---:| 等

Callers

nothing calls this directly

Calls 5

checkForFormulasFunction · 0.85
segmentFormulaAwareBlockFunction · 0.85
processNodesRecursiveFunction · 0.85
flushCurrentSpanFunction · 0.85
logMethod · 0.80

Tested by

no test coverage detected