starlight-sidebar-topics-dropdown 0.2.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/components/Topics.astro +4 -4
- package/components/starlight/Sidebar.astro +28 -0
- package/index.ts +76 -66
- package/libs/config.ts +82 -0
- package/libs/content.ts +15 -0
- package/libs/i18n.ts +27 -0
- package/libs/locals.ts +14 -0
- package/libs/pathname.ts +20 -0
- package/libs/plugin.ts +32 -0
- package/libs/sidebar.ts +210 -0
- package/libs/starlight.ts +29 -0
- package/libs/vite.ts +36 -0
- package/middleware.ts +43 -0
- package/overrides/Sidebar.astro +3 -24
- package/package.json +45 -44
- package/schema.ts +6 -6
- package/starlight.d.ts +2 -2
- package/virtual.d.ts +2 -2
- package/lib/config.ts +0 -74
- package/lib/content.ts +0 -11
- package/lib/i18n.ts +0 -22
- package/lib/pathname.ts +0 -17
- package/lib/plugin.ts +0 -32
- package/lib/sidebar.ts +0 -173
- package/lib/vite.ts +0 -34
- package/overrides/Pagination.astro +0 -19
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { StarlightUserConfig } from "@astrojs/starlight/types";
|
|
2
|
+
import type { AstroIntegrationLogger } from "astro";
|
|
3
|
+
|
|
4
|
+
export function overrideStarlightComponent(
|
|
5
|
+
components: StarlightUserConfig["components"],
|
|
6
|
+
logger: AstroIntegrationLogger,
|
|
7
|
+
override: keyof NonNullable<StarlightUserConfig["components"]>,
|
|
8
|
+
component: string
|
|
9
|
+
) {
|
|
10
|
+
if (components?.[override]) {
|
|
11
|
+
logger.warn(
|
|
12
|
+
`It looks like you already have a \`${override}\` component override in your Starlight configuration.`
|
|
13
|
+
);
|
|
14
|
+
logger.warn(
|
|
15
|
+
`To use \`starlight-sidebar-topics-dropdown\`, either remove your override or update it to render the content from \`starlight-sidebar-topics-dropdown/components/${component}.astro\`.`
|
|
16
|
+
);
|
|
17
|
+
// if (component === "DynamicVersionBadge") {
|
|
18
|
+
// logger.warn(
|
|
19
|
+
// "Notice that the `DynamicVersionBadge` component must be rendered AFTER the original Starlight `SiteTitle` component in the DOM. This ensures proper layout and behavior within the application."
|
|
20
|
+
// );
|
|
21
|
+
// }
|
|
22
|
+
|
|
23
|
+
return {};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
[override]: `starlight-sidebar-topics-dropdown/overrides/${override}.astro`,
|
|
28
|
+
};
|
|
29
|
+
}
|
package/libs/vite.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ViteUserConfig } from "astro";
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
StarlightSidebarTopicsDropdownConfig,
|
|
5
|
+
StarlightSidebarTopicsDropdownSharedConfig,
|
|
6
|
+
} from "./config";
|
|
7
|
+
|
|
8
|
+
const moduleId = "virtual:starlight-sidebar-topics-dropdown/config";
|
|
9
|
+
|
|
10
|
+
export function vitePluginStarlightSidebarTopicsDropdown(
|
|
11
|
+
config: StarlightSidebarTopicsDropdownConfig
|
|
12
|
+
): VitePlugin {
|
|
13
|
+
const resolvedModuleId = `\0${moduleId}`;
|
|
14
|
+
|
|
15
|
+
const sharedConfig: StarlightSidebarTopicsDropdownSharedConfig = config.map(
|
|
16
|
+
(topic) => {
|
|
17
|
+
if (!("items" in topic)) return { ...topic, type: "link" };
|
|
18
|
+
const { items, ...topicWithoutItems } = topic;
|
|
19
|
+
return { ...topicWithoutItems, type: "group" };
|
|
20
|
+
}
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const moduleContent = `export default ${JSON.stringify(sharedConfig)}`;
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
name: "vite-plugin-starlight-sidebar-topics-dropdown",
|
|
27
|
+
load(id) {
|
|
28
|
+
return id === resolvedModuleId ? moduleContent : undefined;
|
|
29
|
+
},
|
|
30
|
+
resolveId(id) {
|
|
31
|
+
return id === moduleId ? resolvedModuleId : undefined;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type VitePlugin = NonNullable<ViteUserConfig["plugins"]>[number];
|
package/middleware.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { defineRouteMiddleware } from "@astrojs/starlight/route-data";
|
|
2
|
+
import config from "virtual:starlight-sidebar-topics-dropdown/config";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
StarlightSidebarTopicsDropdownLocalsSymbol,
|
|
6
|
+
type StarlightSidebarTopicsDropdownLocals,
|
|
7
|
+
} from "./libs/locals";
|
|
8
|
+
import { throwPluginError } from "./libs/plugin";
|
|
9
|
+
import {
|
|
10
|
+
getCurrentTopic,
|
|
11
|
+
isTopicFirstPage,
|
|
12
|
+
isTopicLastPage,
|
|
13
|
+
} from "./libs/sidebar";
|
|
14
|
+
|
|
15
|
+
export const onRequest = defineRouteMiddleware((context) => {
|
|
16
|
+
const { starlightRoute } = context.locals;
|
|
17
|
+
const { entry, id, pagination, sidebar } = starlightRoute;
|
|
18
|
+
|
|
19
|
+
if (entry["data"].template !== "splash") {
|
|
20
|
+
const currentTopic = getCurrentTopic(config, sidebar, id, entry);
|
|
21
|
+
|
|
22
|
+
if (!currentTopic)
|
|
23
|
+
throwPluginError(
|
|
24
|
+
`Failed to find the topic for the \`${id}\` page.`,
|
|
25
|
+
`Either include this page in the sidebar configuration of the desired topic using the \`items\` property or to associate an unlisted page with a topic, use the \`topic\` frontmatter property and set it to the desired topic ID.
|
|
26
|
+
Learn more about unlisted pages in the ["Unlisted pages"](https://starlight-sidebar-topics-dropdown.trueberryless.org/docs/guides/unlisted-pages/) guide.`
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
starlightRoute.sidebar = currentTopic.sidebar;
|
|
30
|
+
// @ts-expect-error - See `libs/locals` for more information.
|
|
31
|
+
context.locals[StarlightSidebarTopicsDropdownLocalsSymbol] = {
|
|
32
|
+
config: currentTopic.config,
|
|
33
|
+
} satisfies StarlightSidebarTopicsDropdownLocals;
|
|
34
|
+
|
|
35
|
+
if (isTopicFirstPage(sidebar, id)) {
|
|
36
|
+
pagination.prev = undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (isTopicLastPage(sidebar, id)) {
|
|
40
|
+
pagination.next = undefined;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
});
|
package/overrides/Sidebar.astro
CHANGED
|
@@ -1,28 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
import Default from '@astrojs/starlight/components/Sidebar.astro'
|
|
3
|
-
import
|
|
4
|
-
import config from 'virtual:starlight-sidebar-topics-dropdown/config'
|
|
5
|
-
|
|
6
|
-
import Topics from '../components/Topics.astro'
|
|
7
|
-
import { throwPluginError } from '../lib/plugin'
|
|
8
|
-
import { getCurrentTopic, type Topic } from '../lib/sidebar'
|
|
9
|
-
|
|
10
|
-
const { entry, sidebar, slug } = Astro.props
|
|
11
|
-
|
|
12
|
-
let currentSidebar = sidebar
|
|
13
|
-
let currentTopic: Topic | undefined = undefined
|
|
14
|
-
|
|
15
|
-
if (entry['data'].template !== 'splash') {
|
|
16
|
-
currentTopic = getCurrentTopic(config, sidebar, slug, entry)
|
|
17
|
-
if (!currentTopic)
|
|
18
|
-
throwPluginError(
|
|
19
|
-
`Failed to find the topic for the \`${slug}\` page.`,
|
|
20
|
-
`Either include this page in the sidebar configuration of the desired topic using the \`items\` property or to associate an unlisted page with a topic, use the \`topic\` frontmatter property and set it to the desired topic ID.
|
|
21
|
-
Learn more about unlisted pages in the ["Unlisted pages"](https://starlight-sidebar-topics.netlify.app/docs/guides/unlisted-pages/) guide.`,
|
|
22
|
-
)
|
|
23
|
-
currentSidebar = currentTopic.sidebar
|
|
24
|
-
}
|
|
3
|
+
import StarlightSidebarTopicsDropdownSidebar from '../components/starlight/Sidebar.astro'
|
|
25
4
|
---
|
|
26
5
|
|
|
27
|
-
|
|
28
|
-
<Default
|
|
6
|
+
<StarlightSidebarTopicsDropdownSidebar />
|
|
7
|
+
<Default><slot /></Default>
|
package/package.json
CHANGED
|
@@ -1,46 +1,47 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"
|
|
2
|
+
"name": "starlight-sidebar-topics-dropdown",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Starlight plugin to split your docs page into multiple subpages and switch between them with a dropdown menu in the sidebar.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"starlight",
|
|
7
|
+
"plugin",
|
|
8
|
+
"sidebar",
|
|
9
|
+
"subpages",
|
|
10
|
+
"sections",
|
|
11
|
+
"dropdown",
|
|
12
|
+
"documentation",
|
|
13
|
+
"astro"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/trueberryless-org/starlight-sidebar-topics-dropdown",
|
|
16
|
+
"bugs": "https://github.com/trueberryless-org/starlight-sidebar-topics-dropdown/issues",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/trueberryless-org/starlight-sidebar-topics-dropdown.git",
|
|
20
|
+
"directory": "packages/starlight-sidebar-topics-dropdown"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": "trueberryless (https://trueberryless.org)",
|
|
24
|
+
"type": "module",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./index.ts",
|
|
27
|
+
"./components/Sidebar.astro": "./components/starlight/Sidebar.astro",
|
|
28
|
+
"./middleware": "./middleware.ts",
|
|
29
|
+
"./overrides/Sidebar.astro": "./overrides/Sidebar.astro",
|
|
30
|
+
"./schema": "./schema.ts",
|
|
31
|
+
"./package.json": "./package.json"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@astrojs/starlight": "^0.32.1",
|
|
35
|
+
"astro": "^5.3.0"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@astrojs/starlight": ">=0.30"
|
|
39
|
+
},
|
|
40
|
+
"packageManager": "pnpm@9.3.0",
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": "^18.17.1 || ^20.3.0 || >=21.0.0"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
}
|
|
46
47
|
}
|
package/schema.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { z } from "astro/zod";
|
|
2
2
|
export const topicSchema = z.object({
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
/**
|
|
4
|
+
* ID of the topic to associate with the current page if the page is not listed in any topic sidebar configuration.
|
|
5
|
+
*
|
|
6
|
+
* @see https://starlight-sidebar-topics-dropdown.trueberryless.org/docs/guides/unlisted-pages/
|
|
7
|
+
*/
|
|
8
|
+
topic: z.string().optional(),
|
|
9
9
|
});
|
|
10
10
|
export type TopicFrontmatterSchema = z.input<typeof topicSchema>;
|
package/starlight.d.ts
CHANGED
package/virtual.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
declare module "virtual:starlight-sidebar-topics-dropdown/config" {
|
|
2
|
-
|
|
2
|
+
const StarlightSidebarTopicsDropdownConfig: import("./libs/config").StarlightSidebarTopicsDropdownSharedConfig;
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
export default StarlightSidebarTopicsDropdownConfig;
|
|
5
5
|
}
|
package/lib/config.ts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import type { StarlightUserConfig } from "@astrojs/starlight/types";
|
|
2
|
-
import { z } from "astro/zod";
|
|
3
|
-
|
|
4
|
-
const sidebarTopicBadgeSchema = z.object({
|
|
5
|
-
text: z.union([z.string(), z.record(z.string())]),
|
|
6
|
-
variant: z.enum(["note", "danger", "success", "caution", "tip", "default"]).default("default"),
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
const sidebarTopicBaseSchema = z.object({
|
|
10
|
-
/**
|
|
11
|
-
* An optional badge to display next to the topic label.
|
|
12
|
-
*
|
|
13
|
-
* This option accepts the same configuration as the Starlight badge sidebar item configuration.
|
|
14
|
-
* @see https://starlight.astro.build/guides/sidebar/#badges
|
|
15
|
-
*/
|
|
16
|
-
badge: z
|
|
17
|
-
.union([z.string(), sidebarTopicBadgeSchema])
|
|
18
|
-
.transform((badge) =>
|
|
19
|
-
typeof badge === "string" ? { variant: "default" as const, text: badge } : badge
|
|
20
|
-
)
|
|
21
|
-
.optional(),
|
|
22
|
-
/**
|
|
23
|
-
* The name of an optional icon to display before the topic label set to one of Starlight’s built-in icons.
|
|
24
|
-
* @see https://starlight.astro.build/reference/icons/#all-icons
|
|
25
|
-
*/
|
|
26
|
-
icon: z.string().optional(),
|
|
27
|
-
/**
|
|
28
|
-
* The topic label visible at the top of the sidebar.
|
|
29
|
-
*
|
|
30
|
-
* The value can be a string, or for multilingual sites, an object with values for each different locale. When using
|
|
31
|
-
* the object form, the keys must be BCP-47 tags (e.g. en, fr, or zh-CN).
|
|
32
|
-
*/
|
|
33
|
-
label: z.union([z.string(), z.record(z.string())]),
|
|
34
|
-
/**
|
|
35
|
-
* The link to the topic’s content which an be a relative link to local files or the full URL of an external page.
|
|
36
|
-
*
|
|
37
|
-
* For internal links, the link can either be a page included in the items array or a different page acting as the
|
|
38
|
-
* topic’s landing page.
|
|
39
|
-
*/
|
|
40
|
-
link: z.string(),
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
const sidebarTopicLinkSchema = sidebarTopicBaseSchema;
|
|
44
|
-
|
|
45
|
-
const sidebarTopicGroupSchema = sidebarTopicBaseSchema.extend({
|
|
46
|
-
/**
|
|
47
|
-
* An optional unique identifier for the topic that can be used to associate content pages that are not listed in any
|
|
48
|
-
* topic sidebar configuration with this topic.
|
|
49
|
-
*/
|
|
50
|
-
id: z.string().optional(),
|
|
51
|
-
/**
|
|
52
|
-
* The sidebar items (links and subcategories) to display for this topic.
|
|
53
|
-
*
|
|
54
|
-
* The topic’s sidebar navigation items. This represents the sidebar displayed when the topic `link` page or any of
|
|
55
|
-
* the pages configured in the `items` array is the current page.
|
|
56
|
-
*/
|
|
57
|
-
items: z.any().array() as z.Schema<NonNullable<StarlightUserConfig["sidebar"]>>,
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
export const StarlightSidebarTopicsDropdownConfigSchema = z
|
|
61
|
-
.union([sidebarTopicGroupSchema, sidebarTopicLinkSchema])
|
|
62
|
-
.array();
|
|
63
|
-
|
|
64
|
-
export type StarlightSidebarTopicsDropdownUserConfig = z.input<
|
|
65
|
-
typeof StarlightSidebarTopicsDropdownConfigSchema
|
|
66
|
-
>;
|
|
67
|
-
export type StarlightSidebarTopicsDropdownConfig = z.output<
|
|
68
|
-
typeof StarlightSidebarTopicsDropdownConfigSchema
|
|
69
|
-
>;
|
|
70
|
-
|
|
71
|
-
export type StarlightSidebarTopicsDropdownSharedConfig = (
|
|
72
|
-
| (z.output<typeof sidebarTopicLinkSchema> & { type: "link" })
|
|
73
|
-
| (Omit<z.output<typeof sidebarTopicGroupSchema>, "items"> & { type: "group" })
|
|
74
|
-
)[];
|
package/lib/content.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { Props } from "@astrojs/starlight/props";
|
|
2
|
-
import type { TopicFrontmatterSchema } from "../schema";
|
|
3
|
-
export function isStarlightEntryWithTopic(entry: StarlightEntry): entry is StarlightEntryWithTopic {
|
|
4
|
-
return (
|
|
5
|
-
"data" in entry &&
|
|
6
|
-
"topic" in (entry as StarlightEntryWithTopic).data &&
|
|
7
|
-
typeof (entry as StarlightEntryWithTopic).data.topic === "string"
|
|
8
|
-
);
|
|
9
|
-
}
|
|
10
|
-
export type StarlightEntry = Props["entry"];
|
|
11
|
-
export type StarlightEntryWithTopic = StarlightEntry & { data: TopicFrontmatterSchema };
|
package/lib/i18n.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import starlightConfig from "virtual:starlight/user-config";
|
|
2
|
-
|
|
3
|
-
import { stripTrailingSlash } from "./pathname";
|
|
4
|
-
|
|
5
|
-
export function getLocalizedSlug(slug: string, locale: string | undefined): string {
|
|
6
|
-
const slugLocale = getLocaleFromSlug(slug);
|
|
7
|
-
if (slugLocale === locale) return slug;
|
|
8
|
-
locale ??= "";
|
|
9
|
-
if (slugLocale === slug) return locale;
|
|
10
|
-
|
|
11
|
-
if (slugLocale) {
|
|
12
|
-
return stripTrailingSlash(slug.replace(`${slugLocale}/`, locale ? `${locale}/` : ""));
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return slug ? `${locale}/${slug}` : locale;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function getLocaleFromSlug(slug: string): string | undefined {
|
|
19
|
-
const locales = Object.keys(starlightConfig.locales ?? {});
|
|
20
|
-
const baseSegment = slug.split("/")[0];
|
|
21
|
-
return baseSegment && locales.includes(baseSegment) ? baseSegment : undefined;
|
|
22
|
-
}
|
package/lib/pathname.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export function arePathnamesEqual(pathnameA: string, pathnameB: string) {
|
|
2
|
-
return stripLeadingAndTrailingSlashes(pathnameA) === stripLeadingAndTrailingSlashes(pathnameB);
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
export function stripLeadingAndTrailingSlashes(pathname: string): string {
|
|
6
|
-
return stripLeadingSlash(stripTrailingSlash(pathname));
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function stripLeadingSlash(pathname: string) {
|
|
10
|
-
if (pathname.startsWith("/")) pathname = pathname.slice(1);
|
|
11
|
-
return pathname;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function stripTrailingSlash(pathname: string) {
|
|
15
|
-
if (pathname.endsWith("/")) pathname = pathname.slice(0, -1);
|
|
16
|
-
return pathname;
|
|
17
|
-
}
|
package/lib/plugin.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { StarlightUserConfig } from "@astrojs/starlight/types";
|
|
2
|
-
import type { AstroIntegrationLogger } from "astro";
|
|
3
|
-
import { AstroError } from "astro/errors";
|
|
4
|
-
|
|
5
|
-
export function throwPluginError(message: string, hint?: string): never {
|
|
6
|
-
throw new AstroError(
|
|
7
|
-
message,
|
|
8
|
-
hint ??
|
|
9
|
-
`See the error report above for more informations.\n\nIf you believe this is a bug, please file an issue at https://github.com/HiDeoo/starlight-sidebar-topics-dropdown/issues/new/choose`
|
|
10
|
-
);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function overrideStarlightComponent(
|
|
14
|
-
components: StarlightUserConfig["components"],
|
|
15
|
-
logger: AstroIntegrationLogger,
|
|
16
|
-
component: keyof NonNullable<StarlightUserConfig["components"]>
|
|
17
|
-
) {
|
|
18
|
-
if (components?.[component]) {
|
|
19
|
-
logger.warn(
|
|
20
|
-
`It looks like you already have a \`${component}\` component override in your Starlight configuration.`
|
|
21
|
-
);
|
|
22
|
-
logger.warn(
|
|
23
|
-
`To use \`starlight-sidebar-topics-dropdown\`, either remove your override or update it to render the content from \`starlight-sidebar-topics-dropdown/overrides/${component}.astro\`.`
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
-
return {};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return {
|
|
30
|
-
[component]: `starlight-sidebar-topics-dropdown/overrides/${component}.astro`,
|
|
31
|
-
};
|
|
32
|
-
}
|
package/lib/sidebar.ts
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import type { Props } from "@astrojs/starlight/props";
|
|
2
|
-
|
|
3
|
-
import type { StarlightSidebarTopicsDropdownSharedConfig } from "./config";
|
|
4
|
-
import { isStarlightEntryWithTopic, type StarlightEntry } from "./content";
|
|
5
|
-
import { getLocaleFromSlug, getLocalizedSlug } from "./i18n";
|
|
6
|
-
import { arePathnamesEqual, stripLeadingAndTrailingSlashes } from "./pathname";
|
|
7
|
-
|
|
8
|
-
const absoluteLinkRegex = /^https?:\/\//;
|
|
9
|
-
|
|
10
|
-
export function getCurrentTopic(
|
|
11
|
-
config: StarlightSidebarTopicsDropdownSharedConfig,
|
|
12
|
-
sidebar: SidebarEntry[],
|
|
13
|
-
currentSlug: string,
|
|
14
|
-
entry: StarlightEntry
|
|
15
|
-
): Topic | undefined {
|
|
16
|
-
// If the current page has a topic ID, use it to find the topic.
|
|
17
|
-
const topicId = getTopicIdFromEntry(entry);
|
|
18
|
-
if (topicId) return getTopicById(config, sidebar, topicId);
|
|
19
|
-
|
|
20
|
-
// Start by checking if the current page is a topic root.
|
|
21
|
-
const topicFromSlug = getTopicFromSlug(config, sidebar, currentSlug);
|
|
22
|
-
if (topicFromSlug) return topicFromSlug;
|
|
23
|
-
|
|
24
|
-
// Otherwise, find the current topic by looking for the current page in the sidebar.
|
|
25
|
-
const currentSidebarTopic = getCurrentSidebarTopic(sidebar);
|
|
26
|
-
if (!currentSidebarTopic) return;
|
|
27
|
-
|
|
28
|
-
const currentTopicConfig = config[Number.parseInt(currentSidebarTopic.label, 10)];
|
|
29
|
-
if (!currentTopicConfig) return;
|
|
30
|
-
|
|
31
|
-
return { config: currentTopicConfig, sidebar: currentSidebarTopic.entries };
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function isTopicFirstPage(sidebar: SidebarEntry[], currentSlug: string): boolean {
|
|
35
|
-
const currentSidebarTopic = getCurrentSidebarTopic(sidebar);
|
|
36
|
-
if (!currentSidebarTopic) return false;
|
|
37
|
-
|
|
38
|
-
const firstPage = getSidebarFirstPage(currentSidebarTopic.entries);
|
|
39
|
-
if (!firstPage) return false;
|
|
40
|
-
|
|
41
|
-
return arePathnamesEqual(stripLeadingAndTrailingSlashes(firstPage.href), currentSlug);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function isTopicLastPage(sidebar: SidebarEntry[], currentSlug: string): boolean {
|
|
45
|
-
const currentSidebarTopic = getCurrentSidebarTopic(sidebar);
|
|
46
|
-
if (!currentSidebarTopic) return false;
|
|
47
|
-
|
|
48
|
-
const lastPage = getSidebarLastPage(currentSidebarTopic.entries);
|
|
49
|
-
if (!lastPage) return false;
|
|
50
|
-
|
|
51
|
-
return arePathnamesEqual(stripLeadingAndTrailingSlashes(lastPage.href), currentSlug);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function getSidebarFirstPage(sidebar: SidebarEntry[]) {
|
|
55
|
-
const entry = sidebar[0];
|
|
56
|
-
if (!entry) return;
|
|
57
|
-
if (entry.type === "link") return entry;
|
|
58
|
-
|
|
59
|
-
return getSidebarFirstPage(entry.entries);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function getSidebarLastPage(sidebar: SidebarEntry[]) {
|
|
63
|
-
const entry = sidebar.at(-1);
|
|
64
|
-
if (!entry) return;
|
|
65
|
-
if (entry.type === "link") return entry;
|
|
66
|
-
|
|
67
|
-
return getSidebarLastPage(entry.entries);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function getTopicFromSlug(
|
|
71
|
-
config: StarlightSidebarTopicsDropdownSharedConfig,
|
|
72
|
-
sidebar: SidebarEntry[],
|
|
73
|
-
slug: string
|
|
74
|
-
): Topic | undefined {
|
|
75
|
-
let topicConfig: Topic["config"] | undefined;
|
|
76
|
-
let topicSidebar: Topic["sidebar"] | undefined;
|
|
77
|
-
|
|
78
|
-
// Start by checking if the current page is a topic homepage.
|
|
79
|
-
let groupTopicIndex = -1;
|
|
80
|
-
const slugLocale = getLocaleFromSlug(slug);
|
|
81
|
-
|
|
82
|
-
for (const topic of config) {
|
|
83
|
-
if (topic.type === "group") groupTopicIndex++;
|
|
84
|
-
|
|
85
|
-
if (
|
|
86
|
-
!absoluteLinkRegex.test(topic.link) &&
|
|
87
|
-
arePathnamesEqual(
|
|
88
|
-
getLocalizedSlug(stripLeadingAndTrailingSlashes(topic.link), slugLocale),
|
|
89
|
-
slug
|
|
90
|
-
) &&
|
|
91
|
-
groupTopicIndex !== -1
|
|
92
|
-
) {
|
|
93
|
-
const sidebarTopic = sidebar[groupTopicIndex];
|
|
94
|
-
|
|
95
|
-
if (sidebarTopic?.type === "group") {
|
|
96
|
-
topicConfig = topic;
|
|
97
|
-
topicSidebar = sidebarTopic.entries;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
break;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if (!topicConfig || !topicSidebar) return;
|
|
105
|
-
|
|
106
|
-
return { config: topicConfig, sidebar: topicSidebar };
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function getTopicById(
|
|
110
|
-
config: StarlightSidebarTopicsDropdownSharedConfig,
|
|
111
|
-
sidebar: SidebarEntry[],
|
|
112
|
-
id: string
|
|
113
|
-
): Topic | undefined {
|
|
114
|
-
let topicConfig: Topic["config"] | undefined;
|
|
115
|
-
let topicSidebar: Topic["sidebar"] | undefined;
|
|
116
|
-
let groupTopicIndex = -1;
|
|
117
|
-
for (const topic of config) {
|
|
118
|
-
if (topic.type === "group") groupTopicIndex++;
|
|
119
|
-
if (topic.type === "group" && topic.id === id) {
|
|
120
|
-
const sidebarTopic = sidebar[groupTopicIndex];
|
|
121
|
-
if (sidebarTopic?.type === "group") {
|
|
122
|
-
topicConfig = topic;
|
|
123
|
-
topicSidebar = sidebarTopic.entries;
|
|
124
|
-
}
|
|
125
|
-
break;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
if (!topicConfig || !topicSidebar) return;
|
|
129
|
-
return { config: topicConfig, sidebar: topicSidebar };
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function getCurrentSidebarTopic(sidebar: SidebarEntry[]): SidebarTopic | undefined {
|
|
133
|
-
let currentSidebarTopic: SidebarTopic | undefined;
|
|
134
|
-
|
|
135
|
-
for (const topic of sidebar) {
|
|
136
|
-
if (topic.type === "link") continue;
|
|
137
|
-
|
|
138
|
-
const currentSidebarEntry = getCurrentSidebarEntry(topic.entries);
|
|
139
|
-
|
|
140
|
-
if (currentSidebarEntry) {
|
|
141
|
-
currentSidebarTopic = topic;
|
|
142
|
-
break;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return currentSidebarTopic;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function getCurrentSidebarEntry(sidebar: SidebarEntry[]): SidebarEntry | undefined {
|
|
150
|
-
return sidebar.find((entry) => {
|
|
151
|
-
if (entry.type === "link") {
|
|
152
|
-
return entry.isCurrent;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return getCurrentSidebarEntry(entry.entries);
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function getTopicIdFromEntry(entry: StarlightEntry): string | undefined {
|
|
160
|
-
return isStarlightEntryWithTopic(entry) ? entry.data.topic : undefined;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
type SidebarEntry = Props["sidebar"][number];
|
|
164
|
-
|
|
165
|
-
interface SidebarTopic {
|
|
166
|
-
label: string;
|
|
167
|
-
entries: SidebarEntry[];
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export interface Topic {
|
|
171
|
-
config: StarlightSidebarTopicsDropdownSharedConfig[number];
|
|
172
|
-
sidebar: SidebarEntry[];
|
|
173
|
-
}
|
package/lib/vite.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { ViteUserConfig } from "astro";
|
|
2
|
-
|
|
3
|
-
import type {
|
|
4
|
-
StarlightSidebarTopicsDropdownConfig,
|
|
5
|
-
StarlightSidebarTopicsDropdownSharedConfig,
|
|
6
|
-
} from "./config";
|
|
7
|
-
|
|
8
|
-
const moduleId = "virtual:starlight-sidebar-topics-dropdown/config";
|
|
9
|
-
|
|
10
|
-
export function vitePluginStarlightSidebarTopicsDropdown(
|
|
11
|
-
config: StarlightSidebarTopicsDropdownConfig
|
|
12
|
-
): VitePlugin {
|
|
13
|
-
const resolvedModuleId = `\0${moduleId}`;
|
|
14
|
-
|
|
15
|
-
const sharedConfig: StarlightSidebarTopicsDropdownSharedConfig = config.map((topic) => {
|
|
16
|
-
if (!("items" in topic)) return { ...topic, type: "link" };
|
|
17
|
-
const { items, ...topicWithoutItems } = topic;
|
|
18
|
-
return { ...topicWithoutItems, type: "group" };
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
const moduleContent = `export default ${JSON.stringify(sharedConfig)}`;
|
|
22
|
-
|
|
23
|
-
return {
|
|
24
|
-
name: "vite-plugin-starlight-sidebar-topics-dropdown",
|
|
25
|
-
load(id) {
|
|
26
|
-
return id === resolvedModuleId ? moduleContent : undefined;
|
|
27
|
-
},
|
|
28
|
-
resolveId(id) {
|
|
29
|
-
return id === moduleId ? resolvedModuleId : undefined;
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
type VitePlugin = NonNullable<ViteUserConfig["plugins"]>[number];
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
import Default from '@astrojs/starlight/components/Pagination.astro'
|
|
3
|
-
import type { Props } from '@astrojs/starlight/props'
|
|
4
|
-
|
|
5
|
-
import { isTopicFirstPage, isTopicLastPage } from '../lib/sidebar'
|
|
6
|
-
|
|
7
|
-
const { pagination, sidebar, slug } = Astro.props
|
|
8
|
-
let { prev, next } = pagination
|
|
9
|
-
|
|
10
|
-
if (isTopicFirstPage(sidebar, slug)) {
|
|
11
|
-
prev = undefined
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (isTopicLastPage(sidebar, slug)) {
|
|
15
|
-
next = undefined
|
|
16
|
-
}
|
|
17
|
-
---
|
|
18
|
-
|
|
19
|
-
<Default {...Astro.props} pagination={{ prev, next }}><slot /></Default>
|