@nyxwire/starlight 0.0.0-stage → 0.1.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.
Files changed (44) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +221 -2
  3. package/dist/config.d.ts +9 -0
  4. package/dist/config.js +32 -0
  5. package/dist/docs/foundations/accessibility.md +71 -0
  6. package/dist/docs/foundations/color-roles.md +114 -0
  7. package/dist/docs/foundations/effects-motion.md +150 -0
  8. package/dist/docs/foundations/flavors.md +156 -0
  9. package/dist/docs/foundations/icons.md +45 -0
  10. package/dist/docs/foundations/palette.md +180 -0
  11. package/dist/docs/foundations/spacing-elevation.md +168 -0
  12. package/dist/docs/foundations/typography.md +80 -0
  13. package/dist/docs/index.md +51 -0
  14. package/dist/docs/kitchen-sink.md +283 -0
  15. package/dist/docs/manifest.json +61 -0
  16. package/dist/docs/setup.md +190 -0
  17. package/dist/ec-themes.d.ts +443 -0
  18. package/dist/ec-themes.js +217 -0
  19. package/dist/expressive-code.d.ts +75 -0
  20. package/dist/expressive-code.js +101 -0
  21. package/dist/index.d.ts +15 -0
  22. package/dist/index.js +25 -0
  23. package/dist/options.d.ts +29 -0
  24. package/dist/options.js +56 -0
  25. package/dist/runtime.d.ts +16 -0
  26. package/dist/runtime.js +52 -0
  27. package/dist/theme-bridge.d.ts +27 -0
  28. package/dist/theme-bridge.js +73 -0
  29. package/package.json +67 -3
  30. package/src/chrome.css +191 -0
  31. package/src/components/ThemeProvider.astro +15 -0
  32. package/src/components/ThemeSelect.astro +51 -0
  33. package/src/components/env.d.ts +5 -0
  34. package/src/config.ts +53 -0
  35. package/src/content-components.css +154 -0
  36. package/src/ec-themes.ts +246 -0
  37. package/src/expressive-code.ts +115 -0
  38. package/src/index.ts +32 -0
  39. package/src/options.ts +80 -0
  40. package/src/prose.css +108 -0
  41. package/src/runtime.ts +67 -0
  42. package/src/theme-bridge.css +130 -0
  43. package/src/theme-bridge.ts +89 -0
  44. package/styles.css +299 -0
@@ -0,0 +1,246 @@
1
+ import { ExpressiveCodeTheme } from '@expressive-code/core'
2
+ import { defaultFlavor, flavorMeta, flavorNames, palette, syntaxRoles } from '@nyxwire/palette'
3
+ import type { ColorName, DarkFlavorName, FlavorName } from '@nyxwire/palette'
4
+
5
+ type SyntaxRoleName = keyof typeof syntaxRoles
6
+ export type TokenRoleName = Exclude<SyntaxRoleName, 'editor-bg' | 'selection' | 'line-highlight'>
7
+
8
+ export type ExpressiveCodeTokenColor = {
9
+ readonly name: string
10
+ readonly scope: readonly string[]
11
+ readonly settings: { readonly foreground?: string; readonly fontStyle?: string }
12
+ }
13
+
14
+ /** A VS Code / TextMate theme object: plain JSON, accepted by Expressive Code and Shiki. */
15
+ export type NyxwireExpressiveCodeTheme = {
16
+ readonly name: string
17
+ readonly type: 'dark' | 'light'
18
+ readonly colors: Readonly<Record<string, string>>
19
+ readonly tokenColors: readonly ExpressiveCodeTokenColor[]
20
+ }
21
+
22
+ /** TextMate scopes per palette syntax role (contract §6.4). More specific scopes win in Shiki. */
23
+ export const TOKEN_SCOPES = {
24
+ keyword: [
25
+ 'keyword',
26
+ 'keyword.control',
27
+ 'keyword.other',
28
+ 'keyword.operator.new',
29
+ 'keyword.operator.expression',
30
+ 'keyword.operator.wordlike',
31
+ 'keyword.operator.logical.python',
32
+ 'variable.language',
33
+ ],
34
+ storage: ['storage', 'storage.type'],
35
+ modifier: ['storage.modifier', 'keyword.other.modifier'],
36
+ string: ['string', 'string.quoted', 'string.template', 'punctuation.definition.string'],
37
+ number: ['constant.numeric', 'keyword.other.unit'],
38
+ constant: [
39
+ 'constant',
40
+ 'constant.other',
41
+ 'constant.character',
42
+ 'variable.other.constant',
43
+ 'variable.other.enummember',
44
+ 'support.constant',
45
+ ],
46
+ boolean: ['constant.language', 'constant.language.boolean'],
47
+ null: [
48
+ 'constant.language.null',
49
+ 'constant.language.undefined',
50
+ 'constant.language.nil',
51
+ 'constant.language.none',
52
+ ],
53
+ function: ['entity.name.function', 'support.function', 'variable.function'],
54
+ method: ['entity.name.function.member', 'meta.method-call entity.name.function'],
55
+ type: [
56
+ 'entity.name.type',
57
+ 'support.type',
58
+ 'support.type.primitive',
59
+ 'entity.name.type.parameter',
60
+ ],
61
+ class: ['entity.name.class', 'support.class', 'entity.other.inherited-class'],
62
+ interface: ['entity.name.type.interface'],
63
+ enum: ['entity.name.type.enum'],
64
+ namespace: ['entity.name.namespace', 'entity.name.type.module', 'entity.name.scope-resolution'],
65
+ variable: ['variable', 'variable.other', 'variable.other.readwrite', 'meta.definition.variable'],
66
+ parameter: ['variable.parameter', 'meta.parameter'],
67
+ property: [
68
+ 'variable.other.property',
69
+ 'variable.other.object.property',
70
+ 'support.variable.property',
71
+ 'meta.object-literal.key',
72
+ 'support.type.property-name',
73
+ 'entity.name.tag.yaml',
74
+ ],
75
+ tag: ['entity.name.tag'],
76
+ attribute: ['entity.other.attribute-name'],
77
+ operator: ['keyword.operator', 'punctuation.separator.key-value'],
78
+ punctuation: [
79
+ 'punctuation',
80
+ 'punctuation.separator',
81
+ 'punctuation.terminator',
82
+ 'punctuation.accessor',
83
+ 'punctuation.definition.tag',
84
+ 'meta.brace',
85
+ ],
86
+ comment: ['comment', 'punctuation.definition.comment', 'string.comment'],
87
+ regex: ['string.regexp'],
88
+ escape: [
89
+ 'constant.character.escape',
90
+ 'punctuation.definition.template-expression',
91
+ 'punctuation.section.embedded',
92
+ ],
93
+ decorator: [
94
+ 'meta.decorator',
95
+ 'entity.name.function.decorator',
96
+ 'punctuation.decorator',
97
+ 'storage.type.annotation',
98
+ ],
99
+ 'markup-heading': ['markup.heading', 'entity.name.section.markdown'],
100
+ 'markup-link': ['markup.underline.link', 'string.other.link', 'markup.link'],
101
+ inserted: ['markup.inserted', 'meta.diff.header.to-file', 'punctuation.definition.inserted'],
102
+ deleted: [
103
+ 'markup.deleted',
104
+ 'meta.diff.header.from-file',
105
+ 'punctuation.definition.deleted',
106
+ 'invalid',
107
+ 'invalid.illegal',
108
+ ],
109
+ changed: ['markup.changed', 'meta.diff.range', 'punctuation.definition.changed'],
110
+ } as const satisfies Readonly<Record<TokenRoleName, readonly string[]>>
111
+
112
+ const FONT_STYLE_RULES: readonly ExpressiveCodeTokenColor[] = [
113
+ { name: 'markup-bold', scope: ['markup.bold'], settings: { fontStyle: 'bold' } },
114
+ { name: 'markup-italic', scope: ['markup.italic'], settings: { fontStyle: 'italic' } },
115
+ {
116
+ name: 'markup-strikethrough',
117
+ scope: ['markup.strikethrough'],
118
+ settings: { fontStyle: 'strikethrough' },
119
+ },
120
+ ]
121
+
122
+ function isTokenRole(role: string): role is TokenRoleName {
123
+ return Object.hasOwn(TOKEN_SCOPES, role)
124
+ }
125
+
126
+ function withAlpha(hex: string, opacity: number): string {
127
+ return `${hex}${Math.round(opacity * 255)
128
+ .toString(16)
129
+ .padStart(2, '0')}`
130
+ }
131
+
132
+ /**
133
+ * Builds a VS Code theme from one flavor. Dark flavors share their accents, so a dark theme only
134
+ * differs in its neutral editor colors; code-block surfaces are re-pointed to CSS variables by
135
+ * `expressiveCodeStyleOverrides`, which keeps them in sync with the active flavor at runtime.
136
+ */
137
+ export function createExpressiveCodeTheme(flavorName: FlavorName): NyxwireExpressiveCodeTheme {
138
+ const colors = palette[flavorName]
139
+ const { dark } = flavorMeta[flavorName]
140
+ const color = (name: ColorName): string => colors[name]
141
+ const role = (name: SyntaxRoleName): string => color(syntaxRoles[name].color)
142
+ const selection = withAlpha(role('selection'), syntaxRoles.selection.opacity)
143
+ const ansiBlack = dark ? color('surface1') : color('subtext1')
144
+ const ansiWhite = dark ? color('subtext1') : color('surface2')
145
+ const tokenColors = Object.keys(syntaxRoles)
146
+ .filter(isTokenRole)
147
+ .map((name): ExpressiveCodeTokenColor => {
148
+ const spec = syntaxRoles[name]
149
+ return {
150
+ name,
151
+ scope: TOKEN_SCOPES[name],
152
+ settings: {
153
+ foreground: color(spec.color),
154
+ ...('fontStyle' in spec ? { fontStyle: spec.fontStyle } : {}),
155
+ },
156
+ }
157
+ })
158
+ return {
159
+ name: `nyxwire-${flavorName}`,
160
+ type: dark ? 'dark' : 'light',
161
+ colors: {
162
+ 'editor.background': role('editor-bg'),
163
+ 'editor.foreground': color('text'),
164
+ 'editor.selectionBackground': selection,
165
+ 'editor.lineHighlightBackground': role('line-highlight'),
166
+ 'editorCursor.foreground': color('indigo'),
167
+ 'editorLineNumber.foreground': color('subtext0'),
168
+ 'editorLineNumber.activeForeground': color('subtext0'),
169
+ 'editorIndentGuide.background1': color('surface1'),
170
+ 'editorGroupHeader.tabsBackground': color('mantle'),
171
+ 'editorGroupHeader.tabsBorder': color('overlay0'),
172
+ 'tab.activeBackground': role('editor-bg'),
173
+ 'tab.activeForeground': color('text'),
174
+ 'tab.activeBorder': role('editor-bg'),
175
+ 'tab.activeBorderTop': color('indigo'),
176
+ 'tab.inactiveBackground': color('mantle'),
177
+ 'tab.inactiveForeground': color('subtext0'),
178
+ 'titleBar.activeBackground': color('mantle'),
179
+ 'titleBar.activeForeground': color('subtext1'),
180
+ 'titleBar.border': color('overlay0'),
181
+ 'terminal.background': role('editor-bg'),
182
+ 'terminal.foreground': color('text'),
183
+ 'terminal.ansiBlack': ansiBlack,
184
+ 'terminal.ansiRed': color('alarm'),
185
+ 'terminal.ansiGreen': color('matrix'),
186
+ 'terminal.ansiYellow': color('volt'),
187
+ 'terminal.ansiBlue': color('arc'),
188
+ 'terminal.ansiMagenta': color('indigo'),
189
+ 'terminal.ansiCyan': color('ice'),
190
+ 'terminal.ansiWhite': ansiWhite,
191
+ 'terminal.ansiBrightBlack': dark ? color('overlay1') : color('overlay2'),
192
+ 'terminal.ansiBrightRed': color('flare'),
193
+ 'terminal.ansiBrightGreen': color('matrix'),
194
+ 'terminal.ansiBrightYellow': color('circuit'),
195
+ 'terminal.ansiBrightBlue': color('steel'),
196
+ 'terminal.ansiBrightMagenta': color('synth'),
197
+ 'terminal.ansiBrightCyan': color('ice'),
198
+ 'terminal.ansiBrightWhite': dark ? color('text') : color('overlay0'),
199
+ focusBorder: color('indigo'),
200
+ 'scrollbarSlider.background': withAlpha(color('overlay1'), 0.4),
201
+ 'scrollbarSlider.hoverBackground': withAlpha(color('overlay2'), 0.6),
202
+ 'menu.selectionBackground': selection,
203
+ 'menu.selectionForeground': color('text'),
204
+ 'textLink.foreground': color('arc'),
205
+ 'widget.shadow': withAlpha(color('crust'), 0.6),
206
+ },
207
+ tokenColors: [...tokenColors, ...FONT_STYLE_RULES],
208
+ }
209
+ }
210
+
211
+ /** Raw dark theme built from Nocturne neutrals and the accents shared by every dark flavor. */
212
+ export const nyxwireDark: NyxwireExpressiveCodeTheme = createExpressiveCodeTheme('nocturne')
213
+ /** Raw light theme built from Lumen. */
214
+ export const nyxwireLight: NyxwireExpressiveCodeTheme = createExpressiveCodeTheme('lumen')
215
+
216
+ /**
217
+ * Rendered theme for one flavor: syntax ink is lifted to 5.5:1 on the dimmer of the two code
218
+ * surfaces (line highlight in dark flavors, editor background in Lumen). Raw themes stay untouched.
219
+ */
220
+ export function createCodeTheme(flavor: FlavorName) {
221
+ const { tokenColors, ...raw } = createExpressiveCodeTheme(flavor)
222
+ const theme = new ExpressiveCodeTheme({
223
+ ...raw,
224
+ tokenColors: tokenColors.map(({ scope, ...rule }) => ({ ...rule, scope: [...scope] })),
225
+ })
226
+ const colors = palette[flavor]
227
+ const weakestBackground = flavorMeta[flavor].dark
228
+ ? colors[syntaxRoles['line-highlight'].color]
229
+ : colors[syntaxRoles['editor-bg'].color]
230
+ theme.ensureMinSyntaxHighlightingColorContrast(5.5, weakestBackground)
231
+ // Starlight may load a different copy of Expressive Code, so hand it plain data.
232
+ return { name: theme.name, type: theme.type, colors: theme.colors, settings: theme.settings }
233
+ }
234
+
235
+ /** One rendered theme per flavor, the configured dark flavor first (Expressive Code's base theme). */
236
+ export function createCodeThemes(
237
+ dark: DarkFlavorName = defaultFlavor === 'lumen' ? 'nocturne' : defaultFlavor,
238
+ ) {
239
+ return [dark, ...flavorNames.filter((flavor) => flavor !== dark)].map(createCodeTheme)
240
+ }
241
+
242
+ /** Scopes each generated theme to `[data-flavor]`; any other theme gets no selector. */
243
+ export function flavorThemeSelector(theme: { readonly name: string }): string | false {
244
+ const flavor = flavorNames.find((id) => theme.name === `nyxwire-${id}`)
245
+ return flavor ? `[data-flavor='${flavor}']` : false
246
+ }
@@ -0,0 +1,115 @@
1
+ import type {
2
+ StarlightExpressiveCodeOptions,
3
+ StyleOverrides,
4
+ } from '@astrojs/starlight/expressive-code'
5
+ import type { StarlightUserConfig } from '@astrojs/starlight/types'
6
+ import { createCodeThemes, flavorThemeSelector } from './ec-themes.js'
7
+ import type { NyxwireStarlightConfig } from './options.js'
8
+
9
+ /**
10
+ * Expressive Code style settings that point every code-block surface at Nyxwire CSS
11
+ * variables, so frames, backgrounds and markers follow the active flavor and accent at runtime.
12
+ * Applied as global style overrides (not inside the theme objects) so user overrides still win.
13
+ */
14
+ export const expressiveCodeStyleOverrides = {
15
+ borderRadius: 'var(--nw-radius-lg)',
16
+ borderWidth: '1px',
17
+ borderColor: 'var(--nw-line)',
18
+ codeBackground: 'var(--nw-canvas-sunken)',
19
+ codeForeground: 'var(--nw-fg)',
20
+ codeFontFamily: 'var(--nw-font-mono)',
21
+ codeSelectionBackground: 'color-mix(in oklab, var(--nw-synth) 30%, transparent)',
22
+ gutterBorderColor: 'var(--nw-line)',
23
+ gutterForeground: 'var(--nw-fg-subtle)',
24
+ gutterHighlightForeground: 'var(--nw-fg-muted)',
25
+ uiFontFamily: 'var(--nw-font-sans)',
26
+ uiSelectionBackground: 'var(--nw-selection)',
27
+ uiSelectionForeground: 'var(--nw-fg)',
28
+ focusBorder: 'var(--nw-ring)',
29
+ scrollbarThumbColor: 'var(--nw-line-strong)',
30
+ scrollbarThumbHoverColor: 'var(--nw-line-bright)',
31
+ frames: {
32
+ frameBoxShadowCssValue: 'none',
33
+ editorBackground: 'var(--nw-canvas-sunken)',
34
+ terminalBackground: 'var(--nw-canvas-sunken)',
35
+ editorTabBarBackground: 'var(--nw-panel)',
36
+ editorTabBarBorderColor: 'var(--nw-line)',
37
+ editorTabBarBorderBottomColor: 'var(--nw-line)',
38
+ editorActiveTabBackground: 'var(--nw-canvas-sunken)',
39
+ editorActiveTabForeground: 'var(--nw-fg)',
40
+ editorActiveTabBorderColor: 'transparent',
41
+ editorActiveTabIndicatorTopColor: 'var(--nw-primary)',
42
+ editorActiveTabIndicatorBottomColor: 'transparent',
43
+ terminalTitlebarBackground: 'var(--nw-panel)',
44
+ terminalTitlebarForeground: 'var(--nw-fg-muted)',
45
+ terminalTitlebarBorderBottomColor: 'var(--nw-line)',
46
+ terminalTitlebarDotsForeground: 'var(--nw-fg-faint)',
47
+ terminalTitlebarDotsOpacity: '0.55',
48
+ inlineButtonBackground: 'var(--nw-primary)',
49
+ inlineButtonForeground: 'var(--nw-fg-muted)',
50
+ inlineButtonBorder: 'var(--nw-line-bright)',
51
+ tooltipSuccessBackground: 'var(--nw-success)',
52
+ tooltipSuccessForeground: 'var(--nw-on-success)',
53
+ },
54
+ textMarkers: {
55
+ markBackground: 'color-mix(in oklab, var(--nw-primary) 14%, transparent)',
56
+ markBorderColor: 'var(--nw-primary)',
57
+ insBackground: 'color-mix(in oklab, var(--nw-success) 14%, transparent)',
58
+ insBorderColor: 'var(--nw-success)',
59
+ insDiffIndicatorColor: 'var(--nw-success)',
60
+ delBackground: 'color-mix(in oklab, var(--nw-danger) 14%, transparent)',
61
+ delBorderColor: 'var(--nw-danger)',
62
+ delDiffIndicatorColor: 'var(--nw-danger)',
63
+ lineMarkerLabelColor: 'var(--nw-on-accent)',
64
+ },
65
+ } as const satisfies StyleOverrides
66
+
67
+ /** Shallow merge per style group; every key the user sets wins over ours. */
68
+ export function mergeStyleOverrides(
69
+ base: StyleOverrides,
70
+ user: StyleOverrides | undefined,
71
+ ): StyleOverrides {
72
+ if (user === undefined) return base
73
+ const { frames, textMarkers, ...rest } = user
74
+ return {
75
+ ...base,
76
+ ...rest,
77
+ frames: { ...base.frames, ...frames },
78
+ textMarkers: { ...base.textMarkers, ...textMarkers },
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Expressive Code lifts token colors to 5.5:1 by default. The generated themes already guarantee
84
+ * that on their dimmest code surface, so the render-time floor only needs WCAG AA (4.5:1) and
85
+ * applies to user themes.
86
+ */
87
+ export const MIN_SYNTAX_CONTRAST = 4.5
88
+
89
+ /**
90
+ * Returns the Expressive Code options to hand back to Starlight, or `undefined` when code blocks
91
+ * must stay untouched (plugin option off, or Expressive Code disabled by the user).
92
+ * Every user-provided option, `themes` and style override wins. Everything is JSON data except
93
+ * `themeCssSelector` when the generated themes are used.
94
+ */
95
+ export function createExpressiveCodeOptions(
96
+ user: StarlightUserConfig['expressiveCode'],
97
+ config: NyxwireStarlightConfig,
98
+ ): StarlightExpressiveCodeOptions | undefined {
99
+ if (!config.expressiveCode || user === false) return undefined
100
+ const options: StarlightExpressiveCodeOptions = typeof user === 'object' ? user : {}
101
+ return {
102
+ minSyntaxHighlightingColorContrast: MIN_SYNTAX_CONTRAST,
103
+ ...(options.themes === undefined
104
+ ? {
105
+ themes: createCodeThemes(config.flavors.dark),
106
+ useStarlightUiThemeColors: false,
107
+ useStarlightDarkModeSwitch: false,
108
+ useDarkModeMediaQuery: false,
109
+ themeCssSelector: flavorThemeSelector,
110
+ }
111
+ : {}),
112
+ ...options,
113
+ styleOverrides: mergeStyleOverrides(expressiveCodeStyleOverrides, options.styleOverrides),
114
+ }
115
+ }
package/src/index.ts ADDED
@@ -0,0 +1,32 @@
1
+ import type { StarlightPlugin } from '@astrojs/starlight/types'
2
+ import { PACKAGE_NAME, createConfigUpdate } from './config.js'
3
+ import { normalizeStarlightOptions } from './options.js'
4
+ import type { NyxwireStarlightOptions } from './options.js'
5
+ import { createRuntimeIntegration } from './runtime.js'
6
+
7
+ export * from './config.js'
8
+ export * from './ec-themes.js'
9
+ export * from './expressive-code.js'
10
+ export * from './options.js'
11
+ export * from './runtime.js'
12
+ export type { StarlightThemeBridgeConfig } from './theme-bridge.js'
13
+ export type { AccentName, DarkFlavorName, FlavorName } from '@nyxwire/palette'
14
+
15
+ /**
16
+ * Starlight plugin: Nyxwire theme (CSS), flavor runtime + selector (component overrides)
17
+ * and palette-generated Expressive Code themes. Options are validated eagerly.
18
+ */
19
+ export function nyxwireStarlight(options: NyxwireStarlightOptions = {}): StarlightPlugin {
20
+ const config = normalizeStarlightOptions(options)
21
+ return {
22
+ name: PACKAGE_NAME,
23
+ hooks: {
24
+ 'config:setup'({ config: userConfig, updateConfig, addIntegration, logger }) {
25
+ updateConfig(createConfigUpdate(userConfig, config, (message) => logger.warn(message)))
26
+ addIntegration(createRuntimeIntegration(config))
27
+ },
28
+ },
29
+ }
30
+ }
31
+
32
+ export default nyxwireStarlight
package/src/options.ts ADDED
@@ -0,0 +1,80 @@
1
+ import { NyxwireOptionsError, normalizeOptions } from '@nyxwire/astro'
2
+ import type { FlavorConfig, FlavorPreference } from '@nyxwire/astro'
3
+ import type { AccentName, DarkFlavorName } from '@nyxwire/palette'
4
+
5
+ export type NyxwireStarlightOptions = {
6
+ readonly flavors?: { readonly dark?: DarkFlavorName; readonly light?: 'lumen' }
7
+ readonly accent?: AccentName
8
+ readonly defaultFlavor?: FlavorPreference
9
+ readonly flavorSelect?: boolean
10
+ readonly expressiveCode?: boolean
11
+ }
12
+
13
+ export type NyxwireStarlightConfig = {
14
+ readonly flavors: { readonly dark: DarkFlavorName; readonly light: 'lumen' }
15
+ readonly defaultFlavor: FlavorPreference
16
+ readonly accent: FlavorConfig['accent']
17
+ readonly flavorSelect: boolean
18
+ readonly expressiveCode: boolean
19
+ }
20
+
21
+ const OPTION_NAMES = [
22
+ 'flavors',
23
+ 'defaultFlavor',
24
+ 'accent',
25
+ 'flavorSelect',
26
+ 'expressiveCode',
27
+ ] as const
28
+
29
+ export class NyxwireStarlightOptionsError extends TypeError {
30
+ readonly option: string
31
+ readonly expected: string
32
+
33
+ constructor(option: string, expected: string) {
34
+ super(`@nyxwire/starlight: ${option} must be ${expected}.`)
35
+ this.name = 'NyxwireStarlightOptionsError'
36
+ this.option = option
37
+ this.expected = expected
38
+ }
39
+ }
40
+
41
+ function record(value: unknown, name: string): Readonly<Record<string, unknown>> {
42
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
43
+ throw new NyxwireStarlightOptionsError(name, 'an object')
44
+ }
45
+ return Object.fromEntries(Object.entries(value))
46
+ }
47
+
48
+ function enabled(value: unknown, name: string): boolean {
49
+ if (value === undefined) return true
50
+ if (typeof value !== 'boolean') throw new NyxwireStarlightOptionsError(name, 'true or false')
51
+ return value
52
+ }
53
+
54
+ /** Validates plugin options; flavor and accent rules are shared with `@nyxwire/astro`. */
55
+ export function normalizeStarlightOptions(options: unknown = {}): NyxwireStarlightConfig {
56
+ const input = record(options, 'options')
57
+ for (const key of Object.keys(input)) {
58
+ if (!OPTION_NAMES.some((name) => name === key)) {
59
+ throw new NyxwireStarlightOptionsError(
60
+ key,
61
+ `a recognized option (${OPTION_NAMES.join(', ')})`,
62
+ )
63
+ }
64
+ }
65
+ const flavorSelect = enabled(input['flavorSelect'], 'flavorSelect')
66
+ const expressiveCode = enabled(input['expressiveCode'], 'expressiveCode')
67
+ try {
68
+ const { flavors, accent, defaultFlavor } = normalizeOptions({
69
+ defaultFlavor: input['defaultFlavor'],
70
+ ...(input['flavors'] === undefined ? {} : { flavors: input['flavors'] }),
71
+ ...(input['accent'] === undefined ? {} : { accent: input['accent'] }),
72
+ })
73
+ return { flavors, defaultFlavor, accent, flavorSelect, expressiveCode }
74
+ } catch (error) {
75
+ if (error instanceof NyxwireOptionsError) {
76
+ throw new NyxwireStarlightOptionsError(error.option, error.expected)
77
+ }
78
+ throw error
79
+ }
80
+ }
package/src/prose.css ADDED
@@ -0,0 +1,108 @@
1
+ .sl-markdown-content :is(h1, h2, h3, h4):not(:where(.not-content *)),
2
+ .sl-container > h1#_top {
3
+ font-weight: 650;
4
+ letter-spacing: var(--nw-tracking-tight);
5
+ text-wrap: balance;
6
+ }
7
+
8
+ .sl-markdown-content h5:not(:where(.not-content *)) {
9
+ font-family: var(--nw-font-sans);
10
+ font-weight: 650;
11
+ }
12
+
13
+ .sl-markdown-content h6:not(:where(.not-content *)) {
14
+ color: var(--nw-fg-muted);
15
+ font-family: var(--nw-font-mono);
16
+ font-size: var(--nw-text-xs);
17
+ font-weight: 500;
18
+ letter-spacing: var(--nw-tracking-wide);
19
+ text-transform: uppercase;
20
+ }
21
+
22
+ .sl-markdown-content :is(strong, b):not(:where(.not-content *)) {
23
+ color: var(--nw-fg);
24
+ font-weight: 650;
25
+ }
26
+
27
+ .sl-markdown-content a:not(:where(.not-content *, .starlight-aside__content *, .sl-anchor-link)) {
28
+ text-decoration-color: color-mix(in oklab, currentColor 50%, transparent);
29
+ text-decoration-thickness: var(--nw-space-px);
30
+ }
31
+
32
+ .sl-markdown-content a:hover:not(:where(.not-content *, .sl-anchor-link)) {
33
+ text-decoration-color: currentColor;
34
+ }
35
+
36
+ .sl-markdown-content :not(pre) > code:not(:where(.not-content *)) {
37
+ border: var(--nw-space-px) solid var(--nw-line);
38
+ border-radius: var(--nw-radius-xs);
39
+ color: var(--sl-color-accent-high);
40
+ }
41
+
42
+ .sl-markdown-content a code:not(:where(.not-content *)) {
43
+ color: inherit;
44
+ }
45
+
46
+ .sl-markdown-content kbd:not(:where(.not-content *)) {
47
+ display: inline-block;
48
+ min-inline-size: 1.6em;
49
+ padding: 0.1em 0.4em;
50
+ border: var(--nw-space-px) solid var(--nw-line-strong);
51
+ border-block-end-width: var(--nw-ring-width);
52
+ border-radius: var(--nw-radius-sm);
53
+ background: var(--nw-panel);
54
+ color: var(--nw-fg);
55
+ font: 500 var(--nw-text-xs) / var(--nw-leading-snug) var(--nw-font-mono);
56
+ text-align: center;
57
+ }
58
+
59
+ .sl-markdown-content mark:not(:where(.not-content *)) {
60
+ padding-inline: var(--nw-space-0-5);
61
+ border-radius: var(--nw-radius-xs);
62
+ background: var(--nw-warning-soft);
63
+ color: var(--nw-fg);
64
+ }
65
+
66
+ .sl-markdown-content li:not(:where(.not-content *))::marker {
67
+ color: var(--nw-primary);
68
+ }
69
+
70
+ .sl-markdown-content ol > li:not(:where(.not-content *))::marker {
71
+ font-family: var(--nw-font-mono);
72
+ }
73
+
74
+ .sl-markdown-content blockquote:not(:where(.not-content *)) {
75
+ border-inline-start-color: var(--nw-line-strong);
76
+ color: var(--nw-fg-muted);
77
+ font-style: italic;
78
+ }
79
+
80
+ .sl-markdown-content table:not(:where(.not-content *)) {
81
+ font-size: var(--nw-text-sm);
82
+ scrollbar-width: thin;
83
+ }
84
+
85
+ .sl-markdown-content thead th:not(:where(.not-content *)) {
86
+ border-block-end-color: var(--nw-line-strong);
87
+ font-family: var(--nw-font-mono);
88
+ font-size: var(--nw-text-xs);
89
+ letter-spacing: var(--nw-tracking-wide);
90
+ }
91
+
92
+ .sl-markdown-content details:not(:where(.not-content *)) {
93
+ --sl-details-border-color: var(--nw-line-strong);
94
+ --sl-details-border-color--hover: var(--nw-line-bright);
95
+ padding: var(--nw-space-2) var(--nw-space-3);
96
+ border: var(--nw-space-px) solid var(--sl-details-border-color);
97
+ border-radius: var(--nw-radius-md);
98
+ background: var(--nw-panel);
99
+ }
100
+
101
+ .sl-markdown-content details:not(:where(.not-content *)):hover {
102
+ border-color: var(--sl-details-border-color--hover);
103
+ }
104
+
105
+ .sl-markdown-content .starlight-aside details:not(:where(.not-content *)) {
106
+ --sl-details-border-color: var(--sl-color-asides-border);
107
+ background: transparent;
108
+ }
package/src/runtime.ts ADDED
@@ -0,0 +1,67 @@
1
+ import { FLAVOR_CHANGE_EVENT, FLAVOR_STORAGE_KEY, buildFlavorScript } from '@nyxwire/astro'
2
+ import { palette } from '@nyxwire/palette'
3
+ import type { AstroIntegration } from 'astro'
4
+ import type { NyxwireStarlightConfig } from './options.js'
5
+ import { starlightThemeBridge } from './theme-bridge.js'
6
+ import type { StarlightThemeBridgeConfig } from './theme-bridge.js'
7
+
8
+ /** Virtual module read by the ThemeProvider override. */
9
+ export const RUNTIME_MODULE_ID = 'virtual:nyxwire/starlight-runtime'
10
+ /** Starlight's own localStorage key for its dark/light/auto select. */
11
+ export const STARLIGHT_THEME_STORAGE_KEY = 'starlight-theme'
12
+
13
+ export type StarlightThemeRuntime = {
14
+ /** Inline no-FOUC script: flavor runtime followed by the Starlight bridge. */
15
+ readonly script: string
16
+ /** Initial `<meta name="theme-color">` (configured dark flavor base); the runtime updates it. */
17
+ readonly themeColor: string
18
+ }
19
+
20
+ export function buildThemeProviderScript(config: NyxwireStarlightConfig): string {
21
+ const bridge = {
22
+ dark: config.flavors.dark,
23
+ light: config.flavors.light,
24
+ storageKey: FLAVOR_STORAGE_KEY,
25
+ legacyKey: STARLIGHT_THEME_STORAGE_KEY,
26
+ changeEvent: FLAVOR_CHANGE_EVENT,
27
+ } satisfies StarlightThemeBridgeConfig
28
+ const runtime = buildFlavorScript({
29
+ flavors: config.flavors,
30
+ defaultFlavor: config.defaultFlavor,
31
+ accent: config.accent ?? undefined,
32
+ })
33
+ const serialized = JSON.stringify(bridge).replace(/</g, '\\u003c')
34
+ return `${runtime}\n(${starlightThemeBridge.toString()})(${serialized});`
35
+ }
36
+
37
+ export function createThemeRuntime(config: NyxwireStarlightConfig): StarlightThemeRuntime {
38
+ return {
39
+ script: buildThemeProviderScript(config),
40
+ themeColor:
41
+ palette[config.defaultFlavor === 'auto' ? config.flavors.dark : config.defaultFlavor].base,
42
+ }
43
+ }
44
+
45
+ /** Astro integration exposing the runtime to the ThemeProvider through a Vite virtual module. */
46
+ export function createRuntimeIntegration(config: NyxwireStarlightConfig): AstroIntegration {
47
+ const resolvedId = `\0${RUNTIME_MODULE_ID}`
48
+ const code = `export default ${JSON.stringify(createThemeRuntime(config))};`
49
+ return {
50
+ name: '@nyxwire/starlight/runtime',
51
+ hooks: {
52
+ 'astro:config:setup': ({ updateConfig }) => {
53
+ updateConfig({
54
+ vite: {
55
+ plugins: [
56
+ {
57
+ name: 'nyxwire-starlight-runtime',
58
+ resolveId: (id: string) => (id === RUNTIME_MODULE_ID ? resolvedId : undefined),
59
+ load: (id: string) => (id === resolvedId ? code : undefined),
60
+ },
61
+ ],
62
+ },
63
+ })
64
+ },
65
+ },
66
+ }
67
+ }