Skip to content

Commit 19b6869

Browse files
authored
Merge pull request #36 from PureStackStudio/extract-more-plugins-from-core
Move replaceable integrations out of `ts-ssg` into default plugins.
2 parents c27f3fc + 9da64d0 commit 19b6869

112 files changed

Lines changed: 4164 additions & 1313 deletions

File tree

Some content is hidden

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

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

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,26 @@ The badge appears on the page. Change its `tone` in `plugins/site.ts` while the
9191

9292
## Standard plugins
9393

94-
PureStack includes three plugins automatically in `build`, `serve`, and `publish`:
94+
PureStack includes nine plugins automatically in `build`, `serve`, and `publish`:
9595

9696
| Package | Factory | Provides |
9797
| --- | --- | --- |
9898
| `@purestack/ts-components` | `componentsPlugin()` | Standard components, icons, and component styles |
99-
| `@purestack/ts-theme` | `themePlugin()` | The `doc` and `splash` templates, generated theme CSS, and theme switching |
100-
| `@purestack/ts-page-scripts` | `pageScriptsPlugin()` | Auth hint, consent and analytics, menus, navigation, code copying, search, and TOC runtimes |
99+
| `@purestack/ts-theme` | `themePlugin()` | The `doc` and `splash` templates, generated theme CSS, favicons, theme switching, and Markdown table presentation |
100+
| `@purestack/ts-page-scripts` | `pageScriptsPlugin()` | Menus, navigation, code copying, search, TOC, Tabs, and Modal runtimes |
101+
| `@purestack/ts-page-scripts` | `authStateHintPlugin()` | Early signed-in state hint |
102+
| `@purestack/ts-page-scripts` | `consentPlugin()` | Consent browser runtime |
103+
| `@purestack/ts-page-scripts` | `analyticsPlugin()` | Direct or consent-managed GA4 integration |
104+
| `@purestack/ts-seo` | `seoPlugin()` | Social previews, canonical URLs, and translated alternate links |
105+
| `@purestack/ts-highlight` | `highlightPlugin()` | Shiki or highlight.js code highlighting |
106+
| `@purestack/ts-pagefind` | `pagefindPlugin()` | Full-build and lazy development search indexing |
101107

102108
The stylesheet provider initializes before component factories, then all config hooks run in plugin order. Names do not affect initialization. Components belong to each build, so hooks can override them through `context.components`. The theme and page runtime plugins supply head templates that run before the selected page template, preserving its ability to inspect or change the completed head. The lifecycle hook set is unchanged.
103109

110+
Metadata defaults compose through `headConfig({ site, pageInfo })` in plugin order, before explicit frontmatter `head` overrides and before head markup is rendered. It returns `BasicHeadConfig` or a promise. The SEO plugin uses this contribution for Open Graph, Twitter, canonical URLs, and translated alternate links; page templates receive the completed `headConfig`. Basic titles, descriptions, explicit head settings, and `index: false` enforcement remain in core.
111+
112+
Auth hints, consent, and analytics are independent plugins in `ts-page-scripts`. The analytics plugin contributes automatic GA4 consent services during configuration resolution, before any factory or lifecycle hook runs. Omitting it removes automatic registration and direct tracking scripts. Explicitly configured consent services remain available. Analytics never bypasses enabled consent when the consent runtime plugin is omitted.
113+
104114
To choose the standard integrations yourself, disable the automatic set and list the plugins you want. The factories are exported from `purestack`, `@purestack/ts-ssg`, and their individual packages:
105115

106116
```ts
@@ -112,10 +122,59 @@ export default defineConfig({
112122
})
113123
```
114124

115-
This keeps the layouts and components while omitting the page runtimes. Theme switching belongs to the theme plugin; component-specific Tabs and Modal embeds still follow the page's `embed` settings. Without the theme plugin, another plugin must supply your page templates and any CSS output you need.
125+
This keeps the layouts and components while omitting automatic social metadata, page runtimes, code highlighting, and search indexing. Theme switching, favicon generation, and Markdown table wrappers belong to the theme plugin. Tabs and Modal scripts belong to the page scripts plugin, including automatic component embeds and explicit frontmatter settings. Without the theme plugin, another plugin must supply your page templates and any CSS output you need.
116126

117127
An explicitly listed standard plugin replaces the automatic instance by name and keeps its initialization position. You can also use `createDefaultPlugins()` to obtain the complete set. In programmatic builds, use `options.defaultPlugins` alongside `options.plugins`.
118128

129+
The highlighting plugin selects the configured backend. Configure native backend factories through `highlightPlugin({ createShiki, createHighlightJs })`, or supply `markdown.createHighlighter(config)` for a different engine. The generic provider returns `{ codeToHtml(code, lang?) }`. Ordinary providers override the default without constructing it; per-language `markdown.resolveHighlighter` callbacks retain precedence.
130+
131+
The Pagefind plugin supplies a `search` provider. To use another engine, replace `purestack:pagefind` or disable the default set and install your own provider. The builder supplies excluded output paths and the rendered-page list for lazy requests, and manages invalidation on edits and deletions. Sitemap and robots generation remain built in.
132+
133+
## Configure a plugin
134+
135+
Plugins contribute configuration through `config.resolve({ config, input, file })` and `config.finalize({ config, input, file })`. Both are optional and synchronous. Each returns an object to merge into the config; do not mutate the inputs. All resolvers run first in plugin order, applying configuration overrides. Then all finalizers run in plugin order, deriving dependent settings from the completed overrides. Finalizers should derive settings rather than change resolver inputs. Both phases finish before highlighters, Markdown processors, styles, search providers, components, and templates initialize. The analytics plugin finalizes its automatic GA4 service here, so later site resolvers can enable analytics, change its ID or category, or provide a manual service. The existing `onConfigResolved` hook still runs after factories, so it can customize initialized components and templates. Publishing applies its output-directory and minification overrides after configuration composition.
136+
137+
Custom site settings belong under `extensions` in `siteConfig.json`. File values merge with programmatic input, which takes precedence. The standard JSON schema allows plugin namespaces here while continuing to reject unknown top-level fields. Each plugin owns validation, defaults, and normalization for its namespace. Other namespaces are preserved.
138+
139+
```json
140+
{ "extensions": { "brand": { "label": "Acme" } } }
141+
```
142+
143+
Use TypeScript module augmentation for typed settings:
144+
145+
```ts
146+
import { definePlugin } from 'purestack'
147+
148+
declare module '@purestack/ts-common' {
149+
interface SiteExtensions {
150+
brand?: { label: string }
151+
}
152+
}
153+
154+
export const brandPlugin = definePlugin({
155+
name: 'brand',
156+
config: {
157+
resolve({ config }) {
158+
const brand = config.extensions.brand
159+
if (brand !== undefined &&
160+
(brand === null || typeof brand !== 'object' ||
161+
typeof brand.label !== 'string')) {
162+
throw new Error('extensions.brand requires a string label.')
163+
}
164+
return { extensions: { brand: { label: brand?.label ?? 'Docs' } } }
165+
},
166+
},
167+
components(config) {
168+
// config.extensions.brand contains the normalized settings here.
169+
return {}
170+
},
171+
})
172+
```
173+
174+
Raw `resolveSiteConfig(input)` resolves core settings and extension input. CLI and programmatic builds additionally compose their selected plugins' configuration contributions. Configuration contributions are synchronous and may run whenever a project configuration is resolved; keep file writes and other side effects in lifecycle hooks.
175+
176+
Builder style state is isolated across asynchronous operations, including concurrent sites. Its owned theme options remain linked to `context.config.style.theme`, so in-place config-hook changes and `themes.setOptions` update page colors, CSS, and generated favicons consistently. Head, metadata, template, Markdown, provider, and lifecycle errors identify their owning plugin and preserve the original cause. Markdown wrappers preserve unified presets, duplicate-plugin option merging, and callback transforms.
177+
119178
## How the config loads
120179

121180
- **Where it lives.** The CLI looks for `purestack.config.ts` in the folder passed to `--content`. It must default-export `defineConfig({ plugins: [...] })`.
@@ -204,6 +263,8 @@ Besides the prepared `head` and the page's `bodyHtml`, a template receives the p
204263

205264
## Transform Markdown
206265

266+
Ordinary `rehypePlugins` run before `rehypePostPlugins`, which apply final presentation before outline collection and code highlighting. The theme plugin uses this final stage for `div.table-scroll`, so tables introduced by custom transforms receive the same scrolling behavior as source tables. Both stages run on pages and shared Markdown partials.
267+
207268
`markdown` adds <a href="https://github.com/remarkjs/remark" target="_blank" rel="noopener">remark</a> plugins, which change the Markdown tree, and <a href="https://github.com/rehypejs/rehype" target="_blank" rel="noopener">rehype</a> plugins, which change the HTML tree. Every page, header, and footer goes through these steps:
208269

209270
1. PureStack reads the Markdown. Regor markup, such as `<Badge tone="success">`, stays raw HTML.
@@ -525,11 +586,13 @@ Every field except `name` is optional.
525586
| `name` | `string`, unique among the site's plugins |
526587
| `defaults` | `boolean`; makes component and template contributions overridable defaults without changing initialization order |
527588
| `skins` | Skins by name, each `{ create(): ThemeSkinPair }` |
589+
| `config` | `{ resolve?: SiteConfigResolver, finalize?: SiteConfigResolver }`; overrides, then derived settings, before factories |
528590
| `components` | `(config) => ({ [name]: component })` |
529591
| `templates` | Templates by name, each `(input) => TSNode<'html'>` |
530592
| `head` | `(input) => TSNode<'head'>`, or a promise; composes head markup before the page template |
593+
| `headConfig` | `({ site, pageInfo }) => BasicHeadConfig`, or a promise; metadata defaults before explicit page overrides |
531594
| `styles` | `(config) => SiteStyles`; one stylesheet provider, initialized before component factories |
532-
| `markdown` | `{ remarkPlugins?: PluggableList, rehypePlugins?: PluggableList }` |
595+
| `markdown` | `PureStackMarkdown`: remark, rehype, final rehype transforms, and highlighter providers |
533596
| `pages` | `({ config, files }) => { path, source }[]`, or a promise of them; `source` is the text, or a function that returns it |
534597
| `hooks` | Any of the hooks above |
535598
| `devMiddleware` | `(request, response) => void`, or a promise |

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "purestack-repo",
4-
"version": "1.1.14",
4+
"version": "1.1.15",
55
"packageManager": "yarn@4.18.1",
66
"type": "module",
77
"sideEffects": false,

‎packages/purestack/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "purestack",
3-
"version": "1.1.14",
3+
"version": "1.1.15",
44
"packageManager": "yarn@4.9.2",
55
"module": "./dist/purestack.mjs",
66
"types": "./dist/purestack.d.mts",

‎packages/purestack/src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export const version: string = '1.1.14'
1+
export const version: string = '1.1.15'
22

33
export * from '@purestack/ts-ssg'

‎packages/ts-common/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@purestack/ts-common",
3-
"version": "1.1.14",
3+
"version": "1.1.15",
44
"packageManager": "yarn@4.9.2",
55
"module": "./dist/ts-common.mjs",
66
"types": "./dist/ts-common.d.mts",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** Converts source code to highlighted pre/code HTML. */
2+
export interface MdxCodeHighlighter {
3+
codeToHtml: (code: string, lang?: string) => string | Promise<string>
4+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
export type { MdxCodeHighlighter } from './code-highlighter'
2+
export {
3+
DEFAULT_CODE_LANGUAGES,
4+
LANGUAGE_BY_EXTENSION,
5+
normalizeCodeLanguage,
6+
} from './codeLanguages'
17
export type {
28
ConsentCategory,
39
ConsentConfig,
@@ -33,6 +39,8 @@ export type {
3339
PageNavigationLinks,
3440
} from './navMenu-types'
3541
export type {
42+
PageHeadConfigInput,
43+
PageHeadConfigTemplate,
3644
PageHeadTemplate,
3745
PageInfo,
3846
PageTemplate,
@@ -56,17 +64,24 @@ export type {
5664
PreviewConfig,
5765
RobotsConfig,
5866
SiteConfig,
67+
SiteConfigContributions,
5968
SiteConfigInput,
69+
SiteConfigResolution,
70+
SiteConfigResolver,
71+
SiteExtensions,
6072
SiteHtmlConfig,
6173
SiteMdxConfig,
6274
SitemapConfig,
6375
SiteScriptsConfig,
6476
SiteStyleConfig,
6577
SiteStyleConfigInput,
6678
} from './site-config-types'
79+
export type { SiteSearch } from './site-search'
6780
export type { SiteStyles, WriteStylesResult } from './site-styles'
6881
export type {
6982
PageContext,
7083
PageOutlineItem,
84+
RuntimeEmbedPosition,
85+
RuntimeEmbeds,
7186
TsSsgNavigation,
7287
} from './ts-ssg-context'

‎packages/ts-common/src/page-template-types.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,24 @@ export interface PageTranslationInfo {
2020
relPath: string
2121
}
2222

23-
export interface PageTemplateInput {
23+
export interface PageHeadConfigInput {
24+
site: SiteConfig
25+
pageInfo: PageInfo
26+
}
27+
28+
/** Supplies head defaults before explicit page frontmatter overrides. */
29+
export type PageHeadConfigTemplate = (
30+
input: PageHeadConfigInput,
31+
) => BasicHeadConfig | Promise<BasicHeadConfig>
32+
33+
export interface PageTemplateInput extends PageHeadConfigInput {
2434
head: TSNode<'head'>
2535
bodyHtml: string
2636
headConfig?: BasicHeadConfig
2737
styleLinks?: ThemeStylesheetLink[]
2838
templateName: string
29-
site: SiteConfig
3039
navigation?: PageNavigation
3140
outline?: PageOutlineItem[]
32-
pageInfo: PageInfo
3341
headerHtml?: string
3442
footerHtml?: string
3543
}

‎packages/ts-common/src/site-config-types.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import type { SiteLogoConfig } from './logo-types'
99
import type { NavigationConfig } from './navMenu-types'
1010

1111
export interface SiteConfig {
12+
/** Plugin-owned settings, normalized by their configuration contributions. */
13+
extensions: SiteExtensions
1214
rootDir: string
1315
contentDir: string
1416
outDir: string
@@ -33,6 +35,11 @@ export interface SiteConfig {
3335
mdx: SiteMdxConfig
3436
}
3537

38+
/** Plugins may augment this interface with optional, typed namespaces. */
39+
export interface SiteExtensions {
40+
[plugin: string]: unknown
41+
}
42+
3643
export interface DocLayoutConfig {
3744
/** CSS length reserved above desktop navigation and TOC sidebars. */
3845
sidebarTop: string
@@ -147,3 +154,20 @@ export type SiteStyleConfigInput = DeepPartial<
147154
export type SiteConfigInput = DeepPartial<Omit<SiteConfig, 'style'>> & {
148155
style?: SiteStyleConfigInput
149156
}
157+
158+
/** Inputs for a pure configuration contribution, before build factories run. */
159+
export interface SiteConfigResolution {
160+
config: SiteConfig
161+
input: SiteConfigInput
162+
file: SiteConfigInput
163+
}
164+
165+
export type SiteConfigResolver = (
166+
source: SiteConfigResolution,
167+
) => DeepPartial<SiteConfig>
168+
169+
/** All overrides resolve before any plugin derives dependent settings. */
170+
export interface SiteConfigContributions {
171+
resolve?: SiteConfigResolver
172+
finalize?: SiteConfigResolver
173+
}

0 commit comments

Comments
 (0)