This commit is contained in:
Sorin Savu
2026-07-13 09:51:28 +03:00
parent 0ad947ba56
commit dcc6bba61b
10 changed files with 1612 additions and 8 deletions
+25
View File
@@ -0,0 +1,25 @@
"""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)