@rimelight/ui 0.0.15 → 0.0.16
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/package.json +3 -1
- package/src/env.d.ts +3 -0
- package/src/integrations/sri.ts +92 -92
- package/src/middleware/security.ts +9 -2
- package/src/middleware/sri.ts +78 -78
- package/src/plugins/starlightAddons/Sidebar.astro +89 -0
- package/src/plugins/starlightAddons/data.ts +39 -0
- package/src/plugins/starlightAddons/index.ts +132 -63
- package/src/plugins/starlightAddons/libs/config.ts +107 -0
- package/src/plugins/starlightAddons/libs/content.ts +20 -0
- package/src/plugins/starlightAddons/libs/i18n.ts +51 -0
- package/src/plugins/starlightAddons/libs/locals.ts +12 -0
- package/src/plugins/starlightAddons/libs/pathname.ts +22 -0
- package/src/plugins/starlightAddons/libs/plugin.ts +9 -0
- package/src/plugins/starlightAddons/libs/sidebar.ts +183 -0
- package/src/plugins/starlightAddons/libs/vite.ts +46 -0
- package/src/plugins/starlightAddons/locals.d.ts +14 -0
- package/src/plugins/starlightAddons/middleware.ts +132 -0
- package/src/plugins/starlightAddons/overrides/Sidebar.astro +8 -0
- package/src/plugins/starlightAddons/schema.ts +13 -0
- package/src/plugins/starlightAddons/starlight.d.ts +6 -0
- package/src/plugins/starlightAddons/virtual.d.ts +13 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { defineRouteMiddleware } from "@astrojs/starlight/route-data"
|
|
2
|
+
import type { APIContext } from "astro"
|
|
3
|
+
import { getRelativeLocaleUrl } from "astro:i18n"
|
|
4
|
+
import picomatch from "picomatch"
|
|
5
|
+
import config from "virtual:starlight-sidebar-topics/config"
|
|
6
|
+
import options from "virtual:starlight-sidebar-topics/options"
|
|
7
|
+
import type { StarlightSidebarTopicsSharedConfig } from "./libs/config"
|
|
8
|
+
import type { StarlightSidebarTopicsSharedOptions } from "./libs/config"
|
|
9
|
+
import type { StarlightEntry } from "./libs/content"
|
|
10
|
+
|
|
11
|
+
import type { StarlightSidebarTopicsRouteData } from "./data"
|
|
12
|
+
import { getTranslation } from "./libs/i18n"
|
|
13
|
+
import { ensureLeadingSlash } from "./libs/pathname"
|
|
14
|
+
import { throwPluginError } from "./libs/plugin"
|
|
15
|
+
import {
|
|
16
|
+
getCurrentTopic,
|
|
17
|
+
getTopicById,
|
|
18
|
+
isTopicFirstPage,
|
|
19
|
+
isTopicLastPage,
|
|
20
|
+
type Topic
|
|
21
|
+
} from "./libs/sidebar"
|
|
22
|
+
|
|
23
|
+
type StarlightRouteLike = {
|
|
24
|
+
entry: StarlightEntry
|
|
25
|
+
hasSidebar: boolean
|
|
26
|
+
id: string
|
|
27
|
+
pagination: { prev?: unknown; next?: unknown }
|
|
28
|
+
sidebar: any[]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const onRequest = defineRouteMiddleware((context) => {
|
|
32
|
+
// Locals are augmented to include starlightRoute for typing without casts
|
|
33
|
+
const { starlightRoute } = context.locals
|
|
34
|
+
const { entry, hasSidebar, id, pagination, sidebar } = starlightRoute
|
|
35
|
+
|
|
36
|
+
const topicsConfig = config as unknown as StarlightSidebarTopicsSharedConfig
|
|
37
|
+
const topicsOptions = options as unknown as StarlightSidebarTopicsSharedOptions
|
|
38
|
+
|
|
39
|
+
let currentTopic: Topic | undefined
|
|
40
|
+
|
|
41
|
+
if (hasSidebar) {
|
|
42
|
+
currentTopic = getCurrentTopic(topicsConfig, sidebar, id, entry)
|
|
43
|
+
|
|
44
|
+
if (!currentTopic) {
|
|
45
|
+
const normalizedId = ensureLeadingSlash(id)
|
|
46
|
+
|
|
47
|
+
for (const [topicId, patterns] of Object.entries(topicsOptions.topics)) {
|
|
48
|
+
if (!picomatch(patterns)(normalizedId)) continue
|
|
49
|
+
currentTopic = getTopicById(topicsConfig, sidebar, topicId)
|
|
50
|
+
break
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!currentTopic) {
|
|
55
|
+
if (picomatch(options.exclude)(ensureLeadingSlash(id))) {
|
|
56
|
+
attachRouteData(context, currentTopic)
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
throwPluginError(
|
|
61
|
+
`Failed to find the topic for the \`${id}\` page.`,
|
|
62
|
+
`Either include this page in the sidebar configuration of the desired topic using the \`items\` property, associate an unlisted page with a topic using the \`topic\` frontmatter property or the \`topics\` option, or exclude this page from any topic using the \`exclude\` option.
|
|
63
|
+
|
|
64
|
+
Learn more in the following guides:
|
|
65
|
+
|
|
66
|
+
- [Unlisted pages](https://starlight-sidebar-topics.netlify.app/docs/guides/unlisted-pages/)
|
|
67
|
+
- [Excluded pages](https://starlight-sidebar-topics.netlify.app/docs/guides/excluded-pages/)`
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
starlightRoute.sidebar = currentTopic.sidebar
|
|
72
|
+
|
|
73
|
+
if (isTopicFirstPage(sidebar, id)) {
|
|
74
|
+
pagination.prev = undefined
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (isTopicLastPage(sidebar, id)) {
|
|
78
|
+
pagination.next = undefined
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
attachRouteData(context, currentTopic)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
function attachRouteData(context: APIContext, currentTopic: Topic | undefined) {
|
|
86
|
+
context.locals.starlightSidebarTopics = getRouteData(context.currentLocale, currentTopic)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function getRouteData(
|
|
90
|
+
currentLocale: APIContext["currentLocale"],
|
|
91
|
+
currentTopic: Topic | undefined
|
|
92
|
+
): StarlightSidebarTopicsRouteData {
|
|
93
|
+
// Build route data with strongly typed config
|
|
94
|
+
const topicConfigs = config as unknown as StarlightSidebarTopicsSharedConfig
|
|
95
|
+
return {
|
|
96
|
+
isPageWithTopic: currentTopic !== undefined,
|
|
97
|
+
topics: topicConfigs.map((topic) => {
|
|
98
|
+
const isLinkTopic = topic.type === "link"
|
|
99
|
+
|
|
100
|
+
const topicRouteData: StarlightSidebarTopicsRouteData["topics"][number] = {
|
|
101
|
+
isCurrent:
|
|
102
|
+
isLinkTopic || !currentTopic
|
|
103
|
+
? false
|
|
104
|
+
: topic.label === currentTopic.config.label && topic.link === currentTopic.config.link,
|
|
105
|
+
label:
|
|
106
|
+
typeof topic.label === "string"
|
|
107
|
+
? topic.label
|
|
108
|
+
: getTranslation(currentLocale, topic.label, topic.link, "topic label"),
|
|
109
|
+
link:
|
|
110
|
+
!isLinkTopic && currentLocale
|
|
111
|
+
? getRelativeLocaleUrl(currentLocale, topic.link)
|
|
112
|
+
: topic.link
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (topic.badge) {
|
|
116
|
+
topicRouteData.badge = {
|
|
117
|
+
text:
|
|
118
|
+
typeof topic.badge.text === "string"
|
|
119
|
+
? topic.badge.text
|
|
120
|
+
: getTranslation(currentLocale, topic.badge.text, topic.link, "topic badge text"),
|
|
121
|
+
variant: topic.badge.variant
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (topic.icon) {
|
|
126
|
+
topicRouteData.icon = topic.icon
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return topicRouteData
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { z } from "astro/zod"
|
|
2
|
+
|
|
3
|
+
export const topicSchema = z.object({
|
|
4
|
+
/**
|
|
5
|
+
* ID of the topic to associate with the current page if the page is not listed in any topic
|
|
6
|
+
* sidebar configuration.
|
|
7
|
+
*
|
|
8
|
+
* @see https://starlight-sidebar-topics.netlify.app/docs/guides/unlisted-pages/
|
|
9
|
+
*/
|
|
10
|
+
topic: z.string().optional()
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
export type TopicFrontmatterSchema = z.input<typeof topicSchema>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare module "virtual:starlight-sidebar-topics/config" {
|
|
2
|
+
import type { StarlightSidebarTopicsSharedConfig } from "./libs/config"
|
|
3
|
+
const StarlightSidebarTopicsConfig: StarlightSidebarTopicsSharedConfig
|
|
4
|
+
|
|
5
|
+
export default StarlightSidebarTopicsConfig
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
declare module "virtual:starlight-sidebar-topics/options" {
|
|
9
|
+
import type { StarlightSidebarTopicsSharedOptions } from "./libs/config"
|
|
10
|
+
const StarlightSidebarTopicsOptions: StarlightSidebarTopicsSharedOptions
|
|
11
|
+
|
|
12
|
+
export default StarlightSidebarTopicsOptions
|
|
13
|
+
}
|