starlight-sidebar-topics-dropdown 0.2.1 → 0.2.2

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/index.ts CHANGED
@@ -1,82 +1,82 @@
1
- import type { StarlightPlugin, StarlightUserConfig } from "@astrojs/starlight/types";
2
-
3
- import {
4
- StarlightSidebarTopicsDropdownConfigSchema,
5
- type StarlightSidebarTopicsDropdownUserConfig,
6
- } from "./lib/config";
7
- import { overrideStarlightComponent, throwPluginError } from "./lib/plugin";
8
- import { vitePluginStarlightSidebarTopicsDropdown } from "./lib/vite";
9
-
10
- export type {
11
- StarlightSidebarTopicsDropdownConfig,
12
- StarlightSidebarTopicsDropdownUserConfig,
13
- } from "./lib/config";
14
-
15
- export default function starlightSidebarTopicsDropdownPlugin(
16
- userConfig: StarlightSidebarTopicsDropdownUserConfig
17
- ): StarlightPlugin {
18
- const parsedConfig = StarlightSidebarTopicsDropdownConfigSchema.safeParse(userConfig);
19
-
20
- if (!parsedConfig.success) {
21
- throwPluginError(
22
- `The provided plugin configuration is invalid.\n${parsedConfig.error.issues
23
- .map((issue) => issue.message)
24
- .join("\n")}`
25
- );
26
- }
27
-
28
- const config = parsedConfig.data;
29
-
30
- return {
31
- name: "starlight-sidebar-topics-dropdown-plugin",
32
- hooks: {
33
- setup({ addIntegration, command, config: starlightConfig, logger, updateConfig }) {
34
- if (command !== "dev" && command !== "build") return;
35
-
36
- if (starlightConfig.sidebar) {
37
- throwPluginError(
38
- "It looks like you have a `sidebar` configured in your Starlight configuration. To use `starlight-sidebar-topics-dropdown`, create a new topic with your sidebar items.",
39
- "Learn more about topic configuration at https://starlight-sidebar-topics-dropdown.netlify.app/docs/configuration/"
40
- );
41
- }
42
-
43
- const sidebar: StarlightUserConfig["sidebar"] = [];
44
-
45
- for (const [index, topic] of config.entries()) {
46
- if ("items" in topic) {
47
- sidebar.push({ label: String(index), items: topic.items });
48
- }
49
- }
50
- updateConfig({
51
- components: {
52
- ...starlightConfig.components,
53
- ...overrideStarlightComponent(
54
- starlightConfig.components,
55
- logger,
56
- "Sidebar"
57
- ),
58
- ...overrideStarlightComponent(
59
- starlightConfig.components,
60
- logger,
61
- "Pagination"
62
- ),
63
- },
64
- sidebar,
65
- });
66
-
67
- addIntegration({
68
- name: "starlight-sidebar-topics-dropdown-integration",
69
- hooks: {
70
- "astro:config:setup": ({ updateConfig }) => {
71
- updateConfig({
72
- vite: {
73
- plugins: [vitePluginStarlightSidebarTopicsDropdown(config)],
74
- },
75
- });
76
- },
77
- },
78
- });
79
- },
80
- },
81
- };
82
- }
1
+ import type { StarlightPlugin, StarlightUserConfig } from "@astrojs/starlight/types";
2
+
3
+ import {
4
+ StarlightSidebarTopicsDropdownConfigSchema,
5
+ type StarlightSidebarTopicsDropdownUserConfig,
6
+ } from "./lib/config";
7
+ import { overrideStarlightComponent, throwPluginError } from "./lib/plugin";
8
+ import { vitePluginStarlightSidebarTopicsDropdown } from "./lib/vite";
9
+
10
+ export type {
11
+ StarlightSidebarTopicsDropdownConfig,
12
+ StarlightSidebarTopicsDropdownUserConfig,
13
+ } from "./lib/config";
14
+
15
+ export default function starlightSidebarTopicsDropdownPlugin(
16
+ userConfig: StarlightSidebarTopicsDropdownUserConfig
17
+ ): StarlightPlugin {
18
+ const parsedConfig = StarlightSidebarTopicsDropdownConfigSchema.safeParse(userConfig);
19
+
20
+ if (!parsedConfig.success) {
21
+ throwPluginError(
22
+ `The provided plugin configuration is invalid.\n${parsedConfig.error.issues
23
+ .map((issue) => issue.message)
24
+ .join("\n")}`
25
+ );
26
+ }
27
+
28
+ const config = parsedConfig.data;
29
+
30
+ return {
31
+ name: "starlight-sidebar-topics-dropdown-plugin",
32
+ hooks: {
33
+ setup({ addIntegration, command, config: starlightConfig, logger, updateConfig }) {
34
+ if (command !== "dev" && command !== "build") return;
35
+
36
+ if (starlightConfig.sidebar) {
37
+ throwPluginError(
38
+ "It looks like you have a `sidebar` configured in your Starlight configuration. To use `starlight-sidebar-topics-dropdown`, create a new topic with your sidebar items.",
39
+ "Learn more about topic configuration at https://starlight-sidebar-topics-dropdown.netlify.app/docs/configuration/"
40
+ );
41
+ }
42
+
43
+ const sidebar: StarlightUserConfig["sidebar"] = [];
44
+
45
+ for (const [index, topic] of config.entries()) {
46
+ if ("items" in topic) {
47
+ sidebar.push({ label: String(index), items: topic.items });
48
+ }
49
+ }
50
+ updateConfig({
51
+ components: {
52
+ ...starlightConfig.components,
53
+ ...overrideStarlightComponent(
54
+ starlightConfig.components,
55
+ logger,
56
+ "Sidebar"
57
+ ),
58
+ ...overrideStarlightComponent(
59
+ starlightConfig.components,
60
+ logger,
61
+ "Pagination"
62
+ ),
63
+ },
64
+ sidebar,
65
+ });
66
+
67
+ addIntegration({
68
+ name: "starlight-sidebar-topics-dropdown-integration",
69
+ hooks: {
70
+ "astro:config:setup": ({ updateConfig }) => {
71
+ updateConfig({
72
+ vite: {
73
+ plugins: [vitePluginStarlightSidebarTopicsDropdown(config)],
74
+ },
75
+ });
76
+ },
77
+ },
78
+ });
79
+ },
80
+ },
81
+ };
82
+ }
package/lib/config.ts CHANGED
@@ -1,74 +1,74 @@
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
- )[];
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 CHANGED
@@ -1,11 +1,11 @@
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 };
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 CHANGED
@@ -1,22 +1,22 @@
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
- }
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 CHANGED
@@ -1,17 +1,17 @@
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
- }
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 CHANGED
@@ -1,32 +1,32 @@
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
- }
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
+ }