Improve MDHelper themes and packaging

This commit is contained in:
shenw
2026-05-25 22:00:37 +08:00
parent 2d896a618f
commit 0d2f62817b
41 changed files with 3729 additions and 86 deletions
+56 -1
View File
@@ -12,7 +12,7 @@ os.environ.setdefault("QTWEBENGINE_DISABLE_SANDBOX", "1")
PROJECT_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(PROJECT_ROOT))
from PySide6.QtCore import QTimer
from PySide6.QtCore import QTimer, QUrl
from PySide6.QtWidgets import QApplication
from mdhelper.main_window import MainWindow, ReaderView
@@ -27,6 +27,8 @@ def main() -> int:
checks: list[str] = []
def verify() -> None:
window.find_box.setText("斐波那契")
window.find_next()
if window.current_file != (project_root / "sample.md").resolve():
checks.append("current_file")
if window.tabs.count() < 1:
@@ -37,8 +39,61 @@ def main() -> int:
checks.append("export_pdf_action")
if not window.export_html_action.isEnabled():
checks.append("export_html_action")
if hasattr(window, "theme_action"):
checks.append("theme_toolbar_action")
if not hasattr(window, "theme_menu"):
checks.append("theme_menu")
if window.search_index.search("斐波那契") == []:
checks.append("search_index")
if not window._is_current_page_anchor_url(QUrl("#2-表格示例")):
checks.append("plain_anchor_url")
base_anchor = QUrl.fromLocalFile(str((project_root / "sample.md").resolve().parent))
base_anchor.setFragment("2-表格示例")
if not window._is_current_page_anchor_url(base_anchor):
checks.append("base_anchor_url")
if window._is_current_page_anchor_url(QUrl("mailto:user@example.com#message")):
checks.append("external_fragment_url")
window.katex_action.trigger()
if window.math_engine != "katex":
checks.append("katex_action")
window.mathjax_action.trigger()
if window.math_engine != "mathjax":
checks.append("mathjax_action")
snapshot = window._configuration_snapshot()
if snapshot.get("version") is None:
checks.append("config_version")
if "external_applications" not in snapshot:
checks.append("config_external_applications")
if snapshot.get("reader", {}).get("theme_background") is None:
checks.append("config_theme_background")
window._apply_configuration(
{
"reader": {
"theme_color": "dark_gray",
"theme_background": "circuit",
"math_engine": "katex",
"font_scale": 1.2,
"document_font_family": "Microsoft YaHei UI",
"document_font_size": 18,
"ui_font_family": "Microsoft YaHei UI",
"ui_font_point_size": 10,
},
"recent_files": [str(project_root / "sample.md")],
"bookmarks": [{"path": str(project_root / "sample.md"), "fragment": "", "label": "sample"}],
"scroll_positions": {str(project_root / "sample.md"): 42},
"external_applications": {".mp4": "C:/Tools/player.exe"},
}
)
if window.theme != "dark" or window.theme_color != "dark_gray" or window.math_engine != "katex":
checks.append("config_apply_reader")
if window.theme_background != "circuit":
checks.append("config_apply_theme_background")
if window.document_font_size != 18 or window.document_font_family != "Microsoft YaHei UI":
checks.append("config_apply_document_font")
if window.ui_font_family != "Microsoft YaHei UI" or window.ui_font_point_size != 10:
checks.append("config_apply_ui_font")
if ".mp4" not in window.external_applications:
checks.append("config_apply_external_apps")
window.close()
app.quit()
+50
View File
@@ -4,6 +4,7 @@ from tempfile import TemporaryDirectory
from mdhelper.app import parse_arguments
from mdhelper.doc_check import check_documents
from mdhelper.document_tree import discover_documents
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
@@ -25,18 +26,49 @@ def test_sample_media_and_code_rendering() -> None:
assert "media/animation.gif" in html
assert "language-pseudocode" in html
assert 'id="2-表格示例"' in html
assert 'href="#2-%E8%A1%A8%E6%A0%BC%E7%A4%BA%E4%BE%8B"' in html
assert f'{project_root.as_uri()}#2-%E8%A1%A8%E6%A0%BC%E7%A4%BA%E4%BE%8B' not in html
assert "copy-code-button" in html
assert "code-line" in html
assert "toggle-code-button" in html
assert "image-lightbox" in html
assert "image-lightbox__toolbar" in html
assert 'data-action="fit"' in html
assert 'data-action="actual-size"' in html
assert 'data-action="save"' in html
assert 'data-action="reveal"' in html
assert "copy-table-button" in html
assert "table-wrapper" in html
assert "task-list-item-checkbox" in html
assert "admonition admonition-tip" in html
assert "admonition admonition-note" in html
assert "admonition admonition-important" in html
assert "admonition admonition-warning" in html
assert "admonition admonition-caution" in html
assert '<pre class="mermaid">' in html
assert "flowchart LR" in html
assert "math-block" in html
assert "data-math-engine=\"katex\"" in html
assert "--reader-font-family:" in html
assert "--reader-font-size:" in html
assert "katex.min.css" in html
assert "auto-render.min.js" in html
assert "window.mermaid" in html
assert "mermaid.min.js" in html
assert "qrc:///qtwebchannel/qwebchannel.js" in html
mathjax_html = renderer.render_file(project_root / "sample.md", math_engine="mathjax")
assert "data-math-engine=\"mathjax\"" in mathjax_html
assert "window.MathJax" in mathjax_html
custom_font_html = renderer.render_file(
project_root / "sample.md",
document_font_family="Microsoft YaHei UI",
document_font_size=18,
)
assert '"Microsoft YaHei UI"' in custom_font_html
assert "--reader-font-size: 18px;" in custom_font_html
def test_heading_extraction_matches_rendered_anchor() -> None:
project_root = Path(__file__).resolve().parent.parent
@@ -44,6 +76,7 @@ def test_heading_extraction_matches_rendered_anchor() -> None:
assert any(heading.slug == "2-表格示例" for heading in headings)
assert any(heading.title == "7. 视频/动画示例" for heading in headings)
assert any(heading.title == "10. Mermaid 图表示例" for heading in headings)
def test_full_text_search_finds_document_body() -> None:
@@ -53,11 +86,14 @@ def test_full_text_search_finds_document_body() -> None:
assert matches
assert matches[0].entry.path.name == "sample.md"
assert matches[0].match_count >= 1
assert matches[0].match_kind in {"标题", "路径", "正文"}
index = SearchIndex(entries)
indexed_matches = index.search("斐波那契")
assert indexed_matches
assert indexed_matches[0].entry.path.name == "sample.md"
assert indexed_matches[0].match_count >= 1
def test_markdown_link_extraction_for_watchers() -> None:
@@ -109,3 +145,17 @@ def test_app_arguments_support_initial_file_and_check_mode() -> None:
check_args = parse_arguments(["--check", "--project-root", "."])
assert check_args.check
assert check_args.project_root == "."
def test_navigation_history_preserves_scroll_position() -> None:
history = NavigationHistory()
first = HistoryItem(path=Path("sample.md"), scroll_y=320)
second = HistoryItem(path=Path("docs/user_manual.md"), fragment="intro", scroll_y=0)
history.push(first)
history.push(second)
history.replace_current(HistoryItem(path=second.path, fragment=second.fragment, scroll_y=120))
assert history.current is not None
assert history.current.scroll_y == 120
assert history.back() == first