Skip to content

Seeing the link graph

Every link between two notes in a bundle is an arrow, and okfctl will show you all of them. okfctl serve draws the arrows in your browser, where you can filter by type or folder and click a note to see what it links to and what links to it. okfctl graph export prints the same graph as JSON or as Graphviz DOT for a script to query or for Graphviz to draw. Notes that nothing points at come out dashed either way.

The OKF spec says which arrows exist. Its “The bundle graph” section names two kinds. One is a Markdown link from one note to another, and the other is a header path field (like resource) whose value leads to another note. It treats each arrow as a plain relationship with no label, and it stops there. The web view, the two export formats, the folder grouping, and the orphan flag are okfctl’s additions. The spec doesn’t use the word orphan. Neither command writes to the bundle. Point them at any bundle you can read.

Run everything below in an empty directory. If bundles and links are new to you, read Concepts first.

Make a bundle with three notes, two in wine/ and one in grapes/, then write a fourth by hand:

$ okfctl bundle init cellar
Initialized OKF bundle in cellar
$ okfctl node new wine/tannin --type Concept --title Tannin --bundle cellar
Created cellar/wine/tannin.md
$ okfctl node new wine/oak --type Concept --title Oak --bundle cellar
Created cellar/wine/oak.md
$ okfctl node new grapes/nebbiolo --type Grape --title Nebbiolo --bundle cellar
Created cellar/grapes/nebbiolo.md
$ printf '\nOak aging adds [tannin](tannin.md). Barrels deserve a [note](barrel.md) of their own.\n' >> cellar/wine/oak.md
$ printf '\nNebbiolo is high in [tannin](../wine/tannin.md).\n' >> cellar/grapes/nebbiolo.md
$ printf -- '---\ntype: Concept\ntitle: Acidity\n---\n\n# Acidity\n\nAcidity balances [tannin](tannin.md).\n' > cellar/wine/acidity.md

Oak and Nebbiolo each link to Tannin, and so does Acidity. Oak also links to barrel.md. No one has written that note. node new added the first three notes to their folder’s index.md. Acidity, written with printf, isn’t on any index.

Point serve at the bundle, then load the address it prints:

$ okfctl serve cellar
okfctl serve: http://127.0.0.1:8080 (Ctrl-C to stop)

The okfctl serve page for the cellar bundle. Four notes sit in a cluster, with Tannin selected in the middle and Oak, Nebbiolo, and Acidity linked to it. Acidity has a dashed pink ring, and a side panel shows Tannin’s path, type, folder, and the three notes that link to it.

The count in the top bar reads 4 nodes · 3 edges. Each note is a dot with its title beside it (or its file name, if it has no title), and a dashed pink ring marks an orphan. The layout pushes dots apart and pulls linked ones close from a random start, and the picture comes out a little different every time you load it.

Click a dot to open a panel on the right. It shows the note’s path, its type, its folder, the word orphan when that applies, and two lists (the notes it links to and the notes that link to it). Click Tannin and the second list names all three of the others. Close the panel with the ✕.

The two menus in the bar filter the drawing. type holds every type the bundle uses (here Concept and Grape). neighborhood holds each folder at the bundle root (here grapes and wine). Pick one and the rest disappear, along with any arrow that has an end out of view. Notes at the top of the bundle, outside any folder, have no neighborhood. They show only while that menu says all.

The page is a snapshot. serve loads the bundle at startup and writes nothing back. If you edit a note, reloading shows the old version until you stop serve with Ctrl-C and run it again. Give it a path that doesn’t exist (okfctl serve missing) and it exits 1 with no such file or directory in place of an address.

By default the server listens on 127.0.0.1:8080, and only your own machine can open it. Use --addr when port 8080 is taken (--addr 127.0.0.1:9000). Think twice about 0.0.0.0:8080: that opens the page to your whole network with no login in front of it. On a busy port, serve prints its address line and then dies with bind: address already in use.

The page itself draws from /graph.json, and that path returns the bytes graph export prints (it’s the same code). Any other path gets a 404.

graph export prints JSON by default. Each note has a path, title, type, neighborhood (the first folder in its path, empty at the top of the bundle), and orphan flag. Each edge has a from and a to:

$ okfctl graph export cellar | jq '.nodes[1]'
{
"path": "wine/acidity.md",
"title": "Acidity",
"type": "Concept",
"neighborhood": "wine",
"orphan": true
}
$ okfctl graph export cellar | jq -c '.edges[]'
{"from":"grapes/nebbiolo.md","to":"wine/tannin.md"}
{"from":"wine/acidity.md","to":"wine/tannin.md"}
{"from":"wine/oak.md","to":"wine/tannin.md"}

Notes come out sorted by path and edges by from, then to. Export an unchanged bundle twice and you get the same bytes. Commit the export and diff it later. Scripts want JSON. These queries count the notes that link to Tannin and list the notes nothing links to:

$ okfctl graph export cellar | jq '[.edges[] | select(.to == "wine/tannin.md")] | length'
3
$ okfctl graph export cellar | jq -r '.nodes[] | select(.orphan) | .path'
wine/acidity.md

To fail CI on an orphan, you don’t need jq. okfctl lint --strict does it by the same rule (see the curation health guide).

--format dot prints Graphviz input. The graph reads left to right, each note is labeled with its title, and orphans are dashed:

$ okfctl graph export --format dot cellar
digraph okf {
rankdir=LR;
"grapes/nebbiolo.md" [label="Nebbiolo"];
"wine/acidity.md" [label="Acidity", style=dashed];
"wine/oak.md" [label="Oak"];
"wine/tannin.md" [label="Tannin"];
"grapes/nebbiolo.md" -> "wine/tannin.md";
"wine/acidity.md" -> "wine/tannin.md";
"wine/oak.md" -> "wine/tannin.md";
}

okfctl doesn’t draw pictures itself. Install Graphviz and pipe the DOT into its dot command. Out comes an SVG to open in a browser or attach to a pull request. PNG works too:

Terminal window
okfctl graph export --format dot cellar | dot -Tsvg > cellar.svg
okfctl graph export --format dot cellar | dot -Tpng > cellar.png

Request SVG from graph export and it points you to Graphviz. It rejects any other format name:

$ okfctl graph export --format svg cellar
okfctl: no native svg format; pipe dot to Graphviz: okfctl graph export --format dot | dot -Tsvg > graph.svg
$ okfctl graph export --format png cellar
okfctl: unknown format "png" (want json or dot)

Orphans are notes no other file links to. Each line on an index or in a log counts as a link here, even if index.md and log.md never get dots of their own. That’s why Oak and Nebbiolo aren’t dashed, even though no arrow in the picture points at them: node new put each on its folder’s index. You listed Acidity nowhere. Rebuild the index and Acidity joins the rest:

$ okfctl index build cellar
Wrote 3 index.md file(s) under cellar
$ okfctl graph export cellar | jq -r '.nodes[] | select(.orphan) | .path'

The query prints nothing now. Self-links are odd. The export draws them as edges, but a note that links only to itself is still an orphan. lint reports orphans by the same rule.

A header field that holds a path, like resource, makes an arrow when its value leads to another note. The spec’s “Path fields” section lists the fields. Here a note points at Tannin from its header and nowhere in its body:

$ printf -- '---\ntype: Metric\ntitle: Grip\nresource: /wine/tannin.md\n---\n\n# Grip\n' > cellar/wine/grip.md
$ okfctl graph export --format dot cellar | grep grip
"wine/grip.md" [label="Grip", style=dashed];
"wine/grip.md" -> "wine/tannin.md";

Grip links out and gets dashed. Nothing links in to it. The leading / makes the path start at the top of the bundle. Leave the slash off and a bundle that declares OKF 0.3 reads the path from the note’s own folder, and the OKF versions guide shows the graph losing an arrow over that one character.

A broken link isn’t drawn. Oak’s link to barrel.md leads to no note, and the graph skips it. The spec says a broken link is fine (it can be a note no one has written), and the graph only joins notes that exist. okfctl analyze lists every one under its dangling forward-links heading, and okfctl lint flags those that look like a typo for a note that exists elsewhere.

A note that links to the same target twice gets two identical edges in the export. Count with care if you total edges in a script.

Only notes are drawn. Markdown files with no frontmatter (or with no type) aren’t notes and never appear, and neither does anything in a folder whose name starts with a dot.

graph export skips vendored folders like node_modules, prints a note on stderr saying so, and takes --no-ignore to include them. serve skips the same folders silently and has no --no-ignore:

$ mkdir -p cellar/node_modules/pkg
$ cp cellar/wine/oak.md cellar/node_modules/pkg/oak.md
$ okfctl graph export cellar | jq '.nodes | length'
note: skipped 1 vendored/derived directory (node_modules); pass --no-ignore to include them
5
$ okfctl graph export --no-ignore cellar | jq '.nodes | length'
6

You can’t zoom or pan the browser view, and it won’t show you a note’s text. It works best on a bundle you can take in at a glance. For a large one, filter by neighborhood or export DOT and let Graphviz lay it out.

  • The curation health guide shows lint and analyze reporting orphans and broken links, and failing CI on them.
  • The search guide walks the same graph with search --neighbors. It ignores the direction of each arrow.
  • The command reference lists every flag on serve and graph export.

Made by Casey West.