30 lines
1.0 KiB
Python
30 lines
1.0 KiB
Python
"""Handler for importing Markdown files into Obsidian."""
|
|
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
from utils.config import ensure_unique_path, sanitize_filename
|
|
|
|
|
|
def handle_markdown(file_path: Path, vault_path: Path) -> Path:
|
|
"""Import a Markdown file into the Obsidian vault.
|
|
|
|
- Copies contents to a new note.
|
|
- Uses the filename (without extension) as the note title.
|
|
- Prepends a title heading if the file does not already start with it.
|
|
"""
|
|
title = sanitize_filename(file_path.stem)
|
|
target_name = f"{title}.md"
|
|
target_path = ensure_unique_path(vault_path / target_name)
|
|
|
|
content = file_path.read_text(encoding="utf-8")
|
|
|
|
# Prepend title if the file doesn't already start with it as an H1
|
|
first_line = content.splitlines()[0] if content.strip() else ""
|
|
if not first_line.strip().startswith(f"# {title}"):
|
|
content = f"# {title}\n\n{content}"
|
|
|
|
target_path.write_text(content, encoding="utf-8")
|
|
print(f"[MD] Created note: {target_path}")
|
|
return target_path
|