Skip to content

Commit b702941

Browse files
committed
docs: add live title-option demos and dev-mode image fallback example
1 parent b48d35a commit b702941

20 files changed

Lines changed: 282 additions & 103 deletions

‎docs/.vitepress/config.mts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ const docsRoot = path.resolve(import.meta.dirname, '..');
2020

2121
const SITE_URL = 'https://binarynoir.github.io';
2222

23+
// @binarynoir/vitepress-auto-sidebar and -auto-navbar: how titles are written in both
24+
// menus. Markdown Tags syntax is stripped from titles by default (`stripTags`).
25+
const menuTitleOptions = {
26+
// A title made from a file or folder name writes `github` as "GitHub".
27+
titleReplacements: { github: 'GitHub' },
28+
// An "Example: " prefix on a page's title is left out of the menus.
29+
transformTitle: (title: string, { source }: { source: string }) =>
30+
source === 'page' ? title.replace(/^Example: /, '') : undefined,
31+
};
32+
2333
// @binarynoir/vitepress-downloads: the same options go to all three pieces below.
2434
const downloadsOptions = { folders: ['downloads', 'files'] };
2535

@@ -79,12 +89,17 @@ export default defineConfig({
7989
nav: generateNav(docsRoot, {
8090
maxDepth: 2,
8191
configFilenames: ['.nav', '.sidebar'],
92+
...menuTitleOptions,
8293
}),
8394

8495
// @binarynoir/vitepress-auto-sidebar: one sidebar per top-level folder.
96+
// A folder no `.sidebar` names is labelled with its landing page's title
97+
// (see plugins/auto-sidebar/titles-demo/).
8598
sidebar: generateSidebar(docsRoot, {
8699
maxDepth: 3,
87100
maxTitleLength: 50,
101+
...menuTitleOptions,
102+
groupTitleFromIndex: true,
88103
}),
89104

90105
socialLinks: [{ icon: 'github', link: 'https://github.com/binarynoir' }],

‎docs/assets/image-fallback-dev.svg‎

Lines changed: 10 additions & 0 deletions
Loading

‎docs/guide/how-this-site-works.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ docs/
3636
| You see | Plugin | Where it is wired |
3737
| ------------------------------- | --------------------------------------------- | ------------------------------------- |
3838
| Top navbar | [Auto Navbar](/plugins/auto-navbar) | `themeConfig.nav` in `config.mts` |
39-
| Left sidebar | [Auto Sidebar](/plugins/auto-sidebar) | `themeConfig.sidebar` in `config.mts` |
39+
| Left sidebar | [Auto Sidebar](/plugins/auto-sidebar/) | `themeConfig.sidebar` in `config.mts` |
4040
| Status badges | [Markdown Tags](/plugins/markdown-tags/) | `markdown.config`, plus the theme |
4141
| "Reading time" box | [Reading Time Tag](/plugins/reading-time-tag) | `markdown.config` |
4242
| Dotted-underline hovers | [Glossary Tooltips](/plugins/glossary/) | `markdown.config` |

‎docs/plugins/.sidebar‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# [Group] lines match the groups on the home page and the plugins overview.
44
index.md:Overview
55
[Navigation from your folders]
6-
auto-sidebar.md:Auto Sidebar
6+
auto-sidebar:Auto Sidebar
77
auto-navbar.md:Auto Navbar
88
[Richer pages]
99
markdown-tags:Markdown Tags

‎docs/plugins/auto-navbar.md‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ title: Auto Navbar
88

99
[![GitHub repo](https://img.shields.io/badge/GitHub-binarynoir%2Fvitepress--auto--navbar-181717?logo=github)](https://github.com/binarynoir/vitepress-auto-navbar) [![npm version](https://img.shields.io/npm/v/@binarynoir/vitepress-auto-navbar)](https://www.npmjs.com/package/@binarynoir/vitepress-auto-navbar) [![CI](https://img.shields.io/github/actions/workflow/status/binarynoir/vitepress-auto-navbar/ci.yml?branch=main&label=CI)](https://github.com/binarynoir/vitepress-auto-navbar/actions/workflows/ci.yml) [![License](https://img.shields.io/github/license/binarynoir/vitepress-auto-navbar)](https://github.com/binarynoir/vitepress-auto-navbar/blob/main/LICENSE)
1010

11-
`@binarynoir/vitepress-auto-navbar` is the companion to [Auto Sidebar](/plugins/auto-sidebar). It builds `themeConfig.nav` from the same folder scan, with the same ordering and exclusion syntax, in `.nav` files.
11+
`@binarynoir/vitepress-auto-navbar` is the companion to [Auto Sidebar](/plugins/auto-sidebar/). It builds `themeConfig.nav` from the same folder scan, with the same ordering and exclusion syntax, in `.nav` files.
1212

1313
## See it live
1414

@@ -55,8 +55,39 @@ Each top-level folder becomes a navbar entry: a dropdown if it has several pages
5555
| `indexTitle` | `'Overview'` | Label for the dropdown entry that links to the folder's landing page. |
5656
| `flattenSinglePage` | `true` | A folder with one entry becomes a plain link. |
5757
| `activeMatch` | `true` | Keep items highlighted on every page beneath them. |
58+
| `titleReplacements` | `{}` | Words to replace in titles made from file and folder names. |
59+
| `stripTags` | `true` | Remove Markdown Tags syntax (`((tag\|draft))`) from titles. |
60+
| `transformTitle` | none | A function that gets every title before truncation. |
5861
| `verbose` | `false` | Log each folder as it is processed. |
5962

60-
In `.nav` files, `.link` renders a folder as one plain link, and `.inherit` borrows `name:Title` overrides from the other config files.
63+
In `.nav` files, `.link` renders a folder as one plain link, and `.inherit` borrows `name:Title` overrides from the other config files. 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.
64+
65+
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](/plugins/readme-index/) to serve it at the folder's URL.
66+
67+
## Titles
68+
69+
This site passes the same title options to both menus. See them working in the [Sidebar Titles Demo](/plugins/auto-sidebar/titles-demo/), and in this site's [config](https://github.com/binarynoir/binarynoir.github.io/blob/main/docs/.vitepress/config.mts).
70+
71+
A title made from a file or folder name is formatted: `getting-started.md` becomes "Getting Started". Only first letters are capitalised, so use `titleReplacements` for words with other casing. Keys match whole words, ignoring case:
72+
73+
```ts
74+
generateNav(docsRoot, {
75+
titleReplacements: { github: 'GitHub', api: 'API', cicd: 'CI/CD' },
76+
});
77+
// github-actions/ → "GitHub Actions", cicd-setup.md → "CI/CD Setup"
78+
```
79+
80+
Titles an author wrote (a heading, `title` frontmatter, a `.nav` line) are never changed by `titleReplacements`.
81+
82+
[Markdown Tags](/plugins/markdown-tags/) syntax is removed from every title, so `# Design ((tag|draft))` is listed as "Design". Set `stripTags: false` to keep it.
83+
84+
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:
85+
86+
```ts
87+
generateNav(docsRoot, {
88+
transformTitle: (title, { source }) =>
89+
source === 'page' ? title.replace(/^Guide: /, '') : undefined,
90+
});
91+
```
6192

6293
[Source and full README on GitHub](https://github.com/binarynoir/vitepress-auto-navbar)

‎docs/plugins/auto-sidebar.md‎

Lines changed: 0 additions & 76 deletions
This file was deleted.

‎docs/plugins/auto-sidebar/.sidebar‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# titles-demo has no title here on purpose: its group label comes from its
2+
# landing page, with groupTitleFromIndex.
3+
index.md:Overview
4+
titles-demo

‎docs/plugins/auto-sidebar/index.md‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
title: Auto Sidebar
3+
---
4+
5+
# Auto Sidebar ((tag|VitePress plugin|blue))
6+
7+
[[readingTime]]
8+
9+
[![GitHub repo](https://img.shields.io/badge/GitHub-binarynoir%2Fvitepress--auto--sidebar-181717?logo=github)](https://github.com/binarynoir/vitepress-auto-sidebar) [![npm version](https://img.shields.io/npm/v/@binarynoir/vitepress-auto-sidebar)](https://www.npmjs.com/package/@binarynoir/vitepress-auto-sidebar) [![CI](https://img.shields.io/github/actions/workflow/status/binarynoir/vitepress-auto-sidebar/ci.yml?branch=main&label=CI)](https://github.com/binarynoir/vitepress-auto-sidebar/actions/workflows/ci.yml) [![License](https://img.shields.io/github/license/binarynoir/vitepress-auto-sidebar)](https://github.com/binarynoir/vitepress-auto-sidebar/blob/main/LICENSE)
10+
11+
`@binarynoir/vitepress-auto-sidebar` builds your sidebar by scanning your docs folder, so you stop hand-maintaining `themeConfig.sidebar`. Add a Markdown file and it appears in the sidebar, titled from its heading.
12+
13+
## See it live
14+
15+
The sidebar on the left is generated. The order and titles for this section come from one plain-text file, `plugins/.sidebar`:
16+
17+
<<< @/plugins/.sidebar{txt}
18+
19+
Reorder those lines, rebuild, and the sidebar follows. No config code changes.
20+
21+
## Install
22+
23+
```sh
24+
npm install --save-dev @binarynoir/vitepress-auto-sidebar
25+
```
26+
27+
## Use
28+
29+
```ts
30+
// .vitepress/config.mts
31+
import { defineConfig } from 'vitepress';
32+
import { generateSidebar } from '@binarynoir/vitepress-auto-sidebar';
33+
import path from 'node:path';
34+
35+
export default defineConfig({
36+
themeConfig: {
37+
sidebar: generateSidebar(path.resolve(import.meta.dirname, '..'), {
38+
maxDepth: 3,
39+
maxTitleLength: 50,
40+
}),
41+
},
42+
});
43+
```
44+
45+
Use `import.meta.dirname`, not `__dirname`: VitePress configs are ESM.
46+
47+
Each top-level folder becomes its own sidebar, keyed by its URL prefix. 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](/plugins/readme-index/) to serve it at the folder's URL. A page title comes from, in order: the `.sidebar` file, the `title` frontmatter, the first `# Heading`, then the formatted filename.
48+
49+
## `.sidebar` syntax
50+
51+
| Line | Meaning |
52+
| -------------------- | ------------------------------------------------- |
53+
| `name` | Order this file or folder. |
54+
| `name:Custom Title` | Order it and override its title. |
55+
| `"https://…":Title` | Insert an external link in place. |
56+
| `...` | Everything not listed goes here, alphabetically. |
57+
| `-name` | Hide this entry. |
58+
| `.hide` / `.hideall` | Hide this folder's own link, or the whole folder. |
59+
60+
Titles can be quoted, with single or double quotes, and may have a space after the colon: `name.md: "Title"` and `https://example.com: 'Title'` both work. A line that can't be parsed, such as a link with no title, logs a warning with its file and line number.
61+
62+
Files need their `.md` extension in these lines. A `.exclude` file removes drafts by pattern, and a `sidebar.json` next to a folder is used verbatim when you want to hand-author one section.
63+
64+
## Options
65+
66+
| Option | Default | Description |
67+
| --------------------- | -------------- | --------------------------------------------------------------------- |
68+
| `maxDepth` | `3` | How many folder levels deep to recurse. |
69+
| `maxTitleLength` | `50` | Truncate generated titles beyond this length. |
70+
| `configFilenames` | `['.sidebar']` | Per-folder ordering file names. |
71+
| `excludeFilenames` | `['.exclude']` | Per-folder exclusion file names. |
72+
| `collapsed` | `false` | Initial collapsed state for section headers. |
73+
| `flattenSinglePage` | `true` | A folder with one page becomes a plain link instead of its own group. |
74+
| `titleReplacements` | `{}` | Words to replace in titles made from file and folder names. |
75+
| `stripTags` | `true` | Remove Markdown Tags syntax (`((tag\|draft))`) from titles. |
76+
| `transformTitle` | none | A function that gets every title before truncation. |
77+
| `groupTitleFromIndex` | `false` | Label a folder's group with its landing page's title. |
78+
| `verbose` | `false` | Log each folder as it is processed. |
79+
80+
## Titles
81+
82+
::: tip See it live
83+
The [Sidebar Titles Demo](./titles-demo/) folder, in the sidebar under **Auto Sidebar**, uses each option below on this site's real sidebar.
84+
:::
85+
86+
A title made from a file or folder name is formatted: `getting-started.md` becomes "Getting Started". Only first letters are capitalised, so use `titleReplacements` for words with other casing. Keys match whole words, ignoring case:
87+
88+
```ts
89+
generateSidebar(docsRoot, {
90+
titleReplacements: { github: 'GitHub', api: 'API', cicd: 'CI/CD' },
91+
});
92+
// github-actions/ → "GitHub Actions", cicd-setup.md → "CI/CD Setup"
93+
```
94+
95+
Titles an author wrote (a heading, `title` frontmatter, a `.sidebar` line) are never changed by `titleReplacements`.
96+
97+
[Markdown Tags](/plugins/markdown-tags/) syntax is removed from every title, so `# Design ((tag|draft))` is listed as "Design". Set `stripTags: false` to keep it.
98+
99+
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 `.sidebar` line). Return the title to use, or `undefined` to keep it:
100+
101+
```ts
102+
generateSidebar(docsRoot, {
103+
transformTitle: (title, { source }) =>
104+
source === 'page' ? title.replace(/^Guide: /, '') : undefined,
105+
});
106+
```
107+
108+
A folder's group label comes from, in order: its line in the parent's `.sidebar`, a `section-title` in its landing page's frontmatter, then the formatted folder name. With `groupTitleFromIndex: true`, the landing page's `title` or first `# Heading` is tried before the folder name.
109+
110+
Pairs with [Auto Navbar](/plugins/auto-navbar): one folder structure drives both menus.
111+
112+
[Source and full README on GitHub](https://github.com/binarynoir/vitepress-auto-sidebar)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# The demo pages are left untitled here, so each sidebar title is one the
2+
# new title options produce. Only the link below sets its own title, quoted
3+
# after a space.
4+
index.md
5+
github-actions-setup.md
6+
draft-notes.md
7+
example-renamed-page.md
8+
https://vitepress.dev: 'VitePress docs'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Draft notes ((tag|draft|orange))
2+
3+
This heading carries a [Markdown Tags](/plugins/markdown-tags/) badge, written as `((tag|draft|orange))`. On the page it renders as a badge. In the sidebar, [Auto Sidebar](/plugins/auto-sidebar/) removes the tag syntax, so the entry reads **Draft notes**.
4+
5+
That is `stripTags`, on by default. Set `stripTags: false` to keep the raw syntax in titles.
6+
7+
[Back to the demo](./)

0 commit comments

Comments
 (0)