@blogmcp/sdk 0.12.0 → 0.14.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.
- package/dist/community/links.d.ts +6 -0
- package/dist/community/links.js +9 -1
- package/dist/community/outcome.d.ts +1 -1
- package/dist/community/outcome.js +60 -23
- package/dist/community/private/browser.d.ts +18 -0
- package/dist/community/private/browser.js +17 -0
- package/dist/community/private/client.d.ts +259 -0
- package/dist/community/private/client.js +98 -0
- package/dist/community/private/dm.d.ts +547 -0
- package/dist/community/private/dm.js +852 -0
- package/dist/community/private/dpop.d.ts +59 -0
- package/dist/community/private/dpop.js +46 -0
- package/dist/community/private/encoding.d.ts +17 -0
- package/dist/community/private/encoding.js +50 -0
- package/dist/community/private/host-state.d.ts +20 -0
- package/dist/community/private/host-state.js +15 -0
- package/dist/community/private/index.d.ts +51 -0
- package/dist/community/private/index.js +71 -0
- package/dist/community/private/keystore.d.ts +73 -0
- package/dist/community/private/keystore.js +91 -0
- package/dist/community/private/marker.d.ts +17 -0
- package/dist/community/private/marker.js +11 -0
- package/dist/community/private/private-routes.d.ts +6 -0
- package/dist/community/private/private-routes.js +72 -0
- package/dist/community/private/promise.d.ts +65 -0
- package/dist/community/private/promise.js +39 -0
- package/dist/community/private/realtime.d.ts +127 -0
- package/dist/community/private/realtime.js +199 -0
- package/dist/community/private/session.d.ts +175 -0
- package/dist/community/private/session.js +441 -0
- package/dist/community/private/transport.d.ts +47 -0
- package/dist/community/private/transport.js +65 -0
- package/dist/community/server/grant-handoff.d.ts +53 -0
- package/dist/community/server/grant-handoff.js +96 -0
- package/dist/community/server/grant.d.ts +100 -0
- package/dist/community/server/grant.js +165 -0
- package/dist/community/server/index.d.ts +4 -0
- package/dist/community/server/index.js +20 -16
- package/dist/community/shapes.d.ts +27 -1
- package/dist/community/shapes.js +27 -17
- package/dist/core/client.d.ts +9 -0
- package/dist/core/client.js +54 -50
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +113 -103
- package/dist/core/queries.d.ts +1 -0
- package/dist/core/queries.js +76 -71
- package/dist/core/review/types.d.ts +7 -0
- package/dist/core/style-layers.d.ts +88 -0
- package/dist/core/style-layers.js +69 -0
- package/package.json +7 -3
- package/src/community/links.ts +8 -0
- package/src/community/outcome.ts +37 -0
- package/src/community/private/browser.ts +31 -0
- package/src/community/private/client.ts +304 -0
- package/src/community/private/dm.ts +1687 -0
- package/src/community/private/dpop.ts +110 -0
- package/src/community/private/encoding.ts +71 -0
- package/src/community/private/host-state.ts +37 -0
- package/src/community/private/index.ts +169 -0
- package/src/community/private/keystore.ts +179 -0
- package/src/community/private/marker.ts +28 -0
- package/src/community/private/private-routes.ts +80 -0
- package/src/community/private/promise.ts +125 -0
- package/src/community/private/realtime.ts +431 -0
- package/src/community/private/session.ts +848 -0
- package/src/community/private/transport.ts +125 -0
- package/src/community/server/grant-handoff.ts +179 -0
- package/src/community/server/grant.ts +292 -0
- package/src/community/server/index.ts +6 -0
- package/src/community/shapes.ts +46 -1
- package/src/core/client.ts +12 -0
- package/src/core/index.ts +14 -0
- package/src/core/queries.ts +8 -0
- package/src/core/review/types.ts +8 -0
- package/src/core/style-layers.ts +169 -0
package/src/core/client.ts
CHANGED
|
@@ -28,6 +28,7 @@ import type {
|
|
|
28
28
|
UpdateReportSettingsInput,
|
|
29
29
|
} from './types'
|
|
30
30
|
import type { NavNode } from './navigation'
|
|
31
|
+
import { parseStyleLayers, type StyleLayer } from './style-layers'
|
|
31
32
|
import * as Q from './queries'
|
|
32
33
|
import { createMemoryTokenStorage } from './auth'
|
|
33
34
|
import { KbGraphQLError } from './errors'
|
|
@@ -208,6 +209,14 @@ export interface KbClient {
|
|
|
208
209
|
navigation: {
|
|
209
210
|
get(params?: { scope?: string; locale?: string }): Promise<{ kbNavigation: NavNode[] }>
|
|
210
211
|
}
|
|
212
|
+
/**
|
|
213
|
+
* Style layers: the PUBLISHED layers of the site, in the order they apply (see `./style-layers`).
|
|
214
|
+
* Unlike the other groups this returns the parsed list, not the GraphQL field: an entry that is
|
|
215
|
+
* not a layer is dropped, so a caller can render what comes back.
|
|
216
|
+
*/
|
|
217
|
+
styleLayers: {
|
|
218
|
+
list(): Promise<StyleLayer[]>
|
|
219
|
+
}
|
|
211
220
|
/**
|
|
212
221
|
* Editorial access to translation ROWS — INTERNAL tokens only (a server client holding a
|
|
213
222
|
* kb_* internal key). The server refuses every other caller: a translation row carries the
|
|
@@ -369,6 +378,9 @@ export function createKbClient(config: KbClientConfig): KbClient {
|
|
|
369
378
|
navigation: {
|
|
370
379
|
get: (params) => exec(Q.KB_NAVIGATION, params ?? {}),
|
|
371
380
|
},
|
|
381
|
+
styleLayers: {
|
|
382
|
+
list: async () => parseStyleLayers((await exec<{ kbStyleLayers?: unknown } | undefined>(Q.KB_STYLE_LAYERS))?.kbStyleLayers),
|
|
383
|
+
},
|
|
372
384
|
translations: {
|
|
373
385
|
list: (articleId) => exec(Q.ARTICLE_TRANSLATIONS, { articleId }),
|
|
374
386
|
get: (sourceFilename, languageCode) =>
|
package/src/core/index.ts
CHANGED
|
@@ -113,6 +113,20 @@ export {
|
|
|
113
113
|
flattenNav,
|
|
114
114
|
} from './navigation'
|
|
115
115
|
|
|
116
|
+
// Style layers (CSS the site applies on top of its own design)
|
|
117
|
+
export type { StyleLayer, StyleLayerMatch } from './style-layers'
|
|
118
|
+
export {
|
|
119
|
+
LAYER_SCOPE_TOKEN,
|
|
120
|
+
parseStyleLayers,
|
|
121
|
+
sortStyleLayers,
|
|
122
|
+
layerMatches,
|
|
123
|
+
matchLayers,
|
|
124
|
+
mergePinnedLayers,
|
|
125
|
+
layerScope,
|
|
126
|
+
applyLayerScope,
|
|
127
|
+
layerStyleText,
|
|
128
|
+
} from './style-layers'
|
|
129
|
+
|
|
116
130
|
// Auth
|
|
117
131
|
export type { AuthManager } from './auth'
|
|
118
132
|
export {
|
package/src/core/queries.ts
CHANGED
|
@@ -246,6 +246,14 @@ export const KB_NAVIGATION = `
|
|
|
246
246
|
}
|
|
247
247
|
`
|
|
248
248
|
|
|
249
|
+
// ==================== Style layers ====================
|
|
250
|
+
|
|
251
|
+
export const KB_STYLE_LAYERS = `
|
|
252
|
+
query KbStyleLayers {
|
|
253
|
+
kbStyleLayers
|
|
254
|
+
}
|
|
255
|
+
`
|
|
256
|
+
|
|
249
257
|
// ==================== Tags ====================
|
|
250
258
|
|
|
251
259
|
export const ACCESSIBLE_TAGS = `
|
package/src/core/review/types.ts
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
* and by server code that formats a review for the agent.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import type { StyleLayer } from '../style-layers'
|
|
8
|
+
|
|
7
9
|
/** The route a site serves the review page on; `{token}` is the link's token. */
|
|
8
10
|
export const REVIEW_PAGE_PATH = '/preview/review/{token}'
|
|
9
11
|
/** Express-style pattern of the same route. */
|
|
@@ -85,6 +87,12 @@ export interface ReviewSession {
|
|
|
85
87
|
article: ReviewArticle
|
|
86
88
|
/** Media the working copy references, keyed by slug (signed URLs for non-public ones). */
|
|
87
89
|
media: Record<string, ReviewMediaItem>
|
|
90
|
+
/**
|
|
91
|
+
* Style-layer versions this link pins: on the review page a pinned version stands in for the
|
|
92
|
+
* published version of its layer (`mergePinnedLayers`). Absent from a knowledge base older than
|
|
93
|
+
* style layers; read it with `parseStyleLayers`, which takes that as an empty list.
|
|
94
|
+
*/
|
|
95
|
+
styleLayers?: StyleLayer[]
|
|
88
96
|
}
|
|
89
97
|
|
|
90
98
|
export interface ReviewState {
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Style layers: named CSS a tenant keeps in the knowledge base and its site applies ON TOP of the
|
|
3
|
+
* design that ships in the site's code — a token, a campaign accent, the print layout of one kind
|
|
4
|
+
* of article — without a deploy of the site.
|
|
5
|
+
*
|
|
6
|
+
* The knowledge base validates a layer when it is saved and hands a site CSS in which every
|
|
7
|
+
* selector already lives under a placeholder scope root (`LAYER_SCOPE_TOKEN`). A site decides what
|
|
8
|
+
* that root is on its pages and substitutes it — a plain string replace, so no site needs a CSS
|
|
9
|
+
* parser:
|
|
10
|
+
*
|
|
11
|
+
* const layers = await client.styleLayers.list() // published, in order
|
|
12
|
+
* for (const layer of matchLayers(layers, { articleSlug })) {
|
|
13
|
+
* const css = layerStyleText(layer, layerScope('.my-article-root', layer.match))
|
|
14
|
+
* if (css) render(<style data-kb-layer={layer.name}>{css}</style>) // after your own stylesheets
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* Three rules for a site:
|
|
18
|
+
* - render the layers AFTER its own stylesheets, in the order given (a later layer wins);
|
|
19
|
+
* - with no layer, render nothing; a failure to read the layers renders the page without them;
|
|
20
|
+
* - choose a scope root that leaves out what a layer must never restyle (a checkout, an account,
|
|
21
|
+
* a consent dialog). A layer cannot reach outside the root it is given.
|
|
22
|
+
*
|
|
23
|
+
* Pure and dependency-free: safe in a browser bundle, on a server and in a test.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/** The scope root in the CSS the knowledge base serves. A type selector no element has. */
|
|
27
|
+
export const LAYER_SCOPE_TOKEN = '__KB_LAYER_SCOPE__'
|
|
28
|
+
|
|
29
|
+
/** What a layer applies to. `value` is an article slug (the filename without `.mdx`) or its start. */
|
|
30
|
+
export type StyleLayerMatch =
|
|
31
|
+
| { kind: 'site' }
|
|
32
|
+
| { kind: 'article_prefix'; value: string }
|
|
33
|
+
| { kind: 'article'; value: string }
|
|
34
|
+
|
|
35
|
+
/** One version of a layer as a site receives it. */
|
|
36
|
+
export interface StyleLayer {
|
|
37
|
+
name: string
|
|
38
|
+
match: StyleLayerMatch
|
|
39
|
+
/** Layers apply in ascending position, ties by name. */
|
|
40
|
+
position: number
|
|
41
|
+
version: number
|
|
42
|
+
versionId: string
|
|
43
|
+
/** Validated CSS with `LAYER_SCOPE_TOKEN` where the scope root goes. */
|
|
44
|
+
css: string
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const NAME_RE = /^[a-z][a-z0-9-]{0,62}$/
|
|
48
|
+
const MATCH_VALUE_RE = /^[a-z0-9][a-z0-9-]{0,199}$/
|
|
49
|
+
/** A scope root is one compound or complex selector: no list, no block, no markup. */
|
|
50
|
+
const SCOPE_FORBIDDEN_RE = /[,{}<;]|\/\*|^\s*$/
|
|
51
|
+
|
|
52
|
+
function parseMatch(input: unknown): StyleLayerMatch | null {
|
|
53
|
+
if (!input || typeof input !== 'object') return null
|
|
54
|
+
const { kind, value } = input as { kind?: unknown; value?: unknown }
|
|
55
|
+
if (kind === 'site') return { kind: 'site' }
|
|
56
|
+
if ((kind === 'article' || kind === 'article_prefix') && typeof value === 'string' && MATCH_VALUE_RE.test(value)) {
|
|
57
|
+
return { kind, value }
|
|
58
|
+
}
|
|
59
|
+
return null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function parseLayer(input: unknown): StyleLayer | null {
|
|
63
|
+
if (!input || typeof input !== 'object') return null
|
|
64
|
+
const raw = input as Record<string, unknown>
|
|
65
|
+
const match = parseMatch(raw.match)
|
|
66
|
+
if (
|
|
67
|
+
typeof raw.name !== 'string' ||
|
|
68
|
+
!NAME_RE.test(raw.name) ||
|
|
69
|
+
!match ||
|
|
70
|
+
typeof raw.css !== 'string' ||
|
|
71
|
+
typeof raw.version !== 'number' ||
|
|
72
|
+
typeof raw.versionId !== 'string'
|
|
73
|
+
) {
|
|
74
|
+
return null
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
name: raw.name,
|
|
78
|
+
match,
|
|
79
|
+
position: typeof raw.position === 'number' && Number.isFinite(raw.position) ? raw.position : 0,
|
|
80
|
+
version: raw.version,
|
|
81
|
+
versionId: raw.versionId,
|
|
82
|
+
css: raw.css,
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The order layers apply in: ascending position, ties by name. Returns a new array. */
|
|
87
|
+
export function sortStyleLayers<T extends Pick<StyleLayer, 'position' | 'name'>>(layers: readonly T[]): T[] {
|
|
88
|
+
return [...layers].sort((a, b) => a.position - b.position || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The layers of an API answer — the `kbStyleLayers` field, a review session's `styleLayers`, or the
|
|
93
|
+
* REST list — as a sorted list. An entry that is not a layer is dropped, never thrown on: what a
|
|
94
|
+
* site does with a bad answer is render without it.
|
|
95
|
+
*/
|
|
96
|
+
export function parseStyleLayers(payload: unknown): StyleLayer[] {
|
|
97
|
+
const list = Array.isArray(payload)
|
|
98
|
+
? payload
|
|
99
|
+
: payload && typeof payload === 'object' && Array.isArray((payload as { layers?: unknown }).layers)
|
|
100
|
+
? (payload as { layers: unknown[] }).layers
|
|
101
|
+
: []
|
|
102
|
+
const layers: StyleLayer[] = []
|
|
103
|
+
for (const entry of list) {
|
|
104
|
+
const layer = parseLayer(entry)
|
|
105
|
+
if (layer) layers.push(layer)
|
|
106
|
+
}
|
|
107
|
+
return sortStyleLayers(layers)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Whether a layer applies to a page: a `site` layer always, an article layer by the page's article slug. */
|
|
111
|
+
export function layerMatches(match: StyleLayerMatch, page: { articleSlug?: string | null }): boolean {
|
|
112
|
+
if (match.kind === 'site') return true
|
|
113
|
+
const slug = page.articleSlug
|
|
114
|
+
if (!slug) return false
|
|
115
|
+
return match.kind === 'article' ? slug === match.value : slug.startsWith(match.value)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The layers that apply to a page, in order. */
|
|
119
|
+
export function matchLayers(layers: readonly StyleLayer[], page: { articleSlug?: string | null }): StyleLayer[] {
|
|
120
|
+
return sortStyleLayers(layers.filter((layer) => layerMatches(layer.match, page)))
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The layers of a review page: a version the review link pins stands in for the published version
|
|
125
|
+
* of the same layer, and a pinned layer that is not published is added. In order.
|
|
126
|
+
*/
|
|
127
|
+
export function mergePinnedLayers(published: readonly StyleLayer[], pinned: readonly StyleLayer[]): StyleLayer[] {
|
|
128
|
+
const pinnedNames = new Set(pinned.map((layer) => layer.name))
|
|
129
|
+
return sortStyleLayers([...published.filter((layer) => !pinnedNames.has(layer.name)), ...pinned])
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The scope root of a layer on a site that marks an article's root element with its slug
|
|
134
|
+
* (`<div class="article" data-article="my-slug">`): `base` for a `site` layer, `base[attr="slug"]`
|
|
135
|
+
* for an article, `base[attr^="prefix"]` for an article prefix. `base` is the selector of the
|
|
136
|
+
* element that carries the attribute.
|
|
137
|
+
*/
|
|
138
|
+
export function layerScope(base: string, match: StyleLayerMatch, attribute = 'data-article'): string {
|
|
139
|
+
if (match.kind === 'site') return base
|
|
140
|
+
return `${base}[${attribute}${match.kind === 'article' ? '=' : '^='}"${match.value}"]`
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The layer's CSS with the site's scope root in place of the placeholder.
|
|
145
|
+
*
|
|
146
|
+
* @throws TypeError when `scope` is not one selector (a list would scope only its last part).
|
|
147
|
+
*/
|
|
148
|
+
export function applyLayerScope(css: string, scope: string): string {
|
|
149
|
+
if (typeof scope !== 'string' || SCOPE_FORBIDDEN_RE.test(scope)) {
|
|
150
|
+
throw new TypeError('applyLayerScope: the scope must be one selector, e.g. ".article" or ".article[data-article=\\"x\\"]"')
|
|
151
|
+
}
|
|
152
|
+
return css.split(LAYER_SCOPE_TOKEN).join(scope.trim())
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The text of a layer's `<style>` element, or null when the layer must not be rendered: an empty
|
|
157
|
+
* layer, a scope that is not one selector, or CSS holding `</` or `<!--`. The knowledge base
|
|
158
|
+
* refuses those sequences when a layer is saved; this is the same check at the last step before
|
|
159
|
+
* the text goes into a page, where a `</style` would end the element.
|
|
160
|
+
*/
|
|
161
|
+
export function layerStyleText(layer: Pick<StyleLayer, 'css'>, scope: string): string | null {
|
|
162
|
+
const css = layer.css
|
|
163
|
+
if (typeof css !== 'string' || css.trim() === '' || css.includes('</') || css.includes('<!--')) return null
|
|
164
|
+
try {
|
|
165
|
+
return applyLayerScope(css, scope)
|
|
166
|
+
} catch {
|
|
167
|
+
return null
|
|
168
|
+
}
|
|
169
|
+
}
|