fix(core): remove file-promotion + slugify paths + clean dev output + full watch rebuild#132
Closed
sinsombat wants to merge 1 commit into
Closed
Conversation
When a folder contains .md files but no index.md, the auto-router
designated the first file (alphabetically by title) as the folder-level
index by overwriting its path with basePath. This had two problems:
1. Silent URL mismatch (fixed in the previous commit via the generator.ts
trailing-slash fix) — the nav link pointed to the folder URL while the
file was still output at its own path, producing a 404 on click.
2. Even when working correctly (after the generator.ts fix), the promotion
drops the filename from the URL entirely:
docs/SA/feature-discovery/my-doc.md → /SA/feature-discovery/
instead of the expected:
docs/SA/feature-discovery/my-doc.md → /SA/feature-discovery/my-doc/
This creates inconsistent URL behaviour: files in folders WITH index.md
keep their filename in the URL; files in folders WITHOUT index.md do not.
It also makes it impossible to distinguish between two same-named files
in sibling folders by URL alone.
Fix: remove the promotion block so every .md file always generates a URL
that includes its own filename, regardless of whether the parent folder
has an index.md.
Before:
docs/SA/feature-discovery/my-doc.md → nav: /SA/feature-discovery
→ output: site/SA/feature-discovery/index.html
After:
docs/SA/feature-discovery/my-doc.md → nav: /SA/feature-discovery/my-doc/
→ output: site/SA/feature-discovery/my-doc/index.html
Impact: folders without index.md are rendered as collapsible section
headers in the sidebar (no link) rather than having the first child file
silently represent the section. This is more explicit and predictable.
The _sourceFile field on nav items is no longer set; the corresponding
navDesignatedIndexFiles logic in generator.ts becomes a no-op (harmless).
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.
Background
This PR follows #131. Four related bugs were found while investigating that issue.
Fix 1 — Remove implicit file-promotion (
auto-router.ts)When a folder had
.mdfiles but noindex.md, the first file was promoted to the folder URL, silently dropping its filename. This caused inconsistent URLs (files in folders withindex.mdkept their filename; files without did not) and made same-named files in sibling folders indistinguishable. Removed the promotion block. Every file now always generates a URL that includes its own filename.Fix 2 — Slugify path segments (
auto-router.ts+generator.ts)Problem
Files and folders whose names contain spaces or URL-unsafe characters produced 404 in both dev and production:
normalizeInternalHrefadds a trailing slash but does not slugify segments. The raw OS filename was used verbatim in both the nav href and thesite/output path.Fix
Added a
slugifySegmenthelper and applied it in both places:auto-router.ts— when building eachrelPathsegment fromfs.readdirSyncgenerator.ts— when buildinghtmlOutputPathfromrelativePathBoth sides use identical logic so the nav URL and the output file path always agree:
Slugification rules: spaces →
-,[^a-zA-Z0-9\-_.~]→-, consecutive hyphens collapsed, leading/trailing hyphens stripped. Files with already-safe names are unaffected.Fix 3 — Clean output dir before initial dev build (
dev.ts)After any change to auto-router URL structure (e.g. applying this PR), old HTML files built under the previous URL structure remain in
site/. New nav links point to new URLs but new files may not exist yet — causing 404 on nav click. Addedfs.remove(rootOutputDir)before the initial dev build so everydocmd devsession starts clean.Fix 4 — Full rebuild on file watch (
dev.ts)The file watcher called
buildSitewithtargetFiles: [filePath], regenerating only the modified page. Every other page kept its old nav HTML. Adding a new.mdfile while the dev server was running meant the link never appeared in the sidebar of existing pages. RemovedtargetFilesfrom the watcher rebuild so every change triggers a full rebuild (~200–300 ms for typical doc projects).Note
With Fix 1,
_sourceFileis never set on nav items, so thenavDesignatedIndexFileslogic ingenerator.tsbecomes a no-op. Left in place as it may be useful for future features.