Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion packages/core/src/sync/sidebar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,54 @@
}

/**
* Resolve the link for a nav entry, falling back to the first child link.
* Known slugs for entry/overview pages, checked in priority order.
*
* @private
*/
const ENTRY_PAGE_SLUGS = ['overview', 'introduction', 'index', 'readme'] as const

/**
* Find a child entry whose link ends with a known entry-page slug.
*
* Checks children (one level) against `ENTRY_PAGE_SLUGS` in priority order,
* returning the first match. Returns `undefined` when no child matches.
*
* @private
* @param items - Direct children of a section
* @returns Link of the first matching entry page, or undefined
*/
function findEntryPageLink(items: readonly ResolvedEntry[]): string | undefined {
const childLinks = items.filter((c) => c.link).map((c) => c.link as string)
return ENTRY_PAGE_SLUGS.reduce<string | undefined>((found, slug) => {
if (found) {
return found
}
return childLinks.find((link) => {
const last = link.split('/').pop()
return last === slug
})
}, undefined)

Check warning on line 181 in packages/core/src/sync/sidebar/index.ts

View workflow job for this annotation

GitHub Actions / ci

eslint-plugin-unicorn(no-useless-undefined)

Do not use useless `undefined`.
}

/**
* Resolve the link for a nav entry.
*
* For sections with children, prefers a child whose slug matches a known
* entry-page name (overview, introduction, index, readme) so the nav
* points to an actual content page rather than a generated landing page.
* Falls back to the section's own link, then the first child link.
*
* @private
* @param entry - Resolved entry to extract link from
* @returns Link string or undefined
*/
function resolveLink(entry: ResolvedEntry): string | undefined {
if (entry.items && entry.items.length > 0) {
const entryPage = findEntryPageLink(entry.items)
if (entryPage) {
return entryPage
}
}
if (entry.link) {
return entry.link
}
Expand Down
10 changes: 5 additions & 5 deletions zpress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,11 +604,11 @@ export default defineConfig({
},
],
nav: [
{ title: 'Getting Started', link: '/getting-started' },
{ title: 'Concepts', link: '/concepts' },
{ title: 'Guides', link: '/guides' },
{ title: 'Framework', link: '/framework' },
{ title: 'Reference', link: '/reference' },
{ title: 'Getting Started', link: '/getting-started/introduction' },
{ title: 'Concepts', link: '/concepts/content' },
{ title: 'Guides', link: '/guides/deploying-to-vercel' },
{ title: 'Framework', link: '/framework/overview' },
{ title: 'Reference', link: '/reference/configuration' },
],
socialLinks: [
{ icon: 'github', mode: 'link', content: 'https://github.com/joggrdocs/zpress' },
Expand Down
Loading