2026-10-29 19:18:19 +08:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
from mdhelper.doc_check import check_markdown_file
|
|
|
|
|
from mdhelper.document_resources import analyze_document, apply_source_edits, resolve_local_reference
|
|
|
|
|
from mdhelper.editor_assets import plan_image_relocation
|
|
|
|
|
from mdhelper.export_check import check_export
|
|
|
|
|
from mdhelper.main_window import MainWindow
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_resource_consumers_agree_on_examples_and_references(tmp_path):
|
|
|
|
|
path = tmp_path / "note.md"
|
|
|
|
|
source = '```md\n\n```\n\n![real][picture]\n\n[picture]: real.png\n'
|
|
|
|
|
path.write_text(source, encoding="utf-8")
|
|
|
|
|
(tmp_path / "real.png").write_bytes(b"image")
|
|
|
|
|
assert [(r.url, r.line) for r in analyze_document(source).references] == [("real.png", 5)]
|
|
|
|
|
assert check_markdown_file(path) == []
|
|
|
|
|
assert check_export(source, path, "html") == []
|
2026-10-29 19:50:56 +08:00
|
|
|
from mdhelper.file_watch import FileWatchService
|
|
|
|
|
assert FileWatchService.referenced_local_paths(path) == {tmp_path / "real.png"}
|
2026-10-29 19:18:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_images_inside_links_and_html_attributes_have_correct_spans(tmp_path):
|
|
|
|
|
source = '[](guide.md)\n\n<img alt=\'src="fake.png"\' src="real.png">\n<!-- <img src="hidden.png"> -->'
|
|
|
|
|
references = analyze_document(source).references
|
|
|
|
|
assert [(r.url, r.image) for r in references] == [("image.png", True), ("guide.md", False), ("real.png", True)]
|
|
|
|
|
edits = [r.edit_url("new.png") for r in references if r.image]
|
|
|
|
|
assert apply_source_edits(source, edits) == source.replace('](image.png', '](new.png').replace('src="real.png"','src="new.png"')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_relocation_plan_does_not_write_before_all_resources_validate(tmp_path):
|
|
|
|
|
original, target = tmp_path / "original.md", tmp_path / "new" / "copy.md"
|
|
|
|
|
(tmp_path / "one.png").write_bytes(b"image")
|
|
|
|
|
import pytest
|
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
|
plan_image_relocation('\n', original, target)
|
|
|
|
|
assert not target.parent.exists()
|
|
|
|
|
plan = plan_image_relocation('', original, target)
|
|
|
|
|
assert plan.files and not target.parent.exists()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_file_url_and_encoded_relative_path_resolve_once(tmp_path):
|
|
|
|
|
path = tmp_path / "note.md"
|
|
|
|
|
asset = tmp_path / "100%20图片.png"
|
|
|
|
|
assert resolve_local_reference(path, asset.as_uri()) == asset
|
|
|
|
|
assert resolve_local_reference(path, "100%2520%E5%9B%BE%E7%89%87.png") == asset
|
|
|
|
|
assert resolve_local_reference(path, "https://example.com/image.png") is None
|