Skip to content

fix(core): remove file-promotion + slugify paths + clean dev output + full watch rebuild#132

Closed
sinsombat wants to merge 1 commit into
docmd-io:mainfrom
sinsombat:fix/auto-router-remove-file-promotion
Closed

fix(core): remove file-promotion + slugify paths + clean dev output + full watch rebuild#132
sinsombat wants to merge 1 commit into
docmd-io:mainfrom
sinsombat:fix/auto-router-remove-file-promotion

Conversation

@sinsombat
Copy link
Copy Markdown
Contributor

@sinsombat sinsombat commented May 16, 2026

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 .md files but no index.md, the first file was promoted to the folder URL, silently dropping its filename. This caused inconsistent URLs (files in folders with index.md kept 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:

docs/SA/my feature/page one.md
  nav link → ./SA/my feature/page one/   (literal space)
  browser  → /SA/my%20feature/page%20one/ (%20-encodes the space)
  server   → tries SA/my%20feature/ on disk  → not found → 404

normalizeInternalHref adds a trailing slash but does not slugify segments. The raw OS filename was used verbatim in both the nav href and the site/ output path.

Fix

Added a slugifySegment helper and applied it in both places:

  • auto-router.ts — when building each relPath segment from fs.readdirSync
  • generator.ts — when building htmlOutputPath from relativePath

Both sides use identical logic so the nav URL and the output file path always agree:

docs/SA/my feature/page one.md
  nav: /SA/my-feature/page-one/   ✓  200
  out: site/SA/my-feature/page-one/index.html  ✓

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. Added fs.remove(rootOutputDir) before the initial dev build so every docmd dev session starts clean.


Fix 4 — Full rebuild on file watch (dev.ts)

The file watcher called buildSite with targetFiles: [filePath], regenerating only the modified page. Every other page kept its old nav HTML. Adding a new .md file while the dev server was running meant the link never appeared in the sidebar of existing pages. Removed targetFiles from the watcher rebuild so every change triggers a full rebuild (~200–300 ms for typical doc projects).


Note

With Fix 1, _sourceFile is never set on nav items, so the navDesignatedIndexFiles logic in generator.ts becomes a no-op. Left in place as it may be useful for future features.

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).
@sinsombat sinsombat closed this May 16, 2026
@sinsombat sinsombat changed the title fix(core): remove implicit file-promotion in auto-router fix(core): remove implicit file-promotion + clean dev output + full rebuild on watch May 16, 2026
@sinsombat sinsombat changed the title fix(core): remove implicit file-promotion + clean dev output + full rebuild on watch fix(core): remove file-promotion + slugify paths + clean dev output + full watch rebuild May 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant