28 lines
1.2 KiB
Python
28 lines
1.2 KiB
Python
"""HTML 预览生成服务"""
|
|
from typing import List, Dict, Any
|
|
|
|
|
|
def generate_preview_html(blocks: List[Dict[str, Any]]) -> str:
|
|
"""
|
|
将 block 列表转为带 data-block-id 的 HTML 预览字符串。
|
|
每个元素都会带上 data-block-id 属性供前端点击交互。
|
|
"""
|
|
html_parts = ['<div class="doc-preview-content">']
|
|
|
|
for block in blocks:
|
|
block_id = block.get("block_id", "")
|
|
block_type = block.get("type", "")
|
|
text = block.get("text", "")
|
|
|
|
if block_type == "heading":
|
|
html = f'<h2 data-block-id="{block_id}" style="font-size:16px;font-weight:600;margin:20px 0 10px;padding-bottom:6px;border-bottom:2px solid #1a1d24">{text}</h2>'
|
|
elif block_type == "table":
|
|
html = f'<div data-block-id="{block_id}" style="margin:12px 0;padding:8px;background:#f7f8fa;border:1px dashed #ccc;border-radius:4px;color:#5b626e">📊 {text[:80]}</div>'
|
|
else:
|
|
html = f'<p data-block-id="{block_id}" style="text-indent:2em;margin:8px 0;line-height:1.8;text-align:justify">{text}</p>'
|
|
|
|
html_parts.append(html)
|
|
|
|
html_parts.append('</div>')
|
|
return "\n".join(html_parts)
|