import json, re, os, glob input_dir = r'D:\desktop\feishu_docs_summary\output\feishu-docs' output_dir = r'D:\desktop\feishu_docs_summary\output\feishu-docs\md' os.makedirs(output_dir, exist_ok=True) def html_to_md(html): if not html: return '' text = html text = re.sub(r']*alt="([^"]*?)"[^>]*/>', r'[图片: \1]', text) text = re.sub(r']*/>', '[图片]', text) text = re.sub(r']*/>', '[表格]', text) text = re.sub(r']*>', '', text) text = re.sub(r']*>', '', text) text = re.sub(r'', '', text) text = re.sub(r']*>(.*?)', r'$\1$', text, flags=re.DOTALL) text = re.sub(r']*>(.*?)', r'# \1', text, flags=re.DOTALL) text = re.sub(r']*>(.*?)', r'## \1', text, flags=re.DOTALL) text = re.sub(r']*>(.*?)', r'### \1', text, flags=re.DOTALL) text = re.sub(r']*>(.*?)', r'#### \1', text, flags=re.DOTALL) text = re.sub(r'(.*?)', r'**\1**', text, flags=re.DOTALL) text = re.sub(r'(.*?)', r'**\1**', text, flags=re.DOTALL) text = re.sub(r'(.*?)', r'*\1*', text, flags=re.DOTALL) text = re.sub(r'(.*?)', r'*\1*', text, flags=re.DOTALL) text = re.sub(r']*href="([^"]*?)"[^>]*>(.*?)', r'[\2](\1)', text, flags=re.DOTALL) text = re.sub(r']*>', '', text) text = re.sub(r'', '', text) text = re.sub(r']*>', '', text) text = re.sub(r'', '', text) text = re.sub(r']*>', '- ', text) text = re.sub(r'', '', text) text = re.sub(r']*>', '> ', text) text = re.sub(r'', '', text) text = re.sub(r']*>', '', text) text = re.sub(r'

', '\n', text) text = re.sub(r']*/>', '\n---\n', text) text = re.sub(r'(.*?)', r'`\1`', text, flags=re.DOTALL) text = re.sub(r'<[^>]+>', '', text) text = re.sub(r'\n{3,}', '\n\n', text) return text.strip() count = 0 for f in sorted(glob.glob(os.path.join(input_dir, '*.json'))): try: with open(f, 'r', encoding='utf-8') as fh: raw = fh.read() lines = raw.split('\n') json_lines = [l for l in lines if not l.startswith('2026/')] data = json.loads('\n'.join(json_lines)) if not data.get('ok'): print(f'SKIP: {os.path.basename(f)}') continue doc = data['data']['document'] doc_id = doc['document_id'] title = '' content_html = doc['content'] title_match = re.search(r'(.*?)', content_html) if title_match: title = title_match.group(1) md_content = html_to_md(content_html) md_path = os.path.join(output_dir, f'{doc_id}.md') with open(md_path, 'w', encoding='utf-8') as fh: if title: fh.write(f'# {title}\n\n') fh.write(md_content) fh.write(f'\n\n---\n*Source: feishu doc {doc_id}*\n') print(f'OK: {doc_id} -> {title[:60] if title else "(untitled)"} ({len(md_content)} chars)') count += 1 except Exception as e: print(f'ERROR: {os.path.basename(f)}: {e}') print(f'\nTotal: {count}')