fix: protect editor content and harden document exports

Protect pending edits on window close, keep save-as paths and drafts consistent,
copy relative image resources, reject oversized table imports without truncation,
and preserve independent preview scrolling during refresh.

Archive rendered MHTML resources with portable references and embedded local
fonts. Preserve existing outputs on export cancellation or failure. Convert
editor match positions and filter directory results in linear passes.

Include the previously completed editor image/table/history improvements,
undoable replacement, export snapshots, shared page settings, and cancellable
PDF/Word export workflow. Update documentation and version to 0.3.1.

Validation: 142 pytest cases passed; GUI smoke, improvements integration, and
offline MHTML/scroll reliability smoke checks passed.
This commit is contained in:
2026-10-29 18:22:42 +08:00
parent fac5cbb011
commit 8dae91c2ca
26 changed files with 2722 additions and 385 deletions
+55 -17
View File
@@ -5,6 +5,7 @@ import shutil
import subprocess
import sys
import tempfile
import time
import zipfile
from dataclasses import dataclass
from pathlib import Path
@@ -55,18 +56,39 @@ def export_markdown_to_docx(
document_font_family: str = "",
document_font_size: float = 16,
preset: str = "default",
page_settings: object = None,
cancel: object = None,
progress: object = None,
) -> Path:
from .docx_style import apply_docx_styles
if engine == "pandoc":
result = export_markdown_to_docx_pandoc(source, output_path, source_path=source_path)
elif engine == "python-docx":
result = export_markdown_to_docx_python_docx(source, output_path)
else:
result = export_markdown_to_docx_builtin(source, output_path)
apply_docx_styles(result, style_rules, font_family=document_font_family,
font_size_px=document_font_size, preset=preset)
return result
from .export_jobs import check_cancelled
from .file_versions import atomic_write
if output_path.suffix.lower() != ".docx":
output_path = output_path.with_suffix(".docx")
check_cancelled(cancel)
if progress:
progress("正在转换 Word 内容…")
with tempfile.TemporaryDirectory(prefix="mdhelper-export-") as directory:
target = Path(directory) / "document.docx"
if engine == "pandoc":
result = export_markdown_to_docx_pandoc(source, target, source_path=source_path, cancel=cancel)
elif engine == "python-docx":
result = export_markdown_to_docx_python_docx(source, target)
else:
result = export_markdown_to_docx_builtin(source, target)
check_cancelled(cancel)
if progress:
progress("正在应用字体、段落和页面设置…")
apply_docx_styles(result, style_rules, font_family=document_font_family,
font_size_px=document_font_size, preset=preset)
if page_settings is not None:
from .export_settings import apply_docx_page_settings
apply_docx_page_settings(result, page_settings)
check_cancelled(cancel)
data = result.read_bytes()
check_cancelled(cancel)
atomic_write(output_path, data)
return output_path
def export_markdown_to_docx_builtin(source: str, output_path: Path) -> Path:
@@ -86,7 +108,7 @@ def export_markdown_to_docx_builtin(source: str, output_path: Path) -> Path:
return output_path
def export_markdown_to_docx_pandoc(source: str, output_path: Path, source_path: Path | None = None) -> Path:
def export_markdown_to_docx_pandoc(source: str, output_path: Path, source_path: Path | None = None, cancel: object = None) -> Path:
output_path = output_path.resolve()
if output_path.suffix.lower() != ".docx":
output_path = output_path.with_suffix(".docx")
@@ -109,15 +131,31 @@ def export_markdown_to_docx_pandoc(source: str, output_path: Path, source_path:
if sys.platform == "win32":
run_options["creationflags"] = subprocess.CREATE_NO_WINDOW
try:
result = subprocess.run(
from .export_jobs import check_cancelled
process = subprocess.Popen(
command,
cwd=str(resource_directory),
capture_output=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=120,
check=False,
encoding="utf-8", errors="replace",
**run_options,
)
started = time.monotonic()
try:
while True:
check_cancelled(cancel)
if time.monotonic() - started > 120:
raise subprocess.TimeoutExpired(command, 120)
try:
stdout, stderr = process.communicate(timeout=0.1)
break
except subprocess.TimeoutExpired:
continue
finally:
if process.poll() is None:
process.kill()
process.communicate()
except FileNotFoundError as exc:
raise DocxExportError("程序内置的 Pandoc 无法启动,当前 MDHelper 安装包可能不完整。") from exc
except subprocess.TimeoutExpired as exc:
@@ -129,8 +167,8 @@ def export_markdown_to_docx_pandoc(source: str, output_path: Path, source_path:
except OSError:
pass
if result.returncode != 0:
detail = (result.stderr or result.stdout or "pandoc 返回错误。").strip()
if process.returncode != 0:
detail = (stderr or stdout or "pandoc 返回错误。").strip()
raise DocxExportError(detail)
return output_path