Skip to content

fix(core): strip trailing slash from _sourceFile in navDesignatedIndexFiles#131

Merged
mgks merged 1 commit into
docmd-io:mainfrom
sinsombat:fix/auto-router-trailing-slash-nav-designated-index
May 16, 2026
Merged

fix(core): strip trailing slash from _sourceFile in navDesignatedIndexFiles#131
mgks merged 1 commit into
docmd-io:mainfrom
sinsombat:fix/auto-router-trailing-slash-nav-designated-index

Conversation

@sinsombat
Copy link
Copy Markdown
Contributor

Bug

When a folder contains .md files but no index.md, the auto-router promotes the first file to the folder-level URL (e.g. _sourceFile is stored, path is reassigned to basePath). However, clicking that nav link results in a 404.

Root Cause

In auto-router.ts, normalizeInternalHref() adds a trailing slash to linkPath before it is stored as _sourceFile:

_sourceFile = "/SA/feature-discovery/feature-active-reward-edit/"  ← trailing slash

In generator.ts, the Set is populated by stripping the leading slash only:

navDesignatedIndexFiles.add(item._sourceFile.replace(/^\//, ));
// → "SA/feature-discovery/feature-active-reward-edit/"  ← still has trailing slash

But relWithoutExt (the key used in .has()) has no trailing slash:

const relWithoutExt = relativePath.replace(/\.(md|markdown|ejs)$/i, );
// → "SA/feature-discovery/feature-active-reward-edit"  ← no trailing slash

So navDesignatedIndexFiles.has(relWithoutExt) is always false, meaning isNavDesignatedIndex is never set, effectivelyIndex stays false, and the file is output at SA/feature-discovery/feature-active-reward-edit/index.html instead of the intended SA/feature-discovery/index.html.

Result: nav link /SA/feature-discovery has no matching index.html404.

Reproduction

docs/
  SA/
    feature-discovery/
      my-page.md        ← no index.md in this folder

Expected: clicking feature-discovery in the sidebar → 200 (content of my-page.md)
Actual: 404 (site/SA/feature-discovery/index.html is never generated)

Fix

Strip the trailing slash when normalising _sourceFile before adding to the Set, making it consistent with relWithoutExt:

// before
navDesignatedIndexFiles.add(item._sourceFile.replace(/^\//, ));

// after
navDesignatedIndexFiles.add(item._sourceFile.replace(/^\//, ).replace(/\/$/, ));

Tested

  • Built the fixed binary locally
  • Removed index.md workaround from a project folder
  • site/SA/feature-discovery/index.html now generated correctly
  • Nav link /SA/feature-discoveryHTTP 200

…xFiles

auto-router.js stores _sourceFile with a trailing slash (added by
normalizeInternalHref), but generator.ts compares it against relWithoutExt
which has no trailing slash. This causes Set.has() to always return false,
so files promoted to folder-level nav URLs were never output at the folder
path — resulting in a 404 when clicking those nav links.

Fix: strip trailing slash when normalising _sourceFile before adding to the
Set, making it consistent with relWithoutExt.

Reproduces with any folder that contains .md files but no index.md:
  docs/SA/feature-discovery/my-page.md
  → nav link:   /SA/feature-discovery           (correct)
  → file output: /SA/feature-discovery/index.html (was missing, now fixed)
@docmd-io docmd-io deleted a comment from alternatino May 16, 2026
@mgks
Copy link
Copy Markdown
Member

mgks commented May 16, 2026

Implementation verified. The fix correctly handles auto-designated index normalization by stripping the trailing slash to match the internal comparison format. Great catch, merging!

@mgks mgks merged commit aaf0304 into docmd-io:main May 16, 2026
1 check passed
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.

2 participants