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

Function _remove_doc_from_pages

openkb/agent/compiler.py:1298–1378  ·  view source on GitHub ↗

Update or delete pages in ``page_dir`` affected by removing a document. For each ``{page_dir}/*.md`` whose frontmatter ``sources:`` lists ``summaries/{doc_name}``: - Remove that source from the frontmatter list. - Remove any ``- [[summaries/{doc_name}]]`` entries from the ``#

(
    wiki_dir: Path,
    doc_name: str,
    *,
    page_dir: str,
    keep_empty: bool = False,
)

Source from the content-addressed store, hash-verified

1296
1297
1298def _remove_doc_from_pages(
1299 wiki_dir: Path,
1300 doc_name: str,
1301 *,
1302 page_dir: str,
1303 keep_empty: bool = False,
1304) -> dict[str, list[str]]:
1305 """Update or delete pages in ``page_dir`` affected by removing a document.
1306
1307 For each ``{page_dir}/*.md`` whose frontmatter ``sources:`` lists
1308 ``summaries/{doc_name}``:
1309
1310 - Remove that source from the frontmatter list.
1311 - Remove any ``- [[summaries/{doc_name}]]`` entries from the
1312 ``## Related Documents`` section.
1313 - Remove any standalone ``See also: [[summaries/{doc_name}]]`` lines
1314 (left by ``_add_related_link``).
1315 - If the ``sources:`` list becomes empty AND ``keep_empty`` is False,
1316 delete the page entirely.
1317
1318 Shared by the concept and entity removal wrappers so the cleanup (in
1319 particular the standalone ``See also:`` strip) can never drift between
1320 the two page types.
1321
1322 Returns ``{"modified": [slugs...], "deleted": [slugs...]}``.
1323 """
1324 pages_dir = wiki_dir / page_dir
1325 if not pages_dir.is_dir():
1326 return {"modified": [], "deleted": []}
1327
1328 source_file = f"summaries/{doc_name}.md"
1329 bare_source = f"summaries/{doc_name}"
1330 link = f"[[{bare_source}]]"
1331
1332 modified: list[str] = []
1333 deleted: list[str] = []
1334
1335 for path in sorted(pages_dir.glob("*.md")):
1336 text = path.read_text(encoding="utf-8")
1337 # Cheap filter: skip pages that don't reference the doc at all.
1338 if source_file not in text and bare_source not in text:
1339 continue
1340
1341 new_text, sources_empty = _remove_source_from_frontmatter(text, source_file)
1342
1343 # Drop the doc's entry from the "## Related Documents" section.
1344 if link in new_text:
1345 lines = new_text.split("\n")
1346 while _remove_section_entry(lines, "## Related Documents", link):
1347 pass
1348 new_text = "\n".join(lines)
1349
1350 # Drop standalone "See also: [[summaries/{doc_name}]]" lines.
1351 # The dominant form (written by ``_add_related_link``) is a
1352 # paragraph: preceded by a blank line and trailed by either a
1353 # newline or end-of-string. The first regex matches that shape
1354 # exactly, preserving one trailing newline so paragraph spacing
1355 # in surrounding content survives.

Callers 2

Calls 3

atomic_write_textFunction · 0.90
_remove_section_entryFunction · 0.85

Tested by

no test coverage detected