grafo-conocimiento.sh
· 1.5 KiB · Bash
Surowy
#!/usr/bin/env bash
# ==============================================================
# grafo-conocimiento.sh — Grafo de conocimientos desde front matter
#
# Extrae el front matter de todas las notas Markdown de un vault
# y construye un grafo JSON: nodos = notas, aristas = tags
# compartidos. Sin Python, sin IA. Solo fd, yq y jq.
#
# Uso: ./grafo-conocimiento.sh [ruta-al-vault] [salida.json]
# ./grafo-conocimiento.sh ~/notas grafo.json
#
# Dependencias: fd, yq (Mike Farah), jq
# ==============================================================
VAULT="${1:-.}"
OUT="${2:-grafo.json}"
# 1. Un nodo (objeto JSON) por cada nota, con su ruta real como id.
# Se filtran los .md que no empiezan por '---' (sin front matter).
fd -e md . "$VAULT" -x sh -c '
head -1 "$1" | grep -q "^---" || exit 0
yq --front-matter=extract -o=json \
"{\"id\": \"$1\", \"title\": .title, \"created\": .created,
\"updated\": .updated, \"tags\": (.tags // [])}" "$1" 2>/dev/null
' _ {} \; > /tmp/nodos.ndjson
# 2. Construye nodos + aristas (dos notas comparten al menos un tag).
jq -s '
. as $n
| {
nodes: [ $n[] | {id: .id, title: .title, tags: .tags} ],
edges: [
$n[] as $a | $n[] as $b
| select($a.id < $b.id)
| ([ $a.tags[] | select(. as $t | $b.tags | index($t)) ]) as $c
| select(($c | length) > 0)
| {source: $a.id, target: $b.id, tags: ($c | unique)}
]
}
' /tmp/nodos.ndjson > "$OUT"
echo "Grafo generado en $OUT"
jq '{nodos: (.nodes | length), aristas: (.edges | length)}' "$OUT"
| 1 | #!/usr/bin/env bash |
| 2 | # ============================================================== |
| 3 | # grafo-conocimiento.sh — Grafo de conocimientos desde front matter |
| 4 | # |
| 5 | # Extrae el front matter de todas las notas Markdown de un vault |
| 6 | # y construye un grafo JSON: nodos = notas, aristas = tags |
| 7 | # compartidos. Sin Python, sin IA. Solo fd, yq y jq. |
| 8 | # |
| 9 | # Uso: ./grafo-conocimiento.sh [ruta-al-vault] [salida.json] |
| 10 | # ./grafo-conocimiento.sh ~/notas grafo.json |
| 11 | # |
| 12 | # Dependencias: fd, yq (Mike Farah), jq |
| 13 | # ============================================================== |
| 14 | |
| 15 | VAULT="${1:-.}" |
| 16 | OUT="${2:-grafo.json}" |
| 17 | |
| 18 | # 1. Un nodo (objeto JSON) por cada nota, con su ruta real como id. |
| 19 | # Se filtran los .md que no empiezan por '---' (sin front matter). |
| 20 | fd -e md . "$VAULT" -x sh -c ' |
| 21 | head -1 "$1" | grep -q "^---" || exit 0 |
| 22 | yq --front-matter=extract -o=json \ |
| 23 | "{\"id\": \"$1\", \"title\": .title, \"created\": .created, |
| 24 | \"updated\": .updated, \"tags\": (.tags // [])}" "$1" 2>/dev/null |
| 25 | ' _ {} \; > /tmp/nodos.ndjson |
| 26 | |
| 27 | # 2. Construye nodos + aristas (dos notas comparten al menos un tag). |
| 28 | jq -s ' |
| 29 | . as $n |
| 30 | | { |
| 31 | nodes: [ $n[] | {id: .id, title: .title, tags: .tags} ], |
| 32 | edges: [ |
| 33 | $n[] as $a | $n[] as $b |
| 34 | | select($a.id < $b.id) |
| 35 | | ([ $a.tags[] | select(. as $t | $b.tags | index($t)) ]) as $c |
| 36 | | select(($c | length) > 0) |
| 37 | | {source: $a.id, target: $b.id, tags: ($c | unique)} |
| 38 | ] |
| 39 | } |
| 40 | ' /tmp/nodos.ndjson > "$OUT" |
| 41 | |
| 42 | echo "Grafo generado en $OUT" |
| 43 | jq '{nodos: (.nodes | length), aristas: (.edges | length)}' "$OUT" |