auto-sync: 2026-06-29_11:50:01
This commit is contained in:
parent
0bc554354b
commit
84b59c65f1
45
vault-graph-api.py
Normal file
45
vault-graph-api.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Vault Graph API - extracts wikilinks and returns graph data."""
|
||||||
|
import json, os, re, sys
|
||||||
|
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
VAULT_DIR = '/opt/jarvis-vault'
|
||||||
|
|
||||||
|
def build_graph():
|
||||||
|
files = {}
|
||||||
|
links = []
|
||||||
|
for root, dirs, fnames in os.walk(VAULT_DIR):
|
||||||
|
for f in fnames:
|
||||||
|
if not f.endswith('.md'): continue
|
||||||
|
rel = os.path.relpath(os.path.join(root, f), VAULT_DIR)
|
||||||
|
path_no_ext = rel[:-3]
|
||||||
|
files[path_no_ext] = {'id': path_no_ext, 'label': os.path.basename(rel)[:-3], 'path': rel, 'group': rel.split('/')[0] if '/' in rel else 'root'}
|
||||||
|
with open(os.path.join(root, f)) as fp:
|
||||||
|
content = fp.read()
|
||||||
|
# Extract [[wikilinks]]
|
||||||
|
for m in re.finditer(r'\[\[([^\]]+)\]\]', content):
|
||||||
|
target = m.group(1).split('|')[0].strip()
|
||||||
|
if target:
|
||||||
|
links.append({'source': path_no_ext, 'target': target})
|
||||||
|
return {'nodes': list(files.values()), 'links': links}
|
||||||
|
|
||||||
|
class GraphHandler(BaseHTTPRequestHandler):
|
||||||
|
def _send(self, data):
|
||||||
|
self.send_response(200)
|
||||||
|
self.send_header('Content-Type', 'application/json')
|
||||||
|
self.send_header('Access-Control-Allow-Origin', '*')
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(json.dumps(data).encode())
|
||||||
|
def do_GET(self):
|
||||||
|
parsed = urlparse(self.path)
|
||||||
|
if parsed.path == '/graph':
|
||||||
|
self._send(build_graph())
|
||||||
|
else:
|
||||||
|
self._send({'error': 'not found'})
|
||||||
|
def do_OPTIONS(self):
|
||||||
|
self._send({})
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
HTTPServer(('0.0.0.0', 8802), GraphHandler).serve_forever()
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user