MCPcopy Create free account
hub / github.com/VectifyAI/OpenKB / strip_ghost_wikilinks

Function strip_ghost_wikilinks

openkb/lint.py:63–127  ·  view source on GitHub ↗

Strip [[wikilinks]] whose targets do not exist in ``known_targets``. For each ``[[X]]`` or ``[[X|alias]]`` in ``content``: - If ``X`` is in ``known_targets`` exactly, the link is kept as-is. - Otherwise, ``X`` is normalized (see :func:`_normalize_target`) and matched against the

(
    content: str,
    known_targets: set[str],
    *,
    norm_index: dict[str, str] | None = None,
)

Source from the content-addressed store, hash-verified

61
62
63def strip_ghost_wikilinks(
64 content: str,
65 known_targets: set[str],
66 *,
67 norm_index: dict[str, str] | None = None,
68) -> tuple[str, list[str]]:
69 """Strip [[wikilinks]] whose targets do not exist in ``known_targets``.
70
71 For each ``[[X]]`` or ``[[X|alias]]`` in ``content``:
72
73 - If ``X`` is in ``known_targets`` exactly, the link is kept as-is.
74 - Otherwise, ``X`` is normalized (see :func:`_normalize_target`) and
75 matched against the normalized form of each known target. On a hit,
76 the link is rewritten to the canonical target form.
77 - Otherwise, the brackets are removed and the link becomes plain text
78 (the alias if provided, otherwise the slug rendered as words).
79
80 Args:
81 content: Markdown text containing zero or more ``[[wikilinks]]``.
82 known_targets: Valid link targets, e.g.
83 ``{"concepts/attention", "summaries/paper"}``.
84 norm_index: Optional pre-built normalized index from
85 :func:`build_norm_index`. Pass this when calling in a loop
86 with the same ``known_targets`` to skip redundant rebuilds.
87
88 Returns:
89 Tuple of ``(rewritten_content, ghost_targets)`` where
90 ``ghost_targets`` is the list of unresolved targets that were
91 stripped (one entry per occurrence, in document order).
92 """
93 if norm_index is None:
94 norm_index = build_norm_index(known_targets)
95
96 ghosts: list[str] = []
97
98 def _repl(m: re.Match) -> str:
99 raw = m.group(1)
100 if "|" in raw:
101 target, alias = raw.split("|", 1)
102 target = target.strip()
103 alias = alias.strip()
104 else:
105 target = raw.strip()
106 alias = None
107
108 # Direct hit
109 if target in known_targets:
110 return m.group(0)
111
112 # Fuzzy normalized hit → rewrite to canonical
113 canonical = norm_index.get(_normalize_target(target))
114 if canonical is not None:
115 if alias:
116 return f"[[{canonical}|{alias}]]"
117 return f"[[{canonical}]]"
118
119 # Ghost — strip brackets, leave readable display
120 ghosts.append(target)

Calls 1

build_norm_indexFunction · 0.85