feat: optimize startup and add Windows installer

This commit is contained in:
2026-09-17 14:56:48 +08:00
parent 4113236128
commit a097ce8f4b
36 changed files with 2661 additions and 935 deletions
+123 -16
View File
@@ -1,6 +1,8 @@
from pathlib import Path
from tempfile import TemporaryDirectory
from PySide6.QtGui import QPageLayout
from mdhelper.app import parse_arguments
from mdhelper import search as search_module
from mdhelper.doc_check import check_documents
@@ -10,6 +12,7 @@ from mdhelper.history import HistoryItem, NavigationHistory
from mdhelper.main_window import re_find_markdown_links
from mdhelper.markdown_utils import extract_headings
from mdhelper.markdown_renderer import MarkdownRenderer
from mdhelper.pdf_export import PdfMargins, build_pdf_export_html, build_pdf_page_layout
from mdhelper.search import SearchIndex, search_entries
from mdhelper.style_dialog import build_document_style_css
@@ -69,11 +72,21 @@ def test_sample_media_and_code_rendering() -> None:
assert "data-math-engine=\"katex\"" in html
assert "data-wheel-scroll-speed=\"1.0\"" in html
assert "configuredWheelSpeed" in html
assert "VISUAL_SCROLL_INTERVAL" not in html
assert "scheduleScrollEndUpdate" not in html
assert "headingElements" not in html
assert "backdrop-filter" not in html
assert "--reader-font-family:" in html
assert "--reader-font-size:" in html
assert "--reader-paper-width: 794px;" in html
assert "katex.min.css" in html
assert "auto-render.min.js" in html
assert ".math-block .katex" in html
assert ".math-display mjx-container" in html
assert ".math-inline .katex" in html
assert ".math-inline mjx-container" in html
assert "--reader-math-font-size: var(--reader-font-size);" in html
assert "syncMathFontSize" in html
assert "window.mermaid" in html
assert "mermaid.min.js" in html
assert "qrc:///qtwebchannel/qwebchannel.js" in html
@@ -102,27 +115,120 @@ def test_sample_media_and_code_rendering() -> None:
assert ".reader { max-width: 720px; }" in custom_style_html
def test_plain_document_omits_optional_runtime_assets(tmp_path: Path) -> None:
renderer = MarkdownRenderer(tmp_path)
rendered = renderer.render_source("# Plain\n\nNo formula or diagram.", tmp_path / "plain.md")
assert "mermaid.min.js" not in rendered
assert "katex.min.js" not in rendered
assert "tex-svg-full.js" not in rendered
def test_reader_chrome_can_be_embedded_in_html() -> None:
def test_formula_and_diagram_load_independently(tmp_path: Path) -> None:
renderer = MarkdownRenderer(tmp_path)
formula = renderer.render_source("Formula $x^2$.", tmp_path / "formula.md")
assert "katex.min.js" in formula
assert "mermaid.min.js" not in formula
diagram = renderer.render_source("```mermaid\ngraph LR\nA-->B\n```", tmp_path / "diagram.md")
assert "mermaid.min.js" in diagram
assert "katex.min.js" not in diagram
mathjax = renderer.render_source(r"Formula \(x^2\).", tmp_path / "math.md", math_engine="mathjax")
assert "tex-svg-full.js" in mathjax
assert "katex.min.js" not in mathjax
def test_latex_math_is_protected_from_markdown_emphasis(tmp_path: Path) -> None:
renderer = MarkdownRenderer(tmp_path)
source = r"""普通强调 _仍然生效_,行内公式 $\widehat{\nabla_0W}^{(i)}_{0ij}$ 不应变成斜体。
$$
\mathbf g^{\mathrm{raw}}_{ij}
=\nabla_{\boldsymbol\xi_i}W_{0ij}
=-\nabla_{\boldsymbol\xi_{ij}}W(\boldsymbol\xi_{ij},h_s)
\in\mathbb R^2.
$$
"""
rendered = renderer.markdown.render(source)
assert "<em>仍然生效</em>" in rendered
assert '<span class="math-inline">$\\widehat{\\nabla_0W}^{(i)}_{0ij}$</span>' in rendered
assert '<div class="math-block">$$' in rendered
assert "\\mathbf g^{\\mathrm{raw}}_{ij}" in rendered
assert "\\nabla_{\\boldsymbol\\xi_i}W_{0ij}" in rendered
assert "<em>{ij}" not in rendered
assert "<em>{0ij}" not in rendered
def test_parenthesized_latex_math_is_protected_from_markdown_escapes(tmp_path: Path) -> None:
renderer = MarkdownRenderer(tmp_path)
rendered = renderer.markdown.render(
r"""公式 \(\mathbf g^{c}_{0ij}\) 正常。
\[
\mathbf H_i=\sum_j \mathbf g^{\mathrm{raw}}_{ij}
\]
同一行展示公式 $$\mathbf L_i=\mathbf H_i^{-1}$$ 正常。
"""
)
assert '<span class="math-inline">\\(\\mathbf g^{c}_{0ij}\\)</span>' in rendered
assert '<div class="math-block">\\[' in rendered
assert "\\mathbf H_i=\\sum_j \\mathbf g^{\\mathrm{raw}}_{ij}" in rendered
assert '<span class="math-display">$$\\mathbf L_i=\\mathbf H_i^{-1}$$</span>' in rendered
assert "<em>" not in rendered
def test_reader_html_excludes_outline_and_progress_chrome() -> None:
project_root = Path(__file__).resolve().parent.parent
renderer = MarkdownRenderer(project_root)
html = renderer.render_file(
project_root / "sample.md",
include_reader_toc=True,
include_reader_progress=True,
)
html = renderer.render_file(project_root / "sample.md")
assert 'class="reader-shell"' in html
assert 'id="mdhelper-reader"' in html
assert "reader-toc" not in html
assert "reader-progress" not in html
assert "data-reader-toc" not in html
assert "data-reader-progress" not in html
assert "reportScroll" not in html
def test_pdf_page_layout_uses_configured_margins() -> None:
configured = PdfMargins(top=12.0, bottom=18.0, left=20.0, right=16.0)
layout = build_pdf_page_layout(configured)
margins = layout.margins(QPageLayout.Unit.Millimeter)
assert margins.left() == configured.left
assert margins.right() == configured.right
assert margins.top() == configured.top
assert margins.bottom() == configured.bottom
def test_pdf_export_html_has_numbered_toc_white_background_and_page_break() -> None:
project_root = Path(__file__).resolve().parent.parent
renderer = MarkdownRenderer(project_root)
headings = extract_headings(project_root / "sample.md")
html = renderer.render_file(project_root / "sample.md")
exported = build_pdf_export_html(html, headings, {index: index + 2 for index in range(len(headings))})
assert 'class="pdf-toc' in exported
assert 'class="reader-toc"' not in exported
assert 'class="reader-progress"' not in exported
assert "break-after: page" in exported
assert "background: #ffffff !important" in exported
assert "MDHELPER_PDF_BODY_START_7A61" in exported
assert "MDHELPER_PDF_HEADING_0000" in exported
assert '<span class="pdf-toc__page">2</span>' in exported
final_export = build_pdf_export_html(html, headings, {0: 2}, include_page_markers=False)
assert "MDHELPER_PDF_BODY_START_7A61" not in final_export
assert "MDHELPER_PDF_HEADING_0000" not in final_export
assert 'data-reader-toc="1"' in html
assert 'data-reader-progress="1"' in html
assert 'class="reader-toc"' in html
assert 'class="reader-progress"' in html
assert 'data-toc-target="2-表格示例"' in html
assert "reader-toc__link" in html
assert "grid-template-columns: minmax(0, var(--reader-paper-width)) minmax(190px, 240px);" in html
assert ".reader-shell.has-reader-toc .reader" in html
assert "grid-column: 1;" in html
assert "grid-column: 2;" in html
def test_heading_extraction_matches_rendered_anchor() -> None:
project_root = Path(__file__).resolve().parent.parent
headings = extract_headings(project_root / "sample.md")
@@ -160,6 +266,7 @@ def test_document_style_builder_supports_word_like_paragraph_options() -> None:
)
assert ".reader p" in css
assert "--reader-math-font-size: 18px;" in css
assert 'font-family: "Times New Roman", "Microsoft YaHei UI"' in css
assert "font-size: 18px;" in css
assert "line-height: 1.7;" in css