Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Guide
description: User guide
sidebar_position: 1
---

# Guide

This guide page has sidebar_position: 1.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Overview
description: Plugin overview page
---

# Overview

This is the overview page with no sidebar_position set.
8 changes: 8 additions & 0 deletions packages/plugin-docs-cli/src/scanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ describe('scanDocsFolder', () => {
expect(result.manifest.pages).toHaveLength(4); // home, guide, advanced, config
});

it('should sort root index.md first even when sidebar_position is not set', async () => {
const noPositionPath = join(__dirname, '__fixtures__', 'no-position-index-docs');
const result = await scanDocsFolder(noPositionPath);

expect(result.manifest.pages[0].slug).toBe('index');
expect(result.manifest.pages[0].file).toBe('index.md');
});

it('should sort pages by sidebar_position', async () => {
const result = await scanDocsFolder(testDocsPath);

Expand Down
11 changes: 6 additions & 5 deletions packages/plugin-docs-cli/src/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,12 @@ function treeToPages(node: TreeNode): Page[] {
}
}

// convert children to array and sort by sidebar_position
const sortedEntries = childEntries.sort(
(a, b) =>
(a[1].file?.frontmatter.sidebar_position ?? Infinity) - (b[1].file?.frontmatter.sidebar_position ?? Infinity)
);
// convert children to array and sort by sidebar_position; root index.md always first
const sortedEntries = childEntries.sort((a, b) => {
if (a[0] === 'index.md') { return -1; }
if (b[0] === 'index.md') { return 1; }
return (a[1].file?.frontmatter.sidebar_position ?? Infinity) - (b[1].file?.frontmatter.sidebar_position ?? Infinity);
});

for (const [name, child] of sortedEntries) {
if (child.file) {
Expand Down
Loading