fix: gbrain export auto-appends .md to internal slug-form links#123
Open
vinsew wants to merge 1 commit intogarrytan:masterfrom
Open
fix: gbrain export auto-appends .md to internal slug-form links#123vinsew wants to merge 1 commit intogarrytan:masterfrom
vinsew wants to merge 1 commit intogarrytan:masterfrom
Conversation
GBrain stores internal cross-page references in slug form (e.g. `[Alice](./alice)`) because the slug is the canonical identifier in the DB. That works inside GBrain's own resolution layer. But when those pages are exported as `.md` files on disk and opened in standard markdown viewers (Obsidian, VS Code preview, GitHub web view, typical mkdocs/jekyll renderers), the viewers look for a literal file at `./alice` — which doesn't exist. The actual file is `./alice.md`. Result: every internal link in an exported brain is silently broken on disk. The user clicks `[小龙]` in `龙虾群.md`, sees a 404 / empty page, and cannot navigate the brain outside of GBrain itself. This defeats half the value of having the brain stored as portable markdown. Fix: Add `normalizeInternalLinks(content)` that runs over each page's serialized markdown right before `writeFileSync` and rewrites slug-form internal links to filename-form by appending `.md`: [Alice](./alice) -> [Alice](./alice.md) [Alice](alice) -> [Alice](alice.md) [Alice](../people/alice) -> [Alice](../people/alice.md) [小龙](../people/小龙) -> [小龙](../people/小龙.md) Conservative: leaves untouched anything that looks external or already extended: - URL schemes (http:, https:, mailto:, ftp:, file:, tel:, ...) — skip - Anchors (#section) — skip - Empty targets — skip - Trailing slash (directory references) — skip - Already has any extension (.md, .png, .pdf, .MD, ...) — skip - Preserves query strings and anchors when appending: [Section](./alice#bio) -> [Section](./alice.md#bio) [Search](./alice?q=t) -> [Search](./alice.md?q=t) The DB content stays slug-form (GBrain's internal convention is unchanged). Only the on-disk export gets the `.md` annotation, so the exported markdown is viewable as-is by any standard renderer. Real-world reproduction this fix addresses: $ gbrain put 龙虾群 < <(echo '[小龙](./小龙)') $ gbrain export --dir /tmp/out $ cat /tmp/out/龙虾群.md # before this PR: contains [小龙](./小龙) — clicking 404s # after this PR: contains [小龙](./小龙.md) — clicking opens the file Impact: - 2 files changed, +149 / -1 lines (1 line of helper invocation + ~40 lines of helper + comment + 26 tests) - Zero behavior change for external URLs, anchors, or already-extended links - DB content unchanged — only the on-disk export representation gains the `.md` annotation - Existing exports remain valid (re-running export on an already-exported brain is idempotent because already-extended links are skipped) Tests: - 26 new tests covering: same-dir slug, parent-dir slug, deep nesting, CJK slugs, multiple links per line, multi-line markdown, all 6 external schemes (http/https/mailto/file/ftp/tel), all 4 extension cases (md/png/pdf/uppercase), anchor preservation, query preservation, empty/trailing-slash/no-link edge cases. - All 26 tests pass. - Full suite: 612 pass / no new regressions (4 pre-existing PGLiteEngine failures are unrelated and exist on master). Fifth in a series of practical PRs from a real Chinese-speaking deploy. Companion to: - garrytan#114 (chunker CJK) - garrytan#115 (slugify CJK) - garrytan#119 (sync git quotepath CJK) - garrytan#121 (self-contained API keys) Same theme: GBrain is meaningfully more useful when the markdown export is a first-class deliverable, not a half-broken side-effect.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
GBrain stores internal cross-page references in slug form (e.g.
[Alice](./alice)) because the slug is the canonical identifier in the DB. That works inside GBrain's own resolution layer.But when those pages are exported as
.mdfiles on disk and opened in standard markdown viewers (Obsidian, VS Code preview, GitHub web view, mkdocs/jekyll/hugo renderers), the viewers look for a literal file at./alice— which doesn't exist. The actual file is./alice.md.Result: every internal link in an exported brain is silently broken on disk. The user clicks
[小龙]in龙虾群.md, sees a 404, and cannot navigate the brain outside of GBrain itself. This defeats half the value of having the brain stored as portable markdown.Reproduction
Real-world hit: a Chinese-speaking deployment where Hermes (an agent) generated 14 entity pages with internal cross-links. Every link in the exported
.mdfiles was broken, even thoughgbrain queryworked perfectly.Fix
Add
normalizeInternalLinks(content)insrc/commands/export.tsthat runs over each page's serialized markdown right beforewriteFileSyncand rewrites slug-form internal links to filename-form by appending.md.Conservative — leaves untouched anything that looks external or already extended:
#section).md,.png, ...)The DB content stays slug-form (GBrain's internal convention is unchanged). Only the on-disk export gets the
.mdannotation.Impact
gbrain syncreading back the.md-annotated files normalizes paths back to slugs via existing slugify logic — round-trip worksTest plan
test/export.test.tscovering:.md/.png/.pdf/ uppercase.MD)bun test: 612 pass, no new regressions (the 4 pre-existingPGLiteEnginefailures are unrelated and exist onmaster)Context
Fifth in a series of small, focused PRs from a real Chinese-speaking deployment. Companion to:
Same theme: GBrain is meaningfully more useful when the markdown export is a first-class deliverable, not a half-broken side-effect. Combined with #121 (self-contained keys), users running
gbrain exportfrom cron / agent subprocess now get a fully-functional, portable markdown brain on disk — true to the "markdown is the source of truth" architecture this project advocates.