Files
wiki/wikilib.py
Sorin Savu dcc6bba61b upgrades
2026-07-13 09:51:28 +03:00

26 lines
749 B
Python

"""Small shared helpers: slugs and [[wiki link]] parsing."""
from __future__ import annotations
import re
_WIKILINK_RE = re.compile(r"\[\[([^\]]+)\]\]")
_SLUG_STRIP = re.compile(r"[^a-z0-9]+")
def slugify(text: str) -> str:
"""Lowercase, collapse non-alphanumerics to single hyphens, trim.
Mirrors the JS slugify() in editor.html — keep the two in sync.
"""
s = _SLUG_STRIP.sub("-", text.strip().lower()).strip("-")
return s or "untitled"
def parse_links(body_md: str) -> list[str]:
"""Return the distinct target slugs referenced by [[...]] in a note body."""
seen: dict[str, None] = {}
for m in _WIKILINK_RE.finditer(body_md or ""):
seen.setdefault(slugify(m.group(1)), None)
return list(seen)