@elastic/isomer-sdk 0.1.0 → 0.2.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/README.md +13 -2
- package/dist/author/authored_fields.d.ts +61 -11
- package/dist/author/authored_fields.d.ts.map +1 -1
- package/dist/author/authored_fields.js +73 -29
- package/dist/author/authored_fields.js.map +1 -1
- package/dist/author/authoring_input.d.ts +7 -0
- package/dist/author/authoring_input.d.ts.map +1 -0
- package/dist/author/authoring_input.js +60 -0
- package/dist/author/authoring_input.js.map +1 -0
- package/dist/author/authoring_model.d.ts +27 -0
- package/dist/author/authoring_model.d.ts.map +1 -0
- package/dist/author/authoring_model.js +98 -0
- package/dist/author/authoring_model.js.map +1 -0
- package/dist/author/declarations.d.ts +13 -0
- package/dist/author/declarations.d.ts.map +1 -0
- package/dist/author/declarations.js +277 -0
- package/dist/author/declarations.js.map +1 -0
- package/dist/author/index.d.ts +3 -1
- package/dist/author/index.d.ts.map +1 -1
- package/dist/author/index.js +3 -1
- package/dist/author/index.js.map +1 -1
- package/dist/author/jsx_shim.d.ts +6 -5
- package/dist/author/jsx_shim.d.ts.map +1 -1
- package/dist/author/jsx_shim.js +28 -111
- package/dist/author/jsx_shim.js.map +1 -1
- package/dist/author/schema_to_typescript.d.ts +37 -0
- package/dist/author/schema_to_typescript.d.ts.map +1 -0
- package/dist/author/schema_to_typescript.js +286 -0
- package/dist/author/schema_to_typescript.js.map +1 -0
- package/dist/cjs/author/authored_fields.js +77 -33
- package/dist/cjs/author/authored_fields.js.map +1 -1
- package/dist/cjs/author/authoring_input.js +64 -0
- package/dist/cjs/author/authoring_input.js.map +1 -0
- package/dist/cjs/author/authoring_model.js +104 -0
- package/dist/cjs/author/authoring_model.js.map +1 -0
- package/dist/cjs/author/declarations.js +281 -0
- package/dist/cjs/author/declarations.js.map +1 -0
- package/dist/cjs/author/index.js +6 -1
- package/dist/cjs/author/index.js.map +1 -1
- package/dist/cjs/author/jsx_shim.js +31 -114
- package/dist/cjs/author/jsx_shim.js.map +1 -1
- package/dist/cjs/author/schema_to_typescript.js +295 -0
- package/dist/cjs/author/schema_to_typescript.js.map +1 -0
- package/dist/cjs/composition/body_node_base.js +11 -4
- package/dist/cjs/composition/body_node_base.js.map +1 -1
- package/dist/cjs/composition/error.js +1 -0
- package/dist/cjs/composition/error.js.map +1 -1
- package/dist/cjs/composition/index.js +2 -1
- package/dist/cjs/composition/index.js.map +1 -1
- package/dist/cjs/define/frame.js +2 -3
- package/dist/cjs/define/frame.js.map +1 -1
- package/dist/cjs/define/index.js +7 -1
- package/dist/cjs/define/index.js.map +1 -1
- package/dist/cjs/define/primitive_example.js +26 -0
- package/dist/cjs/define/primitive_example.js.map +1 -0
- package/dist/cjs/define/primitive_icon.js +17 -0
- package/dist/cjs/define/primitive_icon.js.map +1 -0
- package/dist/cjs/define/primitive_module.js +14 -3
- package/dist/cjs/define/primitive_module.js.map +1 -1
- package/dist/cjs/define/slack_assets.js.map +1 -1
- package/dist/cjs/entries/index.js +7 -2
- package/dist/cjs/entries/index.js.map +1 -1
- package/dist/cjs/entries/slack.js +1 -2
- package/dist/cjs/entries/slack.js.map +1 -1
- package/dist/cjs/pack/capabilities.js +17 -11
- package/dist/cjs/pack/capabilities.js.map +1 -1
- package/dist/cjs/pack/compose.js +4 -1
- package/dist/cjs/pack/compose.js.map +1 -1
- package/dist/cjs/pack/index.js.map +1 -1
- package/dist/cjs/pack/primitive_pack.js +14 -2
- package/dist/cjs/pack/primitive_pack.js.map +1 -1
- package/dist/cjs/render/anchors.js +1 -1
- package/dist/cjs/render/anchors.js.map +1 -1
- package/dist/cjs/render/html/envelope.js.map +1 -1
- package/dist/cjs/render/layout_check.js.map +1 -1
- package/dist/cjs/render/markdown/builder.js +14 -8
- package/dist/cjs/render/markdown/builder.js.map +1 -1
- package/dist/cjs/render/markdown/format.js +30 -12
- package/dist/cjs/render/markdown/format.js.map +1 -1
- package/dist/cjs/render/primitive_dispatch.js +11 -25
- package/dist/cjs/render/primitive_dispatch.js.map +1 -1
- package/dist/cjs/render/slack/format.js +1 -601
- package/dist/cjs/render/slack/format.js.map +1 -1
- package/dist/cjs/render/slack/index.js +1 -2
- package/dist/cjs/render/slack/index.js.map +1 -1
- package/dist/cjs/render/slack/markdown_content.js +166 -115
- package/dist/cjs/render/slack/markdown_content.js.map +1 -1
- package/dist/cjs/testing/conformance.js +40 -24
- package/dist/cjs/testing/conformance.js.map +1 -1
- package/dist/cjs/testing/icon.js +248 -0
- package/dist/cjs/testing/icon.js.map +1 -0
- package/dist/cjs/testing/index.js +3 -1
- package/dist/cjs/testing/index.js.map +1 -1
- package/dist/cjs/testing/registration.js +5 -3
- package/dist/cjs/testing/registration.js.map +1 -1
- package/dist/cjs/validate/authoring_schema.js +17 -1
- package/dist/cjs/validate/authoring_schema.js.map +1 -1
- package/dist/cjs/validate/index.js +2 -1
- package/dist/cjs/validate/index.js.map +1 -1
- package/dist/cjs/validate/validation.js +2 -45
- package/dist/cjs/validate/validation.js.map +1 -1
- package/dist/composition/body_node_base.d.ts +7 -6
- package/dist/composition/body_node_base.d.ts.map +1 -1
- package/dist/composition/body_node_base.js +10 -3
- package/dist/composition/body_node_base.js.map +1 -1
- package/dist/composition/error.d.ts +1 -0
- package/dist/composition/error.d.ts.map +1 -1
- package/dist/composition/error.js +1 -0
- package/dist/composition/error.js.map +1 -1
- package/dist/composition/index.d.ts +1 -1
- package/dist/composition/index.d.ts.map +1 -1
- package/dist/composition/index.js +1 -1
- package/dist/composition/index.js.map +1 -1
- package/dist/define/frame.d.ts +9 -20
- package/dist/define/frame.d.ts.map +1 -1
- package/dist/define/frame.js +2 -3
- package/dist/define/frame.js.map +1 -1
- package/dist/define/index.d.ts +3 -1
- package/dist/define/index.d.ts.map +1 -1
- package/dist/define/index.js +3 -1
- package/dist/define/index.js.map +1 -1
- package/dist/define/primitive_example.d.ts +30 -0
- package/dist/define/primitive_example.d.ts.map +1 -0
- package/dist/define/primitive_example.js +21 -0
- package/dist/define/primitive_example.js.map +1 -0
- package/dist/define/primitive_icon.d.ts +11 -0
- package/dist/define/primitive_icon.d.ts.map +1 -0
- package/dist/define/primitive_icon.js +14 -0
- package/dist/define/primitive_icon.js.map +1 -0
- package/dist/define/primitive_module.d.ts +38 -24
- package/dist/define/primitive_module.d.ts.map +1 -1
- package/dist/define/primitive_module.js +14 -3
- package/dist/define/primitive_module.js.map +1 -1
- package/dist/define/slack_assets.d.ts +1 -1
- package/dist/define/slack_assets.d.ts.map +1 -1
- package/dist/define/slack_assets.js.map +1 -1
- package/dist/entries/index.d.ts +3 -3
- package/dist/entries/index.d.ts.map +1 -1
- package/dist/entries/index.js +2 -2
- package/dist/entries/index.js.map +1 -1
- package/dist/entries/slack.d.ts +1 -1
- package/dist/entries/slack.d.ts.map +1 -1
- package/dist/entries/slack.js +1 -1
- package/dist/entries/slack.js.map +1 -1
- package/dist/pack/capabilities.d.ts +24 -10
- package/dist/pack/capabilities.d.ts.map +1 -1
- package/dist/pack/capabilities.js +17 -11
- package/dist/pack/capabilities.js.map +1 -1
- package/dist/pack/compose.d.ts +4 -1
- package/dist/pack/compose.d.ts.map +1 -1
- package/dist/pack/compose.js +4 -1
- package/dist/pack/compose.js.map +1 -1
- package/dist/pack/index.d.ts +1 -1
- package/dist/pack/index.d.ts.map +1 -1
- package/dist/pack/index.js +1 -1
- package/dist/pack/index.js.map +1 -1
- package/dist/pack/primitive_pack.d.ts +11 -2
- package/dist/pack/primitive_pack.d.ts.map +1 -1
- package/dist/pack/primitive_pack.js +12 -1
- package/dist/pack/primitive_pack.js.map +1 -1
- package/dist/render/anchors.d.ts +2 -2
- package/dist/render/anchors.d.ts.map +1 -1
- package/dist/render/anchors.js +1 -1
- package/dist/render/anchors.js.map +1 -1
- package/dist/render/html/envelope.d.ts +3 -3
- package/dist/render/html/envelope.js.map +1 -1
- package/dist/render/layout_check.d.ts +1 -1
- package/dist/render/layout_check.d.ts.map +1 -1
- package/dist/render/layout_check.js.map +1 -1
- package/dist/render/markdown/builder.d.ts +2 -4
- package/dist/render/markdown/builder.d.ts.map +1 -1
- package/dist/render/markdown/builder.js +14 -7
- package/dist/render/markdown/builder.js.map +1 -1
- package/dist/render/markdown/format.d.ts +7 -0
- package/dist/render/markdown/format.d.ts.map +1 -1
- package/dist/render/markdown/format.js +27 -11
- package/dist/render/markdown/format.js.map +1 -1
- package/dist/render/primitive_dispatch.d.ts +6 -6
- package/dist/render/primitive_dispatch.d.ts.map +1 -1
- package/dist/render/primitive_dispatch.js +12 -26
- package/dist/render/primitive_dispatch.js.map +1 -1
- package/dist/render/slack/format.d.ts +0 -25
- package/dist/render/slack/format.d.ts.map +1 -1
- package/dist/render/slack/format.js +1 -599
- package/dist/render/slack/format.js.map +1 -1
- package/dist/render/slack/index.d.ts +1 -1
- package/dist/render/slack/index.d.ts.map +1 -1
- package/dist/render/slack/index.js +1 -1
- package/dist/render/slack/index.js.map +1 -1
- package/dist/render/slack/markdown_content.d.ts +10 -4
- package/dist/render/slack/markdown_content.d.ts.map +1 -1
- package/dist/render/slack/markdown_content.js +163 -113
- package/dist/render/slack/markdown_content.js.map +1 -1
- package/dist/testing/conformance.d.ts +19 -14
- package/dist/testing/conformance.d.ts.map +1 -1
- package/dist/testing/conformance.js +40 -24
- package/dist/testing/conformance.js.map +1 -1
- package/dist/testing/icon.d.ts +11 -0
- package/dist/testing/icon.d.ts.map +1 -0
- package/dist/testing/icon.js +244 -0
- package/dist/testing/icon.js.map +1 -0
- package/dist/testing/index.d.ts +2 -1
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +1 -0
- package/dist/testing/index.js.map +1 -1
- package/dist/testing/registration.d.ts +1 -1
- package/dist/testing/registration.d.ts.map +1 -1
- package/dist/testing/registration.js +6 -4
- package/dist/testing/registration.js.map +1 -1
- package/dist/validate/authoring_schema.d.ts +6 -0
- package/dist/validate/authoring_schema.d.ts.map +1 -1
- package/dist/validate/authoring_schema.js +15 -0
- package/dist/validate/authoring_schema.js.map +1 -1
- package/dist/validate/index.d.ts +1 -1
- package/dist/validate/index.d.ts.map +1 -1
- package/dist/validate/index.js +1 -1
- package/dist/validate/index.js.map +1 -1
- package/dist/validate/validation.d.ts +1 -11
- package/dist/validate/validation.d.ts.map +1 -1
- package/dist/validate/validation.js +3 -46
- package/dist/validate/validation.js.map +1 -1
- package/package.json +3 -2
|
@@ -23,17 +23,17 @@ export interface PrimitiveDispatcher<TNode extends PrimitiveNode, T extends Pack
|
|
|
23
23
|
/** Mutates `styles` with this node's contribution. */
|
|
24
24
|
collectStyles(node: TNode, styles: T['collector'], context: PrimitiveStyleCollectionContext): void;
|
|
25
25
|
/**
|
|
26
|
-
* The node's `react` renderer, gated on `
|
|
26
|
+
* The node's `react` renderer, gated on `snapshot` visibility and keyed for a
|
|
27
27
|
* frame's sibling list. `key` must be unique among those siblings.
|
|
28
28
|
* `theme` is the frame's resolved palette for this render, or `undefined`
|
|
29
29
|
* outside a frame.
|
|
30
30
|
*/
|
|
31
|
-
|
|
31
|
+
renderSnapshot(node: TNode, context: T['context'], theme: T['theme'] | undefined, key: string): ReactNode;
|
|
32
32
|
/** `''` when the node is hidden from `text` or its primitive declares no `text` renderer. */
|
|
33
33
|
renderText(node: TNode): string;
|
|
34
|
-
/**
|
|
34
|
+
/** The node's content serialized as GFM. */
|
|
35
35
|
renderMarkdown(node: TNode): string;
|
|
36
|
-
/**
|
|
36
|
+
/** Empty when the node is hidden from `markdown`. */
|
|
37
37
|
renderMarkdownContent(node: TNode): MarkdownContent;
|
|
38
38
|
/**
|
|
39
39
|
* Empty when the node is hidden from `slack`, `sanitize` drops it, or its
|
|
@@ -42,8 +42,8 @@ export interface PrimitiveDispatcher<TNode extends PrimitiveNode, T extends Pack
|
|
|
42
42
|
* `collector` can allocate.
|
|
43
43
|
*/
|
|
44
44
|
renderSlack(node: TNode, collector?: T['slackCollector']): readonly T['slackBlock'][];
|
|
45
|
-
/** `0` when the node is hidden from `
|
|
46
|
-
|
|
45
|
+
/** `0` when the node is hidden from `snapshot` or its primitive declares no `metrics.snapshotHeight`. */
|
|
46
|
+
estimateSnapshotHeight(node: TNode): number;
|
|
47
47
|
/**
|
|
48
48
|
* Appends schema failures under `path` to `errors`, each naming `node`'s
|
|
49
49
|
* type, and an unknown key on `node` listing the fields it declares.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"primitive_dispatch.d.ts","sourceRoot":"","sources":["../../src/render/primitive_dispatch.ts"],"names":[],"mappings":"AAOA,OAAO,EAAgC,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAErE,OAAO,EAEL,KAAK,WAAW,EACjB,MAAM,+BAA+B,CAAC;AAEvC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EACL,KAAK,sBAAsB,EAC3B,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,+BAA+B,EACpC,KAAK,eAAe,EAGpB,KAAK,UAAU,EAChB,MAAM,4BAA4B,CAAC;
|
|
1
|
+
{"version":3,"file":"primitive_dispatch.d.ts","sourceRoot":"","sources":["../../src/render/primitive_dispatch.ts"],"names":[],"mappings":"AAOA,OAAO,EAAgC,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAErE,OAAO,EAEL,KAAK,WAAW,EACjB,MAAM,+BAA+B,CAAC;AAEvC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EACL,KAAK,sBAAsB,EAC3B,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,+BAA+B,EACpC,KAAK,eAAe,EAGpB,KAAK,UAAU,EAChB,MAAM,4BAA4B,CAAC;AAQpC;;;;;;;GAOG;AACH,MAAM,WAAW,mBAAmB,C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C,EAAE,CAAC,CAAC,gBAAgB,CAAC,GAC9B,SAAS,CAAC,CAAC,YAAY,CAAC,EAAE,CAAC;IAC9B,yGAAyG;IACzG,sBAAsB,CAAC,IAAI,EAAE,KAAK,GAAG,MAAM,CAAC;IAC5C;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,GAAG,IAAI,CAAC;CACtE;AAoBD,wFAAwF;AACxF,MAAM,WAAW,0BAA0B;IACzC,+CAA+C;IAC/C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,CAAC,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,SAAS,CAAC;CAC5D;AAED;;;;;;GAMG;AACH,eAAO,MAAM,yBAAyB,GACpC,KAAK,SAAS,aAAa,EAC3B,CAAC,SAAS,SAAS,GAAG,gBAAgB,EAEtC,aAAa,SAAS,sBAAsB,EAAE,EAC9C,UAAS,0BAA+B,KACvC,mBAAmB,CAAC,KAAK,EAAE,CAAC,CA4K9B,CAAC"}
|
|
@@ -8,19 +8,18 @@ import { cloneElement, isValidElement } from 'react';
|
|
|
8
8
|
import { isVisibleOnSurface, } from '../composition/body_node_base.js';
|
|
9
9
|
import { IsomerError } from '../composition/error.js';
|
|
10
10
|
import { createNodeIssueFormatter } from '../validate/node_issues.js';
|
|
11
|
-
import {
|
|
12
|
-
import { gfmToSlackBlocks } from './slack/format.js';
|
|
11
|
+
import { serializeMarkdown } from './markdown/builder.js';
|
|
13
12
|
import { markdownContentToSlackBlocks } from './slack/markdown_content.js';
|
|
14
13
|
/**
|
|
15
14
|
* Which of a definition's renderers each surface dispatches to.
|
|
16
15
|
*
|
|
17
|
-
* `
|
|
16
|
+
* `snapshot` maps to `react`: a rasterizer lays out the pack's DOM tree and
|
|
18
17
|
* stylesheet, so the two surfaces differ only in visibility and in what
|
|
19
18
|
* surrounds them.
|
|
20
19
|
*/
|
|
21
20
|
const RENDERER_FOR_SURFACE = {
|
|
22
21
|
react: 'react',
|
|
23
|
-
|
|
22
|
+
snapshot: 'react',
|
|
24
23
|
text: 'text',
|
|
25
24
|
markdown: 'markdown',
|
|
26
25
|
slack: 'slack',
|
|
@@ -74,18 +73,8 @@ export const createPrimitiveDispatcher = (definitions, options = {}) => {
|
|
|
74
73
|
const renderer = definition.renderers[RENDERER_FOR_SURFACE[surface]];
|
|
75
74
|
return renderer?.(safeNode, { ...extras, scope: scope() });
|
|
76
75
|
};
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
return typeof rendered === 'string'
|
|
80
|
-
? rendered
|
|
81
|
-
: serializeMarkdown(rendered);
|
|
82
|
-
};
|
|
83
|
-
const renderMarkdownContent = (node) => {
|
|
84
|
-
const rendered = renderOn('markdown', node, {}) ?? '';
|
|
85
|
-
return typeof rendered === 'string'
|
|
86
|
-
? markdownFromString(rendered)
|
|
87
|
-
: rendered;
|
|
88
|
-
};
|
|
76
|
+
const renderMarkdownContent = (node) => renderOn('markdown', node, {}) ?? [];
|
|
77
|
+
const renderMarkdown = (node) => serializeMarkdown(renderMarkdownContent(node));
|
|
89
78
|
self = {
|
|
90
79
|
definitions,
|
|
91
80
|
getDefinition,
|
|
@@ -100,8 +89,8 @@ export const createPrimitiveDispatcher = (definitions, options = {}) => {
|
|
|
100
89
|
const collectStyles = definition.collectStyles;
|
|
101
90
|
collectStyles?.(node, { styles, context });
|
|
102
91
|
},
|
|
103
|
-
|
|
104
|
-
const rendered = renderOn('
|
|
92
|
+
renderSnapshot: (node, context, theme, key) => {
|
|
93
|
+
const rendered = renderOn('snapshot', node, { context, theme });
|
|
105
94
|
// The frame hands these to React as an array; a `key` has to sit on the
|
|
106
95
|
// element itself, since wrapping in a Fragment would hide the single
|
|
107
96
|
// root an image backend lays out from.
|
|
@@ -140,10 +129,7 @@ export const createPrimitiveDispatcher = (definitions, options = {}) => {
|
|
|
140
129
|
// Degrading here rather than in the envelope is what reaches nested
|
|
141
130
|
// children: a container's `slack` renderer recurses through this
|
|
142
131
|
// method, and the envelope only ever sees the container's own output.
|
|
143
|
-
|
|
144
|
-
return asSlackPayload(typeof markdown === 'string'
|
|
145
|
-
? gfmToSlackBlocks(markdown)
|
|
146
|
-
: markdownContentToSlackBlocks(markdown));
|
|
132
|
+
return asSlackPayload(markdownContentToSlackBlocks(renderMarkdownContent(node)));
|
|
147
133
|
}
|
|
148
134
|
const safeNode = sanitizeNode(definition, node);
|
|
149
135
|
if (safeNode === null) {
|
|
@@ -155,12 +141,12 @@ export const createPrimitiveDispatcher = (definitions, options = {}) => {
|
|
|
155
141
|
});
|
|
156
142
|
return isReadonlyArray(rendered) ? rendered : [rendered];
|
|
157
143
|
},
|
|
158
|
-
|
|
159
|
-
if (!isVisibleOnSurface(node, '
|
|
144
|
+
estimateSnapshotHeight: (node) => {
|
|
145
|
+
if (!isVisibleOnSurface(node, 'snapshot')) {
|
|
160
146
|
return 0;
|
|
161
147
|
}
|
|
162
|
-
const
|
|
163
|
-
return
|
|
148
|
+
const snapshotHeight = getDefinition(node).metrics?.snapshotHeight;
|
|
149
|
+
return snapshotHeight?.(node) ?? 0;
|
|
164
150
|
},
|
|
165
151
|
validate: (node, path, errors) => {
|
|
166
152
|
const result = getDefinition(node).schema.safeParse(node, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"primitive_dispatch.js","sourceRoot":"","sources":["../../src/render/primitive_dispatch.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,YAAY,EAAE,cAAc,EAAkB,MAAM,OAAO,CAAC;AAErE,OAAO,EACL,kBAAkB,GAEnB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAcnD,OAAO,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AAEnE,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,oBAAoB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AE,SACR,CAAC;YACxC,OAAO,SAAS,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAChC,CAAC;QACD,QAAQ,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE;YAC/B,MAAM,MAAM,GAAG,aAAa,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE;gBACxD,WAAW,EAAE,IAAI;aAClB,CAAC,CAAC;YACH,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;gBACpB,MAAM,CAAC,IAAI,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;YAC1E,CAAC;QACH,CAAC;KACF,CAAC;IACF,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEF,MAAM,eAAe,GAAG,CAAI,KAAuB,EAAyB,EAAE,CAC5E,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAEvB,+FAA+F;AAC/F,MAAM,qBAAqB,GAAG,CAAC,KAAc,EAAgC,EAAE,CAC7E,OAAO,KAAK,KAAK,QAAQ;IACzB,KAAK,KAAK,IAAI;IACd,OAAQ,KAAgC,CAAC,QAAQ,KAAK,UAAU,CAAC;AAEnE,iEAAiE;AACjE,MAAM,cAAc,GAAG,CACrB,MAA6B,EACL,EAAE,CAAC,MAA2C,CAAC","sourcesContent":["/*\n * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one\n * or more contributor license agreements. Licensed under the Elastic License\n * 2.0; you may not use this file except in compliance with the Elastic License\n * 2.0.\n */\n\nimport { cloneElement, isValidElement, type ReactNode } from 'react';\n\nimport {\n isVisibleOnSurface,\n type SurfaceName,\n} from '../composition/body_node_base';\nimport { IsomerError } from '../composition/error';\nimport type { ValidationError } from '../composition/validation_error';\nimport type { MarkdownContent } from '../define/markdown_content';\nimport {\n type AnyPrimitiveDefinition,\n type DefaultPackTypes,\n type PackTypes,\n type PrimitiveNode,\n type PrimitiveStyleCollectionContext,\n type ReactContextArg,\n type Renderer,\n type RenderScope,\n type SurfaceMap,\n} from '../define/primitive_module';\nimport { createNodeIssueFormatter } from '../validate/node_issues';\n\nimport { markdownFromString, serializeMarkdown } from './markdown/builder';\nimport type { SlackAssetCollector } from './slack/assets';\nimport type { SlackBlock, SlackImageBlock } from './slack/blocks';\nimport { gfmToSlackBlocks } from './slack/format';\nimport { markdownContentToSlackBlocks } from './slack/markdown_content';\n\n/**\n * Renders a node on any surface by looking its `type` up in one flattened\n * inventory.\n *\n * The seam that makes packs composable: a container recurses through the\n * {@link RenderScope} it is passed rather than through its own pack, so a node\n * owned by another pack still renders.\n */\nexport interface PrimitiveDispatcher<\n TNode extends PrimitiveNode,\n T extends PackTypes = DefaultPackTypes,\n> {\n /** Every primitive this dispatcher can reach, across all composed packs. */\n readonly definitions: readonly AnyPrimitiveDefinition[];\n /** Throws for an unknown node type; a missing primitive is a bug, not a degradation. */\n getDefinition(node: TNode): AnyPrimitiveDefinition;\n /** `undefined` when the node is hidden from `surface`, `sanitize` drops it, or the primitive declares no renderer for `surface`. */\n renderOn<S extends SurfaceName>(\n surface: S,\n node: TNode,\n extras: Omit<SurfaceMap<T>[S]['env'], 'scope'>\n ): SurfaceMap<T>[S]['output'] | undefined;\n /** The node's `react` renderer, or `undefined` when it is hidden or has none. `context` may be omitted only when `{}` is a complete context for the pack. */\n renderReact(\n node: TNode,\n ...context: ReactContextArg<T['context']>\n ): ReactNode;\n /** Mutates `styles` with this node's contribution. */\n collectStyles(\n node: TNode,\n styles: T['collector'],\n context: PrimitiveStyleCollectionContext\n ): void;\n /**\n * The node's `react` renderer, gated on `svg` visibility and keyed for a\n * frame's sibling list. `key` must be unique among those siblings.\n * `theme` is the frame's resolved palette for this render, or `undefined`\n * outside a frame.\n */\n renderSvg(\n node: TNode,\n context: T['context'],\n theme: T['theme'] | undefined,\n key: string\n ): ReactNode;\n /** `''` when the node is hidden from `text` or its primitive declares no `text` renderer. */\n renderText(node: TNode): string;\n /** Builder content is serialized per node. */\n renderMarkdown(node: TNode): string;\n /** A string result comes back as content printed as written. */\n renderMarkdownContent(node: TNode): MarkdownContent;\n /**\n * Empty when the node is hidden from `slack`, `sanitize` drops it, or its\n * renderer returns nothing. A node with no `slack` renderer degrades through\n * its markdown; one `isSlackAssetType` names becomes an `image` block when\n * `collector` can allocate.\n */\n renderSlack(\n node: TNode,\n collector?: T['slackCollector']\n ): readonly T['slackBlock'][];\n /** `0` when the node is hidden from `svg` or its primitive declares no `metrics.svgHeight`. */\n estimateSvgHeight(node: TNode): number;\n /**\n * Appends schema failures under `path` to `errors`, each naming `node`'s\n * type, and an unknown key on `node` listing the fields it declares.\n */\n validate(node: TNode, path: string, errors: ValidationError[]): void;\n}\n\n/**\n * Which of a definition's renderers each surface dispatches to.\n *\n * `svg` maps to `react`: an image backend lays out the pack's DOM tree and\n * stylesheet, so the two surfaces differ only in visibility and in what\n * surrounds them.\n */\nconst RENDERER_FOR_SURFACE = {\n react: 'react',\n svg: 'react',\n text: 'text',\n markdown: 'markdown',\n slack: 'slack',\n} as const satisfies Record<\n SurfaceName,\n keyof AnyPrimitiveDefinition['renderers']\n>;\n\n/** Runtime facts {@link createPrimitiveDispatcher} cannot derive from the inventory. */\nexport interface PrimitiveDispatcherOptions {\n /** Names this dispatcher in error messages. */\n label?: string;\n /**\n * Which node types render as a picture rather than as text, so the Slack\n * surface uploads an image instead of degrading to markdown. Supplied by the\n * runtime from each pack's `slackAssetTypes`.\n */\n isSlackAssetType?: ((type: string) => boolean) | undefined;\n}\n\n/**\n * Flattens `definitions` into one type-keyed inventory.\n *\n * Throws on a duplicate `type`: two packs claiming one node type has no correct\n * resolution, so it fails at construction rather than resolving arbitrarily per\n * render.\n */\nexport const createPrimitiveDispatcher = <\n TNode extends PrimitiveNode,\n T extends PackTypes = DefaultPackTypes,\n>(\n definitions: readonly AnyPrimitiveDefinition[],\n options: PrimitiveDispatcherOptions = {}\n): PrimitiveDispatcher<TNode, T> => {\n const { isSlackAssetType } = options;\n const formatIssues = createNodeIssueFormatter(definitions);\n const byType = new Map<string, AnyPrimitiveDefinition>();\n for (const definition of definitions) {\n const existing = byType.get(definition.type);\n if (existing) {\n const label = options.label ? `${options.label}: ` : '';\n throw new IsomerError(\n 'DUPLICATE_PRIMITIVE_TYPE',\n `${label}duplicate primitive type \"${definition.type}\" registered`\n );\n }\n byType.set(definition.type, definition);\n }\n\n const getDefinition = (node: TNode): AnyPrimitiveDefinition => {\n const definition = byType.get(node.type);\n if (!definition) {\n throw new IsomerError(\n 'UNKNOWN_PRIMITIVE_TYPE',\n `Unknown primitive type \"${node.type}\"`\n );\n }\n return definition;\n };\n\n const sanitizeNode = (\n definition: AnyPrimitiveDefinition,\n node: TNode\n ): TNode | null => {\n const sanitize = definition.sanitize as\n ((node: TNode) => TNode | null) | undefined;\n return sanitize ? sanitize(node) : node;\n };\n\n // Assigned after helpers that close over this binding.\n // eslint-disable-next-line prefer-const\n let self: PrimitiveDispatcher<TNode, T>;\n\n const scope = (): RenderScope<T> => self;\n\n const renderOn = <S extends SurfaceName>(\n surface: S,\n node: TNode,\n extras: Omit<SurfaceMap<T>[S]['env'], 'scope'>\n ): SurfaceMap<T>[S]['output'] | undefined => {\n if (!isVisibleOnSurface(node, surface)) {\n return undefined;\n }\n const definition = getDefinition(node);\n const safeNode = sanitizeNode(definition, node);\n return safeNode === null\n ? undefined\n : renderSanitized(surface, definition, safeNode, extras);\n };\n\n // `renderOn` for a node its sanitizer has already run on.\n const renderSanitized = <S extends SurfaceName>(\n surface: S,\n definition: AnyPrimitiveDefinition,\n safeNode: TNode,\n extras: Omit<SurfaceMap<T>[S]['env'], 'scope'>\n ): SurfaceMap<T>[S]['output'] | undefined => {\n const renderer = definition.renderers[RENDERER_FOR_SURFACE[surface]] as\n Renderer<TNode, S, T> | undefined;\n return renderer?.(safeNode, { ...extras, scope: scope() });\n };\n\n const renderMarkdown = (node: TNode): string => {\n const rendered = renderOn('markdown', node, {}) ?? '';\n return typeof rendered === 'string'\n ? rendered\n : serializeMarkdown(rendered);\n };\n\n const renderMarkdownContent = (node: TNode): MarkdownContent => {\n const rendered = renderOn('markdown', node, {}) ?? '';\n return typeof rendered === 'string'\n ? markdownFromString(rendered)\n : rendered;\n };\n\n self = {\n definitions,\n getDefinition,\n renderOn,\n // `{}` only ever runs when the argument was optional, which\n // `ReactContextArg` allows exactly when `{}` is a complete context.\n renderReact: (node, ...context) =>\n renderOn('react', node, {\n context: context[0] ?? {},\n }),\n collectStyles: (node, styles, context) => {\n const definition = getDefinition(node);\n const collectStyles = definition.collectStyles as\n | ((\n node: TNode,\n env: {\n styles: T['collector'];\n context: PrimitiveStyleCollectionContext;\n }\n ) => void)\n | undefined;\n collectStyles?.(node, { styles, context });\n },\n renderSvg: (node, context, theme, key) => {\n const rendered = renderOn('svg', node, { context, theme });\n // The frame hands these to React as an array; a `key` has to sit on the\n // element itself, since wrapping in a Fragment would hide the single\n // root an image backend lays out from.\n return isValidElement(rendered)\n ? cloneElement(rendered, { key })\n : rendered;\n },\n renderText: (node) => renderOn('text', node, {}) ?? '',\n renderMarkdown,\n renderMarkdownContent,\n renderSlack: (node, collector) => {\n if (!isVisibleOnSurface(node, 'slack')) {\n return [];\n }\n const assets = isSlackAssetCollector(collector) ? collector : undefined;\n if (assets && isSlackAssetType?.(node.type)) {\n const definition = getDefinition(node);\n const safeNode = sanitizeNode(definition, node);\n if (safeNode === null) {\n return [];\n }\n const altText =\n (isVisibleOnSurface(safeNode, 'text') &&\n renderSanitized('text', definition, safeNode, {})) ||\n node.type;\n const ref = assets.allocate(safeNode, altText);\n const block = {\n type: 'image',\n alt_text: altText,\n slack_file: { ref },\n } satisfies SlackImageBlock;\n return asSlackPayload<T['slackBlock']>([block]);\n }\n const definition = getDefinition(node);\n const slackRenderer = definition.renderers.slack as\n Renderer<TNode, 'slack', T> | undefined;\n if (!slackRenderer) {\n // Degrading here rather than in the envelope is what reaches nested\n // children: a container's `slack` renderer recurses through this\n // method, and the envelope only ever sees the container's own output.\n const markdown = renderOn('markdown', node, {}) ?? '';\n return asSlackPayload<T['slackBlock']>(\n typeof markdown === 'string'\n ? gfmToSlackBlocks(markdown)\n : markdownContentToSlackBlocks(markdown)\n );\n }\n const safeNode = sanitizeNode(definition, node);\n if (safeNode === null) {\n return [];\n }\n const rendered = slackRenderer(safeNode, {\n ...(collector === undefined ? {} : { collector }),\n scope: scope(),\n });\n return isReadonlyArray(rendered) ? rendered : [rendered];\n },\n estimateSvgHeight: (node) => {\n if (!isVisibleOnSurface(node, 'svg')) {\n return 0;\n }\n const svgHeight = getDefinition(node).metrics?.svgHeight as\n ((node: TNode) => number) | undefined;\n return svgHeight?.(node) ?? 0;\n },\n validate: (node, path, errors) => {\n const result = getDefinition(node).schema.safeParse(node, {\n reportInput: true,\n });\n if (!result.success) {\n errors.push(...formatIssues([[node, path]], result.error.issues, path));\n }\n },\n };\n return self;\n};\n\nconst isReadonlyArray = <T>(value: T | readonly T[]): value is readonly T[] =>\n Array.isArray(value);\n\n/** The picture swap needs a collector that can allocate; any other collector type skips it. */\nconst isSlackAssetCollector = (value: unknown): value is SlackAssetCollector =>\n typeof value === 'object' &&\n value !== null &&\n typeof (value as { allocate?: unknown }).allocate === 'function';\n\n/** Rebinds checked Block Kit to the erased payload parameter. */\nconst asSlackPayload = <TSlackBlock>(\n blocks: readonly SlackBlock[]\n): readonly TSlackBlock[] => blocks as unknown as readonly TSlackBlock[];\n"]}
|
|
1
|
+
{"version":3,"file":"primitive_dispatch.js","sourceRoot":"","sources":["../../src/render/primitive_dispatch.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,YAAY,EAAE,cAAc,EAAkB,MAAM,OAAO,CAAC;AAErE,OAAO,EACL,kBAAkB,GAEnB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,C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,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;YAC1E,CAAC;QACH,CAAC;KACF,CAAC;IACF,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEF,MAAM,eAAe,GAAG,CAAI,KAAuB,EAAyB,EAAE,CAC5E,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAEvB,+FAA+F;AAC/F,MAAM,qBAAqB,GAAG,CAAC,KAAc,EAAgC,EAAE,CAC7E,OAAO,KAAK,KAAK,QAAQ;IACzB,KAAK,KAAK,IAAI;IACd,OAAQ,KAAgC,CAAC,QAAQ,KAAK,UAAU,CAAC;AAEnE,iEAAiE;AACjE,MAAM,cAAc,GAAG,CACrB,MAA6B,EACL,EAAE,CAAC,MAA2C,CAAC","sourcesContent":["/*\n * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one\n * or more contributor license agreements. Licensed under the Elastic License\n * 2.0; you may not use this file except in compliance with the Elastic License\n * 2.0.\n */\n\nimport { cloneElement, isValidElement, type ReactNode } from 'react';\n\nimport {\n isVisibleOnSurface,\n type SurfaceName,\n} from '../composition/body_node_base';\nimport { IsomerError } from '../composition/error';\nimport type { ValidationError } from '../composition/validation_error';\nimport type { MarkdownContent } from '../define/markdown_content';\nimport {\n type AnyPrimitiveDefinition,\n type DefaultPackTypes,\n type PackTypes,\n type PrimitiveNode,\n type PrimitiveStyleCollectionContext,\n type ReactContextArg,\n type Renderer,\n type RenderScope,\n type SurfaceMap,\n} from '../define/primitive_module';\nimport { createNodeIssueFormatter } from '../validate/node_issues';\n\nimport { serializeMarkdown } from './markdown/builder';\nimport type { SlackAssetCollector } from './slack/assets';\nimport type { SlackBlock, SlackImageBlock } from './slack/blocks';\nimport { markdownContentToSlackBlocks } from './slack/markdown_content';\n\n/**\n * Renders a node on any surface by looking its `type` up in one flattened\n * inventory.\n *\n * The seam that makes packs composable: a container recurses through the\n * {@link RenderScope} it is passed rather than through its own pack, so a node\n * owned by another pack still renders.\n */\nexport interface PrimitiveDispatcher<\n TNode extends PrimitiveNode,\n T extends PackTypes = DefaultPackTypes,\n> {\n /** Every primitive this dispatcher can reach, across all composed packs. */\n readonly definitions: readonly AnyPrimitiveDefinition[];\n /** Throws for an unknown node type; a missing primitive is a bug, not a degradation. */\n getDefinition(node: TNode): AnyPrimitiveDefinition;\n /** `undefined` when the node is hidden from `surface`, `sanitize` drops it, or the primitive declares no renderer for `surface`. */\n renderOn<S extends SurfaceName>(\n surface: S,\n node: TNode,\n extras: Omit<SurfaceMap<T>[S]['env'], 'scope'>\n ): SurfaceMap<T>[S]['output'] | undefined;\n /** The node's `react` renderer, or `undefined` when it is hidden or has none. `context` may be omitted only when `{}` is a complete context for the pack. */\n renderReact(\n node: TNode,\n ...context: ReactContextArg<T['context']>\n ): ReactNode;\n /** Mutates `styles` with this node's contribution. */\n collectStyles(\n node: TNode,\n styles: T['collector'],\n context: PrimitiveStyleCollectionContext\n ): void;\n /**\n * The node's `react` renderer, gated on `snapshot` visibility and keyed for a\n * frame's sibling list. `key` must be unique among those siblings.\n * `theme` is the frame's resolved palette for this render, or `undefined`\n * outside a frame.\n */\n renderSnapshot(\n node: TNode,\n context: T['context'],\n theme: T['theme'] | undefined,\n key: string\n ): ReactNode;\n /** `''` when the node is hidden from `text` or its primitive declares no `text` renderer. */\n renderText(node: TNode): string;\n /** The node's content serialized as GFM. */\n renderMarkdown(node: TNode): string;\n /** Empty when the node is hidden from `markdown`. */\n renderMarkdownContent(node: TNode): MarkdownContent;\n /**\n * Empty when the node is hidden from `slack`, `sanitize` drops it, or its\n * renderer returns nothing. A node with no `slack` renderer degrades through\n * its markdown; one `isSlackAssetType` names becomes an `image` block when\n * `collector` can allocate.\n */\n renderSlack(\n node: TNode,\n collector?: T['slackCollector']\n ): readonly T['slackBlock'][];\n /** `0` when the node is hidden from `snapshot` or its primitive declares no `metrics.snapshotHeight`. */\n estimateSnapshotHeight(node: TNode): number;\n /**\n * Appends schema failures under `path` to `errors`, each naming `node`'s\n * type, and an unknown key on `node` listing the fields it declares.\n */\n validate(node: TNode, path: string, errors: ValidationError[]): void;\n}\n\n/**\n * Which of a definition's renderers each surface dispatches to.\n *\n * `snapshot` maps to `react`: a rasterizer lays out the pack's DOM tree and\n * stylesheet, so the two surfaces differ only in visibility and in what\n * surrounds them.\n */\nconst RENDERER_FOR_SURFACE = {\n react: 'react',\n snapshot: 'react',\n text: 'text',\n markdown: 'markdown',\n slack: 'slack',\n} as const satisfies Record<\n SurfaceName,\n keyof AnyPrimitiveDefinition['renderers']\n>;\n\n/** Runtime facts {@link createPrimitiveDispatcher} cannot derive from the inventory. */\nexport interface PrimitiveDispatcherOptions {\n /** Names this dispatcher in error messages. */\n label?: string;\n /**\n * Which node types render as a picture rather than as text, so the Slack\n * surface uploads an image instead of degrading to markdown. Supplied by the\n * runtime from each pack's `slackAssetTypes`.\n */\n isSlackAssetType?: ((type: string) => boolean) | undefined;\n}\n\n/**\n * Flattens `definitions` into one type-keyed inventory.\n *\n * Throws on a duplicate `type`: two packs claiming one node type has no correct\n * resolution, so it fails at construction rather than resolving arbitrarily per\n * render.\n */\nexport const createPrimitiveDispatcher = <\n TNode extends PrimitiveNode,\n T extends PackTypes = DefaultPackTypes,\n>(\n definitions: readonly AnyPrimitiveDefinition[],\n options: PrimitiveDispatcherOptions = {}\n): PrimitiveDispatcher<TNode, T> => {\n const { isSlackAssetType } = options;\n const formatIssues = createNodeIssueFormatter(definitions);\n const byType = new Map<string, AnyPrimitiveDefinition>();\n for (const definition of definitions) {\n const existing = byType.get(definition.type);\n if (existing) {\n const label = options.label ? `${options.label}: ` : '';\n throw new IsomerError(\n 'DUPLICATE_PRIMITIVE_TYPE',\n `${label}duplicate primitive type \"${definition.type}\" registered`\n );\n }\n byType.set(definition.type, definition);\n }\n\n const getDefinition = (node: TNode): AnyPrimitiveDefinition => {\n const definition = byType.get(node.type);\n if (!definition) {\n throw new IsomerError(\n 'UNKNOWN_PRIMITIVE_TYPE',\n `Unknown primitive type \"${node.type}\"`\n );\n }\n return definition;\n };\n\n const sanitizeNode = (\n definition: AnyPrimitiveDefinition,\n node: TNode\n ): TNode | null => {\n const sanitize = definition.sanitize as\n ((node: TNode) => TNode | null) | undefined;\n return sanitize ? sanitize(node) : node;\n };\n\n // Assigned after helpers that close over this binding.\n // eslint-disable-next-line prefer-const\n let self: PrimitiveDispatcher<TNode, T>;\n\n const scope = (): RenderScope<T> => self;\n\n const renderOn = <S extends SurfaceName>(\n surface: S,\n node: TNode,\n extras: Omit<SurfaceMap<T>[S]['env'], 'scope'>\n ): SurfaceMap<T>[S]['output'] | undefined => {\n if (!isVisibleOnSurface(node, surface)) {\n return undefined;\n }\n const definition = getDefinition(node);\n const safeNode = sanitizeNode(definition, node);\n return safeNode === null\n ? undefined\n : renderSanitized(surface, definition, safeNode, extras);\n };\n\n // `renderOn` for a node its sanitizer has already run on.\n const renderSanitized = <S extends SurfaceName>(\n surface: S,\n definition: AnyPrimitiveDefinition,\n safeNode: TNode,\n extras: Omit<SurfaceMap<T>[S]['env'], 'scope'>\n ): SurfaceMap<T>[S]['output'] | undefined => {\n const renderer = definition.renderers[RENDERER_FOR_SURFACE[surface]] as\n Renderer<TNode, S, T> | undefined;\n return renderer?.(safeNode, { ...extras, scope: scope() });\n };\n\n const renderMarkdownContent = (node: TNode): MarkdownContent =>\n renderOn('markdown', node, {}) ?? [];\n\n const renderMarkdown = (node: TNode): string =>\n serializeMarkdown(renderMarkdownContent(node));\n\n self = {\n definitions,\n getDefinition,\n renderOn,\n // `{}` only ever runs when the argument was optional, which\n // `ReactContextArg` allows exactly when `{}` is a complete context.\n renderReact: (node, ...context) =>\n renderOn('react', node, {\n context: context[0] ?? {},\n }),\n collectStyles: (node, styles, context) => {\n const definition = getDefinition(node);\n const collectStyles = definition.collectStyles as\n | ((\n node: TNode,\n env: {\n styles: T['collector'];\n context: PrimitiveStyleCollectionContext;\n }\n ) => void)\n | undefined;\n collectStyles?.(node, { styles, context });\n },\n renderSnapshot: (node, context, theme, key) => {\n const rendered = renderOn('snapshot', node, { context, theme });\n // The frame hands these to React as an array; a `key` has to sit on the\n // element itself, since wrapping in a Fragment would hide the single\n // root an image backend lays out from.\n return isValidElement(rendered)\n ? cloneElement(rendered, { key })\n : rendered;\n },\n renderText: (node) => renderOn('text', node, {}) ?? '',\n renderMarkdown,\n renderMarkdownContent,\n renderSlack: (node, collector) => {\n if (!isVisibleOnSurface(node, 'slack')) {\n return [];\n }\n const assets = isSlackAssetCollector(collector) ? collector : undefined;\n if (assets && isSlackAssetType?.(node.type)) {\n const definition = getDefinition(node);\n const safeNode = sanitizeNode(definition, node);\n if (safeNode === null) {\n return [];\n }\n const altText =\n (isVisibleOnSurface(safeNode, 'text') &&\n renderSanitized('text', definition, safeNode, {})) ||\n node.type;\n const ref = assets.allocate(safeNode, altText);\n const block = {\n type: 'image',\n alt_text: altText,\n slack_file: { ref },\n } satisfies SlackImageBlock;\n return asSlackPayload<T['slackBlock']>([block]);\n }\n const definition = getDefinition(node);\n const slackRenderer = definition.renderers.slack as\n Renderer<TNode, 'slack', T> | undefined;\n if (!slackRenderer) {\n // Degrading here rather than in the envelope is what reaches nested\n // children: a container's `slack` renderer recurses through this\n // method, and the envelope only ever sees the container's own output.\n return asSlackPayload<T['slackBlock']>(\n markdownContentToSlackBlocks(renderMarkdownContent(node))\n );\n }\n const safeNode = sanitizeNode(definition, node);\n if (safeNode === null) {\n return [];\n }\n const rendered = slackRenderer(safeNode, {\n ...(collector === undefined ? {} : { collector }),\n scope: scope(),\n });\n return isReadonlyArray(rendered) ? rendered : [rendered];\n },\n estimateSnapshotHeight: (node) => {\n if (!isVisibleOnSurface(node, 'snapshot')) {\n return 0;\n }\n const snapshotHeight = getDefinition(node).metrics?.snapshotHeight as\n ((node: TNode) => number) | undefined;\n return snapshotHeight?.(node) ?? 0;\n },\n validate: (node, path, errors) => {\n const result = getDefinition(node).schema.safeParse(node, {\n reportInput: true,\n });\n if (!result.success) {\n errors.push(...formatIssues([[node, path]], result.error.issues, path));\n }\n },\n };\n return self;\n};\n\nconst isReadonlyArray = <T>(value: T | readonly T[]): value is readonly T[] =>\n Array.isArray(value);\n\n/** The picture swap needs a collector that can allocate; any other collector type skips it. */\nconst isSlackAssetCollector = (value: unknown): value is SlackAssetCollector =>\n typeof value === 'object' &&\n value !== null &&\n typeof (value as { allocate?: unknown }).allocate === 'function';\n\n/** Rebinds checked Block Kit to the erased payload parameter. */\nconst asSlackPayload = <TSlackBlock>(\n blocks: readonly SlackBlock[]\n): readonly TSlackBlock[] => blocks as unknown as readonly TSlackBlock[];\n"]}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { type SlackBlock } from './blocks.js';
|
|
2
1
|
/**
|
|
3
2
|
* Escapes the three characters Slack treats as HTML in `mrkdwn` text.
|
|
4
3
|
*
|
|
@@ -68,28 +67,4 @@ export declare const cutSlackValue: (value: string, max: number) => string;
|
|
|
68
67
|
* text is `plain_text`, so no markdown survives — do not pre-format it.
|
|
69
68
|
*/
|
|
70
69
|
export declare const formatHeaderText: (value: string) => string;
|
|
71
|
-
/**
|
|
72
|
-
* Translates GFM into one Slack `mrkdwn` string:
|
|
73
|
-
*
|
|
74
|
-
* - Bold `**X**` to `*X*`, links `[L](U)` to `<U|L>`.
|
|
75
|
-
* - ATX headings to bold, since Slack renders no headings.
|
|
76
|
-
* - Pipe tables into a fenced code block, mrkdwn having no table syntax — the
|
|
77
|
-
* monospace alignment is all that survives. Prefer
|
|
78
|
-
* {@link gfmToSlackBlocks} when a native `table` block is acceptable.
|
|
79
|
-
*
|
|
80
|
-
* Free text is HTML-escaped (`&` `<` `>`) per Slack's rules; code spans and
|
|
81
|
-
* fenced blocks pass through untouched.
|
|
82
|
-
*/
|
|
83
|
-
export declare const gfmToSlackMrkdwn: (gfm: string) => string;
|
|
84
|
-
/**
|
|
85
|
-
* Translates GFM into structural Block Kit: prose runs as `section`, fenced
|
|
86
|
-
* code as `rich_text_preformatted`, pipe tables as a native `table` block.
|
|
87
|
-
*
|
|
88
|
-
* The markdown fallback for a primitive without a `slack` renderer whose
|
|
89
|
-
* markdown is a string; builder content goes through
|
|
90
|
-
* `markdownContentToSlackBlocks`. Preferred over {@link gfmToSlackMrkdwn},
|
|
91
|
-
* which collapses code and tables into
|
|
92
|
-
* a section-level fence that Slack and pixel-faithful previews render poorly.
|
|
93
|
-
*/
|
|
94
|
-
export declare const gfmToSlackBlocks: (gfm: string) => SlackBlock[];
|
|
95
70
|
//# sourceMappingURL=format.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../../src/render/slack/format.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../../src/render/slack/format.ts"],"names":[],"mappings":"AA4BA;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,GAAI,OAAO,MAAM,KAAG,MACuB,CAAC;AAErE,yDAAyD;AACzD,eAAO,MAAM,IAAI,GAAI,MAAM,MAAM,KAAG,MAAmC,CAAC;AACxE,2DAA2D;AAC3D,eAAO,MAAM,MAAM,GAAI,MAAM,MAAM,KAAG,MAAmC,CAAC;AAC1E,kEAAkE;AAClE,eAAO,MAAM,MAAM,GAAI,MAAM,MAAM,KAAG,MAAmC,CAAC;AAE1E;;;;GAIG;AACH,eAAO,MAAM,IAAI,GAAI,MAAM,MAAM,KAAG,MACG,CAAC;AAExC;;;;GAIG;AACH,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,KAAG,MACe,CAAC;AAgCzD;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAAI,KAAK,MAAM,KAAG,OAM/C,CAAC;AAmCF;;;;GAIG;AACH,eAAO,MAAM,YAAY,GAAI,MAAM,MAAM,KAAG,MAAM,GAAG,IAMpD,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,IAAI,GAAI,KAAK,MAAM,EAAE,QAAQ,MAAM,KAAG,MAUlD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,cAAc,GAAI,OAAO,MAAM,EAAE,KAAK,MAAM,KAAG,MAgB3D,CAAC;AAgBF;;;;GAIG;AACH,eAAO,MAAM,WAAW,GAAI,OAAO,MAAM,EAAE,KAAK,MAAM,KAAG,MAiBxD,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,aAAa,GAAI,OAAO,MAAM,EAAE,KAAK,MAAM,KAAG,MAG1D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,gBAAgB,GAAI,OAAO,MAAM,KAAG,MAI9C,CAAC"}
|