Skip to content

Commit 4d6d632

Browse files
committed
Rename tsSsgContext to pageContext.
1 parent 271911a commit 4d6d632

41 files changed

Lines changed: 266 additions & 208 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎frontend/components/studioBrand.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
type LogoConfig,
33
type LogoSize,
4-
resolveTsSsgContext,
4+
resolvePageContext,
55
} from '@purestack/ts-common'
66
import { defineComponent, html, type RefOrValue } from 'regor'
77

@@ -17,7 +17,7 @@ export function defineStudioBrandComponent() {
1717
props: ['size'],
1818
context: (head) => ({
1919
...head.props,
20-
config: resolveTsSsgContext(head).site.logo,
20+
config: resolvePageContext(head).site.logo,
2121
}),
2222
})
2323
}

‎frontend/purestack.studio/guides/extending/regor.mdx‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ highlights:
1717

1818
# Regor
1919

20-
<a href="https://github.com/PureStackStudio/Regor" target="_blank" rel="noopener">Regor</a> is the component framework PureStack is built on. It runs in two places:
20+
<a
21+
href="https://github.com/PureStackStudio/Regor"
22+
target="_blank"
23+
rel="noopener"
24+
>Regor</a
25+
>
26+
is the component framework PureStack is built on. It runs in two places:
2127

2228
<Grid columns="1" columnsSm="2" class="gap-3 mb-4">
2329
<Panel tone="neutral" bodyClass="p-3">
@@ -153,7 +159,7 @@ Every page goes through the same four steps:
153159
| Where it runs | Your build, once per page | The visitor's browser |
154160
| What it produces | Static HTML | A live, interactive interface |
155161
| Event handlers, `r-model`, timers | Not kept | Run normally |
156-
| Page data (`tsSsgContext`) | Available | Not available |
162+
| Page data (`pageContext`) | Available | Not available |
157163
| Components | All built-in and registered components | The component families you register |
158164

159165
## Regor at build time
@@ -185,24 +191,24 @@ A plain attribute passes a string. An attribute that starts with a colon is a Re
185191

186192
### Expressions and page data
187193

188-
Text in double braces is a Regor expression, anywhere in a page. Inside markup, expressions can read `tsSsgContext`, which holds the page and site data that PureStack provides during the build:
194+
Text in double braces is a Regor expression, anywhere in a page. Inside markup, expressions can read `pageContext`, which holds the page and site data that PureStack provides during the build:
189195

190196
| Field | Contents |
191197
| --- | --- |
192-
| `tsSsgContext.pageInfo.frontmatter` | The page's frontmatter, including any custom fields you add |
193-
| `tsSsgContext.pageInfo.urlPath` | The page's URL, such as `/guides/extending/regor/` |
194-
| `tsSsgContext.pageInfo.relPath` | The source file path inside the content folder |
195-
| `tsSsgContext.site` | The resolved site configuration |
196-
| `tsSsgContext.navigation`, `tsSsgContext.outline` | The page's navigation and heading outline |
197-
| `tsSsgContext.locale`, `tsSsgContext.locales` | The page's language and the site's languages |
198+
| `pageContext.pageInfo.frontmatter` | The page's frontmatter, including any custom fields you add |
199+
| `pageContext.pageInfo.urlPath` | The page's URL, such as `/guides/extending/regor/` |
200+
| `pageContext.pageInfo.relPath` | The source file path inside the content folder |
201+
| `pageContext.site` | The resolved site configuration |
202+
| `pageContext.navigation`, `pageContext.outline` | The page's navigation and heading outline |
203+
| `pageContext.locale`, `pageContext.locales` | The page's language and the site's languages |
198204

199205
This page lists three highlights in its frontmatter. The panel below reads them during the build:
200206

201207
<Panel tone="accent" variant="surface" bodyClass="p-3" class="mb-4">
202208
<p class="text-eyebrow mt-0">From this page's frontmatter</p>
203-
<strong>{{ tsSsgContext.pageInfo.frontmatter.title }}</strong>
209+
<strong>{{ pageContext.pageInfo.frontmatter.title }}</strong>
204210
<ul class="mb-0">
205-
<li r-for="item in tsSsgContext.pageInfo.frontmatter.highlights">
211+
<li r-for="item in pageContext.pageInfo.frontmatter.highlights">
206212
{{ item }}
207213
</li>
208214
</ul>
@@ -218,9 +224,9 @@ highlights:
218224
---
219225

220226
<Panel tone="accent" variant="surface" bodyClass="p-3">
221-
<strong>{{ tsSsgContext.pageInfo.frontmatter.title }}</strong>
227+
<strong>{{ pageContext.pageInfo.frontmatter.title }}</strong>
222228
<ul>
223-
<li r-for="item in tsSsgContext.pageInfo.frontmatter.highlights">{{ item }}</li>
229+
<li r-for="item in pageContext.pageInfo.frontmatter.highlights">{{ item }}</li>
224230
</ul>
225231
</Panel>
226232
```
@@ -294,9 +300,10 @@ export default defineConfig({
294300
>
295301
Export the interface that types a component, as <code>ReleaseNote</code> is
296302
exported here. The
297-
<a href="/guides/getting-started/vscode-extension/">VS Code extension</a> finds components
298-
through their exported interfaces. Without the export, it can't complete the
299-
component's props or jump from a tag in your markup to the component's source.
303+
<a href="/guides/getting-started/vscode-extension/">VS Code extension</a>
304+
finds components through their exported interfaces. Without the export, it
305+
can't complete the component's props or jump from a tag in your markup to the
306+
component's source.
300307
</AlertBox>
301308

302309
Then use it in any page. Content between the tags fills the `<slot>`:
@@ -307,7 +314,7 @@ Then use it in any page. Content between the tags fills the `<slot>`:
307314
</ReleaseNote>
308315
```
309316

310-
Tag names match registered keys regardless of case, so `<ReleaseNote>` finds `releaseNote`. The kebab-case form `<release-note>` works too. Components can also read page data. Pass the component head to `resolveTsSsgContext` from `@purestack/ts-common`, as the built-in navigation and page-link components do.
317+
Tag names match registered keys regardless of case, so `<ReleaseNote>` finds `releaseNote`. The kebab-case form `<release-note>` works too. Components can also read page data. Pass the component head to `resolvePageContext` from `@purestack/ts-common`, as the built-in navigation and page-link components do.
311318

312319
## Regor in the browser
313320

@@ -332,8 +339,8 @@ Browser apps do not get the build's component list. Each app registers the compo
332339
<AlertBox tone="info" icon="tabler:info-circle" title="Register icons too">
333340
Register each icon your app shows, including the defaults that components draw
334341
themselves, such as the arrow in a select field. The
335-
<a href="/guides/styling/icons/#icons-in-browser-apps">Icons guide</a> lists those
336-
defaults.
342+
<a href="/guides/styling/icons/#icons-in-browser-apps">Icons guide</a> lists
343+
those defaults.
337344
</AlertBox>
338345

339346
### Template syntax

‎packages/ts-common/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ scripts. This package defines site configuration, page frontmatter, navigation,
55
logos, templates, and the rendering context so those packages agree on the same
66
shapes.
77

8-
Import types such as `SiteConfig`, `PageFrontmatter`, and `TsSsgContext` when
9-
building a custom integration. The runtime exports `resolveTsSsgContext` and
10-
`tryResolveTsSsgContext` find the site context in a Regor component.
8+
Import types such as `SiteConfig`, `PageFrontmatter`, and `PageContext` when
9+
building a custom integration. The runtime exports `resolvePageContext` and
10+
`tryResolvePageContext` find the site context in a Regor component.

‎packages/ts-common/src/index.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ export type {
3939
PageTranslationInfo,
4040
} from './page-template-types'
4141
export {
42-
resolveTsSsgContext,
43-
tryResolveTsSsgContext,
44-
} from './resolveTsSsgContext'
42+
resolvePageContext,
43+
tryResolvePageContext,
44+
} from './resolvePageContext'
4545
export type {
4646
AnalyticsConfig,
4747
AuthConfig,
@@ -64,6 +64,6 @@ export type {
6464
} from './site-config-types'
6565
export type {
6666
PageOutlineItem,
67-
TsSsgContext,
67+
PageContext,
6868
TsSsgNavigation,
6969
} from './ts-ssg-context'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type { PageContext } from './ts-ssg-context'
2+
3+
type ContextCarrier = {
4+
pageContext?: PageContext
5+
}
6+
7+
type HeadLike = {
8+
ctx?: unknown[]
9+
}
10+
11+
function hasPageContext(value: unknown): value is ContextCarrier {
12+
return typeof value === 'object' && value !== null && 'pageContext' in value
13+
}
14+
15+
function hasContextStack(value: unknown): value is HeadLike {
16+
return typeof value === 'object' && value !== null && 'ctx' in value
17+
}
18+
19+
export function resolvePageContext(head: unknown): PageContext {
20+
const stack = hasContextStack(head) && Array.isArray(head.ctx) ? head.ctx : []
21+
for (const ctx of stack) {
22+
if (!hasPageContext(ctx)) continue
23+
if (ctx.pageContext) return ctx.pageContext
24+
}
25+
throw new Error('pageContext is not available in the Regor context stack.')
26+
}
27+
28+
export function tryResolvePageContext(
29+
head: unknown,
30+
): PageContext | undefined {
31+
const stack = hasContextStack(head) && Array.isArray(head.ctx) ? head.ctx : []
32+
for (const ctx of stack) {
33+
if (!hasPageContext(ctx)) continue
34+
if (ctx.pageContext) return ctx.pageContext
35+
}
36+
return undefined
37+
}

‎packages/ts-common/src/resolveTsSsgContext.ts‎

Lines changed: 0 additions & 37 deletions
This file was deleted.

‎packages/ts-common/src/ts-ssg-context.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface TsSsgNavigation {
2121
pageLinks?: PageNavigationLinks
2222
}
2323

24-
export interface TsSsgContext {
24+
export interface PageContext {
2525
site: SiteConfig
2626
pageInfo: PageInfo
2727
navigation?: TsSsgNavigation

‎packages/ts-components/src/standard/barChart/barChart.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('BarChart rendering', () => {
1717
const app = createApp(
1818
{
1919
components: defineBarChartComponents(),
20-
tsSsgContext: createTestContext(),
20+
pageContext: createTestContext(),
2121
items: values.map((value, index) => ({
2222
label: `Day ${index + 1}`,
2323
value,
@@ -57,7 +57,7 @@ describe('BarChart rendering', () => {
5757
const app = createApp(
5858
{
5959
components: defineBarChartComponents(),
60-
tsSsgContext: createTestContext(),
60+
pageContext: createTestContext(),
6161
items: [
6262
{ label: 'Core', value: 42, color: '#18a999' },
6363
{ label: 'Edge', value: 28, color: '#f4a261' },
@@ -102,7 +102,7 @@ describe('BarChart rendering', () => {
102102
const app = createApp(
103103
{
104104
components: defineBarChartComponents(),
105-
tsSsgContext: createTestContext(),
105+
pageContext: createTestContext(),
106106
items: [{ label: 'Invalid', value: Number.NaN }],
107107
},
108108
{
@@ -131,7 +131,7 @@ describe('BarChart rendering', () => {
131131
const app = createApp(
132132
{
133133
components: defineBarChartComponents(),
134-
tsSsgContext: createTestContext(),
134+
pageContext: createTestContext(),
135135
items: [{ label: 'Core', value: 42 }],
136136
},
137137
{

‎packages/ts-components/src/standard/btn/btn.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type TsSsgContext, tryResolveTsSsgContext } from '@purestack/ts-common'
1+
import { type PageContext, tryResolvePageContext } from '@purestack/ts-common'
22
import type { SemanticTone } from '@purestack/ts-style'
33
import {
44
type ComputedRef,
@@ -104,7 +104,7 @@ function defineButtonLinkComponent() {
104104
'iconOnly',
105105
'ariaLabel',
106106
],
107-
context: (head) => resolveBtnLink(head.props, tryResolveTsSsgContext(head)),
107+
context: (head) => resolveBtnLink(head.props, tryResolvePageContext(head)),
108108
})
109109
}
110110

@@ -132,7 +132,7 @@ function resolveBtn(props: Btn): Btn {
132132

133133
function resolveBtnLink(
134134
props: BtnLink,
135-
context: TsSsgContext | undefined,
135+
context: PageContext | undefined,
136136
): BtnLink {
137137
return {
138138
...props,
@@ -176,7 +176,7 @@ function resolveShowEndIcon(props: BtnBase) {
176176
return hasIcon && unref(props.iconPosition) === 'end'
177177
}
178178

179-
function resolveButtonHref(props: BtnLink, context: TsSsgContext | undefined) {
179+
function resolveButtonHref(props: BtnLink, context: PageContext | undefined) {
180180
const value = unref(props.href)
181181
const href = typeof value === 'string' ? value.trim() : undefined
182182
return href ? (context?.resolvePublicHref(href) ?? href) : undefined

‎packages/ts-components/src/standard/classicLogo/classicLogo.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
type ClassicLogoConfig,
3-
type TsSsgContext,
4-
tryResolveTsSsgContext,
3+
type PageContext,
4+
tryResolvePageContext,
55
} from '@purestack/ts-common'
66
import {
77
getCurrentThemePaletteVar,
@@ -19,8 +19,16 @@ import {
1919
} from 'regor'
2020

2121
const logoTemplate = html`<div class="classic-logo" :style="logo.layoutStyle">
22-
<a class="classic-logo__link" :href="logo.href === null ? undefined : (logo.href ?? '/')" :aria-label="logo.ariaLabel">
23-
<span class="classic-logo__glyph" :style="logo.glyphStyle" aria-hidden="true">
22+
<a
23+
class="classic-logo__link"
24+
:href="logo.href === null ? undefined : (logo.href ?? '/')"
25+
:aria-label="logo.ariaLabel"
26+
>
27+
<span
28+
class="classic-logo__glyph"
29+
:style="logo.glyphStyle"
30+
aria-hidden="true"
31+
>
2432
<Icon :name="logo.icon" r-if="logo.icon"/>
2533
<span class="classic-logo__glyph-mark" r-else></span>
2634
</span>
@@ -105,7 +113,7 @@ interface ResolvedClassicLogo {
105113

106114
function resolveClassicLogo(
107115
props: Partial<ClassicLogoConfig>,
108-
context: TsSsgContext | undefined,
116+
context: PageContext | undefined,
109117
): ResolvedClassicLogo {
110118
const brand = props.brand
111119
const letterColors = props.letterColors
@@ -416,7 +424,7 @@ function defineClassicLogoComponent() {
416424
Object.entries(direct).filter(([, value]) => value !== undefined),
417425
),
418426
}
419-
return resolveClassicLogo(options, tryResolveTsSsgContext(head))
427+
return resolveClassicLogo(options, tryResolvePageContext(head))
420428
}),
421429
}),
422430
})

0 commit comments

Comments
 (0)