Skip to content

Commit f7a1a92

Browse files
committed
fix(converter): 保留來源副檔名避免 Markdown 檔名衝突
將輸出檔名改為保留來源完整檔名後再附加 .md。\n這樣可避免像 A.docx 與 A.xlsx 轉出後發生同名覆蓋,並同步更新相關測試。
1 parent 3f6fd8d commit f7a1a92

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

app/converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def is_supported(self, task: SyncTask, path: Path) -> bool:
2121

2222
def target_path_for(self, task: SyncTask, source_path: Path) -> Path:
2323
relative = source_path.relative_to(task.paths.source_path())
24-
return task.paths.output_dir_path() / relative.with_suffix(".md")
24+
return task.paths.output_dir_path() / relative.parent / f"{relative.name}.md"
2525

2626
def convert_path(self, task: SyncTask, source_path: Path) -> str:
2727
ext = source_path.suffix.lower()

tests/test_converter.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,24 @@ def test_target_path_for(tmp_path: Path) -> None:
3333

3434
target = converter.target_path_for(task, source_path)
3535

36-
assert target == Path(task.paths.target_root) / "md" / "nested" / "report.md"
36+
assert target == Path(task.paths.target_root) / "md" / "nested" / "report.docx.md"
37+
38+
39+
def test_target_path_for_keeps_source_extension_to_avoid_collisions(tmp_path: Path) -> None:
40+
task = sample_task(tmp_path)
41+
converter = Converter()
42+
docx_path = Path(task.paths.source_dir) / "nested" / "A.docx"
43+
xlsx_path = Path(task.paths.source_dir) / "nested" / "A.xlsx"
44+
docx_path.parent.mkdir()
45+
docx_path.write_text("placeholder", encoding="utf-8")
46+
xlsx_path.write_text("placeholder", encoding="utf-8")
47+
48+
docx_target = converter.target_path_for(task, docx_path)
49+
xlsx_target = converter.target_path_for(task, xlsx_path)
50+
51+
assert docx_target.name == "A.docx.md"
52+
assert xlsx_target.name == "A.xlsx.md"
53+
assert docx_target != xlsx_target
3754

3855

3956
def test_convert_text_file_to_markdown(tmp_path: Path) -> None:

tests/test_sync_engine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_sync_engine_copies_markdown_file(tmp_path: Path) -> None:
3333
engine.queued_paths[task.id or 0][source_file] = 0
3434
engine._process_task_queue(task)
3535

36-
target_file = Path(task.paths.target_root) / task.paths.output_subdir / "123.md"
36+
target_file = Path(task.paths.target_root) / task.paths.output_subdir / "123.md.md"
3737
assert target_file.read_text(encoding="utf-8") == "hello\n"
3838

3939

@@ -50,7 +50,7 @@ def test_sync_engine_commits_inside_target_root(tmp_path: Path) -> None:
5050

5151
assert engine.statuses[task.id or 0].last_error is None
5252
assert (Path(task.paths.target_root) / ".git").exists()
53-
assert (Path(task.paths.target_root) / task.paths.output_subdir / "123.md").exists()
53+
assert (Path(task.paths.target_root) / task.paths.output_subdir / "123.md.md").exists()
5454

5555

5656
def test_reload_tasks_clears_queue_on_disable_and_removes_deleted_task(tmp_path: Path) -> None:

0 commit comments

Comments
 (0)