Skip to content

Commit 4946c71

Browse files
josh-leung-KRsafishamsi
authored andcommitted
fix(obsidian): keep non-ASCII letters in community tags (#2862)
_obsidian_tag stripped every non-ASCII character, so a Korean or Japanese community label collapsed to underscores and every note in that community carried the same tag. Python's \w is Unicode-aware, so switching the filter keeps letters from any script while still dropping spaces and punctuation. Also route the .obsidian/graph.json colour-group query through the same sanitizer: it was built from the raw label, so on a non-ASCII label it queried a tag that no note carries.
1 parent 56c1060 commit 4946c71

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

‎graphify/export.py‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,19 @@ def backup_if_protected(out_dir: Path) -> "Path | None":
100100
def _obsidian_tag(name: str) -> str:
101101
"""Sanitize a community name for use as an Obsidian tag.
102102
103-
Obsidian tags only allow alphanumerics, hyphens, underscores, and slashes.
104-
Spaces become underscores; everything else is stripped.
103+
Obsidian tags accept letters from any language plus digits, hyphens,
104+
underscores and slashes; spaces and most punctuation are not allowed, and a
105+
tag cannot be digits-only. ``\w`` is Unicode-aware in Python 3, so Hangul,
106+
CJK, Cyrillic and accented Latin survive instead of being stripped (#2862):
107+
an ASCII-only filter collapsed every non-Latin community label to
108+
underscores, so every note in that community carried the same tag.
105109
"""
106-
return re.sub(r"[^a-zA-Z0-9_\-/]", "", name.replace(" ", "_"))
110+
tag = re.sub(r"[^\w\-/]", "", name.replace(" ", "_"))
111+
if not tag.strip("_-/"):
112+
return "unnamed" # label was punctuation only
113+
if tag.isdigit():
114+
return f"c{tag}" # Obsidian ignores digits-only tags
115+
return tag
107116

108117

109118
def _strip_diacritics(text: str | None) -> str:
@@ -856,7 +865,10 @@ def _community_name(cid) -> str:
856865
graph_config = {
857866
"colorGroups": [
858867
{
859-
"query": f"tag:#community/{label.replace(' ', '_')}",
868+
# Same sanitizer as the note tags (#2862): built from the raw
869+
# label, the canvas colour group queried a tag that no note
870+
# carries whenever the label held non-ASCII or punctuation.
871+
"query": f"tag:#community/{_obsidian_tag(label)}",
860872
"color": {"a": 1, "rgb": int(COMMUNITY_COLORS[cid % len(COMMUNITY_COLORS)].lstrip('#'), 16)}
861873
}
862874
for cid, label in sorted((community_labels or {}).items())

0 commit comments

Comments
 (0)