Skip to content

Auto Navbar VitePress plugin ​

Reading Time

Reading time for this document is 4 minutes for 1045 words.

GitHub repo npm version CI License

@binarynoir/vitepress-auto-navbar is the companion to Auto Sidebar. It builds themeConfig.nav from the same folder scan, with the same ordering and exclusion syntax, in .nav files.

See it live ​

The top navbar is generated. Its order comes from docs/.nav:

txt
# Top navbar order. Everything not listed falls in at "..." alphabetically.
plugins
apps
services
guide
reference
blog
-legal.md
-privacy.md
-terms.md
...

Notice what is missing: titles. With configFilenames: ['.nav', '.sidebar'], a folder that has only a .sidebar file (like plugins/) lends its titles to the navbar. Open the Plugins dropdown and compare it to the sidebar. Same titles, written once.

The Blog entry is a plain link, not a dropdown of every post, because the blog's .sidebar has a .link line. Auto Sidebar reads the same file and leaves that setting to the navbar:

txt
# The overview stays first; pinned posts come next, then every other post, newest first.
index.md:All posts
.sort: blog.pinned desc, date desc
# Only the 10 newest posts are listed here; the blog page has them all.
.limit: 10
# In the navbar, Blog is one link to the blog page rather than a dropdown of every post.
.link

Install ​

sh
npm install --save-dev @binarynoir/vitepress-auto-navbar

Use ​

ts
// .vitepress/config.mts
import { defineConfig } from 'vitepress';
import { generateNav } from '@binarynoir/vitepress-auto-navbar';
import path from 'node:path';

export default defineConfig({
  themeConfig: {
    nav: generateNav(path.resolve(import.meta.dirname, '..'), {
      maxDepth: 2,
      configFilenames: ['.nav', '.sidebar'],
    }),
  },
});

Each top-level folder becomes a navbar entry: a dropdown if it has several pages, a plain link if it has one. Top-level .md files become plain links.

Options ​

OptionDefaultDescription
maxDepth2Navbar levels, 1 to 3.
maxTitleLength50Truncate generated titles beyond this length.
configFilenames['.nav']Per-folder config file names, checked in order.
excludeFilenames['.exclude']Per-folder exclusion file names.
excludeFrontmatter[]Frontmatter keys that hide a page when true, such as ['draft', 'blog.draft']. See Exclude.
indexTitle'Overview'Label for the dropdown entry that links to the folder's landing page.
flattenSinglePagetrueA folder with one entry becomes a plain link.
activeMatchtrueKeep items highlighted on every page beneath them.
titleReplacements{}Words to replace in titles made from file and folder names.
titleCasetrueTitle-case titles made from file and folder names.
stripTagstrueRemove Markdown Tags syntax (((tag|draft))) from titles.
transformTitlenoneA function that gets every title before truncation.
sort'name'Order for unlisted entries in every folder, e.g. 'date desc'. See Settings.
limit0Most unlisted entries every dropdown shows, after sorting.
morenoneLink text for the landing page when a limit left entries out.
verbosefalseLog each folder as it is processed.

Titles can be single- or double-quoted, with a space after the colon if you like, and a line that can't be parsed logs a warning with its file and line number.

Settings ​

A line in a .nav or .sidebar file that starts with a dot is a setting for the folder, not an entry. Every setting is written the same way: .name turns it on, and .name: value gives it a value. An unknown setting, or a value it can't use, logs a warning with its file and line number. Auto Sidebar reads settings the same way, so one .sidebar file can serve both menus.

SettingWhat it does
.sort: key [asc|desc], …Order the entries this file doesn't list. The keys are the same as Auto Sidebar's: name, title, date, or any frontmatter field.
.limit: nShow at most n of the entries this file doesn't list, after sorting. Same as Auto Sidebar.
.more: TextWhen .limit left entries out, end the dropdown with a link to the landing page titled Text.
.linkNavbar only. Render this folder as one plain link instead of a dropdown.
.inheritBorrow name:Title overrides from the other config files.
.hide / .hideallHide this folder's own link, or the whole folder.

A .sort line in a .sidebar file orders the sidebar and the navbar dropdown alike. For example, a Releases dropdown that always shows the latest five releases, with a link to the rest:

txt
# docs/releases/.sidebar (read by both menus)
index.md:All releases
.sort: version desc
.limit: 5
.more: Older releases →

Auto Sidebar's recipes cover more: meeting notes, a blog, tutorials and decision records. Each works in the navbar too. The sort option sets an order for every folder; a folder's own .sort still wins.

A folder's landing page is its index.md, or its README.md. VitePress serves a README.md at README.html, so add README Index to serve it at the folder's URL.

Titles ​

This site passes the same title options to both menus. See them working in the Sidebar Titles Demo, and in this site's config.

A title made from a file or folder name is formatted: getting-started.md becomes "Getting Started". It is title-cased, so articles, short conjunctions and prepositions (a, the, and, of, to, …) stay lowercase unless they are the first or last word: guide-to-the-cli.md becomes "Guide to the Cli". Set titleCase: false to capitalise every word instead.

Only first letters change case, so use titleReplacements for words with other casing. Keys match whole words, ignoring case:

ts
generateNav(docsRoot, {
  titleReplacements: { github: 'GitHub', api: 'API', cicd: 'CI/CD' },
});
// github-actions/ → "GitHub Actions", cicd-setup.md → "CI/CD Setup"

Titles an author wrote (a heading, title frontmatter, a .nav line) are never changed by titleReplacements.

Markdown Tags syntax is removed from every title, so # Design ((tag|draft)) is listed as "Design". Set stripTags: false to keep it.

For anything else, transformTitle gets every title before it is truncated, with where it came from: 'name' (a file or folder name), 'page' (frontmatter or the first heading) or 'config' (a .nav line). Return the title to use, or undefined to keep it:

ts
generateNav(docsRoot, {
  transformTitle: (title, { source }) =>
    source === 'page' ? title.replace(/^Guide: /, '') : undefined,
});

Use the title helpers in your own code ​

Auto Sidebar and Auto Navbar both export the functions they use to make titles. Your config.mts, a build script or another plugin can call them to get titles that match the menus. Import them from whichever plugin you have installed:

ts
import { formatTitle, truncateTitle, stripTagSyntax } from '@binarynoir/vitepress-auto-sidebar';
// or from '@binarynoir/vitepress-auto-navbar'; they work the same

formatTitle('02-guide-to-the-github-api.md', 50, { github: 'GitHub', api: 'API' }); // "Guide to the GitHub API"
formatTitle('guide-to-the-cli', 50, {}, false); // "Guide To The Cli"
truncateTitle('A fairly long human-written title', 15); // "A fairly long…"
stripTagSyntax('Design ((tag|draft))'); // "Design"
FunctionWhat it does
formatTitle(name, maxLength?, replacements?, titleCase?)Turns a file or folder name into a title, following the rules above. maxLength defaults to 50 (0 for no limit), replacements to {} and titleCase to true.
truncateTitle(title, maxLength?)Shortens a title someone wrote to maxLength (default 50), adding …, without changing its casing or punctuation.
stripTagSyntax(title)Removes Markdown Tags syntax (((tag|draft))) and tidies the spacing.

Pass formatTitle a single file or folder name, not a path, because it doesn't split on /. To match your menus, pass it the same titleReplacements and titleCase you gave the plugin.

Build time only

These functions run in Node: in .vitepress/config.mts, build scripts, and Vite or markdown-it plugins. The package also loads the menu generator, which reads files with fs, so don't import it into code that runs in the browser, such as a Vue component or the theme's enhanceApp. Make the titles at build time instead, and pass them to the page through frontmatter, a data loader or themeConfig.

Source and full README on GitHub