diff --git a/scripts/md_to_blocks.py b/scripts/md_to_blocks.py index 5d66091..7f177c4 100644 --- a/scripts/md_to_blocks.py +++ b/scripts/md_to_blocks.py @@ -15,6 +15,9 @@ TEXT_BLOCK = 2 CODE_BLOCK = 14 PREFIX = 'ecc_' +# 全局表格计数器:每个表格用唯一 ID 前缀,避免多表格时 block_id 冲突 +_table_counter = 0 + LANG_CODES = { '': 1, 'text': 1, 'plain': 1, 'bash': 7, 'sh': 7, 'shell': 7, @@ -87,6 +90,7 @@ def split_row(line): def parse_table(lines, start_idx): """Parse a markdown table. Returns (blocks, end_idx) or (None, start_idx).""" + global _table_counter i = start_idx headers = [] has_header = False @@ -115,8 +119,10 @@ def parse_table(lines, start_idx): data_rows.append(row) i += 1 - # No header case: first line was a data row - if not has_header: + # 表头行必须作为第一行保留(飞书 header_row=True 时首行即表头) + if has_header: + data_rows.insert(0, headers) + else: data_rows.insert(0, first_cells) if not data_rows: @@ -127,7 +133,8 @@ def parse_table(lines, start_idx): # Build nested blocks blocks = [] - table_id = f'{PREFIX}t0' + table_id = f'{PREFIX}t{_table_counter}' + _table_counter += 1 cell_ids = [] # Table container @@ -150,8 +157,8 @@ def parse_table(lines, start_idx): for row_idx, row in enumerate(data_rows): for col_idx in range(num_cols): cell_text = row[col_idx] if col_idx < len(row) else '' - cell_id = f'{PREFIX}t0r{row_idx}c{col_idx}' - text_id = f'{PREFIX}t0r{row_idx}c{col_idx}t' + cell_id = f'{table_id}r{row_idx}c{col_idx}' + text_id = f'{table_id}r{row_idx}c{col_idx}t' cell_ids.append(cell_id) is_header_cell = has_header and row_idx == 0