Skip to content

Commit

Permalink
Headless: Sort by file name & index always at the top
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Dec 30, 2023
1 parent 86e8567 commit db840c2
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions packages/headless/src/source/page-tree-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,30 @@ function buildAll(
ctx: PageTreeBuilderContext,
skipIndex: boolean,
): PageTree.Node[] {
return nodes
.flatMap((child) => {
if (child.type === 'page') {
if (child.file.locale) return [];
if (skipIndex && child.file.name === 'index') return [];

return buildFileNode(child, ctx);
}
const output: PageTree.Node[] = [];
let index: PageTree.Item | undefined;

for (const node of [...nodes].sort((a, b) =>
a.file.name.localeCompare(b.file.name),
)) {
if (node.type === 'page') {
if (node.file.locale) continue;
const treeNode = buildFileNode(node, ctx);

if (node.file.name === 'index') index = treeNode;
else output.push(treeNode);
}

if (node.type === 'folder') {
output.push(buildFolderNode(node, ctx, true));
}
}

if (child.type === 'folder') {
return buildFolderNode(child, ctx, true);
}
if (!skipIndex && index) {
output.unshift(index);
}

return [];
})
.sort((a, b) => a.name.localeCompare(b.name));
return output;
}

function getFolderMeta(
Expand Down Expand Up @@ -113,7 +121,6 @@ function buildFolderNode(
}

const extractResult = extractor.exec(item);

const extractName = extractResult?.groups?.name ?? item;

const itemNode =
Expand Down

0 comments on commit db840c2

Please sign in to comment.