diff --git a/scripts/md_to_blocks.py b/scripts/md_to_blocks.py index 3ea1937..640f563 100644 --- a/scripts/md_to_blocks.py +++ b/scripts/md_to_blocks.py @@ -12,8 +12,23 @@ import re, json, sys TABLE_BLOCK = 31 CELL_BLOCK = 32 TEXT_BLOCK = 2 +CODE_BLOCK = 14 PREFIX = 'ecc_' +LANG_CODES = { + '': 1, 'text': 1, 'plain': 1, + 'bash': 7, 'sh': 7, 'shell': 7, + 'python': 11, 'py': 11, + 'go': 5, 'golang': 5, + 'js': 12, 'javascript': 12, 'json': 14, + 'yaml': 33, 'yml': 33, + 'sql': 25, +} + +def lang_code(lang): + """Map markdown code fence language to Feishu code block language id.""" + return LANG_CODES.get(lang.lower(), 1) + def parse_inline(text): """Parse a line of inline markdown into text_run elements.""" elements = [] @@ -176,6 +191,26 @@ def md_to_blocks(md_text): while i < len(lines): line = lines[i] + # Fenced code block: ```lang ... ``` + fence = re.match(r'^```(\w*)\s*$', line.strip()) + if fence: + lang = fence.group(1) + code_lines = [] + i += 1 + while i < len(lines) and not lines[i].strip().startswith('```'): + code_lines.append(lines[i]) + i += 1 + i += 1 # skip closing fence + code_text = '\n'.join(code_lines) + blocks.append({ + 'block_type': CODE_BLOCK, + 'code': { + 'elements': [{'text_run': {'content': code_text, 'text_element_style': {}}}], + 'style': {'language': lang_code(lang)} + } + }) + continue + # Empty line if not line.strip(): if i + 1 < len(lines) and lines[i + 1].strip() and not lines[i + 1].strip().startswith(('#', '-', '*', '`', '---', '|')):