"""Baut den Index bereits hochgeladener Notizen für die Wikilink-Auflösung.""" from __future__ import annotations from pathlib import Path from bookstack.notiz import lade_notiz def permalink(basis_url: str, seiten_id: int) -> str: """Stabiler Seitenlink. BookStack löst `/link/{id}` auf die Seite auf. Das überlebt Umbenennungen, anders als der Weg über Buch- und Seiten-Slug — und `GET /api/pages/{id}` liefert `book_slug` ohnehin nicht mit. """ return f"{basis_url.rstrip('/')}/link/{seiten_id}" def baue_index(quelle: Path, basis_url: str) -> dict[str, str]: """Dateiname ohne Endung (kleingeschrieben) → Permalink.""" if not quelle.is_dir(): return {} index: dict[str, str] = {} for pfad in sorted(quelle.rglob("*.md")): if pfad.name.startswith((".", "_")): continue roh_id = lade_notiz(pfad).frontmatter.get("bookstack_page_id", "").strip() try: seiten_id = int(roh_id) except ValueError: continue index[pfad.stem.casefold()] = permalink(basis_url, seiten_id) return index