Skip to content

Instantly share code, notes, and snippets.

@noxify
Last active January 22, 2025 14:44
Show Gist options
  • Save noxify/4a53826274af7e524e8b1b172e4a8661 to your computer and use it in GitHub Desktop.
Save noxify/4a53826274af7e524e8b1b172e4a8661 to your computer and use it in GitHub Desktop.
// license: MIT
import type { FileSystemEntry } from "renoun/file-system"
import { Directory, EntryGroup, MemoryFileSystem } from "renoun/file-system"
const renounFileSystem = new MemoryFileSystem({
"index.mdx": "",
"01.section-title/01.introduction.mdx": "",
"01.getting-started.mdx": "",
"03.section-with-index/index.mdx": "",
"03.section-with-index/01.introduction.mdx": "",
})
const testFileSystem = new MemoryFileSystem({
"index.mdx": "",
"01.section-title/01.introduction.mdx": "",
"01.getting-started.mdx": "",
"03.section-with-index/index.mdx": "",
"03.section-with-index/01.introduction.mdx": "",
})
const renounMemoryDirectory = new Directory({
path: "./",
basePath: "renoun-docs",
fileSystem: renounFileSystem,
})
const testMemoryDirectory = new Directory({
path: "./",
basePath: "test-docs",
fileSystem: testFileSystem,
})
const TestCollection = new EntryGroup({
entries: [renounMemoryDirectory, testMemoryDirectory],
})
// inspired by
// * https://github.com/souporserious/renoun/blob/main/packages/renoun/src/file-system/index.tsx#L497
async function getSiblings<GroupTypes extends Record<string, unknown> = Record<string, unknown>>(
pathSegments: string[],
options: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
entryGroup: EntryGroup<GroupTypes, FileSystemEntry<any>[]>
includeAll?: boolean
},
) {
let entries = await options.entryGroup.getEntries({
recursive: true,
})
if (!options.includeAll) {
entries = entries.filter(
(ele) => ele.getPathSegments()[0] == pathSegments[0],
)
}
const currentIndex = entries.findIndex(
(ele) => ele.getPathSegments().join("/") === pathSegments.join("/"),
)
const previousElement =
currentIndex > 0 ? entries[currentIndex - 1] : undefined
const nextElement =
currentIndex < entries.length - 1 ? entries[currentIndex + 1] : undefined
return [previousElement, nextElement]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment