@elastic/isomer-sdk 0.2.0 → 0.4.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 (251) hide show
  1. package/NOTICE.txt +0 -286
  2. package/THIRD_PARTY_LICENSES.md +1 -885
  3. package/dist/author/authored_fields.d.ts +37 -12
  4. package/dist/author/authored_fields.d.ts.map +1 -1
  5. package/dist/author/authored_fields.js +45 -69
  6. package/dist/author/authored_fields.js.map +1 -1
  7. package/dist/author/authoring_input.d.ts.map +1 -1
  8. package/dist/author/authoring_input.js +12 -6
  9. package/dist/author/authoring_input.js.map +1 -1
  10. package/dist/author/authoring_model.d.ts +8 -2
  11. package/dist/author/authoring_model.d.ts.map +1 -1
  12. package/dist/author/authoring_model.js +67 -23
  13. package/dist/author/authoring_model.js.map +1 -1
  14. package/dist/author/declarations.d.ts.map +1 -1
  15. package/dist/author/declarations.js +45 -34
  16. package/dist/author/declarations.js.map +1 -1
  17. package/dist/author/index.d.ts +2 -1
  18. package/dist/author/index.d.ts.map +1 -1
  19. package/dist/author/index.js +2 -1
  20. package/dist/author/index.js.map +1 -1
  21. package/dist/author/jsx_shim.d.ts +49 -30
  22. package/dist/author/jsx_shim.d.ts.map +1 -1
  23. package/dist/author/jsx_shim.js +131 -40
  24. package/dist/author/jsx_shim.js.map +1 -1
  25. package/dist/author/print_jsx.d.ts +19 -0
  26. package/dist/author/print_jsx.d.ts.map +1 -0
  27. package/dist/author/print_jsx.js +372 -0
  28. package/dist/author/print_jsx.js.map +1 -0
  29. package/dist/author/prompt.d.ts +2 -2
  30. package/dist/author/prompt.d.ts.map +1 -1
  31. package/dist/author/prompt.js +7 -2
  32. package/dist/author/prompt.js.map +1 -1
  33. package/dist/author/schema_to_typescript.d.ts.map +1 -1
  34. package/dist/author/schema_to_typescript.js +6 -3
  35. package/dist/author/schema_to_typescript.js.map +1 -1
  36. package/dist/cjs/author/authored_fields.js +47 -70
  37. package/dist/cjs/author/authored_fields.js.map +1 -1
  38. package/dist/cjs/author/authoring_input.js +12 -6
  39. package/dist/cjs/author/authoring_input.js.map +1 -1
  40. package/dist/cjs/author/authoring_model.js +69 -24
  41. package/dist/cjs/author/authoring_model.js.map +1 -1
  42. package/dist/cjs/author/declarations.js +44 -33
  43. package/dist/cjs/author/declarations.js.map +1 -1
  44. package/dist/cjs/author/index.js +5 -1
  45. package/dist/cjs/author/index.js.map +1 -1
  46. package/dist/cjs/author/jsx_shim.js +130 -39
  47. package/dist/cjs/author/jsx_shim.js.map +1 -1
  48. package/dist/cjs/author/print_jsx.js +378 -0
  49. package/dist/cjs/author/print_jsx.js.map +1 -0
  50. package/dist/cjs/author/prompt.js +7 -2
  51. package/dist/cjs/author/prompt.js.map +1 -1
  52. package/dist/cjs/author/schema_to_typescript.js +6 -3
  53. package/dist/cjs/author/schema_to_typescript.js.map +1 -1
  54. package/dist/cjs/composition/error.js +2 -0
  55. package/dist/cjs/composition/error.js.map +1 -1
  56. package/dist/cjs/composition/structured_value.js.map +1 -1
  57. package/dist/cjs/define/index.js.map +1 -1
  58. package/dist/cjs/define/primitive_module.js +10 -4
  59. package/dist/cjs/define/primitive_module.js.map +1 -1
  60. package/dist/cjs/define/slack_blocks.js.map +1 -1
  61. package/dist/cjs/entries/index.js +5 -2
  62. package/dist/cjs/entries/index.js.map +1 -1
  63. package/dist/cjs/entries/markdown.js +3 -1
  64. package/dist/cjs/entries/markdown.js.map +1 -1
  65. package/dist/cjs/entries/react.js +2 -1
  66. package/dist/cjs/entries/react.js.map +1 -1
  67. package/dist/cjs/entries/slack.js +5 -1
  68. package/dist/cjs/entries/slack.js.map +1 -1
  69. package/dist/cjs/pack/enhancements.js.map +1 -1
  70. package/dist/cjs/pack/index.js +2 -1
  71. package/dist/cjs/pack/index.js.map +1 -1
  72. package/dist/cjs/pack/primitive_pack.js +14 -4
  73. package/dist/cjs/pack/primitive_pack.js.map +1 -1
  74. package/dist/cjs/render/format_number.js +2 -1
  75. package/dist/cjs/render/format_number.js.map +1 -1
  76. package/dist/cjs/render/html/envelope.js +12 -6
  77. package/dist/cjs/render/html/envelope.js.map +1 -1
  78. package/dist/cjs/render/markdown/blocks.js +82 -0
  79. package/dist/cjs/render/markdown/blocks.js.map +1 -0
  80. package/dist/cjs/render/markdown/builder.js +6 -2
  81. package/dist/cjs/render/markdown/builder.js.map +1 -1
  82. package/dist/cjs/render/markdown/format.js +46 -24
  83. package/dist/cjs/render/markdown/format.js.map +1 -1
  84. package/dist/cjs/render/markdown/host_tags.js +67 -0
  85. package/dist/cjs/render/markdown/host_tags.js.map +1 -0
  86. package/dist/cjs/render/markdown/index.js +5 -1
  87. package/dist/cjs/render/markdown/index.js.map +1 -1
  88. package/dist/cjs/render/markdown/split.js +325 -0
  89. package/dist/cjs/render/markdown/split.js.map +1 -0
  90. package/dist/cjs/render/primitive_dispatch.js +36 -6
  91. package/dist/cjs/render/primitive_dispatch.js.map +1 -1
  92. package/dist/cjs/render/react/content.js +3 -1
  93. package/dist/cjs/render/react/content.js.map +1 -1
  94. package/dist/cjs/render/react/index.js +3 -1
  95. package/dist/cjs/render/react/index.js.map +1 -1
  96. package/dist/cjs/render/react/markdown_content.js +143 -0
  97. package/dist/cjs/render/react/markdown_content.js.map +1 -0
  98. package/dist/cjs/render/slack/blocks.js +7 -0
  99. package/dist/cjs/render/slack/blocks.js.map +1 -1
  100. package/dist/cjs/render/slack/container.js +62 -0
  101. package/dist/cjs/render/slack/container.js.map +1 -0
  102. package/dist/cjs/render/slack/envelope.js +70 -14
  103. package/dist/cjs/render/slack/envelope.js.map +1 -1
  104. package/dist/cjs/render/slack/format.js +56 -6
  105. package/dist/cjs/render/slack/format.js.map +1 -1
  106. package/dist/cjs/render/slack/index.js +7 -1
  107. package/dist/cjs/render/slack/index.js.map +1 -1
  108. package/dist/cjs/render/slack/markdown_content.js +36 -77
  109. package/dist/cjs/render/slack/markdown_content.js.map +1 -1
  110. package/dist/cjs/render/slack/unmodeled.js +38 -0
  111. package/dist/cjs/render/slack/unmodeled.js.map +1 -0
  112. package/dist/cjs/testing/conformance.js +15 -1
  113. package/dist/cjs/testing/conformance.js.map +1 -1
  114. package/dist/cjs/testing/index.js +2 -1
  115. package/dist/cjs/testing/index.js.map +1 -1
  116. package/dist/cjs/validate/json_schema.js +10 -3
  117. package/dist/cjs/validate/json_schema.js.map +1 -1
  118. package/dist/cjs/validate/url.js +2 -1
  119. package/dist/cjs/validate/url.js.map +1 -1
  120. package/dist/composition/error.d.ts +2 -0
  121. package/dist/composition/error.d.ts.map +1 -1
  122. package/dist/composition/error.js +2 -0
  123. package/dist/composition/error.js.map +1 -1
  124. package/dist/composition/structured_value.d.ts +3 -3
  125. package/dist/composition/structured_value.d.ts.map +1 -1
  126. package/dist/composition/structured_value.js.map +1 -1
  127. package/dist/define/index.d.ts +1 -1
  128. package/dist/define/index.d.ts.map +1 -1
  129. package/dist/define/index.js.map +1 -1
  130. package/dist/define/primitive_module.d.ts +14 -5
  131. package/dist/define/primitive_module.d.ts.map +1 -1
  132. package/dist/define/primitive_module.js +10 -4
  133. package/dist/define/primitive_module.js.map +1 -1
  134. package/dist/define/slack_blocks.d.ts +31 -1
  135. package/dist/define/slack_blocks.d.ts.map +1 -1
  136. package/dist/define/slack_blocks.js.map +1 -1
  137. package/dist/entries/index.d.ts +2 -2
  138. package/dist/entries/index.d.ts.map +1 -1
  139. package/dist/entries/index.js +2 -2
  140. package/dist/entries/index.js.map +1 -1
  141. package/dist/entries/markdown.d.ts +1 -1
  142. package/dist/entries/markdown.d.ts.map +1 -1
  143. package/dist/entries/markdown.js +1 -1
  144. package/dist/entries/markdown.js.map +1 -1
  145. package/dist/entries/react.d.ts +1 -1
  146. package/dist/entries/react.d.ts.map +1 -1
  147. package/dist/entries/react.js +1 -1
  148. package/dist/entries/react.js.map +1 -1
  149. package/dist/entries/slack.d.ts +1 -1
  150. package/dist/entries/slack.d.ts.map +1 -1
  151. package/dist/entries/slack.js +1 -1
  152. package/dist/entries/slack.js.map +1 -1
  153. package/dist/pack/enhancements.d.ts +4 -0
  154. package/dist/pack/enhancements.d.ts.map +1 -1
  155. package/dist/pack/enhancements.js.map +1 -1
  156. package/dist/pack/index.d.ts +1 -1
  157. package/dist/pack/index.d.ts.map +1 -1
  158. package/dist/pack/index.js +1 -1
  159. package/dist/pack/index.js.map +1 -1
  160. package/dist/pack/primitive_pack.d.ts +3 -1
  161. package/dist/pack/primitive_pack.d.ts.map +1 -1
  162. package/dist/pack/primitive_pack.js +13 -3
  163. package/dist/pack/primitive_pack.js.map +1 -1
  164. package/dist/render/format_number.d.ts.map +1 -1
  165. package/dist/render/format_number.js +2 -1
  166. package/dist/render/format_number.js.map +1 -1
  167. package/dist/render/html/envelope.js +12 -6
  168. package/dist/render/html/envelope.js.map +1 -1
  169. package/dist/render/markdown/blocks.d.ts +6 -0
  170. package/dist/render/markdown/blocks.d.ts.map +1 -0
  171. package/dist/render/markdown/blocks.js +77 -0
  172. package/dist/render/markdown/blocks.js.map +1 -0
  173. package/dist/render/markdown/builder.d.ts +3 -0
  174. package/dist/render/markdown/builder.d.ts.map +1 -1
  175. package/dist/render/markdown/builder.js +3 -1
  176. package/dist/render/markdown/builder.js.map +1 -1
  177. package/dist/render/markdown/format.d.ts +2 -0
  178. package/dist/render/markdown/format.d.ts.map +1 -1
  179. package/dist/render/markdown/format.js +42 -22
  180. package/dist/render/markdown/format.js.map +1 -1
  181. package/dist/render/markdown/host_tags.d.ts +14 -0
  182. package/dist/render/markdown/host_tags.d.ts.map +1 -0
  183. package/dist/render/markdown/host_tags.js +63 -0
  184. package/dist/render/markdown/host_tags.js.map +1 -0
  185. package/dist/render/markdown/index.d.ts +2 -0
  186. package/dist/render/markdown/index.d.ts.map +1 -1
  187. package/dist/render/markdown/index.js +2 -0
  188. package/dist/render/markdown/index.js.map +1 -1
  189. package/dist/render/markdown/split.d.ts +32 -0
  190. package/dist/render/markdown/split.d.ts.map +1 -0
  191. package/dist/render/markdown/split.js +321 -0
  192. package/dist/render/markdown/split.js.map +1 -0
  193. package/dist/render/primitive_dispatch.d.ts +4 -2
  194. package/dist/render/primitive_dispatch.d.ts.map +1 -1
  195. package/dist/render/primitive_dispatch.js +36 -6
  196. package/dist/render/primitive_dispatch.js.map +1 -1
  197. package/dist/render/react/content.d.ts +1 -1
  198. package/dist/render/react/content.d.ts.map +1 -1
  199. package/dist/render/react/content.js +3 -1
  200. package/dist/render/react/content.js.map +1 -1
  201. package/dist/render/react/index.d.ts +1 -0
  202. package/dist/render/react/index.d.ts.map +1 -1
  203. package/dist/render/react/index.js +1 -0
  204. package/dist/render/react/index.js.map +1 -1
  205. package/dist/render/react/markdown_content.d.ts +9 -0
  206. package/dist/render/react/markdown_content.d.ts.map +1 -0
  207. package/dist/render/react/markdown_content.js +139 -0
  208. package/dist/render/react/markdown_content.js.map +1 -0
  209. package/dist/render/slack/blocks.d.ts +8 -1
  210. package/dist/render/slack/blocks.d.ts.map +1 -1
  211. package/dist/render/slack/blocks.js +7 -0
  212. package/dist/render/slack/blocks.js.map +1 -1
  213. package/dist/render/slack/container.d.ts +25 -0
  214. package/dist/render/slack/container.d.ts.map +1 -0
  215. package/dist/render/slack/container.js +55 -0
  216. package/dist/render/slack/container.js.map +1 -0
  217. package/dist/render/slack/envelope.d.ts +6 -4
  218. package/dist/render/slack/envelope.d.ts.map +1 -1
  219. package/dist/render/slack/envelope.js +71 -15
  220. package/dist/render/slack/envelope.js.map +1 -1
  221. package/dist/render/slack/format.d.ts +15 -3
  222. package/dist/render/slack/format.d.ts.map +1 -1
  223. package/dist/render/slack/format.js +54 -5
  224. package/dist/render/slack/format.js.map +1 -1
  225. package/dist/render/slack/index.d.ts +4 -2
  226. package/dist/render/slack/index.d.ts.map +1 -1
  227. package/dist/render/slack/index.js +2 -0
  228. package/dist/render/slack/index.js.map +1 -1
  229. package/dist/render/slack/markdown_content.d.ts +3 -2
  230. package/dist/render/slack/markdown_content.d.ts.map +1 -1
  231. package/dist/render/slack/markdown_content.js +27 -68
  232. package/dist/render/slack/markdown_content.js.map +1 -1
  233. package/dist/render/slack/unmodeled.d.ts +16 -0
  234. package/dist/render/slack/unmodeled.d.ts.map +1 -0
  235. package/dist/render/slack/unmodeled.js +32 -0
  236. package/dist/render/slack/unmodeled.js.map +1 -0
  237. package/dist/testing/conformance.d.ts +3 -1
  238. package/dist/testing/conformance.d.ts.map +1 -1
  239. package/dist/testing/conformance.js +14 -0
  240. package/dist/testing/conformance.js.map +1 -1
  241. package/dist/testing/index.d.ts +1 -1
  242. package/dist/testing/index.d.ts.map +1 -1
  243. package/dist/testing/index.js +1 -1
  244. package/dist/testing/index.js.map +1 -1
  245. package/dist/validate/json_schema.d.ts.map +1 -1
  246. package/dist/validate/json_schema.js +10 -3
  247. package/dist/validate/json_schema.js.map +1 -1
  248. package/dist/validate/url.d.ts.map +1 -1
  249. package/dist/validate/url.js +2 -1
  250. package/dist/validate/url.js.map +1 -1
  251. package/package.json +2 -2
@@ -0,0 +1,325 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
4
+ * or more contributor license agreements. Licensed under the Elastic License
5
+ * 2.0; you may not use this file except in compliance with the Elastic License
6
+ * 2.0.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.splitAuthoredMarkdown = void 0;
10
+ const builder_1 = require("./builder");
11
+ const format_1 = require("./format");
12
+ const host_tags_1 = require("./host_tags");
13
+ // A sentinel keeps the tag's own `<` and `>` as its edges, so emphasis and
14
+ // other delimiters around it read exactly as they did around the tag, and
15
+ // holds private-use characters between them, which no Markdown construct
16
+ // reads, so it parses as text wherever the tag was written. Any already in
17
+ // the source, literal or as a character reference (the only other way text
18
+ // can decode to one), are replaced first, so the parsed text holds sentinels
19
+ // only where tags were.
20
+ const SENTINEL_RE = /<\uE000(\d+)\uE001>/g;
21
+ // In a link destination the parser strips the angle brackets.
22
+ const SENTINEL_ANYWHERE_RE = /<?\uE000(\d+)\uE001>?/g;
23
+ const RESERVED_RE = /[\uE000\uE001]|&#(?:[xX]0*[eE]00[01]|0*5734[45]);/g;
24
+ const sentinel = (index) => `<\uE000${index}\uE001>`;
25
+ const withoutReserved = (source) => source.replace(RESERVED_RE, (match) => match.length === 1 ? '\uFFFD' : '&#xFFFD;');
26
+ // Tags written identically get the same sentinel, so a reference label that
27
+ // holds one still matches a definition that holds the same. Sharing by
28
+ // exact text keeps each tag's attributes and spelling its own.
29
+ const substitute = (source, tags) => {
30
+ const first = new Map();
31
+ let out = '';
32
+ let cursor = 0;
33
+ tags.forEach((tag, index) => {
34
+ if (!first.has(tag.raw)) {
35
+ first.set(tag.raw, index);
36
+ }
37
+ out += source.slice(cursor, tag.index) + sentinel(first.get(tag.raw));
38
+ cursor = tag.index + tag.raw.length;
39
+ });
40
+ return out + source.slice(cursor);
41
+ };
42
+ // The GFM serializer's cost grows steeply with nesting depth, while the
43
+ // parse budget bounds breadth, not depth. Nothing authored nests this deep.
44
+ const MAX_DEPTH = 64;
45
+ const exceedsDepth = (nodes, depth = 1) => depth > MAX_DEPTH ||
46
+ nodes.some((node) => 'children' in node && exceedsDepth(node.children, depth + 1));
47
+ // The tag's node in the tree, until it is cut out again.
48
+ const HOST = 'isomerHostElement';
49
+ const isHost = (node) => node.type === HOST;
50
+ const hostTag = (node) => node.tag;
51
+ const element = ({ name, attributes }) => ({
52
+ type: 'element',
53
+ name,
54
+ attributes,
55
+ });
56
+ const markdown = (source) => source.trim() === ''
57
+ ? []
58
+ : [{ type: 'markdown', content: builder_1.md.authored(source), source }];
59
+ // A sentinel in a text node becomes a host node between the text around it.
60
+ // In any other string (code, alt, url, title, HTML) the tag text goes back. A
61
+ // footnote prints at the end of its segment, away from where it is cited, so
62
+ // a tag inside one is literal too.
63
+ const resolve = (node, tags, literal) => {
64
+ if (node.type === 'text' && !literal) {
65
+ const parts = [];
66
+ let cursor = 0;
67
+ for (const match of node.value.matchAll(SENTINEL_RE)) {
68
+ if (match.index > cursor) {
69
+ parts.push({
70
+ type: 'text',
71
+ value: node.value.slice(cursor, match.index),
72
+ });
73
+ }
74
+ parts.push({
75
+ type: HOST,
76
+ tag: tags[Number(match[1])],
77
+ });
78
+ cursor = match.index + match[0].length;
79
+ }
80
+ if (parts.length === 0) {
81
+ return [node];
82
+ }
83
+ if (cursor < node.value.length) {
84
+ parts.push({ type: 'text', value: node.value.slice(cursor) });
85
+ }
86
+ return parts;
87
+ }
88
+ const restored = { ...node };
89
+ for (const [key, value] of Object.entries(restored)) {
90
+ if (typeof value === 'string') {
91
+ restored[key] = value.replace(SENTINEL_ANYWHERE_RE, (_, index) => tags[Number(index)].raw);
92
+ }
93
+ }
94
+ if ('children' in node) {
95
+ restored.children = node.children.flatMap((child) => resolve(child, tags, literal || node.type === 'footnoteDefinition'));
96
+ }
97
+ return [restored];
98
+ };
99
+ // Depth-first, in document order.
100
+ const walk = (node, visit) => {
101
+ visit(node);
102
+ if ('children' in node) {
103
+ node.children.forEach((child) => walk(child, visit));
104
+ }
105
+ };
106
+ const PHRASING_PARENTS = new Set([
107
+ 'delete',
108
+ 'emphasis',
109
+ 'heading',
110
+ 'link',
111
+ 'linkReference',
112
+ 'paragraph',
113
+ 'strong',
114
+ 'tableCell',
115
+ ]);
116
+ // A wrapper the tag leaves empty would print its markers alone (`****` is a
117
+ // thematic break). Containers keep their place: an empty list item keeps the
118
+ // numbering of the items after it.
119
+ const DROPPED_WHEN_EMPTY = new Set([
120
+ 'delete',
121
+ 'emphasis',
122
+ 'heading',
123
+ 'link',
124
+ 'linkReference',
125
+ 'paragraph',
126
+ 'strong',
127
+ ]);
128
+ // Text either side of a removed tag joins, with one space if either side
129
+ // had one. The run's edges are trimmed, and a hard break left at an edge is
130
+ // dropped as `md.paragraph` drops it, since it would print as a bare `\\`.
131
+ const tidyPhrasing = (children) => {
132
+ const joined = [];
133
+ for (const child of children) {
134
+ const previous = joined[joined.length - 1];
135
+ if (previous?.type === 'text' && child.type === 'text') {
136
+ const left = previous.value.replace(/\s+$/, '');
137
+ const right = child.value.replace(/^\s+/, '');
138
+ const spaced = left !== previous.value || right !== child.value;
139
+ joined[joined.length - 1] = {
140
+ type: 'text',
141
+ value: left + (spaced ? ' ' : '') + right,
142
+ };
143
+ }
144
+ else {
145
+ joined.push(child);
146
+ }
147
+ }
148
+ const isEdge = (node) => node?.type === 'break' ||
149
+ (node?.type === 'text' && node.value.trim() === '');
150
+ while (isEdge(joined[0])) {
151
+ joined.shift();
152
+ }
153
+ while (isEdge(joined[joined.length - 1])) {
154
+ joined.pop();
155
+ }
156
+ const first = joined[0];
157
+ if (first?.type === 'text') {
158
+ joined[0] = { ...first, value: first.value.trimStart() };
159
+ }
160
+ const last = joined[joined.length - 1];
161
+ if (last?.type === 'text') {
162
+ joined[joined.length - 1] = { ...last, value: last.value.trimEnd() };
163
+ }
164
+ return joined;
165
+ };
166
+ // `node` without its host nodes, which are appended to `found` in document
167
+ // order; `[]` when nothing is left of it. Only a node a tag was removed
168
+ // from is tidied, so phrasing elsewhere in the block stays as written.
169
+ const strip = (node, found) => {
170
+ if (isHost(node)) {
171
+ found.push(hostTag(node));
172
+ return [];
173
+ }
174
+ if (!('children' in node)) {
175
+ return [node];
176
+ }
177
+ const before = found.length;
178
+ const stripped = node.children.flatMap((child) => strip(child, found));
179
+ if (found.length === before) {
180
+ return [node];
181
+ }
182
+ const children = PHRASING_PARENTS.has(node.type)
183
+ ? tidyPhrasing(stripped)
184
+ : stripped;
185
+ return children.length === 0 && DROPPED_WHEN_EMPTY.has(node.type)
186
+ ? []
187
+ : [{ ...node, children }];
188
+ };
189
+ // The first definition of each identifier, anywhere in the tree, as the
190
+ // parser resolved references.
191
+ const firstDefinitions = (blocks) => {
192
+ const definitions = new Map();
193
+ for (const block of blocks) {
194
+ walk(block, (node) => {
195
+ if (node.type === 'definition' && !definitions.has(node.identifier)) {
196
+ definitions.set(node.identifier, node);
197
+ }
198
+ });
199
+ }
200
+ return definitions;
201
+ };
202
+ // Each segment is a document of its own, so a definition another segment
203
+ // holds is copied ahead of the run that references it.
204
+ const printRun = (run, definitions) => {
205
+ const present = new Set();
206
+ const needed = new Map();
207
+ for (const block of run) {
208
+ walk(block, (node) => {
209
+ present.add(node);
210
+ if (node.type === 'linkReference' || node.type === 'imageReference') {
211
+ const definition = definitions.get(node.identifier);
212
+ if (definition) {
213
+ needed.set(node.identifier, definition);
214
+ }
215
+ }
216
+ });
217
+ }
218
+ const carried = [...needed.values()].filter((node) => !present.has(node));
219
+ return (0, builder_1.printParsed)([...carried, ...run]);
220
+ };
221
+ const splitBlocks = (blocks, tags) => {
222
+ const resolved = blocks.flatMap((block) => resolve(block, tags, false));
223
+ const definitions = firstDefinitions(resolved);
224
+ const segments = [];
225
+ let run = [];
226
+ const flush = () => {
227
+ if (run.length > 0) {
228
+ segments.push(...markdown(printRun(run, definitions)));
229
+ }
230
+ run = [];
231
+ };
232
+ // `block` without its tags joins the run; each tag ends the run after it.
233
+ const push = (block) => {
234
+ const found = [];
235
+ run.push(...strip(block, found));
236
+ for (const tag of found) {
237
+ flush();
238
+ segments.push(element(tag));
239
+ }
240
+ };
241
+ for (const block of resolved) {
242
+ if (block.type === 'definition') {
243
+ // Prints nothing where it stands; it is copied where it is referenced.
244
+ continue;
245
+ }
246
+ if (block.type !== 'paragraph' || !block.children.some(isHost)) {
247
+ push(block);
248
+ continue;
249
+ }
250
+ // A tag directly in a top-level paragraph splits it in place.
251
+ let piece = [];
252
+ const pushPiece = () => {
253
+ const children = tidyPhrasing(piece);
254
+ if (children.length > 0) {
255
+ push({ ...block, children });
256
+ }
257
+ piece = [];
258
+ };
259
+ for (const child of block.children) {
260
+ if (isHost(child)) {
261
+ pushPiece();
262
+ flush();
263
+ segments.push(element(hostTag(child)));
264
+ }
265
+ else {
266
+ piece.push(child);
267
+ }
268
+ }
269
+ pushPiece();
270
+ }
271
+ flush();
272
+ return segments;
273
+ };
274
+ // Past the parse budget there is no tree to consult, so the source is cut at
275
+ // each tag and every piece degrades to inert text, as `md.authored` degrades.
276
+ const cutLinearly = (source, tags) => {
277
+ const segments = [];
278
+ const push = (piece) => {
279
+ const text = piece.trim();
280
+ if (text !== '') {
281
+ segments.push({
282
+ type: 'markdown',
283
+ content: (0, builder_1.verbatim)((0, format_1.inert)(text), text),
284
+ source: text,
285
+ });
286
+ }
287
+ };
288
+ let cursor = 0;
289
+ for (const tag of tags) {
290
+ push(source.slice(cursor, tag.index));
291
+ segments.push(element(tag));
292
+ cursor = tag.index + tag.raw.length;
293
+ }
294
+ push(source.slice(cursor));
295
+ return segments;
296
+ };
297
+ /**
298
+ * Splits authored Markdown at the host's own elements, such as
299
+ * `<render_attachment id="…" />`, so the host can replace each with content
300
+ * of its own without cutting the raw string. The source is parsed once, so a
301
+ * tag in code, a link destination or title, an image's alt text, or raw HTML
302
+ * stays text, and a list, table, quote, or emphasis around a tag stays whole.
303
+ * Each `markdown` segment is sanitized as `md.authored` sanitizes; past the
304
+ * parse budget the source is cut at each tag into inert pieces.
305
+ *
306
+ * @example
307
+ * splitAuthoredMarkdown(message, { elements: ['render_attachment'] })
308
+ */
309
+ const splitAuthoredMarkdown = (source, { elements }) => {
310
+ const cleaned = withoutReserved(source);
311
+ const tags = (0, host_tags_1.findHostTags)(cleaned, elements);
312
+ if (tags.length === 0) {
313
+ return markdown(source);
314
+ }
315
+ // The budget applies to the source as written: a long attribute value
316
+ // shrinks to a sentinel, but comes back in full wherever it is literal.
317
+ const blocks = (0, format_1.exceedsParseBudget)(cleaned)
318
+ ? null
319
+ : (0, format_1.parseGfmBlocks)(substitute(cleaned, tags));
320
+ return blocks && !exceedsDepth(blocks)
321
+ ? splitBlocks(blocks, tags)
322
+ : cutLinearly(cleaned, tags);
323
+ };
324
+ exports.splitAuthoredMarkdown = splitAuthoredMarkdown;
325
+ //# sourceMappingURL=split.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"split.js","sourceRoot":"","sources":["../../../../src/render/markdown/split.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAaH,uCAAsD;AACtD,qCAAqE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,EAAE,QAAQ,EAAgC,EACf,EAAE;IAC7B,MAAM,OAAO,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC;IACxC,MAAM,IAAI,GAAG,IAAA,wBAAY,EAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IAC7C,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACtB,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC1B,CAAC;IACD,sEAAsE;IACtE,wEAAwE;IACxE,MAAM,MAAM,GAAG,IAAA,2BAAkB,EAAC,OAAO,CAAC;QACxC,CAAC,CAAC,IAAI;QACN,CAAC,CAAE,IAAA,uBAAc,EAAC,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,CACT,CAAC;IACrC,OAAO,MAAM,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC;QACpC,CAAC,CAAC,WAAW,CAAC,MAAM,EAAE,IAAI,CAAC;QAC3B,CAAC,CAAC,WAAW,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AACjC,CAAC,CAAC;AAlBW,QAAA,qBAAqB,yBAkBhC","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\n// Splits authored Markdown at a host's own tags without a second grammar:\n// each tag becomes a sentinel, the stock GFM parser parses the whole source\n// once, and where the sentinel lands says what the tag is. In a `text`\n// node it is a tag; anywhere else (a code span or block, an image's alt, a\n// link destination or title, raw HTML) it is literal and the tag text goes\n// back. Blocks that held a tag are then printed from the tree.\n\nimport type { Definition, Nodes, RootContent } from 'mdast';\n\nimport type { MarkdownContent } from '../../define/markdown_content';\n\nimport { md, printParsed, verbatim } from './builder';\nimport { exceedsParseBudget, inert, parseGfmBlocks } from './format';\nimport { findHostTags, type HostTag } from './host_tags';\n\n/** A piece of authored Markdown, or one of the host's elements cut out of it. */\nexport type AuthoredMarkdownSegment =\n | {\n type: 'markdown';\n content: MarkdownContent;\n /** The text `content` was built from, for a host that stores a string and passes it to `md.authored` later. */\n source: string;\n }\n | {\n type: 'element';\n /** The name as listed in {@link SplitAuthoredMarkdownOptions.elements}. */\n name: string;\n /** Attribute values as written, keyed by lower-cased name; a bare attribute is `''`. */\n attributes: Readonly<Record<string, string>>;\n };\n\nexport interface SplitAuthoredMarkdownOptions {\n /** Tag names to cut out, matched case-insensitively and whole. */\n readonly elements: readonly string[];\n}\n\n// A sentinel keeps the tag's own `<` and `>` as its edges, so emphasis and\n// other delimiters around it read exactly as they did around the tag, and\n// holds private-use characters between them, which no Markdown construct\n// reads, so it parses as text wherever the tag was written. Any already in\n// the source, literal or as a character reference (the only other way text\n// can decode to one), are replaced first, so the parsed text holds sentinels\n// only where tags were.\nconst SENTINEL_RE = /<\\uE000(\\d+)\\uE001>/g;\n// In a link destination the parser strips the angle brackets.\nconst SENTINEL_ANYWHERE_RE = /<?\\uE000(\\d+)\\uE001>?/g;\nconst RESERVED_RE = /[\\uE000\\uE001]|&#(?:[xX]0*[eE]00[01]|0*5734[45]);/g;\nconst sentinel = (index: number): string => `<\\uE000${index}\\uE001>`;\nconst withoutReserved = (source: string): string =>\n source.replace(RESERVED_RE, (match) =>\n match.length === 1 ? '\\uFFFD' : '&#xFFFD;'\n );\n\n// Tags written identically get the same sentinel, so a reference label that\n// holds one still matches a definition that holds the same. Sharing by\n// exact text keeps each tag's attributes and spelling its own.\nconst substitute = (source: string, tags: readonly HostTag[]): string => {\n const first = new Map<string, number>();\n let out = '';\n let cursor = 0;\n tags.forEach((tag, index) => {\n if (!first.has(tag.raw)) {\n first.set(tag.raw, index);\n }\n out += source.slice(cursor, tag.index) + sentinel(first.get(tag.raw)!);\n cursor = tag.index + tag.raw.length;\n });\n return out + source.slice(cursor);\n};\n\n// The GFM serializer's cost grows steeply with nesting depth, while the\n// parse budget bounds breadth, not depth. Nothing authored nests this deep.\nconst MAX_DEPTH = 64;\n\nconst exceedsDepth = (nodes: readonly Nodes[], depth = 1): boolean =>\n depth > MAX_DEPTH ||\n nodes.some(\n (node) => 'children' in node && exceedsDepth(node.children, depth + 1)\n );\n\n// The tag's node in the tree, until it is cut out again.\nconst HOST = 'isomerHostElement';\ninterface HostNode {\n type: typeof HOST;\n tag: HostTag;\n}\n\nconst isHost = (node: Nodes): boolean => (node.type as string) === HOST;\nconst hostTag = (node: Nodes): HostTag => (node as unknown as HostNode).tag;\n\nconst element = ({ name, attributes }: HostTag): AuthoredMarkdownSegment => ({\n type: 'element',\n name,\n attributes,\n});\n\nconst markdown = (source: string): AuthoredMarkdownSegment[] =>\n source.trim() === ''\n ? []\n : [{ type: 'markdown', content: md.authored(source), source }];\n\n// A sentinel in a text node becomes a host node between the text around it.\n// In any other string (code, alt, url, title, HTML) the tag text goes back. A\n// footnote prints at the end of its segment, away from where it is cited, so\n// a tag inside one is literal too.\nconst resolve = (\n node: Nodes,\n tags: readonly HostTag[],\n literal: boolean\n): Nodes[] => {\n if (node.type === 'text' && !literal) {\n const parts: Nodes[] = [];\n let cursor = 0;\n for (const match of node.value.matchAll(SENTINEL_RE)) {\n if (match.index > cursor) {\n parts.push({\n type: 'text',\n value: node.value.slice(cursor, match.index),\n });\n }\n parts.push({\n type: HOST,\n tag: tags[Number(match[1])]!,\n } as unknown as Nodes);\n cursor = match.index + match[0].length;\n }\n if (parts.length === 0) {\n return [node];\n }\n if (cursor < node.value.length) {\n parts.push({ type: 'text', value: node.value.slice(cursor) });\n }\n return parts;\n }\n const restored = { ...node } as Record<string, unknown>;\n for (const [key, value] of Object.entries(restored)) {\n if (typeof value === 'string') {\n restored[key] = value.replace(\n SENTINEL_ANYWHERE_RE,\n (_, index: string) => tags[Number(index)]!.raw\n );\n }\n }\n if ('children' in node) {\n restored.children = node.children.flatMap((child) =>\n resolve(child, tags, literal || node.type === 'footnoteDefinition')\n );\n }\n return [restored as unknown as Nodes];\n};\n\n// Depth-first, in document order.\nconst walk = (node: Nodes, visit: (node: Nodes) => void): void => {\n visit(node);\n if ('children' in node) {\n node.children.forEach((child) => walk(child, visit));\n }\n};\n\nconst PHRASING_PARENTS = new Set([\n 'delete',\n 'emphasis',\n 'heading',\n 'link',\n 'linkReference',\n 'paragraph',\n 'strong',\n 'tableCell',\n]);\n\n// A wrapper the tag leaves empty would print its markers alone (`****` is a\n// thematic break). Containers keep their place: an empty list item keeps the\n// numbering of the items after it.\nconst DROPPED_WHEN_EMPTY = new Set([\n 'delete',\n 'emphasis',\n 'heading',\n 'link',\n 'linkReference',\n 'paragraph',\n 'strong',\n]);\n\n// Text either side of a removed tag joins, with one space if either side\n// had one. The run's edges are trimmed, and a hard break left at an edge is\n// dropped as `md.paragraph` drops it, since it would print as a bare `\\\\`.\nconst tidyPhrasing = (children: Nodes[]): Nodes[] => {\n const joined: Nodes[] = [];\n for (const child of children) {\n const previous = joined[joined.length - 1];\n if (previous?.type === 'text' && child.type === 'text') {\n const left = previous.value.replace(/\\s+$/, '');\n const right = child.value.replace(/^\\s+/, '');\n const spaced = left !== previous.value || right !== child.value;\n joined[joined.length - 1] = {\n type: 'text',\n value: left + (spaced ? ' ' : '') + right,\n };\n } else {\n joined.push(child);\n }\n }\n const isEdge = (node: Nodes | undefined): boolean =>\n node?.type === 'break' ||\n (node?.type === 'text' && node.value.trim() === '');\n while (isEdge(joined[0])) {\n joined.shift();\n }\n while (isEdge(joined[joined.length - 1])) {\n joined.pop();\n }\n const first = joined[0];\n if (first?.type === 'text') {\n joined[0] = { ...first, value: first.value.trimStart() };\n }\n const last = joined[joined.length - 1];\n if (last?.type === 'text') {\n joined[joined.length - 1] = { ...last, value: last.value.trimEnd() };\n }\n return joined;\n};\n\n// `node` without its host nodes, which are appended to `found` in document\n// order; `[]` when nothing is left of it. Only a node a tag was removed\n// from is tidied, so phrasing elsewhere in the block stays as written.\nconst strip = (node: Nodes, found: HostTag[]): Nodes[] => {\n if (isHost(node)) {\n found.push(hostTag(node));\n return [];\n }\n if (!('children' in node)) {\n return [node];\n }\n const before = found.length;\n const stripped = node.children.flatMap((child) => strip(child, found));\n if (found.length === before) {\n return [node];\n }\n const children = PHRASING_PARENTS.has(node.type)\n ? tidyPhrasing(stripped)\n : stripped;\n return children.length === 0 && DROPPED_WHEN_EMPTY.has(node.type)\n ? []\n : [{ ...node, children } as Nodes];\n};\n\n// The first definition of each identifier, anywhere in the tree, as the\n// parser resolved references.\nconst firstDefinitions = (\n blocks: readonly Nodes[]\n): ReadonlyMap<string, Definition> => {\n const definitions = new Map<string, Definition>();\n for (const block of blocks) {\n walk(block, (node) => {\n if (node.type === 'definition' && !definitions.has(node.identifier)) {\n definitions.set(node.identifier, node);\n }\n });\n }\n return definitions;\n};\n\n// Each segment is a document of its own, so a definition another segment\n// holds is copied ahead of the run that references it.\nconst printRun = (\n run: readonly Nodes[],\n definitions: ReadonlyMap<string, Definition>\n): string => {\n const present = new Set<Nodes>();\n const needed = new Map<string, Definition>();\n for (const block of run) {\n walk(block, (node) => {\n present.add(node);\n if (node.type === 'linkReference' || node.type === 'imageReference') {\n const definition = definitions.get(node.identifier);\n if (definition) {\n needed.set(node.identifier, definition);\n }\n }\n });\n }\n const carried = [...needed.values()].filter((node) => !present.has(node));\n return printParsed([...carried, ...run]);\n};\n\nconst splitBlocks = (\n blocks: readonly Nodes[],\n tags: readonly HostTag[]\n): AuthoredMarkdownSegment[] => {\n const resolved = blocks.flatMap((block) => resolve(block, tags, false));\n const definitions = firstDefinitions(resolved);\n const segments: AuthoredMarkdownSegment[] = [];\n let run: Nodes[] = [];\n const flush = (): void => {\n if (run.length > 0) {\n segments.push(...markdown(printRun(run, definitions)));\n }\n run = [];\n };\n // `block` without its tags joins the run; each tag ends the run after it.\n const push = (block: Nodes): void => {\n const found: HostTag[] = [];\n run.push(...strip(block, found));\n for (const tag of found) {\n flush();\n segments.push(element(tag));\n }\n };\n for (const block of resolved) {\n if (block.type === 'definition') {\n // Prints nothing where it stands; it is copied where it is referenced.\n continue;\n }\n if (block.type !== 'paragraph' || !block.children.some(isHost)) {\n push(block);\n continue;\n }\n // A tag directly in a top-level paragraph splits it in place.\n let piece: Nodes[] = [];\n const pushPiece = (): void => {\n const children = tidyPhrasing(piece);\n if (children.length > 0) {\n push({ ...block, children } as Nodes);\n }\n piece = [];\n };\n for (const child of block.children) {\n if (isHost(child)) {\n pushPiece();\n flush();\n segments.push(element(hostTag(child)));\n } else {\n piece.push(child);\n }\n }\n pushPiece();\n }\n flush();\n return segments;\n};\n\n// Past the parse budget there is no tree to consult, so the source is cut at\n// each tag and every piece degrades to inert text, as `md.authored` degrades.\nconst cutLinearly = (\n source: string,\n tags: readonly HostTag[]\n): AuthoredMarkdownSegment[] => {\n const segments: AuthoredMarkdownSegment[] = [];\n const push = (piece: string): void => {\n const text = piece.trim();\n if (text !== '') {\n segments.push({\n type: 'markdown',\n content: verbatim(inert(text), text),\n source: text,\n });\n }\n };\n let cursor = 0;\n for (const tag of tags) {\n push(source.slice(cursor, tag.index));\n segments.push(element(tag));\n cursor = tag.index + tag.raw.length;\n }\n push(source.slice(cursor));\n return segments;\n};\n\n/**\n * Splits authored Markdown at the host's own elements, such as\n * `<render_attachment id=\"…\" />`, so the host can replace each with content\n * of its own without cutting the raw string. The source is parsed once, so a\n * tag in code, a link destination or title, an image's alt text, or raw HTML\n * stays text, and a list, table, quote, or emphasis around a tag stays whole.\n * Each `markdown` segment is sanitized as `md.authored` sanitizes; past the\n * parse budget the source is cut at each tag into inert pieces.\n *\n * @example\n * splitAuthoredMarkdown(message, { elements: ['render_attachment'] })\n */\nexport const splitAuthoredMarkdown = (\n source: string,\n { elements }: SplitAuthoredMarkdownOptions\n): AuthoredMarkdownSegment[] => {\n const cleaned = withoutReserved(source);\n const tags = findHostTags(cleaned, elements);\n if (tags.length === 0) {\n return markdown(source);\n }\n // The budget applies to the source as written: a long attribute value\n // shrinks to a sentinel, but comes back in full wherever it is literal.\n const blocks = exceedsParseBudget(cleaned)\n ? null\n : (parseGfmBlocks(substitute(cleaned, tags)) as\n readonly RootContent[] | null);\n return blocks && !exceedsDepth(blocks)\n ? splitBlocks(blocks, tags)\n : cutLinearly(cleaned, tags);\n};\n"]}
@@ -60,7 +60,30 @@ const createPrimitiveDispatcher = (definitions, options = {}) => {
60
60
  // Assigned after helpers that close over this binding.
61
61
  // eslint-disable-next-line prefer-const
62
62
  let self;
63
- const scope = () => self;
63
+ const scopeFor = (surface, extras) => {
64
+ const parent = self;
65
+ if (surface === 'slack') {
66
+ const { baseUrl } = extras;
67
+ return baseUrl === undefined
68
+ ? parent
69
+ : {
70
+ ...parent,
71
+ renderOn: (target, node, targetExtras) => parent.renderOn(target, node, target === 'slack' ? { baseUrl, ...targetExtras } : targetExtras),
72
+ renderSlack: (node, collector) => self.renderSlack(node, collector, { baseUrl }),
73
+ };
74
+ }
75
+ if (surface !== 'snapshot') {
76
+ return parent;
77
+ }
78
+ const { theme } = extras;
79
+ return {
80
+ ...parent,
81
+ renderReact: (node, ...context) => parent.renderOn('snapshot', node, {
82
+ context: context[0] ?? {},
83
+ theme,
84
+ }),
85
+ };
86
+ };
64
87
  const renderOn = (surface, node, extras) => {
65
88
  if (!(0, body_node_base_1.isVisibleOnSurface)(node, surface)) {
66
89
  return undefined;
@@ -74,7 +97,10 @@ const createPrimitiveDispatcher = (definitions, options = {}) => {
74
97
  // `renderOn` for a node its sanitizer has already run on.
75
98
  const renderSanitized = (surface, definition, safeNode, extras) => {
76
99
  const renderer = definition.renderers[RENDERER_FOR_SURFACE[surface]];
77
- return renderer?.(safeNode, { ...extras, scope: scope() });
100
+ return renderer?.(safeNode, {
101
+ ...extras,
102
+ scope: scopeFor(surface, extras),
103
+ });
78
104
  };
79
105
  const renderMarkdownContent = (node) => renderOn('markdown', node, {}) ?? [];
80
106
  const renderMarkdown = (node) => (0, builder_1.serializeMarkdown)(renderMarkdownContent(node));
@@ -104,7 +130,7 @@ const createPrimitiveDispatcher = (definitions, options = {}) => {
104
130
  renderText: (node) => renderOn('text', node, {}) ?? '',
105
131
  renderMarkdown,
106
132
  renderMarkdownContent,
107
- renderSlack: (node, collector) => {
133
+ renderSlack: (node, collector, options = {}) => {
108
134
  if (!(0, body_node_base_1.isVisibleOnSurface)(node, 'slack')) {
109
135
  return [];
110
136
  }
@@ -132,15 +158,19 @@ const createPrimitiveDispatcher = (definitions, options = {}) => {
132
158
  // Degrading here rather than in the envelope is what reaches nested
133
159
  // children: a container's `slack` renderer recurses through this
134
160
  // method, and the envelope only ever sees the container's own output.
135
- return asSlackPayload((0, markdown_content_1.markdownContentToSlackBlocks)(renderMarkdownContent(node)));
161
+ return asSlackPayload((0, markdown_content_1.markdownContentToSlackBlocks)(renderMarkdownContent(node), options));
136
162
  }
137
163
  const safeNode = sanitizeNode(definition, node);
138
164
  if (safeNode === null) {
139
165
  return [];
140
166
  }
141
- const rendered = slackRenderer(safeNode, {
167
+ const extras = {
142
168
  ...(collector === undefined ? {} : { collector }),
143
- scope: scope(),
169
+ ...(options.baseUrl === undefined ? {} : { baseUrl: options.baseUrl }),
170
+ };
171
+ const rendered = slackRenderer(safeNode, {
172
+ ...extras,
173
+ scope: scopeFor('slack', extras),
144
174
  });
145
175
  return isReadonlyArray(rendered) ? rendered : [rendered];
146
176
  },
@@ -1 +1 @@
1
- {"version":3,"file":"primitive_dispatch.js","sourceRoot":"","sources":["../../../src/render/primitive_dispatch.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,iCAAqE;AAErE,kEAGuC;AACvC,gDAAmD;AAcnD,yDAAmE;AAEnE,gDAAuD;AAGvD,+DAAwE;AAwExE;;;;;;GAMG;AACH,MAAM,oBAAoB,GAAG;IAC3B,KAAK,EAAE,OAAO;IACd,QAAQ,EAAE,OAAO;IACjB,IAAI,EAAE,MAAM;IACZ,QAAQ,EAAE,UAAU;IACpB,KAAK,EAAE,OAAO;CAIf,CAAC;AAcF;;;;;;GAMG;AACI,MAAM,yBAAyB,GAAG,CAIvC,WAA8C,EAC9C,UAAsC,EAAE,EACT,EAAE;IACjC,MAAM,EAAE,gB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C,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
+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;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;AA7NW,QAAA,yBAAyB,6BA6NpC;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 type { SlackLinkOptions } 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 `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. `options.baseUrl` reaches the renderer's env and\n * every child it renders through `scope`.\n */\n renderSlack(\n node: TNode,\n collector?: T['slackCollector'],\n options?: SlackLinkOptions\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 scopeFor = <S extends SurfaceName>(\n surface: S,\n extras: Omit<SurfaceMap<T>[S]['env'], 'scope'>\n ): RenderScope<T> => {\n const parent: RenderScope<T> = self;\n if (surface === 'slack') {\n const { baseUrl } = extras as Omit<\n SurfaceMap<T>['slack']['env'],\n 'scope'\n >;\n return baseUrl === undefined\n ? parent\n : {\n ...parent,\n renderOn: (target, node, targetExtras) =>\n parent.renderOn(\n target,\n node,\n target === 'slack' ? { baseUrl, ...targetExtras } : targetExtras\n ),\n renderSlack: (node, collector) =>\n self.renderSlack(node as TNode, collector, { baseUrl }),\n };\n }\n if (surface !== 'snapshot') {\n return parent;\n }\n const { theme } = extras as Omit<SurfaceMap<T>['snapshot']['env'], 'scope'>;\n return {\n ...parent,\n renderReact: (node, ...context) =>\n parent.renderOn('snapshot', node, {\n context: context[0] ?? {},\n theme,\n }),\n };\n };\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, {\n ...extras,\n scope: scopeFor(surface, extras),\n });\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, options = {}) => {\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), options)\n );\n }\n const safeNode = sanitizeNode(definition, node);\n if (safeNode === null) {\n return [];\n }\n const extras = {\n ...(collector === undefined ? {} : { collector }),\n ...(options.baseUrl === undefined ? {} : { baseUrl: options.baseUrl }),\n };\n const rendered = slackRenderer(safeNode, {\n ...extras,\n scope: scopeFor('slack', extras),\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"]}
@@ -38,7 +38,9 @@ const wrapCompositionContent = (content, composition, { framed = true, fluid = f
38
38
  .join(' '),
39
39
  role: 'group',
40
40
  'aria-label': composition.meta?.ariaLabel ?? composition.title ?? defaultAriaLabel,
41
- ...(theme === 'auto' ? {} : { 'data-theme': theme }),
41
+ ...(theme === 'auto'
42
+ ? {}
43
+ : { 'data-theme': theme, style: { colorScheme: theme } }),
42
44
  }, content);
43
45
  exports.wrapCompositionContent = wrapCompositionContent;
44
46
  //# sourceMappingURL=content.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"content.js","sourceRoot":"","sources":["../../../../src/render/react/content.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,iCAKe;AAyBf;;;GAGG;AACI,MAAM,wBAAwB,GAAG,CAItC,WAA+B,EAC/B,UAAmD,EACnD,OAAiB,EACjB,EAAE,OAAO,GAAG,IAAI,KAA0B,EAAE,EACjC,EAAE,CACb,IAAA,qBAAa,EACX,gBAAQ,EACR,IAAI,EACJ,GAAG;IACD,OAAO,IAAI,WAAW,CAAC,KAAK;QAC1B,CAAC,CAAC,IAAA,qBAAa,EAAC,IAAI,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,WAAW,CAAC,KAAK,CAAC;QAC1D,CAAC,CAAC,IAAI;IACR,OAAO,IAAI,WAAW,CAAC,QAAQ;QAC7B,CAAC,CAAC,IAAA,qBAAa,EACX,GAAG,EACH,EAAE,GAAG,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,EAAE,EACrC,WAAW,CAAC,QAAQ,CACrB;QACH,CAAC,CAAC,IAAI;IACR,GAAG,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CACtC,IAAA,qBAAa,EAAC,CAAA,uBAAwC,CAAA,EAAE;QACtD,GAAG,EAAE,QAAQ,KAAK,EAAE;QACpB,IAAI;QACJ,OAAO;QACP,UAAU;KACX,CAAC,CACH;CACF,CAAC,MAAM,CAAC,OAAO,CAAC,CAClB,CAAC;AAhCS,QAAA,wBAAwB,4BAgCjC;AAWJ,MAAM,uBAAuB,GAAG,CAAwC,EACtE,IAAI,EACJ,OAAO,EACP,UAAU,GACoC,EAAa,EAAE,CAC7D,UAAU,CAAC,WAAW,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAcxC;;;GAGG;AACI,MAAM,sBAAsB,GAAG,CACpC,OAAkB,EAClB,WAA+B,EAC/B,EACE,MAAM,GAAG,IAAI,EACb,KAAK,GAAG,KAAK,EACb,KAAK,GAAG,WAAW,CAAC,KAAK,IAAI,MAAM,EACnC,gBAAgB,GAAG,MAAM,MACI,EAAE,EACnB,EAAE,CAChB,IAAA,qBAAa,EACX,SAAS,EACT;IACE,SAAS,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;SAChE,MAAM,CAAC,OAAO,CAAC;SACf,IAAI,CAAC,GAAG,CAAC;IACZ,IAAI,EAAE,OAAO;IACb,YAAY,EACV,WAAW,CAAC,IAAI,EAAE,SAAS,IAAI,WAAW,CAAC,KAAK,IAAI,gBAAgB;IACtE,GAAG,CAAC,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,KAAK,EAAE,CAAC;CACrD,EACD,OAAO,CACR,CAAC;AAtBS,QAAA,sBAAsB,0BAsB/B","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 {\n createElement,\n Fragment,\n type ReactElement,\n type ReactNode,\n} from 'react';\n\nimport type { Composition } from '../../composition';\nimport type { RenderTheme } from '../../composition/named_color';\nimport type { PrimitiveNode, PrimitiveRenderContext } from '../../define';\n\n/** A pack's React renderer for one node of its own type. */\nexport interface ReactContentDispatcher<\n TNode extends PrimitiveNode,\n TContext = PrimitiveRenderContext,\n> {\n /** Must be side-effect free: the HTML surface calls it twice, once to collect styles and once to render. */\n renderReact(node: TNode, context?: TContext): ReactNode;\n}\n\n/** Options for {@link renderCompositionContent}. */\nexport interface ReactContentOptions {\n /**\n * Renders the composition's title and subtitle as the leading `h2` / `p.sub`.\n * Defaults to `true`. Pass `false` when the host already shows the title\n * itself; the title still backs the wrapper's `aria-label`.\n */\n heading?: boolean;\n}\n\n/**\n * A composition's heading and body as React nodes, without the surrounding\n * document or wrapper element.\n */\nexport const renderCompositionContent = <\n TNode extends PrimitiveNode,\n TContext = PrimitiveRenderContext,\n>(\n composition: Composition<TNode>,\n dispatcher: ReactContentDispatcher<TNode, TContext>,\n context: TContext,\n { heading = true }: ReactContentOptions = {}\n): ReactNode =>\n createElement(\n Fragment,\n null,\n ...[\n heading && composition.title\n ? createElement('h2', { key: 'title' }, composition.title)\n : null,\n heading && composition.subtitle\n ? createElement(\n 'p',\n { key: 'subtitle', className: 'sub' },\n composition.subtitle\n )\n : null,\n ...composition.body.map((node, index) =>\n createElement(RenderedCompositionNode<TNode, TContext>, {\n key: `body-${index}`,\n node,\n context,\n dispatcher,\n })\n ),\n ].filter(Boolean)\n );\n\ninterface RenderedCompositionNodeProps<\n TNode extends PrimitiveNode,\n TContext = PrimitiveRenderContext,\n> {\n node: TNode;\n context: TContext;\n dispatcher: ReactContentDispatcher<TNode, TContext>;\n}\n\nconst RenderedCompositionNode = <TNode extends PrimitiveNode, TContext>({\n node,\n context,\n dispatcher,\n}: RenderedCompositionNodeProps<TNode, TContext>): ReactNode =>\n dispatcher.renderReact(node, context);\n\n/** Options for {@link wrapCompositionContent}. */\nexport interface CompositionWrapperOptions {\n /** Adds the `framed` class. Defaults to `true`. */\n framed?: boolean;\n /** Adds the `fluid` class. */\n fluid?: boolean;\n /** Sets `data-theme`; defaults to the composition's `theme`, and `auto` sets nothing so the page's scheme applies. */\n theme?: RenderTheme;\n /** Used only when the composition has neither `meta.ariaLabel` nor a `title`. Defaults to `'View'`. */\n defaultAriaLabel?: string;\n}\n\n/**\n * The one `.isomer[.framed][.fluid]` `section` every surface wraps rendered\n * content in: the `html` surface's document wrapper and a React host's.\n */\nexport const wrapCompositionContent = <TNode extends PrimitiveNode>(\n content: ReactNode,\n composition: Composition<TNode>,\n {\n framed = true,\n fluid = false,\n theme = composition.theme ?? 'auto',\n defaultAriaLabel = 'View',\n }: CompositionWrapperOptions = {}\n): ReactElement =>\n createElement(\n 'section',\n {\n className: ['isomer', framed ? 'framed' : '', fluid ? 'fluid' : '']\n .filter(Boolean)\n .join(' '),\n role: 'group',\n 'aria-label':\n composition.meta?.ariaLabel ?? composition.title ?? defaultAriaLabel,\n ...(theme === 'auto' ? {} : { 'data-theme': theme }),\n },\n content\n );\n"]}
1
+ {"version":3,"file":"content.js","sourceRoot":"","sources":["../../../../src/render/react/content.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,iCAKe;AAyBf;;;GAGG;AACI,MAAM,wBAAwB,GAAG,CAItC,WAA+B,EAC/B,UAAmD,EACnD,OAAiB,EACjB,EAAE,OAAO,GAAG,IAAI,KAA0B,EAAE,EACjC,EAAE,CACb,IAAA,qBAAa,EACX,gBAAQ,EACR,IAAI,EACJ,GAAG;IACD,OAAO,IAAI,WAAW,CAAC,KAAK;QAC1B,CAAC,CAAC,IAAA,qBAAa,EAAC,IAAI,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,WAAW,CAAC,KAAK,CAAC;QAC1D,CAAC,CAAC,IAAI;IACR,OAAO,IAAI,WAAW,CAAC,QAAQ;QAC7B,CAAC,CAAC,IAAA,qBAAa,EACX,GAAG,EACH,EAAE,GAAG,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,EAAE,EACrC,WAAW,CAAC,QAAQ,CACrB;QACH,CAAC,CAAC,IAAI;IACR,GAAG,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IA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ensed 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 {\n createElement,\n Fragment,\n type ReactElement,\n type ReactNode,\n} from 'react';\n\nimport type { Composition } from '../../composition';\nimport type { RenderTheme } from '../../composition/named_color';\nimport type { PrimitiveNode, PrimitiveRenderContext } from '../../define';\n\n/** A pack's React renderer for one node of its own type. */\nexport interface ReactContentDispatcher<\n TNode extends PrimitiveNode,\n TContext = PrimitiveRenderContext,\n> {\n /** Must be side-effect free: the HTML surface calls it twice, once to collect styles and once to render. */\n renderReact(node: TNode, context?: TContext): ReactNode;\n}\n\n/** Options for {@link renderCompositionContent}. */\nexport interface ReactContentOptions {\n /**\n * Renders the composition's title and subtitle as the leading `h2` / `p.sub`.\n * Defaults to `true`. Pass `false` when the host already shows the title\n * itself; the title still backs the wrapper's `aria-label`.\n */\n heading?: boolean;\n}\n\n/**\n * A composition's heading and body as React nodes, without the surrounding\n * document or wrapper element.\n */\nexport const renderCompositionContent = <\n TNode extends PrimitiveNode,\n TContext = PrimitiveRenderContext,\n>(\n composition: Composition<TNode>,\n dispatcher: ReactContentDispatcher<TNode, TContext>,\n context: TContext,\n { heading = true }: ReactContentOptions = {}\n): ReactNode =>\n createElement(\n Fragment,\n null,\n ...[\n heading && composition.title\n ? createElement('h2', { key: 'title' }, composition.title)\n : null,\n heading && composition.subtitle\n ? createElement(\n 'p',\n { key: 'subtitle', className: 'sub' },\n composition.subtitle\n )\n : null,\n ...composition.body.map((node, index) =>\n createElement(RenderedCompositionNode<TNode, TContext>, {\n key: `body-${index}`,\n node,\n context,\n dispatcher,\n })\n ),\n ].filter(Boolean)\n );\n\ninterface RenderedCompositionNodeProps<\n TNode extends PrimitiveNode,\n TContext = PrimitiveRenderContext,\n> {\n node: TNode;\n context: TContext;\n dispatcher: ReactContentDispatcher<TNode, TContext>;\n}\n\nconst RenderedCompositionNode = <TNode extends PrimitiveNode, TContext>({\n node,\n context,\n dispatcher,\n}: RenderedCompositionNodeProps<TNode, TContext>): ReactNode =>\n dispatcher.renderReact(node, context);\n\n/** Options for {@link wrapCompositionContent}. */\nexport interface CompositionWrapperOptions {\n /** Adds the `framed` class. Defaults to `true`. */\n framed?: boolean;\n /** Adds the `fluid` class. */\n fluid?: boolean;\n /** Sets `data-theme` and `color-scheme`, which `light-dark()` resolves against; defaults to the composition's `theme`, and `auto` sets nothing so the page's scheme applies. */\n theme?: RenderTheme;\n /** Used only when the composition has neither `meta.ariaLabel` nor a `title`. Defaults to `'View'`. */\n defaultAriaLabel?: string;\n}\n\n/**\n * The one `.isomer[.framed][.fluid]` `section` every surface wraps rendered\n * content in: the `html` surface's document wrapper and a React host's.\n */\nexport const wrapCompositionContent = <TNode extends PrimitiveNode>(\n content: ReactNode,\n composition: Composition<TNode>,\n {\n framed = true,\n fluid = false,\n theme = composition.theme ?? 'auto',\n defaultAriaLabel = 'View',\n }: CompositionWrapperOptions = {}\n): ReactElement =>\n createElement(\n 'section',\n {\n className: ['isomer', framed ? 'framed' : '', fluid ? 'fluid' : '']\n .filter(Boolean)\n .join(' '),\n role: 'group',\n 'aria-label':\n composition.meta?.ariaLabel ?? composition.title ?? defaultAriaLabel,\n ...(theme === 'auto'\n ? {}\n : { 'data-theme': theme, style: { colorScheme: theme } }),\n },\n content\n );\n"]}
@@ -6,10 +6,12 @@
6
6
  * 2.0.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.applyEnhancements = exports.wrapCompositionContent = exports.renderCompositionContent = void 0;
9
+ exports.renderMarkdownContent = exports.applyEnhancements = exports.wrapCompositionContent = exports.renderCompositionContent = void 0;
10
10
  var content_1 = require("./content");
11
11
  Object.defineProperty(exports, "renderCompositionContent", { enumerable: true, get: function () { return content_1.renderCompositionContent; } });
12
12
  Object.defineProperty(exports, "wrapCompositionContent", { enumerable: true, get: function () { return content_1.wrapCompositionContent; } });
13
13
  var enhancements_1 = require("./enhancements");
14
14
  Object.defineProperty(exports, "applyEnhancements", { enumerable: true, get: function () { return enhancements_1.applyEnhancements; } });
15
+ var markdown_content_1 = require("./markdown_content");
16
+ Object.defineProperty(exports, "renderMarkdownContent", { enumerable: true, get: function () { return markdown_content_1.renderMarkdownContent; } });
15
17
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/render/react/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,qCAMmB;AAFjB,mHAAA,wBAAwB,OAAA;AACxB,iHAAA,sBAAsB,OAAA;AAExB,+CAAmD;AAA1C,iHAAA,iBAAiB,OAAA","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\nexport {\n type CompositionWrapperOptions,\n type ReactContentDispatcher,\n type ReactContentOptions,\n renderCompositionContent,\n wrapCompositionContent,\n} from './content';\nexport { applyEnhancements } from './enhancements';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/render/react/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,qCAMmB;AAFjB,mHAAA,wBAAwB,OAAA;AACxB,iHAAA,sBAAsB,OAAA;AAExB,+CAAmD;AAA1C,iHAAA,iBAAiB,OAAA;AAC1B,uDAA2D;AAAlD,yHAAA,qBAAqB,OAAA","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\nexport {\n type CompositionWrapperOptions,\n type ReactContentDispatcher,\n type ReactContentOptions,\n renderCompositionContent,\n wrapCompositionContent,\n} from './content';\nexport { applyEnhancements } from './enhancements';\nexport { renderMarkdownContent } from './markdown_content';\n"]}