graphify icon indicating copy to clipboard operation
graphify copied to clipboard

feat(export): add --with-sources to wire Obsidian notes to source content (#1968)

Open SinghAman21 opened this issue 4 months ago • 0 comments

Wire Obsidian node notes to their source content (--with-sources) — #1968

Why

Right now every note in an exported Obsidian vault is a bare stub: frontmatter, the H1 title, and a ## Connections list of wikilinks. The source_file field exists in frontmatter but it's just a dead string — it doesn't link to anything. So anyone browsing the vault (or an agent hopping between notes) has to leave the vault entirely to learn what a node actually is. The graph structure is great, but the notes carry no substance.

Users were working around this with a post-processing script that copies sources in and rewires every note — but the exporter overwrites the notes on each re-export, so they had to re-run the script every single time.

What

A new opt-in flag on the Obsidian export:

graphify export obsidian --with-sources

With it, the vault becomes usable standalone:

  • Every distinct source_file is copied into the vault under sources/ as a real note (src.md).
  • Each node note gets a callout right under its title linking to that source:
  • Optional --source-root PATH to override where source paths are resolved from disk.

How

  • graphify/export.py — to_obsidian gains with_sources / source_root params plus four small helpers:
    • resolve each source_file (stored relative to the scan root) to a real file on disk;
    • flatten nested/Windows paths into one collision-safe note name;
    • copy markdown/text verbatim, and embed code/other files in a syntax-highlighted fence;
    • write one deduped note per distinct source, then insert the callout after each node's H1.
  • graphify/cli.py — parses the two new flags and defaults resolution to [scan root, out dir] (the out-dir fallback keeps older pre-0.9.17 graphs working).

Behavior chosen deliberately:

  • Source notes are graphify-owned, so the existing manifest/prune logic cleans them up automatically — a re-export without the flag removes them and the vault returns to plain notes.
  • A node whose source can't be found on disk (or exceeds a 2 MB cap) simply gets no callout rather than a dangling link.
  • Nodes sharing a source_file all link to the same single source note.

Conclusion

This solves the issue's core ask — turn source_file into a resolvable link and bring source content into the vault — without any manual post-processing. It's fully opt-in, so default exports are unchanged, and it plays cleanly with the existing re-export/prune machinery. Covered by a new test file (tests/test_obsidian_with_sources.py, 5 tests) with the existing export suite (91 tests) still green.

SinghAman21 avatar Jul 17 '26 16:07 SinghAman21