Initial MDHelper implementation

This commit is contained in:
shenw
2026-05-25 17:12:00 +08:00
commit 50b15b02c7
45 changed files with 11654 additions and 0 deletions
+111
View File
@@ -0,0 +1,111 @@
from pathlib import Path
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.main_window import re_find_markdown_links
from mdhelper.markdown_utils import extract_headings
from mdhelper.markdown_renderer import MarkdownRenderer
from mdhelper.search import SearchIndex, search_entries
def test_sample_media_and_code_rendering() -> None:
project_root = Path(__file__).resolve().parent.parent
renderer = MarkdownRenderer(project_root)
html = renderer.render_file(project_root / "sample.md")
assert "<table>" in html
assert "<audio controls" in html
assert "<video controls" in html
assert "type=\"video/mp4\"" in html
assert "file:///" in html
assert 'href="file:///' in html
assert "docs/user_manual.md" in html
assert "media/animation.gif" in html
assert "language-pseudocode" in html
assert 'id="2-表格示例"' 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 "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-warning" in html
assert "qrc:///qtwebchannel/qwebchannel.js" in html
def test_heading_extraction_matches_rendered_anchor() -> None:
project_root = Path(__file__).resolve().parent.parent
headings = extract_headings(project_root / "sample.md")
assert any(heading.slug == "2-表格示例" for heading in headings)
assert any(heading.title == "7. 视频/动画示例" for heading in headings)
def test_full_text_search_finds_document_body() -> None:
project_root = Path(__file__).resolve().parent.parent
entries = discover_documents(project_root)
matches = search_entries(entries, "斐波那契")
assert matches
assert matches[0].entry.path.name == "sample.md"
index = SearchIndex(entries)
indexed_matches = index.search("斐波那契")
assert indexed_matches
assert indexed_matches[0].entry.path.name == "sample.md"
def test_markdown_link_extraction_for_watchers() -> None:
links = re_find_markdown_links(
'![截图](media/screenshot.png)\n'
'[用户手册](docs/user_manual.md)\n'
'<video src="media/demo.mp4"></video>'
)
assert "media/screenshot.png" in links
assert "docs/user_manual.md" in links
assert "media/demo.mp4" in links
def test_local_links_are_absolute_file_urls() -> None:
project_root = Path(__file__).resolve().parent.parent
renderer = MarkdownRenderer(project_root)
html = renderer.render_file(project_root / "sample.md")
assert (project_root / "docs" / "user_manual.md").resolve().as_uri() in html
assert (project_root / "media" / "screenshot.png").resolve().as_uri() in html
assert (project_root / "media" / "demo.mp4").resolve().as_uri() in html
def test_document_checker_reports_missing_local_target() -> None:
with TemporaryDirectory() as temp_dir:
root = Path(temp_dir)
docs = root / "docs"
docs.mkdir()
(docs / "ok.md").write_text("# OK\n", encoding="utf-8")
(root / "sample.md").write_text(
"[存在](docs/ok.md)\n"
"![缺失](media/missing.png)\n"
"[网页](https://example.com)\n",
encoding="utf-8",
)
issues = check_documents(root)
assert len(issues) == 1
assert issues[0].target == "media/missing.png"
def test_app_arguments_support_initial_file_and_check_mode() -> None:
args = parse_arguments(["sample.md"])
assert args.file == "sample.md"
assert not args.check
check_args = parse_arguments(["--check", "--project-root", "."])
assert check_args.check
assert check_args.project_root == "."