payload-markdown 1.8.0 → 1.9.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 +16 -0
- package/dist/agent/access.d.ts +1 -1
- package/dist/agent/access.js.map +1 -1
- package/dist/agent/guide.d.ts +3 -0
- package/dist/agent/guide.js +28 -2
- package/dist/agent/guide.js.map +1 -1
- package/dist/agent/palettes.d.ts +55 -0
- package/dist/agent/palettes.js +194 -0
- package/dist/agent/palettes.js.map +1 -0
- package/dist/agent/skill.d.ts +2 -2
- package/dist/agent/skill.js +2 -2
- package/dist/agent/skill.js.map +1 -1
- package/dist/agent/validate.d.ts +1 -1
- package/dist/agent/validate.js +3 -0
- package/dist/agent/validate.js.map +1 -1
- package/dist/appearance/PaletteRowLabel.client.d.ts +2 -0
- package/dist/appearance/PaletteRowLabel.client.js +70 -0
- package/dist/appearance/PaletteRowLabel.client.js.map +1 -0
- package/dist/appearance/config.d.ts +8 -0
- package/dist/appearance/config.js +172 -0
- package/dist/appearance/config.js.map +1 -0
- package/dist/appearance/options.d.ts +11 -0
- package/dist/appearance/options.js +15 -0
- package/dist/appearance/options.js.map +1 -0
- package/dist/appearance/runtime.d.ts +25 -0
- package/dist/appearance/runtime.js +126 -0
- package/dist/appearance/runtime.js.map +1 -0
- package/dist/components/MarkdownRenderer/Component.js +4 -0
- package/dist/components/MarkdownRenderer/Component.js.map +1 -1
- package/dist/core/plugins/rehypeResolveIcons.js +10 -1
- package/dist/core/plugins/rehypeResolveIcons.js.map +1 -1
- package/dist/core/plugins/remarkCompileLayouts.js.map +1 -1
- package/dist/core/plugins/remarkDirectiveLines.d.ts +7 -5
- package/dist/core/plugins/remarkDirectiveLines.js +29 -18
- package/dist/core/plugins/remarkDirectiveLines.js.map +1 -1
- package/dist/core/plugins/remarkLayoutDirectives.js +1 -1
- package/dist/core/plugins/remarkLayoutDirectives.js.map +1 -1
- package/dist/core/plugins/remarkValidateDirectiveThemes.js +7 -14
- package/dist/core/plugins/remarkValidateDirectiveThemes.js.map +1 -1
- package/dist/core/plugins/remarkValidatePalettes.d.ts +9 -0
- package/dist/core/plugins/remarkValidatePalettes.js +51 -0
- package/dist/core/plugins/remarkValidatePalettes.js.map +1 -0
- package/dist/core/renderMarkdown.js +4 -221
- package/dist/core/renderMarkdown.js.map +1 -1
- package/dist/core/sanitizeSchema.d.ts +11 -0
- package/dist/core/sanitizeSchema.js +176 -0
- package/dist/core/sanitizeSchema.js.map +1 -0
- package/dist/directive-spec.json +60 -0
- package/dist/directives/definitions/badge.d.ts +5 -2
- package/dist/directives/definitions/badge.js +138 -10
- package/dist/directives/definitions/badge.js.map +1 -1
- package/dist/directives/definitions/badges.d.ts +1 -2
- package/dist/directives/definitions/badges.js +12 -3
- package/dist/directives/definitions/badges.js.map +1 -1
- package/dist/directives/definitions/button.d.ts +7 -3
- package/dist/directives/definitions/button.js +211 -14
- package/dist/directives/definitions/button.js.map +1 -1
- package/dist/directives/definitions/buttons.d.ts +1 -2
- package/dist/directives/definitions/buttons.js +12 -3
- package/dist/directives/definitions/buttons.js.map +1 -1
- package/dist/directives/definitions/callout.d.ts +1 -2
- package/dist/directives/definitions/callout.js +46 -5
- package/dist/directives/definitions/callout.js.map +1 -1
- package/dist/directives/definitions/card.d.ts +6 -4
- package/dist/directives/definitions/card.js +64 -11
- package/dist/directives/definitions/card.js.map +1 -1
- package/dist/directives/definitions/cards.d.ts +1 -2
- package/dist/directives/definitions/cards.js +49 -4
- package/dist/directives/definitions/cards.js.map +1 -1
- package/dist/directives/definitions/cell.d.ts +2 -2
- package/dist/directives/definitions/cell.js +9 -3
- package/dist/directives/definitions/cell.js.map +1 -1
- package/dist/directives/definitions/columns.d.ts +3 -4
- package/dist/directives/definitions/columns.js +17 -3
- package/dist/directives/definitions/columns.js.map +1 -1
- package/dist/directives/definitions/details.d.ts +1 -2
- package/dist/directives/definitions/details.js +28 -3
- package/dist/directives/definitions/details.js.map +1 -1
- package/dist/directives/definitions/section.d.ts +1 -2
- package/dist/directives/definitions/section.js +26 -3
- package/dist/directives/definitions/section.js.map +1 -1
- package/dist/directives/definitions/steps.d.ts +1 -2
- package/dist/directives/definitions/steps.js +26 -3
- package/dist/directives/definitions/steps.js.map +1 -1
- package/dist/directives/definitions/tab.d.ts +8 -2
- package/dist/directives/definitions/tab.js +45 -3
- package/dist/directives/definitions/tab.js.map +1 -1
- package/dist/directives/definitions/tabs.d.ts +1 -1
- package/dist/directives/definitions/tabs.js +44 -3
- package/dist/directives/definitions/tabs.js.map +1 -1
- package/dist/directives/definitions/toc.d.ts +1 -2
- package/dist/directives/definitions/toc.js +33 -5
- package/dist/directives/definitions/toc.js.map +1 -1
- package/dist/directives/diagnostics.d.ts +2 -10
- package/dist/directives/diagnostics.js +27 -141
- package/dist/directives/diagnostics.js.map +1 -1
- package/dist/directives/iconPlaceholder.d.ts +9 -3
- package/dist/directives/iconPlaceholder.js +8 -1
- package/dist/directives/iconPlaceholder.js.map +1 -1
- package/dist/directives/lint.d.ts +11 -0
- package/dist/directives/lint.js +33 -0
- package/dist/directives/lint.js.map +1 -0
- package/dist/directives/palette.d.ts +23 -0
- package/dist/directives/palette.js +40 -0
- package/dist/directives/palette.js.map +1 -0
- package/dist/directives/registry.d.ts +23 -5
- package/dist/directives/registry.js +52 -40
- package/dist/directives/registry.js.map +1 -1
- package/dist/directives/spec.d.ts +6 -2
- package/dist/directives/spec.js +29 -50
- package/dist/directives/spec.js.map +1 -1
- package/dist/directives/themeDiagnostics.d.ts +8 -0
- package/dist/directives/themeDiagnostics.js +21 -0
- package/dist/directives/themeDiagnostics.js.map +1 -0
- package/dist/directives/types.d.ts +157 -14
- package/dist/directives/types.js +6 -1
- package/dist/directives/types.js.map +1 -1
- package/dist/editor/directiveConfig.d.ts +15 -1
- package/dist/editor/directiveConfig.js +21 -1
- package/dist/editor/directiveConfig.js.map +1 -1
- package/dist/editor/directives/completions.d.ts +4 -2
- package/dist/editor/directives/completions.js +33 -7
- package/dist/editor/directives/completions.js.map +1 -1
- package/dist/editor/directives/diagnostics.d.ts +2 -2
- package/dist/editor/directives/diagnostics.js +10 -3
- package/dist/editor/directives/diagnostics.js.map +1 -1
- package/dist/editor/themes/payload.js +2 -1
- package/dist/editor/themes/payload.js.map +1 -1
- package/dist/exports/client.d.ts +4 -1
- package/dist/exports/client.js +5 -2
- package/dist/exports/client.js.map +1 -1
- package/dist/field/MarkdownField/Component.js +54 -3
- package/dist/field/MarkdownField/Component.js.map +1 -1
- package/dist/icons/entries.d.ts +22 -0
- package/dist/icons/entries.js +50 -0
- package/dist/icons/entries.js.map +1 -0
- package/dist/icons/resolve.d.ts +2 -1
- package/dist/icons/resolve.js +2 -2
- package/dist/icons/resolve.js.map +1 -1
- package/dist/icons/sanitizeSvg.d.ts +12 -2
- package/dist/icons/sanitizeSvg.js +104 -3
- package/dist/icons/sanitizeSvg.js.map +1 -1
- package/dist/index.d.ts +11 -4
- package/dist/index.js +34 -3
- package/dist/index.js.map +1 -1
- package/dist/mcp/index.d.ts +2 -2
- package/dist/mcp/index.js +65 -4
- package/dist/mcp/index.js.map +1 -1
- package/dist/palettes/colors.d.ts +21 -0
- package/dist/palettes/colors.js +43 -0
- package/dist/palettes/colors.js.map +1 -0
- package/dist/palettes/contrast.d.ts +7 -0
- package/dist/palettes/contrast.js +63 -0
- package/dist/palettes/contrast.js.map +1 -0
- package/dist/palettes/defaults.d.ts +8 -0
- package/dist/palettes/defaults.js +275 -0
- package/dist/palettes/defaults.js.map +1 -0
- package/dist/palettes/diagnostics.d.ts +10 -0
- package/dist/palettes/diagnostics.js +38 -0
- package/dist/palettes/diagnostics.js.map +1 -0
- package/dist/palettes/index.d.ts +61 -0
- package/dist/palettes/index.js +246 -0
- package/dist/palettes/index.js.map +1 -0
- package/dist/render/renderMarkdown.d.ts +3 -1
- package/dist/render/renderMarkdown.js +9 -1
- package/dist/render/renderMarkdown.js.map +1 -1
- package/dist/runtime/index.d.ts +12 -1
- package/dist/runtime/index.js +21 -2
- package/dist/runtime/index.js.map +1 -1
- package/dist/types/core.d.ts +43 -0
- package/dist/types/core.js.map +1 -1
- package/dist/types.d.ts +46 -2
- package/dist/types.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +1 -1
- package/skills/payload-markdown/claude/SKILL.md +1 -0
- package/skills/payload-markdown/claude/reference/directive-spec.json +60 -0
- package/skills/payload-markdown/claude/reference/mcp-content-workflow.md +7 -0
- package/skills/payload-markdown/claude/reference/payload-markdown-directives.md +22 -2
- package/skills/payload-markdown/claude/skill.json +2 -2
- package/skills/payload-markdown/codex/SKILL.md +1 -0
- package/skills/payload-markdown/codex/reference/directive-spec.json +60 -0
- package/skills/payload-markdown/codex/reference/mcp-content-workflow.md +7 -0
- package/skills/payload-markdown/codex/reference/payload-markdown-directives.md +22 -2
- package/skills/payload-markdown/codex/skill.json +2 -2
- package/dist/core/plugins/directiveBadge.d.ts +0 -6
- package/dist/core/plugins/directiveBadge.js +0 -94
- package/dist/core/plugins/directiveBadge.js.map +0 -1
- package/dist/core/plugins/directiveButton.d.ts +0 -6
- package/dist/core/plugins/directiveButton.js +0 -115
- package/dist/core/plugins/directiveButton.js.map +0 -1
- package/dist/directives/buttonSyntax.d.ts +0 -7
- package/dist/directives/buttonSyntax.js +0 -6
- package/dist/directives/buttonSyntax.js.map +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { fromHtml } from 'hast-util-from-html';
|
|
2
2
|
import { toString } from 'hast-util-to-string';
|
|
3
3
|
import rehypeRaw from 'rehype-raw';
|
|
4
|
-
import rehypeSanitize
|
|
4
|
+
import rehypeSanitize from 'rehype-sanitize';
|
|
5
5
|
import rehypeStringify from 'rehype-stringify';
|
|
6
6
|
import remarkGfm from 'remark-gfm';
|
|
7
7
|
import remarkParse from 'remark-parse';
|
|
@@ -22,7 +22,9 @@ import { remarkDirectiveLines } from './plugins/remarkDirectiveLines.js';
|
|
|
22
22
|
import { remarkHeadingAnchorsAndToc } from './plugins/remarkHeadingAnchorsAndToc.js';
|
|
23
23
|
import { remarkLayoutDirectives } from './plugins/remarkLayoutDirectives.js';
|
|
24
24
|
import { remarkValidateDirectiveThemes } from './plugins/remarkValidateDirectiveThemes.js';
|
|
25
|
+
import { remarkValidatePalettes } from './plugins/remarkValidatePalettes.js';
|
|
25
26
|
import { getRenderData } from './renderData.js';
|
|
27
|
+
import { sanitizeSchema } from './sanitizeSchema.js';
|
|
26
28
|
function extractCodeLanguage(className) {
|
|
27
29
|
const classes = typeof className === 'string' ? className.split(/\s+/) : Array.isArray(className) ? className : [];
|
|
28
30
|
return classes.find((c)=>typeof c === 'string' && c.startsWith('language-'))?.slice(9);
|
|
@@ -88,225 +90,6 @@ function rehypeShikiCodeBlocks(options = {}) {
|
|
|
88
90
|
}
|
|
89
91
|
};
|
|
90
92
|
}
|
|
91
|
-
function getAttributeDefinitions(value) {
|
|
92
|
-
return value ?? [];
|
|
93
|
-
}
|
|
94
|
-
const sanitizeSchema = {
|
|
95
|
-
...defaultSchema,
|
|
96
|
-
attributes: {
|
|
97
|
-
...defaultSchema.attributes ?? {},
|
|
98
|
-
a: [
|
|
99
|
-
...getAttributeDefinitions(defaultSchema.attributes?.a ?? []),
|
|
100
|
-
'ariaLabel',
|
|
101
|
-
'className',
|
|
102
|
-
'dataButton',
|
|
103
|
-
'dataDirective',
|
|
104
|
-
'dataDirectiveLink',
|
|
105
|
-
'dataIconPosition',
|
|
106
|
-
'dataSize',
|
|
107
|
-
'dataVariant',
|
|
108
|
-
'dataVlLayout',
|
|
109
|
-
'href',
|
|
110
|
-
'target',
|
|
111
|
-
'rel',
|
|
112
|
-
'title'
|
|
113
|
-
],
|
|
114
|
-
article: [
|
|
115
|
-
...getAttributeDefinitions(defaultSchema.attributes?.article ?? []),
|
|
116
|
-
'className',
|
|
117
|
-
'dataDirective',
|
|
118
|
-
'dataEyebrow',
|
|
119
|
-
'dataHref',
|
|
120
|
-
'dataIcon',
|
|
121
|
-
'dataLinkScope',
|
|
122
|
-
'dataNewTab',
|
|
123
|
-
'dataStepCard',
|
|
124
|
-
'dataTabPanel',
|
|
125
|
-
'dataTabValue',
|
|
126
|
-
'dataTheme',
|
|
127
|
-
'dataTitle',
|
|
128
|
-
'dataVlLayout',
|
|
129
|
-
'hidden',
|
|
130
|
-
'id',
|
|
131
|
-
'role',
|
|
132
|
-
'tabIndex'
|
|
133
|
-
],
|
|
134
|
-
button: [
|
|
135
|
-
...getAttributeDefinitions(defaultSchema.attributes?.button ?? []),
|
|
136
|
-
'ariaControls',
|
|
137
|
-
'ariaSelected',
|
|
138
|
-
'className',
|
|
139
|
-
'dataTabTrigger',
|
|
140
|
-
'dataTabValue',
|
|
141
|
-
'disabled',
|
|
142
|
-
'id',
|
|
143
|
-
'role',
|
|
144
|
-
'tabIndex',
|
|
145
|
-
'type'
|
|
146
|
-
],
|
|
147
|
-
code: [
|
|
148
|
-
...getAttributeDefinitions(defaultSchema.attributes?.code ?? []),
|
|
149
|
-
'className',
|
|
150
|
-
'tabIndex'
|
|
151
|
-
],
|
|
152
|
-
details: [
|
|
153
|
-
...getAttributeDefinitions(defaultSchema.attributes?.details ?? []),
|
|
154
|
-
'dataDirective',
|
|
155
|
-
'dataTheme',
|
|
156
|
-
'dataTitle',
|
|
157
|
-
'dataVlLayout',
|
|
158
|
-
'open'
|
|
159
|
-
],
|
|
160
|
-
div: [
|
|
161
|
-
...getAttributeDefinitions(defaultSchema.attributes?.div ?? []),
|
|
162
|
-
'dataAlign',
|
|
163
|
-
'dataDirective',
|
|
164
|
-
'dataDirectiveBody',
|
|
165
|
-
'dataDirectiveTitle',
|
|
166
|
-
'dataEyebrow',
|
|
167
|
-
'dataHref',
|
|
168
|
-
'dataGap',
|
|
169
|
-
'dataCellTheme',
|
|
170
|
-
'dataIcon',
|
|
171
|
-
'dataTabsList',
|
|
172
|
-
'dataTabPanel',
|
|
173
|
-
'dataTabTrigger',
|
|
174
|
-
'dataTabValue',
|
|
175
|
-
'dataTheme',
|
|
176
|
-
'dataTitle',
|
|
177
|
-
'dataStack',
|
|
178
|
-
'dataVariant',
|
|
179
|
-
'dataVlLayout',
|
|
180
|
-
'dataWrap',
|
|
181
|
-
'dataVlCellHeadingDepth',
|
|
182
|
-
'hidden',
|
|
183
|
-
'id',
|
|
184
|
-
'role',
|
|
185
|
-
'tabIndex'
|
|
186
|
-
],
|
|
187
|
-
h1: [
|
|
188
|
-
...getAttributeDefinitions(defaultSchema.attributes?.h1 ?? []),
|
|
189
|
-
'dataDirectiveTitle',
|
|
190
|
-
'dataHeadingAnchor',
|
|
191
|
-
'id'
|
|
192
|
-
],
|
|
193
|
-
h2: [
|
|
194
|
-
...getAttributeDefinitions(defaultSchema.attributes?.h2 ?? []),
|
|
195
|
-
'dataDirectiveTitle',
|
|
196
|
-
'dataHeadingAnchor',
|
|
197
|
-
'id'
|
|
198
|
-
],
|
|
199
|
-
h3: [
|
|
200
|
-
...getAttributeDefinitions(defaultSchema.attributes?.h3 ?? []),
|
|
201
|
-
'dataDirectiveTitle',
|
|
202
|
-
'dataHeadingAnchor',
|
|
203
|
-
'id'
|
|
204
|
-
],
|
|
205
|
-
h4: [
|
|
206
|
-
...getAttributeDefinitions(defaultSchema.attributes?.h4 ?? []),
|
|
207
|
-
'dataHeadingAnchor',
|
|
208
|
-
'id'
|
|
209
|
-
],
|
|
210
|
-
h5: [
|
|
211
|
-
...getAttributeDefinitions(defaultSchema.attributes?.h5 ?? []),
|
|
212
|
-
'dataHeadingAnchor',
|
|
213
|
-
'id'
|
|
214
|
-
],
|
|
215
|
-
h6: [
|
|
216
|
-
...getAttributeDefinitions(defaultSchema.attributes?.h6 ?? []),
|
|
217
|
-
'dataHeadingAnchor',
|
|
218
|
-
'id'
|
|
219
|
-
],
|
|
220
|
-
img: [
|
|
221
|
-
...getAttributeDefinitions(defaultSchema.attributes?.img ?? []),
|
|
222
|
-
'alt',
|
|
223
|
-
'className',
|
|
224
|
-
'dataDirective',
|
|
225
|
-
'src',
|
|
226
|
-
'title',
|
|
227
|
-
'width',
|
|
228
|
-
'height'
|
|
229
|
-
],
|
|
230
|
-
li: [
|
|
231
|
-
...getAttributeDefinitions(defaultSchema.attributes?.li ?? []),
|
|
232
|
-
'dataStep',
|
|
233
|
-
'dataTocLevel'
|
|
234
|
-
],
|
|
235
|
-
nav: [
|
|
236
|
-
...getAttributeDefinitions(defaultSchema.attributes?.nav ?? []),
|
|
237
|
-
'ariaLabel',
|
|
238
|
-
'dataDirective',
|
|
239
|
-
'dataTheme',
|
|
240
|
-
'dataTitle',
|
|
241
|
-
'dataVlLayout'
|
|
242
|
-
],
|
|
243
|
-
p: [
|
|
244
|
-
...getAttributeDefinitions(defaultSchema.attributes?.p ?? []),
|
|
245
|
-
'className',
|
|
246
|
-
'dataDirectiveEyebrow'
|
|
247
|
-
],
|
|
248
|
-
pre: [
|
|
249
|
-
...getAttributeDefinitions(defaultSchema.attributes?.pre ?? []),
|
|
250
|
-
'className',
|
|
251
|
-
'tabindex'
|
|
252
|
-
],
|
|
253
|
-
section: [
|
|
254
|
-
...getAttributeDefinitions(defaultSchema.attributes?.section ?? []),
|
|
255
|
-
'dataColumns',
|
|
256
|
-
'dataCardTheme',
|
|
257
|
-
'dataCellTheme',
|
|
258
|
-
'dataDefault',
|
|
259
|
-
'dataDisabled',
|
|
260
|
-
'dataDirective',
|
|
261
|
-
'dataHref',
|
|
262
|
-
'dataLabel',
|
|
263
|
-
'dataLinkScope',
|
|
264
|
-
'dataLayout',
|
|
265
|
-
'dataNewTab',
|
|
266
|
-
'dataStack',
|
|
267
|
-
'dataNumbered',
|
|
268
|
-
'dataStepTheme',
|
|
269
|
-
'dataTabTheme',
|
|
270
|
-
'dataTabValue',
|
|
271
|
-
'dataTheme',
|
|
272
|
-
'dataVariant',
|
|
273
|
-
'dataValue',
|
|
274
|
-
'dataVlLayout',
|
|
275
|
-
'dataVlCellHeadingDepth',
|
|
276
|
-
'role'
|
|
277
|
-
],
|
|
278
|
-
span: [
|
|
279
|
-
...getAttributeDefinitions(defaultSchema.attributes?.span ?? []),
|
|
280
|
-
'className',
|
|
281
|
-
'dataPmdIcon',
|
|
282
|
-
'dataPmdIconRef',
|
|
283
|
-
'dataStepNumber',
|
|
284
|
-
'focusable',
|
|
285
|
-
'style'
|
|
286
|
-
],
|
|
287
|
-
summary: [
|
|
288
|
-
...getAttributeDefinitions(defaultSchema.attributes?.summary ?? []),
|
|
289
|
-
'className'
|
|
290
|
-
]
|
|
291
|
-
},
|
|
292
|
-
// Clobber protection for author raw HTML is applied by rehypeTrustBoundary
|
|
293
|
-
// (raw-HTML ids/names get the default `user-content-` prefix there), so the
|
|
294
|
-
// pipeline's own heading, footnote and tab ids stay unprefixed.
|
|
295
|
-
clobberPrefix: '',
|
|
296
|
-
tagNames: [
|
|
297
|
-
...defaultSchema.tagNames ?? [],
|
|
298
|
-
'a',
|
|
299
|
-
'article',
|
|
300
|
-
'br',
|
|
301
|
-
'button',
|
|
302
|
-
'details',
|
|
303
|
-
'img',
|
|
304
|
-
'nav',
|
|
305
|
-
'section',
|
|
306
|
-
'span',
|
|
307
|
-
'summary'
|
|
308
|
-
]
|
|
309
|
-
};
|
|
310
93
|
const FAILED_RENDER_HTML = '<p>Failed to render markdown.</p>';
|
|
311
94
|
/**
|
|
312
95
|
* Headless renderer: compiles markdown to sanitized HTML and returns the
|
|
@@ -315,7 +98,7 @@ const FAILED_RENDER_HTML = '<p>Failed to render markdown.</p>';
|
|
|
315
98
|
*/ export async function renderMarkdownDocument(markdown, config = {}) {
|
|
316
99
|
const pipelineNonce = createPipelineNonce();
|
|
317
100
|
try {
|
|
318
|
-
const file = await unified().use(remarkParse).use(remarkGfm).use(remarkDirectiveLines, config).use(remarkCompileLayouts).use(remarkLayoutDirectives).use(remarkValidateDirectiveThemes, config).use(remarkCollectRenderData).use(remarkHeadingAnchorsAndToc).use(remarkRehype, {
|
|
101
|
+
const file = await unified().use(remarkParse).use(remarkGfm).use(remarkDirectiveLines, config).use(remarkCompileLayouts).use(remarkLayoutDirectives).use(remarkValidateDirectiveThemes, config).use(remarkValidatePalettes, config).use(remarkCollectRenderData).use(remarkHeadingAnchorsAndToc).use(remarkRehype, {
|
|
319
102
|
allowDangerousHtml: true
|
|
320
103
|
}).use(rehypeMarkPipelineElements, pipelineNonce).use(rehypeRaw).use(rehypeTrustBoundary, pipelineNonce).use(rehypeStripAuthoredInlineStyles).use(rehypeShikiCodeBlocks, resolveRenderMarkdownOptions(config)).use(rehypeSanitize, sanitizeSchema).use(rehypeApplyLayoutClasses, config).use(rehypeResolveIcons, config).use(rehypeContentLayout).use(rehypeStringify).process(markdown);
|
|
321
104
|
const data = getRenderData(file);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/core/renderMarkdown.ts"],"sourcesContent":["import type { Element, Parent, Root, RootContent } from 'hast'\nimport type { Schema } from 'hast-util-sanitize'\n\nimport { fromHtml } from 'hast-util-from-html'\nimport { toString } from 'hast-util-to-string'\nimport rehypeRaw from 'rehype-raw'\nimport rehypeSanitize, { defaultSchema } from 'rehype-sanitize'\nimport rehypeStringify from 'rehype-stringify'\nimport remarkGfm from 'remark-gfm'\nimport remarkParse from 'remark-parse'\nimport remarkRehype from 'remark-rehype'\nimport { unified } from 'unified'\nimport { visit } from 'unist-util-visit'\n\nimport type { HeadingAnchor } from '../directives/headingAnchors.js'\nimport type { MarkdownRenderConfig, RenderMarkdownOptions, RenderMarkdownResult } from '../types/core.js'\nimport type { DiagnosticFile, DiagnosticPlace, RenderDiagnostic } from './diagnostics.js'\nimport type { MarkdownLink } from './renderData.js'\n\nimport { resolveRenderMarkdownOptions } from './codeConfig.js'\nimport { highlightCode } from './codeToHtml.js'\nimport { reportDiagnostic, toRenderDiagnostic } from './diagnostics.js'\nimport { rehypeApplyLayoutClasses } from './plugins/rehypeApplyLayoutClasses.js'\nimport { rehypeContentLayout } from './plugins/rehypeContentLayout.js'\nimport { rehypeResolveIcons } from './plugins/rehypeResolveIcons.js'\nimport { rehypeStripAuthoredInlineStyles } from './plugins/rehypeStripAuthoredInlineStyles.js'\nimport {\n createPipelineNonce,\n rehypeMarkPipelineElements,\n rehypeTrustBoundary,\n} from './plugins/rehypeTrustBoundary.js'\nimport { remarkCollectRenderData } from './plugins/remarkCollectRenderData.js'\nimport { remarkCompileLayouts } from './plugins/remarkCompileLayouts.js'\nimport { remarkDirectiveLines } from './plugins/remarkDirectiveLines.js'\nimport { remarkHeadingAnchorsAndToc } from './plugins/remarkHeadingAnchorsAndToc.js'\nimport { remarkLayoutDirectives } from './plugins/remarkLayoutDirectives.js'\nimport { remarkValidateDirectiveThemes } from './plugins/remarkValidateDirectiveThemes.js'\nimport { getRenderData } from './renderData.js'\n\nfunction extractCodeLanguage(\n className?: Array<number | string> | boolean | null | number | string ,\n): string | undefined {\n const classes =\n typeof className === 'string'\n ? className.split(/\\s+/)\n : Array.isArray(className)\n ? className\n : []\n\n return classes\n .find((c): c is string => typeof c === 'string' && c.startsWith('language-'))\n ?.slice(9)\n}\n\nfunction isElement(node: RootContent): node is Element {\n return node.type === 'element'\n}\n\nfunction isPreElement(node: RootContent): node is Element {\n return isElement(node) && node.tagName === 'pre'\n}\n\nfunction hasChildren(node: unknown): node is Parent {\n return Boolean(node && typeof node === 'object' && 'children' in node)\n}\n\nfunction findCodeChild(node: Element): Element | undefined {\n return node.children.find(\n (child): child is Element => child.type === 'element' && child.tagName === 'code',\n )\n}\n\nfunction parseHtmlFragment(html: string): RootContent[] {\n const fragment = fromHtml(html, { fragment: true })\n return fragment.children\n}\n\ntype CodeDiagnostic = {\n place?: DiagnosticPlace\n reason: string\n severity: 'info' | 'warning'\n}\n\nfunction rehypeShikiCodeBlocks(options: RenderMarkdownOptions = {}) {\n return async function transformer(tree: Root, file: DiagnosticFile): Promise<void> {\n const work: Array<Promise<CodeDiagnostic[]>> = []\n\n visit(tree, 'element', (node, index, parent) => {\n if (typeof index !== 'number' || !hasChildren(parent) || !isPreElement(node)) return\n\n const codeNode = findCodeChild(node)\n if (!codeNode) return\n\n const code = toString(codeNode)\n const lang = extractCodeLanguage(codeNode.properties?.className)\n const start = node.position?.start\n const place = start ? { column: start.column, line: start.line } : undefined\n\n work.push(\n (async () => {\n const highlighted = await highlightCode(code, {\n ...options,\n lang\n })\n\n const replacementNodes = parseHtmlFragment(highlighted.html)\n parent.children.splice(index, 1, ...replacementNodes)\n\n const info = new Set(highlighted.info ?? [])\n\n return highlighted.warnings.map((reason) => ({\n place,\n reason,\n severity: info.has(reason) ? ('info' as const) : ('warning' as const),\n }))\n })(),\n )\n })\n\n // Report each distinct diagnostic once per render, in document order (the\n // first occurrence's position is kept).\n const seen = new Set<string>()\n\n for (const diagnostic of (await Promise.all(work)).flat()) {\n if (seen.has(diagnostic.reason)) continue\n seen.add(diagnostic.reason)\n\n reportDiagnostic(file, diagnostic.reason, {\n place: diagnostic.place,\n severity: diagnostic.severity,\n source: 'code',\n })\n }\n }\n}\n\ntype SanitizeAttributeValue = boolean | null | number | RegExp | string | undefined\ntype SanitizeAttributeDefinition = [string, ...SanitizeAttributeValue[]] | string\n\nfunction getAttributeDefinitions(\n value: Schema['attributes'] extends infer A\n ? A extends Record<string, infer V>\n ? V\n : never\n : never,\n): SanitizeAttributeDefinition[] {\n return (value as SanitizeAttributeDefinition[] | undefined) ?? []\n}\n\nconst sanitizeSchema: Schema = {\n ...defaultSchema,\n attributes: {\n ...(defaultSchema.attributes ?? {}),\n a: [\n ...getAttributeDefinitions(defaultSchema.attributes?.a ?? []),\n 'ariaLabel',\n 'className',\n 'dataButton',\n 'dataDirective',\n 'dataDirectiveLink',\n 'dataIconPosition',\n 'dataSize',\n 'dataVariant',\n 'dataVlLayout',\n 'href',\n 'target',\n 'rel',\n 'title',\n ],\n article: [\n ...getAttributeDefinitions(defaultSchema.attributes?.article ?? []),\n 'className',\n 'dataDirective',\n 'dataEyebrow',\n 'dataHref',\n 'dataIcon',\n 'dataLinkScope',\n 'dataNewTab',\n 'dataStepCard',\n 'dataTabPanel',\n 'dataTabValue',\n 'dataTheme',\n 'dataTitle',\n 'dataVlLayout',\n 'hidden',\n 'id',\n 'role',\n 'tabIndex',\n ],\n button: [\n ...getAttributeDefinitions(defaultSchema.attributes?.button ?? []),\n 'ariaControls',\n 'ariaSelected',\n 'className',\n 'dataTabTrigger',\n 'dataTabValue',\n 'disabled',\n 'id',\n 'role',\n 'tabIndex',\n 'type',\n ],\n code: [...getAttributeDefinitions(defaultSchema.attributes?.code ?? []), 'className', 'tabIndex'],\n details: [\n ...getAttributeDefinitions(defaultSchema.attributes?.details ?? []),\n 'dataDirective',\n 'dataTheme',\n 'dataTitle',\n 'dataVlLayout',\n 'open',\n ],\n div: [\n ...getAttributeDefinitions(defaultSchema.attributes?.div ?? []),\n 'dataAlign',\n 'dataDirective',\n 'dataDirectiveBody',\n 'dataDirectiveTitle',\n 'dataEyebrow',\n 'dataHref',\n 'dataGap',\n 'dataCellTheme',\n 'dataIcon',\n 'dataTabsList',\n 'dataTabPanel',\n 'dataTabTrigger',\n 'dataTabValue',\n 'dataTheme',\n 'dataTitle',\n 'dataStack',\n 'dataVariant',\n 'dataVlLayout',\n 'dataWrap',\n 'dataVlCellHeadingDepth',\n 'hidden',\n 'id',\n 'role',\n 'tabIndex',\n ],\n h1: [\n ...getAttributeDefinitions(defaultSchema.attributes?.h1 ?? []),\n 'dataDirectiveTitle',\n 'dataHeadingAnchor',\n 'id',\n ],\n h2: [\n ...getAttributeDefinitions(defaultSchema.attributes?.h2 ?? []),\n 'dataDirectiveTitle',\n 'dataHeadingAnchor',\n 'id',\n ],\n h3: [\n ...getAttributeDefinitions(defaultSchema.attributes?.h3 ?? []),\n 'dataDirectiveTitle',\n 'dataHeadingAnchor',\n 'id',\n ],\n h4: [...getAttributeDefinitions(defaultSchema.attributes?.h4 ?? []), 'dataHeadingAnchor', 'id'],\n h5: [...getAttributeDefinitions(defaultSchema.attributes?.h5 ?? []), 'dataHeadingAnchor', 'id'],\n h6: [...getAttributeDefinitions(defaultSchema.attributes?.h6 ?? []), 'dataHeadingAnchor', 'id'],\n img: [\n ...getAttributeDefinitions(defaultSchema.attributes?.img ?? []),\n 'alt',\n 'className',\n 'dataDirective',\n 'src',\n 'title',\n 'width',\n 'height',\n ],\n li: [...getAttributeDefinitions(defaultSchema.attributes?.li ?? []), 'dataStep', 'dataTocLevel'],\n nav: [\n ...getAttributeDefinitions(defaultSchema.attributes?.nav ?? []),\n 'ariaLabel',\n 'dataDirective',\n 'dataTheme',\n 'dataTitle',\n 'dataVlLayout',\n ],\n p: [\n ...getAttributeDefinitions(defaultSchema.attributes?.p ?? []),\n 'className',\n 'dataDirectiveEyebrow',\n ],\n pre: [\n ...getAttributeDefinitions(defaultSchema.attributes?.pre ?? []),\n 'className',\n 'tabindex',\n ],\n section: [\n ...getAttributeDefinitions(defaultSchema.attributes?.section ?? []),\n 'dataColumns',\n 'dataCardTheme',\n 'dataCellTheme',\n 'dataDefault',\n 'dataDisabled',\n 'dataDirective',\n 'dataHref',\n 'dataLabel',\n 'dataLinkScope',\n 'dataLayout',\n 'dataNewTab',\n 'dataStack',\n 'dataNumbered',\n 'dataStepTheme',\n 'dataTabTheme',\n 'dataTabValue',\n 'dataTheme',\n 'dataVariant',\n 'dataValue',\n 'dataVlLayout',\n 'dataVlCellHeadingDepth',\n 'role',\n ],\n span: [\n ...getAttributeDefinitions(defaultSchema.attributes?.span ?? []),\n 'className',\n 'dataPmdIcon',\n 'dataPmdIconRef',\n 'dataStepNumber',\n 'focusable',\n 'style',\n ],\n summary: [...getAttributeDefinitions(defaultSchema.attributes?.summary ?? []), 'className'],\n },\n // Clobber protection for author raw HTML is applied by rehypeTrustBoundary\n // (raw-HTML ids/names get the default `user-content-` prefix there), so the\n // pipeline's own heading, footnote and tab ids stay unprefixed.\n clobberPrefix: '',\n tagNames: [\n ...(defaultSchema.tagNames ?? []),\n 'a',\n 'article',\n 'br',\n 'button',\n 'details',\n 'img',\n 'nav',\n 'section',\n 'span',\n 'summary',\n ],\n}\n\nconst FAILED_RENDER_HTML = '<p>Failed to render markdown.</p>'\n\n/**\n * Structured result of `renderMarkdown`.\n */\nexport type RenderedMarkdown = {\n /** Every diagnostic, in report order, with a source and (when known) a position. */\n diagnostics: RenderDiagnostic[]\n /** Fatal compilation errors; when non-empty, `html` is the failure placeholder. */\n errors: string[]\n /** Heading anchors in document order; `id` is the exact id emitted in `html`. */\n headings: HeadingAnchor[]\n html: string\n /** Authored URLs in document order. */\n links: MarkdownLink[]\n /** Plain text of the document without directive markup or raw HTML. */\n text: string\n /** `diagnostics` messages (kept for compatibility; includes errors). */\n warnings: string[]\n}\n\n/**\n * Headless renderer: compiles markdown to sanitized HTML and returns the\n * structured data the pipeline collects. Pure server code (no CSS, no React);\n * `MarkdownRenderer` is a thin wrapper around it.\n */\nexport async function renderMarkdownDocument(\n markdown: string,\n config: MarkdownRenderConfig = {},\n): Promise<RenderedMarkdown> {\n const pipelineNonce = createPipelineNonce()\n\n try {\n const file = await unified()\n .use(remarkParse)\n .use(remarkGfm)\n .use(remarkDirectiveLines, config)\n .use(remarkCompileLayouts)\n .use(remarkLayoutDirectives)\n .use(remarkValidateDirectiveThemes, config)\n .use(remarkCollectRenderData)\n .use(remarkHeadingAnchorsAndToc)\n .use(remarkRehype, { allowDangerousHtml: true })\n .use(rehypeMarkPipelineElements, pipelineNonce)\n .use(rehypeRaw)\n .use(rehypeTrustBoundary, pipelineNonce)\n .use(rehypeStripAuthoredInlineStyles)\n .use(rehypeShikiCodeBlocks, resolveRenderMarkdownOptions(config))\n .use(rehypeSanitize, sanitizeSchema)\n .use(rehypeApplyLayoutClasses, config)\n .use(rehypeResolveIcons, config)\n .use(rehypeContentLayout)\n .use(rehypeStringify)\n .process(markdown)\n\n const data = getRenderData(file)\n const diagnostics = file.messages.map(toRenderDiagnostic)\n\n return {\n diagnostics,\n errors: [],\n headings: data.headings ?? [],\n html: String(file),\n links: data.links ?? [],\n text: data.text ?? '',\n warnings: diagnostics.map((diagnostic) => diagnostic.message),\n }\n } catch (error) {\n const message = error instanceof Error ? error.message : 'Failed to render markdown.'\n\n // Real compile failures were previously swallowed; log them server-side.\n // eslint-disable-next-line no-console\n console.error('[payload-markdown] Failed to render markdown:', error)\n\n return {\n diagnostics: [{ code: 'render-failed', message, severity: 'error', source: 'render' }],\n errors: [message],\n headings: [],\n html: FAILED_RENDER_HTML,\n links: [],\n text: '',\n warnings: [message],\n }\n }\n}\n\n/**\n * Compiles markdown to sanitized HTML. Same pipeline as `renderMarkdownDocument`,\n * returning only `{ html, warnings, errors }`.\n */\nexport async function compileMarkdown(\n markdown: string,\n config: MarkdownRenderConfig = {},\n): Promise<RenderMarkdownResult> {\n const { errors, html, warnings } = await renderMarkdownDocument(markdown, config)\n\n return { errors, html, warnings }\n}\n"],"names":["fromHtml","toString","rehypeRaw","rehypeSanitize","defaultSchema","rehypeStringify","remarkGfm","remarkParse","remarkRehype","unified","visit","resolveRenderMarkdownOptions","highlightCode","reportDiagnostic","toRenderDiagnostic","rehypeApplyLayoutClasses","rehypeContentLayout","rehypeResolveIcons","rehypeStripAuthoredInlineStyles","createPipelineNonce","rehypeMarkPipelineElements","rehypeTrustBoundary","remarkCollectRenderData","remarkCompileLayouts","remarkDirectiveLines","remarkHeadingAnchorsAndToc","remarkLayoutDirectives","remarkValidateDirectiveThemes","getRenderData","extractCodeLanguage","className","classes","split","Array","isArray","find","c","startsWith","slice","isElement","node","type","isPreElement","tagName","hasChildren","Boolean","findCodeChild","children","child","parseHtmlFragment","html","fragment","rehypeShikiCodeBlocks","options","transformer","tree","file","work","index","parent","codeNode","code","lang","properties","start","position","place","column","line","undefined","push","highlighted","replacementNodes","splice","info","Set","warnings","map","reason","severity","has","seen","diagnostic","Promise","all","flat","add","source","getAttributeDefinitions","value","sanitizeSchema","attributes","a","article","button","details","div","h1","h2","h3","h4","h5","h6","img","li","nav","p","pre","section","span","summary","clobberPrefix","tagNames","FAILED_RENDER_HTML","renderMarkdownDocument","markdown","config","pipelineNonce","use","allowDangerousHtml","process","data","diagnostics","messages","errors","headings","String","links","text","message","error","Error","console","compileMarkdown"],"mappings":"AAGA,SAASA,QAAQ,QAAQ,sBAAqB;AAC9C,SAASC,QAAQ,QAAQ,sBAAqB;AAC9C,OAAOC,eAAe,aAAY;AAClC,OAAOC,kBAAkBC,aAAa,QAAQ,kBAAiB;AAC/D,OAAOC,qBAAqB,mBAAkB;AAC9C,OAAOC,eAAe,aAAY;AAClC,OAAOC,iBAAiB,eAAc;AACtC,OAAOC,kBAAkB,gBAAe;AACxC,SAASC,OAAO,QAAQ,UAAS;AACjC,SAASC,KAAK,QAAQ,mBAAkB;AAOxC,SAASC,4BAA4B,QAAQ,kBAAiB;AAC9D,SAASC,aAAa,QAAQ,kBAAiB;AAC/C,SAASC,gBAAgB,EAAEC,kBAAkB,QAAQ,mBAAkB;AACvE,SAASC,wBAAwB,QAAQ,wCAAuC;AAChF,SAASC,mBAAmB,QAAQ,mCAAkC;AACtE,SAASC,kBAAkB,QAAQ,kCAAiC;AACpE,SAASC,+BAA+B,QAAQ,+CAA8C;AAC9F,SACEC,mBAAmB,EACnBC,0BAA0B,EAC1BC,mBAAmB,QACd,mCAAkC;AACzC,SAASC,uBAAuB,QAAQ,uCAAsC;AAC9E,SAASC,oBAAoB,QAAQ,oCAAmC;AACxE,SAASC,oBAAoB,QAAQ,oCAAmC;AACxE,SAASC,0BAA0B,QAAQ,0CAAyC;AACpF,SAASC,sBAAsB,QAAQ,sCAAqC;AAC5E,SAASC,6BAA6B,QAAQ,6CAA4C;AAC1F,SAASC,aAAa,QAAQ,kBAAiB;AAE/C,SAASC,oBACPC,SAAqE;IAErE,MAAMC,UACJ,OAAOD,cAAc,WACjBA,UAAUE,KAAK,CAAC,SAChBC,MAAMC,OAAO,CAACJ,aACZA,YACA,EAAE;IAEV,OAAOC,QACJI,IAAI,CAAC,CAACC,IAAmB,OAAOA,MAAM,YAAYA,EAAEC,UAAU,CAAC,eAC9DC,MAAM;AACZ;AAEA,SAASC,UAAUC,IAAiB;IACl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sG,GAAG,CAACjH,iBACJmH,OAAO,CAACL;QAEX,MAAMM,OAAO7F,cAAc4B;QAC3B,MAAMkE,cAAclE,KAAKmE,QAAQ,CAAC9C,GAAG,CAAC/D;QAEtC,OAAO;YACL4G;YACAE,QAAQ,EAAE;YACVC,UAAUJ,KAAKI,QAAQ,IAAI,EAAE;YAC7B3E,MAAM4E,OAAOtE;YACbuE,OAAON,KAAKM,KAAK,IAAI,EAAE;YACvBC,MAAMP,KAAKO,IAAI,IAAI;YACnBpD,UAAU8C,YAAY7C,GAAG,CAAC,CAACK,aAAeA,WAAW+C,OAAO;QAC9D;IACF,EAAE,OAAOC,OAAO;QACd,MAAMD,UAAUC,iBAAiBC,QAAQD,MAAMD,OAAO,GAAG;QAEzD,yEAAyE;QACzE,sCAAsC;QACtCG,QAAQF,KAAK,CAAC,iDAAiDA;QAE/D,OAAO;YACLR,aAAa;gBAAC;oBAAE7D,MAAM;oBAAiBoE;oBAASlD,UAAU;oBAASQ,QAAQ;gBAAS;aAAE;YACtFqC,QAAQ;gBAACK;aAAQ;YACjBJ,UAAU,EAAE;YACZ3E,MAAM+D;YACNc,OAAO,EAAE;YACTC,MAAM;YACNpD,UAAU;gBAACqD;aAAQ;QACrB;IACF;AACF;AAEA;;;CAGC,GACD,OAAO,eAAeI,gBACpBlB,QAAgB,EAChBC,SAA+B,CAAC,CAAC;IAEjC,MAAM,EAAEQ,MAAM,EAAE1E,IAAI,EAAE0B,QAAQ,EAAE,GAAG,MAAMsC,uBAAuBC,UAAUC;IAE1E,OAAO;QAAEQ;QAAQ1E;QAAM0B;IAAS;AAClC"}
|
|
1
|
+
{"version":3,"sources":["../../src/core/renderMarkdown.ts"],"sourcesContent":["import type { Element, Parent, Root, RootContent } from 'hast'\n\nimport { fromHtml } from 'hast-util-from-html'\nimport { toString } from 'hast-util-to-string'\nimport rehypeRaw from 'rehype-raw'\nimport rehypeSanitize from 'rehype-sanitize'\nimport rehypeStringify from 'rehype-stringify'\nimport remarkGfm from 'remark-gfm'\nimport remarkParse from 'remark-parse'\nimport remarkRehype from 'remark-rehype'\nimport { unified } from 'unified'\nimport { visit } from 'unist-util-visit'\n\nimport type { HeadingAnchor } from '../directives/headingAnchors.js'\nimport type { MarkdownRenderConfig, RenderMarkdownOptions, RenderMarkdownResult } from '../types/core.js'\nimport type { DiagnosticFile, DiagnosticPlace, RenderDiagnostic } from './diagnostics.js'\nimport type { MarkdownLink } from './renderData.js'\n\nimport { resolveRenderMarkdownOptions } from './codeConfig.js'\nimport { highlightCode } from './codeToHtml.js'\nimport { reportDiagnostic, toRenderDiagnostic } from './diagnostics.js'\nimport { rehypeApplyLayoutClasses } from './plugins/rehypeApplyLayoutClasses.js'\nimport { rehypeContentLayout } from './plugins/rehypeContentLayout.js'\nimport { rehypeResolveIcons } from './plugins/rehypeResolveIcons.js'\nimport { rehypeStripAuthoredInlineStyles } from './plugins/rehypeStripAuthoredInlineStyles.js'\nimport {\n createPipelineNonce,\n rehypeMarkPipelineElements,\n rehypeTrustBoundary,\n} from './plugins/rehypeTrustBoundary.js'\nimport { remarkCollectRenderData } from './plugins/remarkCollectRenderData.js'\nimport { remarkCompileLayouts } from './plugins/remarkCompileLayouts.js'\nimport { remarkDirectiveLines } from './plugins/remarkDirectiveLines.js'\nimport { remarkHeadingAnchorsAndToc } from './plugins/remarkHeadingAnchorsAndToc.js'\nimport { remarkLayoutDirectives } from './plugins/remarkLayoutDirectives.js'\nimport { remarkValidateDirectiveThemes } from './plugins/remarkValidateDirectiveThemes.js'\nimport { remarkValidatePalettes } from './plugins/remarkValidatePalettes.js'\nimport { getRenderData } from './renderData.js'\nimport { sanitizeSchema } from './sanitizeSchema.js'\n\nfunction extractCodeLanguage(\n className?: Array<number | string> | boolean | null | number | string ,\n): string | undefined {\n const classes =\n typeof className === 'string'\n ? className.split(/\\s+/)\n : Array.isArray(className)\n ? className\n : []\n\n return classes\n .find((c): c is string => typeof c === 'string' && c.startsWith('language-'))\n ?.slice(9)\n}\n\nfunction isElement(node: RootContent): node is Element {\n return node.type === 'element'\n}\n\nfunction isPreElement(node: RootContent): node is Element {\n return isElement(node) && node.tagName === 'pre'\n}\n\nfunction hasChildren(node: unknown): node is Parent {\n return Boolean(node && typeof node === 'object' && 'children' in node)\n}\n\nfunction findCodeChild(node: Element): Element | undefined {\n return node.children.find(\n (child): child is Element => child.type === 'element' && child.tagName === 'code',\n )\n}\n\nfunction parseHtmlFragment(html: string): RootContent[] {\n const fragment = fromHtml(html, { fragment: true })\n return fragment.children\n}\n\ntype CodeDiagnostic = {\n place?: DiagnosticPlace\n reason: string\n severity: 'info' | 'warning'\n}\n\nfunction rehypeShikiCodeBlocks(options: RenderMarkdownOptions = {}) {\n return async function transformer(tree: Root, file: DiagnosticFile): Promise<void> {\n const work: Array<Promise<CodeDiagnostic[]>> = []\n\n visit(tree, 'element', (node, index, parent) => {\n if (typeof index !== 'number' || !hasChildren(parent) || !isPreElement(node)) return\n\n const codeNode = findCodeChild(node)\n if (!codeNode) return\n\n const code = toString(codeNode)\n const lang = extractCodeLanguage(codeNode.properties?.className)\n const start = node.position?.start\n const place = start ? { column: start.column, line: start.line } : undefined\n\n work.push(\n (async () => {\n const highlighted = await highlightCode(code, {\n ...options,\n lang\n })\n\n const replacementNodes = parseHtmlFragment(highlighted.html)\n parent.children.splice(index, 1, ...replacementNodes)\n\n const info = new Set(highlighted.info ?? [])\n\n return highlighted.warnings.map((reason) => ({\n place,\n reason,\n severity: info.has(reason) ? ('info' as const) : ('warning' as const),\n }))\n })(),\n )\n })\n\n // Report each distinct diagnostic once per render, in document order (the\n // first occurrence's position is kept).\n const seen = new Set<string>()\n\n for (const diagnostic of (await Promise.all(work)).flat()) {\n if (seen.has(diagnostic.reason)) continue\n seen.add(diagnostic.reason)\n\n reportDiagnostic(file, diagnostic.reason, {\n place: diagnostic.place,\n severity: diagnostic.severity,\n source: 'code',\n })\n }\n }\n}\n\nconst FAILED_RENDER_HTML = '<p>Failed to render markdown.</p>'\n\n/**\n * Structured result of `renderMarkdown`.\n */\nexport type RenderedMarkdown = {\n /** Every diagnostic, in report order, with a source and (when known) a position. */\n diagnostics: RenderDiagnostic[]\n /** Fatal compilation errors; when non-empty, `html` is the failure placeholder. */\n errors: string[]\n /** Heading anchors in document order; `id` is the exact id emitted in `html`. */\n headings: HeadingAnchor[]\n html: string\n /** Authored URLs in document order. */\n links: MarkdownLink[]\n /** Plain text of the document without directive markup or raw HTML. */\n text: string\n /** `diagnostics` messages (kept for compatibility; includes errors). */\n warnings: string[]\n}\n\n/**\n * Headless renderer: compiles markdown to sanitized HTML and returns the\n * structured data the pipeline collects. Pure server code (no CSS, no React);\n * `MarkdownRenderer` is a thin wrapper around it.\n */\nexport async function renderMarkdownDocument(\n markdown: string,\n config: MarkdownRenderConfig = {},\n): Promise<RenderedMarkdown> {\n const pipelineNonce = createPipelineNonce()\n\n try {\n const file = await unified()\n .use(remarkParse)\n .use(remarkGfm)\n .use(remarkDirectiveLines, config)\n .use(remarkCompileLayouts)\n .use(remarkLayoutDirectives)\n .use(remarkValidateDirectiveThemes, config)\n .use(remarkValidatePalettes, config)\n .use(remarkCollectRenderData)\n .use(remarkHeadingAnchorsAndToc)\n .use(remarkRehype, { allowDangerousHtml: true })\n .use(rehypeMarkPipelineElements, pipelineNonce)\n .use(rehypeRaw)\n .use(rehypeTrustBoundary, pipelineNonce)\n .use(rehypeStripAuthoredInlineStyles)\n .use(rehypeShikiCodeBlocks, resolveRenderMarkdownOptions(config))\n .use(rehypeSanitize, sanitizeSchema)\n .use(rehypeApplyLayoutClasses, config)\n .use(rehypeResolveIcons, config)\n .use(rehypeContentLayout)\n .use(rehypeStringify)\n .process(markdown)\n\n const data = getRenderData(file)\n const diagnostics = file.messages.map(toRenderDiagnostic)\n\n return {\n diagnostics,\n errors: [],\n headings: data.headings ?? [],\n html: String(file),\n links: data.links ?? [],\n text: data.text ?? '',\n warnings: diagnostics.map((diagnostic) => diagnostic.message),\n }\n } catch (error) {\n const message = error instanceof Error ? error.message : 'Failed to render markdown.'\n\n // Real compile failures were previously swallowed; log them server-side.\n // eslint-disable-next-line no-console\n console.error('[payload-markdown] Failed to render markdown:', error)\n\n return {\n diagnostics: [{ code: 'render-failed', message, severity: 'error', source: 'render' }],\n errors: [message],\n headings: [],\n html: FAILED_RENDER_HTML,\n links: [],\n text: '',\n warnings: [message],\n }\n }\n}\n\n/**\n * Compiles markdown to sanitized HTML. Same pipeline as `renderMarkdownDocument`,\n * returning only `{ html, warnings, errors }`.\n */\nexport async function compileMarkdown(\n markdown: string,\n config: MarkdownRenderConfig = {},\n): Promise<RenderMarkdownResult> {\n const { errors, html, warnings } = await renderMarkdownDocument(markdown, config)\n\n return { errors, html, warnings }\n}\n"],"names":["fromHtml","toString","rehypeRaw","rehypeSanitize","rehypeStringify","remarkGfm","remarkParse","remarkRehype","unified","visit","resolveRenderMarkdownOptions","highlightCode","reportDiagnostic","toRenderDiagnostic","rehypeApplyLayoutClasses","rehypeContentLayout","rehypeResolveIcons","rehypeStripAuthoredInlineStyles","createPipelineNonce","rehypeMarkPipelineElements","rehypeTrustBoundary","remarkCollectRenderData","remarkCompileLayouts","remarkDirectiveLines","remarkHeadingAnchorsAndToc","remarkLayoutDirectives","remarkValidateDirectiveThemes","remarkValidatePalettes","getRenderData","sanitizeSchema","extractCodeLanguage","className","classes","split","Array","isArray","find","c","startsWith","slice","isElement","node","type","isPreElement","tagName","hasChildren","Boolean","findCodeChild","children","child","parseHtmlFragment","html","fragment","rehypeShikiCodeBlocks","options","transformer","tree","file","work","index","parent","codeNode","code","lang","properties","start","position","place","column","line","undefined","push","highlighted","replacementNodes","splice","info","Set","warnings","map","reason","severity","has","seen","diagnostic","Promise","all","flat","add","source","FAILED_RENDER_HTML","renderMarkdownDocument","markdown","config","pipelineNonce","use","allowDangerousHtml","process","data","diagnostics","messages","errors","headings","String","links","text","message","error","Error","console","compileMarkdown"],"mappings":"AAEA,SAASA,QAAQ,QAAQ,sBAAqB;AAC9C,SAASC,QAAQ,QAAQ,sBAA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,CAAC/E,qBACJ+E,GAAG,CAAC1F,iBACJ4F,OAAO,CAACL;QAEX,MAAMM,OAAOrE,cAAc6B;QAC3B,MAAMyC,cAAczC,KAAK0C,QAAQ,CAACrB,GAAG,CAACjE;QAEtC,OAAO;YACLqF;YACAE,QAAQ,EAAE;YACVC,UAAUJ,KAAKI,QAAQ,IAAI,EAAE;YAC7BlD,MAAMmD,OAAO7C;YACb8C,OAAON,KAAKM,KAAK,IAAI,EAAE;YACvBC,MAAMP,KAAKO,IAAI,IAAI;YACnB3B,UAAUqB,YAAYpB,GAAG,CAAC,CAACK,aAAeA,WAAWsB,OAAO;QAC9D;IACF,EAAE,OAAOC,OAAO;QACd,MAAMD,UAAUC,iBAAiBC,QAAQD,MAAMD,OAAO,GAAG;QAEzD,yEAAyE;QACzE,sCAAsC;QACtCG,QAAQF,KAAK,CAAC,iDAAiDA;QAE/D,OAAO;YACLR,aAAa;gBAAC;oBAAEpC,MAAM;oBAAiB2C;oBAASzB,UAAU;oBAASQ,QAAQ;gBAAS;aAAE;YACtFY,QAAQ;gBAACK;aAAQ;YACjBJ,UAAU,EAAE;YACZlD,MAAMsC;YACNc,OAAO,EAAE;YACTC,MAAM;YACN3B,UAAU;gBAAC4B;aAAQ;QACrB;IACF;AACF;AAEA;;;CAGC,GACD,OAAO,eAAeI,gBACpBlB,QAAgB,EAChBC,SAA+B,CAAC,CAAC;IAEjC,MAAM,EAAEQ,MAAM,EAAEjD,IAAI,EAAE0B,QAAQ,EAAE,GAAG,MAAMa,uBAAuBC,UAAUC;IAE1E,OAAO;QAAEQ;QAAQjD;QAAM0B;IAAS;AAClC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Schema } from 'hast-util-sanitize';
|
|
2
|
+
import type { LayoutDirectiveDefinition } from '../directives/types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The rehype-sanitize schema: the defaults, then the base allowlist, then
|
|
5
|
+
* what each directive declares. A directive's element gets `dataVlLayout`
|
|
6
|
+
* when it carries the layout marker. Default entries come first because the
|
|
7
|
+
* first definition of a property wins (`['className', 'data-footnote-backref']`
|
|
8
|
+
* on `a` keeps restricting link classes).
|
|
9
|
+
*/
|
|
10
|
+
export declare function createSanitizeSchema(definitions: readonly LayoutDirectiveDefinition[]): Schema;
|
|
11
|
+
export declare const sanitizeSchema: Schema;
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { defaultSchema } from 'rehype-sanitize';
|
|
2
|
+
import { layoutDirectiveRegistry } from '../directives/registry.js';
|
|
3
|
+
/**
|
|
4
|
+
* Properties the pipeline keeps beyond rehype-sanitize's defaults that no
|
|
5
|
+
* directive definition declares: heading anchors, footnotes, Shiki output,
|
|
6
|
+
* and hooks of elements that directives build after sanitizing (and that raw
|
|
7
|
+
* HTML may therefore also carry). Directive-specific properties live on the
|
|
8
|
+
* definitions (`sanitize`), see `createSanitizeSchema`.
|
|
9
|
+
*/ const BASE_ATTRIBUTES = {
|
|
10
|
+
a: [
|
|
11
|
+
'dataDirectiveLink',
|
|
12
|
+
'title'
|
|
13
|
+
],
|
|
14
|
+
article: [
|
|
15
|
+
'className',
|
|
16
|
+
'dataStepCard',
|
|
17
|
+
'dataTabPanel',
|
|
18
|
+
'dataTabValue',
|
|
19
|
+
'hidden',
|
|
20
|
+
'id',
|
|
21
|
+
'role',
|
|
22
|
+
'tabIndex'
|
|
23
|
+
],
|
|
24
|
+
button: [
|
|
25
|
+
'ariaControls',
|
|
26
|
+
'ariaSelected',
|
|
27
|
+
'className',
|
|
28
|
+
'dataTabTrigger',
|
|
29
|
+
'dataTabValue',
|
|
30
|
+
'disabled',
|
|
31
|
+
'id',
|
|
32
|
+
'role',
|
|
33
|
+
'tabIndex',
|
|
34
|
+
'type'
|
|
35
|
+
],
|
|
36
|
+
code: [
|
|
37
|
+
'className',
|
|
38
|
+
'tabIndex'
|
|
39
|
+
],
|
|
40
|
+
div: [
|
|
41
|
+
'dataDirectiveBody',
|
|
42
|
+
'dataDirectiveTitle',
|
|
43
|
+
'dataEyebrow',
|
|
44
|
+
'dataHref',
|
|
45
|
+
'dataTabsList',
|
|
46
|
+
'dataTabPanel',
|
|
47
|
+
'dataTabTrigger',
|
|
48
|
+
'dataTabValue',
|
|
49
|
+
'hidden',
|
|
50
|
+
'id',
|
|
51
|
+
'role',
|
|
52
|
+
'tabIndex'
|
|
53
|
+
],
|
|
54
|
+
h1: [
|
|
55
|
+
'dataDirectiveTitle',
|
|
56
|
+
'dataHeadingAnchor',
|
|
57
|
+
'id'
|
|
58
|
+
],
|
|
59
|
+
h2: [
|
|
60
|
+
'dataDirectiveTitle',
|
|
61
|
+
'dataHeadingAnchor',
|
|
62
|
+
'id'
|
|
63
|
+
],
|
|
64
|
+
h3: [
|
|
65
|
+
'dataDirectiveTitle',
|
|
66
|
+
'dataHeadingAnchor',
|
|
67
|
+
'id'
|
|
68
|
+
],
|
|
69
|
+
h4: [
|
|
70
|
+
'dataHeadingAnchor',
|
|
71
|
+
'id'
|
|
72
|
+
],
|
|
73
|
+
h5: [
|
|
74
|
+
'dataHeadingAnchor',
|
|
75
|
+
'id'
|
|
76
|
+
],
|
|
77
|
+
h6: [
|
|
78
|
+
'dataHeadingAnchor',
|
|
79
|
+
'id'
|
|
80
|
+
],
|
|
81
|
+
img: [
|
|
82
|
+
'title',
|
|
83
|
+
'width',
|
|
84
|
+
'height'
|
|
85
|
+
],
|
|
86
|
+
li: [
|
|
87
|
+
'dataStep'
|
|
88
|
+
],
|
|
89
|
+
p: [
|
|
90
|
+
'className',
|
|
91
|
+
'dataDirectiveEyebrow'
|
|
92
|
+
],
|
|
93
|
+
pre: [
|
|
94
|
+
'className',
|
|
95
|
+
'tabindex'
|
|
96
|
+
],
|
|
97
|
+
section: [
|
|
98
|
+
'dataCellTheme',
|
|
99
|
+
'dataStack',
|
|
100
|
+
'dataTabValue',
|
|
101
|
+
'dataVlCellHeadingDepth',
|
|
102
|
+
'role'
|
|
103
|
+
],
|
|
104
|
+
span: [
|
|
105
|
+
'className',
|
|
106
|
+
'dataStepNumber',
|
|
107
|
+
'style'
|
|
108
|
+
],
|
|
109
|
+
summary: [
|
|
110
|
+
'className'
|
|
111
|
+
]
|
|
112
|
+
};
|
|
113
|
+
/** Tag names allowed beyond the defaults that no definition brings in. */ const BASE_TAG_NAMES = [
|
|
114
|
+
'br',
|
|
115
|
+
'button',
|
|
116
|
+
'span',
|
|
117
|
+
'summary'
|
|
118
|
+
];
|
|
119
|
+
function getDefaultAttributeDefinitions(tagName) {
|
|
120
|
+
return defaultSchema.attributes?.[tagName] ?? [];
|
|
121
|
+
}
|
|
122
|
+
function addAll(target, tagName, names) {
|
|
123
|
+
const list = target.get(tagName) ?? [];
|
|
124
|
+
for (const name of names)if (!list.includes(name)) list.push(name);
|
|
125
|
+
target.set(tagName, list);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The rehype-sanitize schema: the defaults, then the base allowlist, then
|
|
129
|
+
* what each directive declares. A directive's element gets `dataVlLayout`
|
|
130
|
+
* when it carries the layout marker. Default entries come first because the
|
|
131
|
+
* first definition of a property wins (`['className', 'data-footnote-backref']`
|
|
132
|
+
* on `a` keeps restricting link classes).
|
|
133
|
+
*/ export function createSanitizeSchema(definitions) {
|
|
134
|
+
const extra = new Map();
|
|
135
|
+
const tagNames = [
|
|
136
|
+
...BASE_TAG_NAMES
|
|
137
|
+
];
|
|
138
|
+
for (const [tagName, names] of Object.entries(BASE_ATTRIBUTES))addAll(extra, tagName, names);
|
|
139
|
+
for (const definition of definitions){
|
|
140
|
+
if (layoutDirectiveRegistry.receivesLayoutMarker(definition, 'container') || layoutDirectiveRegistry.receivesLayoutMarker(definition, 'leaf')) {
|
|
141
|
+
addAll(extra, definition.tagName, [
|
|
142
|
+
'dataVlLayout'
|
|
143
|
+
]);
|
|
144
|
+
tagNames.push(definition.tagName);
|
|
145
|
+
}
|
|
146
|
+
for (const [tagName, names] of Object.entries(definition.sanitize ?? {})){
|
|
147
|
+
if (!names) continue;
|
|
148
|
+
addAll(extra, tagName, names);
|
|
149
|
+
tagNames.push(tagName);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const attributes = {
|
|
153
|
+
...defaultSchema.attributes ?? {}
|
|
154
|
+
};
|
|
155
|
+
for (const [tagName, names] of extra)attributes[tagName] = [
|
|
156
|
+
...getDefaultAttributeDefinitions(tagName),
|
|
157
|
+
...names
|
|
158
|
+
];
|
|
159
|
+
return {
|
|
160
|
+
...defaultSchema,
|
|
161
|
+
attributes,
|
|
162
|
+
// Clobber protection for author raw HTML is applied by rehypeTrustBoundary
|
|
163
|
+
// (raw-HTML ids/names get the default `user-content-` prefix there), so the
|
|
164
|
+
// pipeline's own heading, footnote and tab ids stay unprefixed.
|
|
165
|
+
clobberPrefix: '',
|
|
166
|
+
tagNames: [
|
|
167
|
+
...new Set([
|
|
168
|
+
...defaultSchema.tagNames ?? [],
|
|
169
|
+
...tagNames
|
|
170
|
+
])
|
|
171
|
+
]
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
export const sanitizeSchema = createSanitizeSchema(layoutDirectiveRegistry.all);
|
|
175
|
+
|
|
176
|
+
//# sourceMappingURL=sanitizeSchema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/core/sanitizeSchema.ts"],"sourcesContent":["import type { Schema } from 'hast-util-sanitize'\n\nimport { defaultSchema } from 'rehype-sanitize'\n\nimport type { LayoutDirectiveDefinition } from '../directives/types.js'\n\nimport { layoutDirectiveRegistry } from '../directives/registry.js'\n\ntype SanitizeAttributeValue = boolean | null | number | RegExp | string | undefined\ntype SanitizeAttributeDefinition = [string, ...SanitizeAttributeValue[]] | string\n\n/**\n * Properties the pipeline keeps beyond rehype-sanitize's defaults that no\n * directive definition declares: heading anchors, footnotes, Shiki output,\n * and hooks of elements that directives build after sanitizing (and that raw\n * HTML may therefore also carry). Directive-specific properties live on the\n * definitions (`sanitize`), see `createSanitizeSchema`.\n */\nconst BASE_ATTRIBUTES: Readonly<Record<string, readonly string[]>> = {\n a: ['dataDirectiveLink', 'title'],\n article: [\n 'className',\n 'dataStepCard',\n 'dataTabPanel',\n 'dataTabValue',\n 'hidden',\n 'id',\n 'role',\n 'tabIndex',\n ],\n button: [\n 'ariaControls',\n 'ariaSelected',\n 'className',\n 'dataTabTrigger',\n 'dataTabValue',\n 'disabled',\n 'id',\n 'role',\n 'tabIndex',\n 'type',\n ],\n code: ['className', 'tabIndex'],\n div: [\n 'dataDirectiveBody',\n 'dataDirectiveTitle',\n 'dataEyebrow',\n 'dataHref',\n 'dataTabsList',\n 'dataTabPanel',\n 'dataTabTrigger',\n 'dataTabValue',\n 'hidden',\n 'id',\n 'role',\n 'tabIndex',\n ],\n h1: ['dataDirectiveTitle', 'dataHeadingAnchor', 'id'],\n h2: ['dataDirectiveTitle', 'dataHeadingAnchor', 'id'],\n h3: ['dataDirectiveTitle', 'dataHeadingAnchor', 'id'],\n h4: ['dataHeadingAnchor', 'id'],\n h5: ['dataHeadingAnchor', 'id'],\n h6: ['dataHeadingAnchor', 'id'],\n img: ['title', 'width', 'height'],\n li: ['dataStep'],\n p: ['className', 'dataDirectiveEyebrow'],\n pre: ['className', 'tabindex'],\n section: ['dataCellTheme', 'dataStack', 'dataTabValue', 'dataVlCellHeadingDepth', 'role'],\n span: ['className', 'dataStepNumber', 'style'],\n summary: ['className'],\n}\n\n/** Tag names allowed beyond the defaults that no definition brings in. */\nconst BASE_TAG_NAMES: readonly string[] = ['br', 'button', 'span', 'summary']\n\nfunction getDefaultAttributeDefinitions(tagName: string): SanitizeAttributeDefinition[] {\n return (defaultSchema.attributes?.[tagName] as SanitizeAttributeDefinition[] | undefined) ?? []\n}\n\nfunction addAll(target: Map<string, string[]>, tagName: string, names: readonly string[]) {\n const list = target.get(tagName) ?? []\n\n for (const name of names) if (!list.includes(name)) list.push(name)\n target.set(tagName, list)\n}\n\n/**\n * The rehype-sanitize schema: the defaults, then the base allowlist, then\n * what each directive declares. A directive's element gets `dataVlLayout`\n * when it carries the layout marker. Default entries come first because the\n * first definition of a property wins (`['className', 'data-footnote-backref']`\n * on `a` keeps restricting link classes).\n */\nexport function createSanitizeSchema(definitions: readonly LayoutDirectiveDefinition[]): Schema {\n const extra = new Map<string, string[]>()\n const tagNames: string[] = [...BASE_TAG_NAMES]\n\n for (const [tagName, names] of Object.entries(BASE_ATTRIBUTES)) addAll(extra, tagName, names)\n\n for (const definition of definitions) {\n if (\n layoutDirectiveRegistry.receivesLayoutMarker(definition, 'container') ||\n layoutDirectiveRegistry.receivesLayoutMarker(definition, 'leaf')\n ) {\n addAll(extra, definition.tagName, ['dataVlLayout'])\n tagNames.push(definition.tagName)\n }\n\n for (const [tagName, names] of Object.entries(definition.sanitize ?? {})) {\n if (!names) continue\n\n addAll(extra, tagName, names)\n tagNames.push(tagName)\n }\n }\n\n const attributes: NonNullable<Schema['attributes']> = { ...(defaultSchema.attributes ?? {}) }\n\n for (const [tagName, names] of extra)\n attributes[tagName] = [...getDefaultAttributeDefinitions(tagName), ...names]\n\n return {\n ...defaultSchema,\n attributes,\n // Clobber protection for author raw HTML is applied by rehypeTrustBoundary\n // (raw-HTML ids/names get the default `user-content-` prefix there), so the\n // pipeline's own heading, footnote and tab ids stay unprefixed.\n clobberPrefix: '',\n tagNames: [...new Set([...(defaultSchema.tagNames ?? []), ...tagNames])],\n }\n}\n\nexport const sanitizeSchema: Schema = createSanitizeSchema(layoutDirectiveRegistry.all)\n"],"names":["defaultSchema","layoutDirectiveRegistry","BASE_ATTRIBUTES","a","article","button","code","div","h1","h2","h3","h4","h5","h6","img","li","p","pre","section","span","summary","BASE_TAG_NAMES","getDefaultAttributeDefinitions","tagName","attributes","addAll","target","names","list","get","name","includes","push","set","createSanitizeSchema","definitions","extra","Map","tagNames","Object","entries","definition","receivesLayoutMarker","sanitize","clobberPrefix","Set","sanitizeSchema","all"],"mappings":"AAEA,SAASA,aAAa,QAAQ,kBAAiB;AAI/C,SAASC,uBAAuB,QAAQ,4BAA2B;AAKnE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