@blogmcp/sdk 0.11.0 → 0.13.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 (35) hide show
  1. package/dist/community/client.d.ts +1 -1
  2. package/dist/community/client.js +2 -1
  3. package/dist/community/messages.cs.d.ts +5 -3
  4. package/dist/community/messages.cs.js +7 -3
  5. package/dist/community/messages.en.d.ts +5 -3
  6. package/dist/community/messages.en.js +7 -3
  7. package/dist/community/shapes.d.ts +15 -0
  8. package/dist/community/store.d.ts +12 -0
  9. package/dist/community/store.js +27 -27
  10. package/dist/core/client.d.ts +9 -0
  11. package/dist/core/client.js +54 -50
  12. package/dist/core/index.d.ts +2 -0
  13. package/dist/core/index.js +113 -103
  14. package/dist/core/queries.d.ts +1 -0
  15. package/dist/core/queries.js +76 -71
  16. package/dist/core/review/types.d.ts +7 -0
  17. package/dist/core/style-layers.d.ts +88 -0
  18. package/dist/core/style-layers.js +69 -0
  19. package/dist/nuxt/composables/useAsyncKb.d.ts +10 -4
  20. package/dist/packages/mdx-render-vue/dist/render/gallery/GalleryLightbox.vue.js +2 -2
  21. package/dist/packages/mdx-render-vue/dist/render/tripmap/TripMapDefault.vue.js +36 -36
  22. package/package.json +3 -3
  23. package/src/community/client.ts +3 -2
  24. package/src/community/messages.cs.ts +7 -3
  25. package/src/community/messages.en.ts +7 -3
  26. package/src/community/shapes.ts +15 -0
  27. package/src/community/store.ts +14 -2
  28. package/src/core/client.ts +12 -0
  29. package/src/core/index.ts +14 -0
  30. package/src/core/markdown.ts +1 -1
  31. package/src/core/queries.ts +8 -0
  32. package/src/core/review/types.ts +8 -0
  33. package/src/core/style-layers.ts +169 -0
  34. package/src/nuxt/composables/useAsyncKb.ts +11 -4
  35. package/src/nuxt/nuxt-imports.d.ts +2 -2
@@ -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
+ }
@@ -18,11 +18,18 @@
18
18
  // this package's own build; in a Nuxt app the app's own `#imports` types apply.
19
19
 
20
20
  import { useAsyncData } from '#imports'
21
- import type { Ref } from 'vue'
21
+ import type { Ref, WatchSource } from 'vue'
22
22
  import { useArticleList, useArticle } from '../../vue/composables/useArticles'
23
23
  import type { ArticleListParams } from '../../vue/composables/useArticles'
24
24
  import { useNavigation } from '../../vue/composables/useNavigation'
25
25
 
26
+ /**
27
+ * What `useAsyncData`'s `watch` takes (Vue's `MultiWatchSources`, spelled out because Vue exports that
28
+ * name only from 3.5): refs, getters and reactive objects. The adapters used to type it `unknown[]`,
29
+ * which Nuxt's own `useAsyncData` types refuse (a Nuxt app's typecheck failed in all three).
30
+ */
31
+ export type AsyncWatchDeps = (WatchSource<unknown> | object)[]
32
+
26
33
  /**
27
34
  * SSR-hydrated article list.
28
35
  *
@@ -31,7 +38,7 @@ import { useNavigation } from '../../vue/composables/useNavigation'
31
38
  export async function useAsyncArticleList(
32
39
  key: string,
33
40
  params?: ArticleListParams,
34
- watchDeps?: unknown[],
41
+ watchDeps?: AsyncWatchDeps,
35
42
  ) {
36
43
  const composable = useArticleList(params)
37
44
 
@@ -65,7 +72,7 @@ export async function useAsyncArticle(
65
72
  key: string,
66
73
  filename: Ref<string> | string,
67
74
  languageCode: Ref<string> | string,
68
- watchDeps?: unknown[],
75
+ watchDeps?: AsyncWatchDeps,
69
76
  ) {
70
77
  const composable = useArticle()
71
78
 
@@ -96,7 +103,7 @@ export async function useAsyncArticle(
96
103
  export async function useAsyncNavigation(
97
104
  key: string,
98
105
  params?: { scope?: string; locale?: string },
99
- watchDeps?: unknown[],
106
+ watchDeps?: AsyncWatchDeps,
100
107
  ) {
101
108
  const composable = useNavigation()
102
109
 
@@ -5,10 +5,10 @@
5
5
  * only), and the app's own `.nuxt/imports.d.ts` types the alias there.
6
6
  */
7
7
  declare module '#imports' {
8
- import type { Ref } from 'vue'
8
+ import type { Ref, WatchSource } from 'vue'
9
9
  export function useAsyncData<T>(
10
10
  key: string,
11
11
  handler: () => Promise<T>,
12
- opts?: { watch?: unknown[] },
12
+ opts?: { watch?: (WatchSource<unknown> | object)[] },
13
13
  ): Promise<{ data: Ref<T | null>; pending: Ref<boolean>; error: Ref<Error | null> }>
14
14
  }