Skip to content

Commit 3ee1722

Browse files
rsbhclaude
andauthored
fix: meta.json order sorting for folders without index pages (#67)
* fix: meta.json order sorting for folders without index pages Derive folder path from first child page URL instead of relying on node.index which is absent for folders without an index file. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * docs: add sorting section for pages and folders Explains how to sort pages via frontmatter order and meta.json pages array, and how to sort folders via meta.json order. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: folder sorting only via meta.json, not index page frontmatter Remove index page frontmatter order fallback for folder sorting. Folders sort only by meta.json order. Update docs to clarify. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e23cfae commit 3ee1722

2 files changed

Lines changed: 87 additions & 5 deletions

File tree

examples/basic/content/docs/features.mdx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,72 @@ lastModified: 2024-01-15
128128

129129
---
130130

131+
## Sorting Pages and Folders
132+
133+
Control the order of pages and folders in the sidebar.
134+
135+
### Sorting Pages
136+
137+
Add `order` to page frontmatter. Lower numbers appear first. Pages without `order` appear at the end.
138+
139+
```mdx
140+
---
141+
title: Introduction
142+
order: 1
143+
---
144+
```
145+
146+
```mdx
147+
---
148+
title: Installation
149+
order: 2
150+
---
151+
```
152+
153+
Alternatively, use the `pages` array in `meta.json` to explicitly order pages within a folder:
154+
155+
```json
156+
{
157+
"pages": ["introduction", "installation", "configuration"]
158+
}
159+
```
160+
161+
### Sorting Folders
162+
163+
Add `order` to the folder's `meta.json`. This works even for folders without an index page.
164+
165+
```
166+
docs/
167+
├── getting-started/
168+
│ └── meta.json ← {"title": "Getting Started", "order": 1}
169+
├── guides/
170+
│ └── meta.json ← {"title": "Guides", "order": 2}
171+
└── api/
172+
└── meta.json ← {"title": "API Reference", "order": 3}
173+
```
174+
175+
`getting-started/meta.json`:
176+
```json
177+
{
178+
"title": "Getting Started",
179+
"order": 1
180+
}
181+
```
182+
183+
`guides/meta.json`:
184+
```json
185+
{
186+
"title": "Guides",
187+
"order": 2
188+
}
189+
```
190+
191+
Folders without `order` appear after all ordered folders.
192+
193+
Folder sorting is controlled **only** by `meta.json` `order`. The index page frontmatter `order` does not affect folder position — it only controls the page's position within the folder.
194+
195+
---
196+
131197
## Markdown URLs
132198

133199
Every page has a `.md` URL that returns raw markdown:

packages/chronicle/src/lib/source.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,30 @@ export function invalidate() {
120120
cachedNavMap = null;
121121
}
122122

123+
function getFolderPath(node: Folder): string | null {
124+
const firstPage = findFirstPage(node);
125+
if (!firstPage) return null;
126+
const parts = firstPage.url.split('/').filter(Boolean);
127+
parts.pop();
128+
return '/' + parts.join('/');
129+
}
130+
131+
function findFirstPage(node: Folder): { url: string } | null {
132+
for (const child of node.children) {
133+
if (child.type === 'page') return child;
134+
if (child.type === 'folder') {
135+
const found = findFirstPage(child);
136+
if (found) return found;
137+
}
138+
}
139+
return node.index ?? null;
140+
}
141+
123142
function getOrder(node: Node, pageOrderMap: Map<string, number>, folderOrderMap: Map<string, number>): number | undefined {
124143
if (node.type === 'page') return pageOrderMap.get(node.url);
125144
if (node.type === 'folder') {
126-
if (node.index) {
127-
const fromMeta = folderOrderMap.get(node.index.url);
128-
if (fromMeta !== undefined) return fromMeta;
129-
return pageOrderMap.get(node.index.url);
130-
}
145+
const folderPath = getFolderPath(node);
146+
if (folderPath) return folderOrderMap.get(folderPath);
131147
}
132148
return undefined;
133149
}

0 commit comments

Comments
 (0)