@blogmcp/render-embed 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.
- package/dist/anchor.d.ts +44 -0
- package/dist/anchor.js +62 -0
- package/dist/anchor.js.map +1 -0
- package/dist/host.d.ts +109 -0
- package/dist/host.js +239 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/media.d.ts +10 -0
- package/dist/media.js +17 -0
- package/dist/media.js.map +1 -0
- package/dist/parse.d.ts +50 -0
- package/dist/parse.js +185 -0
- package/dist/parse.js.map +1 -0
- package/dist/protocol.d.ts +145 -0
- package/dist/protocol.js +55 -0
- package/dist/protocol.js.map +1 -0
- package/dist/renderer.d.ts +65 -0
- package/dist/renderer.js +166 -0
- package/dist/renderer.js.map +1 -0
- package/dist/schema.d.ts +1635 -0
- package/dist/schema.js +103 -0
- package/dist/schema.js.map +1 -0
- package/dist/types.d.ts +77 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/package.json +77 -0
- package/src/anchor.ts +83 -0
- package/src/host.ts +302 -0
- package/src/index.ts +25 -0
- package/src/media.ts +17 -0
- package/src/parse.ts +198 -0
- package/src/protocol.ts +154 -0
- package/src/renderer.ts +197 -0
- package/src/schema.ts +139 -0
- package/src/types.ts +76 -0
package/src/schema.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime (Zod) validators for every protocol message — the security boundary. Layered on top of
|
|
3
|
+
* the DOM-free `acceptEnvelope` gate (protocol.ts): channel/nonce/seq/source first, then full
|
|
4
|
+
* payload validation here, BEFORE any field is used.
|
|
5
|
+
*
|
|
6
|
+
* Flat discriminated unions on `type` (NO top-level `.refine`, which would collapse the shape). The
|
|
7
|
+
* hand-written TS unions in protocol.ts are the readable source; `test/schema.spec.ts` cross-checks
|
|
8
|
+
* that a valid message of each TS type passes the matching Zod schema, so the two can't drift.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { z } from 'zod'
|
|
12
|
+
import { RENDER_EMBED_PROTOCOL } from './protocol.js'
|
|
13
|
+
|
|
14
|
+
const channel = z.literal(RENDER_EMBED_PROTOCOL)
|
|
15
|
+
// Reject NaN/Infinity at the boundary — geometry/offsets are clamped downstream, but unguarded
|
|
16
|
+
// numbers (e.g. embed.height.px) would otherwise propagate a non-finite value to consumers.
|
|
17
|
+
const num = z.number().finite()
|
|
18
|
+
const rect = z.object({ x: num, y: num, width: num, height: num })
|
|
19
|
+
const scroll = z.object({ top: num, left: num })
|
|
20
|
+
const theme = z.object({ mode: z.enum(['light', 'dark']), tokens: z.record(z.string()).optional() })
|
|
21
|
+
|
|
22
|
+
function envelope<T extends string, P extends z.ZodTypeAny>(type: T, payload: P) {
|
|
23
|
+
return z.object({
|
|
24
|
+
channel,
|
|
25
|
+
seq: z.number(),
|
|
26
|
+
v: z.number(),
|
|
27
|
+
nonce: z.string(),
|
|
28
|
+
type: z.literal(type),
|
|
29
|
+
payload,
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const renderUnit = z.object({
|
|
34
|
+
type: z.enum(['html', 'component']),
|
|
35
|
+
nodeId: z.string(),
|
|
36
|
+
sourceRange: z.object({ start: num, end: num }),
|
|
37
|
+
html: z.string().optional(),
|
|
38
|
+
name: z.string().optional(),
|
|
39
|
+
props: z.record(z.unknown()).optional(),
|
|
40
|
+
children: z.string().optional(),
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
// ---- Host → renderer ----------------------------------------------------------------------------
|
|
44
|
+
|
|
45
|
+
export const hostInitSchema = envelope(
|
|
46
|
+
'embed.init',
|
|
47
|
+
z.object({
|
|
48
|
+
maxVersion: num,
|
|
49
|
+
theme,
|
|
50
|
+
hostOrigin: z.string(),
|
|
51
|
+
capabilities: z.object({
|
|
52
|
+
wantNodeEvents: z.boolean(),
|
|
53
|
+
wantSelection: z.boolean(),
|
|
54
|
+
wantHeight: z.boolean(),
|
|
55
|
+
}),
|
|
56
|
+
safeAreaInsets: z
|
|
57
|
+
.object({ top: z.number(), right: z.number(), bottom: z.number(), left: z.number() })
|
|
58
|
+
.optional(),
|
|
59
|
+
}),
|
|
60
|
+
)
|
|
61
|
+
export const hostRenderSchema = envelope(
|
|
62
|
+
'embed.render',
|
|
63
|
+
z.object({ blocks: z.array(renderUnit).optional(), content: z.string().optional(), language: z.string() }),
|
|
64
|
+
)
|
|
65
|
+
export const hostScrollSchema = envelope(
|
|
66
|
+
'embed.scrollToNode',
|
|
67
|
+
z.object({ nodeId: z.string(), behavior: z.enum(['smooth', 'auto']).optional() }),
|
|
68
|
+
)
|
|
69
|
+
export const hostHighlightSchema = envelope(
|
|
70
|
+
'embed.highlight',
|
|
71
|
+
z.object({ nodeIds: z.array(z.string()), kind: z.enum(['comment', 'selection', 'clear']) }),
|
|
72
|
+
)
|
|
73
|
+
export const hostSetThemeSchema = envelope('embed.setTheme', theme)
|
|
74
|
+
|
|
75
|
+
export const hostMessageSchema = z.discriminatedUnion('type', [
|
|
76
|
+
hostInitSchema,
|
|
77
|
+
hostRenderSchema,
|
|
78
|
+
hostScrollSchema,
|
|
79
|
+
hostHighlightSchema,
|
|
80
|
+
hostSetThemeSchema,
|
|
81
|
+
])
|
|
82
|
+
|
|
83
|
+
// ---- Renderer → host ----------------------------------------------------------------------------
|
|
84
|
+
|
|
85
|
+
const capabilities = z.object({
|
|
86
|
+
canParse: z.boolean(),
|
|
87
|
+
honorsNodeIds: z.boolean(),
|
|
88
|
+
nodeEvents: z.boolean(),
|
|
89
|
+
selection: z.boolean(),
|
|
90
|
+
height: z.boolean(),
|
|
91
|
+
screenshotFallback: z.boolean(),
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
export const rendererReadySchema = envelope('embed.ready', z.object({ version: num, capabilities }))
|
|
95
|
+
export const rendererHeightSchema = envelope('embed.height', z.object({ px: num }))
|
|
96
|
+
export const rendererNodeClickedSchema = envelope(
|
|
97
|
+
'embed.nodeClicked',
|
|
98
|
+
z.object({
|
|
99
|
+
nodeId: z.string(),
|
|
100
|
+
rect,
|
|
101
|
+
scroll,
|
|
102
|
+
modifiers: z.object({ meta: z.boolean(), shift: z.boolean() }).optional(),
|
|
103
|
+
}),
|
|
104
|
+
)
|
|
105
|
+
export const rendererSelectionSchema = envelope(
|
|
106
|
+
'embed.selection',
|
|
107
|
+
z.object({ nodeId: z.string(), startOffset: num, endOffset: num, text: z.string(), rect, scroll }),
|
|
108
|
+
)
|
|
109
|
+
export const rendererRenderedSchema = envelope(
|
|
110
|
+
'embed.rendered',
|
|
111
|
+
z.object({ units: z.array(z.object({ nodeId: z.string() })) }),
|
|
112
|
+
)
|
|
113
|
+
export const rendererErrorSchema = envelope(
|
|
114
|
+
'embed.error',
|
|
115
|
+
z.object({
|
|
116
|
+
code: z.enum(['parse', 'fetch', 'token', 'unsupported', 'internal']),
|
|
117
|
+
message: z.string(),
|
|
118
|
+
nodeId: z.string().optional(),
|
|
119
|
+
}),
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
export const rendererMessageSchema = z.discriminatedUnion('type', [
|
|
123
|
+
rendererReadySchema,
|
|
124
|
+
rendererHeightSchema,
|
|
125
|
+
rendererNodeClickedSchema,
|
|
126
|
+
rendererSelectionSchema,
|
|
127
|
+
rendererRenderedSchema,
|
|
128
|
+
rendererErrorSchema,
|
|
129
|
+
])
|
|
130
|
+
|
|
131
|
+
/** Validate an inbound host→renderer message (renderer side). */
|
|
132
|
+
export function parseHostMessage(raw: unknown): z.SafeParseReturnType<unknown, unknown> {
|
|
133
|
+
return hostMessageSchema.safeParse(raw)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Validate an inbound renderer→host message (host side). */
|
|
137
|
+
export function parseRendererMessage(raw: unknown): z.SafeParseReturnType<unknown, unknown> {
|
|
138
|
+
return rendererMessageSchema.safeParse(raw)
|
|
139
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared data shapes for the render-embed protocol.
|
|
3
|
+
*
|
|
4
|
+
* The article-review app (the HOST) owns the canonical parse: `parseArticle()` emits a
|
|
5
|
+
* `RenderUnit[]` with app-assigned `nodeId`s + `sourceRange` byte offsets. The renderer is a
|
|
6
|
+
* projector — it renders the units we hand it and stamps `data-mdx-node-id` onto the DOM, keyed by
|
|
7
|
+
* OUR ids. One id space is shared by the live overlay AND the screenshot/structural fallback, so a
|
|
8
|
+
* comment survives a mode switch. Because ids are host-issued, the host can reject any id the
|
|
9
|
+
* renderer invents.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Character offsets into the post-frontmatter body (`stripFrontmatter(content)`). */
|
|
13
|
+
export interface SourceRange {
|
|
14
|
+
start: number
|
|
15
|
+
end: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* One render unit: a block of pre-rendered HTML or an MDX component node. Mirrors the
|
|
20
|
+
* `ParsedItem` shape `parseArticle` produces. `nodeId` is ASSIGNED BY THE HOST APP and is the
|
|
21
|
+
* shared anchor key across live + fallback rendering.
|
|
22
|
+
*/
|
|
23
|
+
export interface RenderUnit {
|
|
24
|
+
type: 'html' | 'component'
|
|
25
|
+
/** e.g. 'mdx-0', 'mdx-3/child-1' — assigned by the host's parser, not the renderer. */
|
|
26
|
+
nodeId: string
|
|
27
|
+
/** Byte offsets into the post-frontmatter body. */
|
|
28
|
+
sourceRange: SourceRange
|
|
29
|
+
/** type==='html': pre-sanitized HTML. The renderer RE-sanitizes regardless (never trusts this). */
|
|
30
|
+
html?: string
|
|
31
|
+
/** type==='component': MDX component name. */
|
|
32
|
+
name?: string
|
|
33
|
+
props?: Record<string, unknown>
|
|
34
|
+
children?: string
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Result of `parseArticle`. */
|
|
38
|
+
export interface ParsedArticle {
|
|
39
|
+
/** 'mdx' when any MDX component is present; 'html' for pure markdown (informational). */
|
|
40
|
+
mode: 'html' | 'mdx'
|
|
41
|
+
/** Pre-rendered HTML for the whole-body fallback (only set when `items` is empty). */
|
|
42
|
+
html: string
|
|
43
|
+
/** Block-level html + component units. */
|
|
44
|
+
items: RenderUnit[]
|
|
45
|
+
/** Post-frontmatter source the units' `sourceRange` offsets are relative to. */
|
|
46
|
+
body: string
|
|
47
|
+
/** Offset at which `body` begins within the original `content` (`content.slice(bodyOffset, …)`). */
|
|
48
|
+
bodyOffset: number
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Renderer mode. `production` = inert passthrough (no protocol, no message listener, no reporting).
|
|
53
|
+
* `edit` = protocol active (annotate blocks, report clicks/selections, apply host messages).
|
|
54
|
+
*/
|
|
55
|
+
export type RenderMode = 'production' | 'edit'
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* A durable comment/selection anchor, host-derived. Stored as a W3C TextQuoteSelector (re-derived
|
|
59
|
+
* host-side from `sourceRange` — durable) + a TextPositionSelector (a hint). Renderer-supplied
|
|
60
|
+
* `text`/offsets are NEVER stored directly (see anchor.ts).
|
|
61
|
+
*/
|
|
62
|
+
export interface Anchor {
|
|
63
|
+
nodeId: string
|
|
64
|
+
/** TextQuoteSelector — re-derived host-side from the raw body slice. */
|
|
65
|
+
quote: { exact: string; prefix?: string; suffix?: string }
|
|
66
|
+
/** TextPositionSelector — body-relative offsets (clamped). */
|
|
67
|
+
position: { start: number; end: number }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A viewport rect reported by the renderer (untrusted until clamped host-side). */
|
|
71
|
+
export interface Rect {
|
|
72
|
+
x: number
|
|
73
|
+
y: number
|
|
74
|
+
width: number
|
|
75
|
+
height: number
|
|
76
|
+
}
|