-
Notifications
You must be signed in to change notification settings - Fork 213
feat: add the obsidian connector for nao cli [nao-cli] #1075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Francois-lenne
wants to merge
5
commits into
getnao:main
Choose a base branch
from
Francois-lenne:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
124838e
feat: add the obsidian connector for nao cli [nao-cli]
Francois-lenne 3c591d2
Fix Obsidian context type narrowing
Francois-lenne 5b3a7a4
Clarify Obsidian installation in CLI README
Francois-lenne cfd85be
Share Obsidian output directory between sync and templates
Francois-lenne 5fe1773
Fix Obsidian circular import
Francois-lenne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """Obsidian syncing functionality for syncing local Obsidian vault notes.""" | ||
|
|
||
| from .provider import ObsidianSyncProvider | ||
|
|
||
| __all__ = ["ObsidianSyncProvider"] |
115 changes: 115 additions & 0 deletions
115
cli/nao_core/commands/sync/providers/obsidian/provider.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| from pathlib import Path, PurePosixPath | ||
|
|
||
| from rich.console import Console | ||
|
|
||
| from nao_core.config.base import NaoConfig | ||
| from nao_core.config.obsidian import ObsidianConfig | ||
| from nao_core.obsidian import OBSIDIAN_OUTPUT_DIR | ||
|
|
||
| from ..base import SyncProvider, SyncResult | ||
|
|
||
| console = Console() | ||
|
|
||
| IGNORED_DIR_NAMES = {".obsidian"} | ||
|
|
||
|
|
||
| def cleanup_stale_notes(synced_files: set[str], output_path: Path, verbose: bool = False) -> int: | ||
| """Remove markdown files and empty directories that were not synced.""" | ||
| if not output_path.exists(): | ||
| return 0 | ||
|
|
||
| removed_count = 0 | ||
| for file_path in sorted(output_path.rglob("*.md"), reverse=True): | ||
| relative = PurePosixPath(file_path.relative_to(output_path)).as_posix() | ||
| if relative not in synced_files: | ||
| file_path.unlink() | ||
| removed_count += 1 | ||
| if verbose: | ||
| console.print(f" [dim red]removing stale note:[/dim red] {relative}") | ||
|
|
||
| for dir_path in sorted((p for p in output_path.rglob("*") if p.is_dir()), reverse=True): | ||
| if dir_path != output_path: | ||
| try: | ||
| dir_path.rmdir() | ||
| except OSError: | ||
| pass | ||
|
|
||
| return removed_count | ||
|
|
||
|
|
||
| def iter_markdown_files(vault_path: Path): | ||
| """Yield markdown files from the vault, skipping Obsidian metadata directories.""" | ||
| for path in vault_path.rglob("*.md"): | ||
| if any(part in IGNORED_DIR_NAMES for part in path.parts): | ||
| continue | ||
| if not path.is_file(): | ||
| continue | ||
| yield path | ||
|
|
||
|
|
||
| class ObsidianSyncProvider(SyncProvider): | ||
| """Provider for syncing local Obsidian vault notes.""" | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| return "Obsidian" | ||
|
|
||
| @property | ||
| def emoji(self) -> str: | ||
| return "🗂️" | ||
|
|
||
| @property | ||
| def default_output_dir(self) -> str: | ||
| return OBSIDIAN_OUTPUT_DIR | ||
|
|
||
| def get_items(self, config: NaoConfig) -> list[ObsidianConfig]: | ||
| return [config.obsidian] if config.obsidian else [] | ||
|
|
||
| def sync( | ||
| self, | ||
| items: list[ObsidianConfig], | ||
| output_path: Path, | ||
| project_path: Path | None = None, | ||
| *, | ||
| threads: int = 1, | ||
| ) -> SyncResult: | ||
| if not items: | ||
| console.print("\n[dim]No Obsidian vault configured[/dim]") | ||
| return SyncResult(provider_name=self.name, items_synced=0, summary="No Obsidian configuration configured") | ||
|
|
||
| obsidian_config = items[0] | ||
| vault_path = Path(obsidian_config.path).expanduser().resolve() | ||
| if not vault_path.exists(): | ||
| raise FileNotFoundError(f"Obsidian vault path does not exist: {vault_path}") | ||
| if not vault_path.is_dir(): | ||
| raise ValueError(f"Obsidian vault path is not a directory: {vault_path}") | ||
|
|
||
| output_path.mkdir(parents=True, exist_ok=True) | ||
| notes_synced = 0 | ||
| synced_files: set[str] = set() | ||
|
|
||
| console.print(f"\n[bold cyan]{self.emoji} Syncing {self.name}[/bold cyan]") | ||
| console.print(f"[dim]Vault:[/dim] {vault_path}") | ||
| console.print(f"[dim]Location:[/dim] {output_path.absolute()}\n") | ||
|
|
||
| for note_path in iter_markdown_files(vault_path): | ||
| relative_path = note_path.relative_to(vault_path) | ||
| destination = output_path / relative_path | ||
| destination.parent.mkdir(parents=True, exist_ok=True) | ||
| destination.write_text(note_path.read_text(encoding="utf-8"), encoding="utf-8") | ||
|
|
||
| notes_synced += 1 | ||
| synced_files.add(PurePosixPath(relative_path).as_posix()) | ||
|
|
||
| removed_count = cleanup_stale_notes(synced_files, output_path, verbose=True) | ||
|
|
||
| summary = f"{notes_synced} markdown notes synced" | ||
| if removed_count > 0: | ||
| summary += f", {removed_count} stale removed" | ||
|
|
||
| return SyncResult( | ||
| provider_name=self.name, | ||
| items_synced=notes_synced, | ||
| details={"removed": removed_count}, | ||
| summary=summary, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from pydantic import BaseModel, Field | ||
|
|
||
| from nao_core.ui import UI, ask_text | ||
|
|
||
|
|
||
| class ObsidianConfig(BaseModel): | ||
| """Obsidian configuration.""" | ||
|
|
||
| path: str = Field(description="The local path to the Obsidian vault") | ||
|
|
||
| @classmethod | ||
| def promptConfig(cls) -> "ObsidianConfig": | ||
| """Interactively prompt the user for Obsidian configuration.""" | ||
| UI.info("Enter the local path to your Obsidian vault:") | ||
| path = ask_text("Vault path:", required_field=True) | ||
| return ObsidianConfig(path=path) # type: ignore[arg-type] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| """Shared Obsidian integration constants.""" | ||
|
|
||
| OBSIDIAN_OUTPUT_DIR = "docs/obsidian" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.