@rimelight/ui 0.0.16 → 0.0.17

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.
@@ -1,132 +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
- }
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
+ }
@@ -1,78 +0,0 @@
1
- import { defineMiddleware } from "astro:middleware"
2
- // @ts-ignore - virtual module generated by the SRI integration
3
- import { manifest } from "virtual:sri-manifest"
4
-
5
- declare const HTMLRewriter: any
6
-
7
- function assertManifest(obj: unknown): asserts obj is Record<string, string> {
8
- if (!obj || typeof obj !== "object") throw new Error("Expected manifest")
9
- }
10
-
11
- /**
12
- * SRI Middleware Injects integrity attributes into script tags based on the pre-generated manifest.
13
- * Handles both SSR streaming (via HTMLRewriter) and post-rendering fallback.
14
- */
15
- export const sri = defineMiddleware(async (_, next) => {
16
- const response = await next()
17
- const contentType = response.headers.get("content-type") || ""
18
-
19
- // Only process HTML responses
20
- if (!contentType.includes("text/html")) {
21
- return response
22
- }
23
-
24
- // Use Cloudflare's HTMLRewriter if available (SSR Production/Preview)
25
- if (typeof HTMLRewriter !== "undefined") {
26
- return new HTMLRewriter()
27
- .on("script[src]", {
28
- element(el: any) {
29
- const src = el.getAttribute("src")
30
- if (src && manifest[src]) {
31
- el.setAttribute("integrity", manifest[src])
32
- el.setAttribute("crossorigin", "anonymous")
33
- }
34
- }
35
- })
36
- .on('link[rel="stylesheet"][href]', {
37
- element(el: any) {
38
- const href = el.getAttribute("href")
39
- if (href && manifest[href]) {
40
- el.setAttribute("integrity", manifest[href])
41
- el.setAttribute("crossorigin", "anonymous")
42
- }
43
- }
44
- })
45
- .transform(response)
46
- }
47
-
48
- // Fallback for Dev
49
- let modifiedHtml = await response.text()
50
-
51
- // Simple RegEx replacement for Dev inspection
52
- assertManifest(manifest)
53
-
54
- const entries = Object.entries(manifest)
55
- for (const [url, hash] of entries) {
56
- if (!hash) continue
57
-
58
- // Look for script tags with the specific URL
59
- const scriptRegex = new RegExp(`(<script[^>]+src=["']${url}["'][^>]*)(/?>)`, "g")
60
- modifiedHtml = modifiedHtml.replace(
61
- scriptRegex,
62
- `$1 integrity="${hash}" crossorigin="anonymous"$2`
63
- )
64
-
65
- // Look for link tags with the specific URL
66
- const linkRegex = new RegExp(`(<link[^>]+href=["']${url}["'][^>]*)(/?>)`, "g")
67
- modifiedHtml = modifiedHtml.replace(
68
- linkRegex,
69
- `$1 integrity="${hash}" crossorigin="anonymous"$2`
70
- )
71
- }
72
-
73
- return new Response(modifiedHtml, {
74
- status: response.status,
75
- statusText: response.statusText,
76
- headers: response.headers
77
- })
78
- })