fix: TinyMCE 允许 style 属性 + 对齐支持扩展到标题和列表

- Editor: valid_elements + extended_valid_elements 保留内联 style
- docx_to_html: 对齐注入覆盖 p/h1-h6/li 块级元素
This commit is contained in:
zwt13703 2026-07-06 22:41:54 +08:00
parent 81cb155e3f
commit b1de886828
2 changed files with 8 additions and 6 deletions

View File

@ -21,18 +21,18 @@ async def docx_to_html(file_content: bytes) -> str:
doc = Document(BytesIO(file_content))
soup = BeautifulSoup(html, "html.parser")
paragraphs = doc.paragraphs
html_paras = soup.find_all("p")
html_blocks = soup.find_all(["p", "h1", "h2", "h3", "h4", "h5", "h6", "li"])
for i, para in enumerate(paragraphs):
if i >= len(html_paras):
if i >= len(html_blocks):
break
if para.alignment and para.alignment in ALIGN_MAP:
css = ALIGN_MAP[para.alignment]
existing_style = html_paras[i].get("style", "")
existing = html_blocks[i].get("style", "")
styles = f"text-align:{css}"
if existing_style:
styles = existing_style.rstrip(";") + ";" + styles
html_paras[i]["style"] = styles
if existing:
styles = existing.rstrip(";") + ";" + styles
html_blocks[i]["style"] = styles
html = str(soup)
except Exception:

View File

@ -363,6 +363,8 @@ export default function TemplateEditor() {
toolbar:
"undo redo | blocks | bold italic forecolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | removeformat | help",
content_style: "body { font-family: Arial, sans-serif; font-size: 14px; }",
valid_elements: "*[*]",
extended_valid_elements: "p[style],h1[style],h2[style],h3[style],h4[style],h5[style],h6[style],div[style],span[style],li[style]",
}}
/>
</Content>