#!/usr/bin/env python3 """Notion Markdown Export → SQLite brain.db importer""" import sqlite3, os, re, json, uuid DB = "/Users/bingshuolingdianyuanhe/WorkBuddy/2026-06-04-13-44-41/persona-brain-db/brain.db" CONTENT = "/tmp/fifth-domain-extract/content/私人与共享" def parse_md(filepath): """Parse a Notion-exported .md file""" fn = os.path.basename(filepath).replace('.md', '') uuid = None title = fn m = re.search(r'([a-f0-9]{32})$', fn) if m: uuid = m.group(1) title = fn[:fn.rindex(uuid)].strip().rstrip(' ·').strip() with open(filepath, 'r', encoding='utf-8') as f: raw = f.read()[:8000] return {"title": title, "uuid": uuid, "content": raw, "path": filepath} def main(): conn = sqlite3.connect(DB) c = conn.cursor() # Clear existing imported data c.execute("DELETE FROM documents") c.execute("DELETE FROM persona_memory") conn.commit() pages = [] dir_to_uuid = {} # directory path -> uuid of the Notion page it represents # First pass: build dir_to_uuid map for walk_root, dirs, files in os.walk(CONTENT): for f in files: if not f.endswith('.md'): continue fpath = os.path.join(walk_root, f) page = parse_md(fpath) if page['uuid']: # This file IS the Notion page for walk_root dir dir_to_uuid[walk_root] = page['uuid'] # Second pass: build pages with parent relationships for walk_root, dirs, files in os.walk(CONTENT): for f in sorted(files): if not f.endswith('.md'): continue fpath = os.path.join(walk_root, f) page = parse_md(fpath) if not page['uuid']: continue rel = os.path.relpath(fpath, CONTENT) depth = max(0, rel.count('/')) # Find parent: the directory that contains this file # is the parent dir; its Notion page UUID is in dir_to_uuid parent_id = dir_to_uuid.get(walk_root) if depth > 0 else None pages.append({ "depth": depth, "parent_id": parent_id, "path": rel, **page }) for p in pages: c.execute(""" INSERT OR REPLACE INTO documents (document_id, title, content, parent_id, path, content_type) VALUES (?, ?, ?, ?, ?, 'notion') """, (p['uuid'], p['title'], p['content'][:8000], p['parent_id'], p['path'])) conn.commit() c.execute("SELECT count(*) FROM documents") total = c.fetchone()[0] c.execute("SELECT count(*) FROM documents WHERE parent_id IS NULL") roots = c.fetchone()[0] print(f"Imported {total} pages ({roots} root pages)") c.execute("SELECT document_id, title FROM documents WHERE parent_id IS NULL") for row in c.fetchall(): print(f" [{row[0][:8]}..] {row[1][:70]}") conn.close() print("Done.") if __name__ == '__main__': main()