Mirrored from https://github.com/Camo67/OCIU-npo.git
Synced Nov 21, 06:00 UTC
fix/restore-static-site
| 1 | import os |
| 2 | from pathlib import Path |
| 3 | |
| 4 | class ObsidianVault: |
| 5 | def __init__(self): |
| 6 | self._vault_path = None |
| 7 | |
| 8 | @property |
| 9 | def vault_path(self): |
| 10 | return os.getenv("OBSIDIAN_VAULT_PATH", "./obsidian_vault") |
| 11 | |
| 12 | def write_note(self, title: str, content: str, folder: str = ""): |
| 13 | os.makedirs(self.vault_path, exist_ok=True) |
| 14 | target_dir = Path(self.vault_path) / folder |
| 15 | target_dir.mkdir(parents=True, exist_ok=True) |
| 16 | |
| 17 | file_path = target_dir / f"{title}.md" |
| 18 | with open(file_path, "w") as f: |
| 19 | f.write(content) |
| 20 | return str(file_path) |
| 21 | |
| 22 | def read_note(self, title: str, folder: str = ""): |
| 23 | file_path = Path(self.vault_path) / folder / f"{title}.md" |
| 24 | if file_path.exists(): |
| 25 | with open(file_path, "r") as f: |
| 26 | return f.read() |
| 27 | return None |
| 28 | |
| 29 | def list_notes(self, folder: str = ""): |
| 30 | target_dir = Path(self.vault_path) / folder |
| 31 | if target_dir.exists(): |
| 32 | return [f.stem for f in target_dir.glob("*.md")] |
| 33 | return [] |
| 34 | |
| 35 | obsidian = ObsidianVault() |