@emdash-cms/gutenberg-to-portable-text 0.0.1

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.
@@ -0,0 +1,18 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __defProp = Object.defineProperty;
3
+ var __exportAll = (all, no_symbols) => {
4
+ let target = {};
5
+ for (var name in all) {
6
+ __defProp(target, name, {
7
+ get: all[name],
8
+ enumerable: true
9
+ });
10
+ }
11
+ if (!no_symbols) {
12
+ __defProp(target, Symbol.toStringTag, { value: "Module" });
13
+ }
14
+ return target;
15
+ };
16
+
17
+ //#endregion
18
+ export { __exportAll as t };
@@ -0,0 +1,431 @@
1
+ //#region src/types.d.ts
2
+ /**
3
+ * Types for Gutenberg to Portable Text conversion
4
+ */
5
+ /**
6
+ * Gutenberg block as parsed by @wordpress/block-serialization-default-parser
7
+ */
8
+ interface GutenbergBlock {
9
+ /** Block name like "core/paragraph" or null for freeform HTML */
10
+ blockName: string | null;
11
+ /** Block attributes from the JSON comment */
12
+ attrs: Record<string, unknown>;
13
+ /** Inner HTML content */
14
+ innerHTML: string;
15
+ /** Nested blocks (for columns, groups, etc.) */
16
+ innerBlocks: GutenbergBlock[];
17
+ /** Content parts between inner blocks */
18
+ innerContent: Array<string | null>;
19
+ }
20
+ /**
21
+ * Portable Text span (inline text with marks)
22
+ */
23
+ interface PortableTextSpan {
24
+ _type: "span";
25
+ _key: string;
26
+ text: string;
27
+ marks?: string[];
28
+ }
29
+ /**
30
+ * Portable Text mark definition (for links, annotations)
31
+ */
32
+ interface PortableTextMarkDef {
33
+ _type: string;
34
+ _key: string;
35
+ [key: string]: unknown;
36
+ }
37
+ /**
38
+ * Portable Text text block
39
+ */
40
+ interface PortableTextTextBlock {
41
+ _type: "block";
42
+ _key: string;
43
+ style?: "normal" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "blockquote";
44
+ listItem?: "bullet" | "number";
45
+ level?: number;
46
+ children: PortableTextSpan[];
47
+ markDefs?: PortableTextMarkDef[];
48
+ }
49
+ /**
50
+ * Portable Text image block
51
+ */
52
+ interface PortableTextImageBlock {
53
+ _type: "image";
54
+ _key: string;
55
+ asset: {
56
+ _type: "reference";
57
+ _ref: string;
58
+ url?: string;
59
+ };
60
+ alt?: string;
61
+ caption?: string;
62
+ alignment?: "left" | "center" | "right" | "wide" | "full";
63
+ link?: string;
64
+ }
65
+ /**
66
+ * Portable Text code block
67
+ */
68
+ interface PortableTextCodeBlock {
69
+ _type: "code";
70
+ _key: string;
71
+ code: string;
72
+ language?: string;
73
+ }
74
+ /**
75
+ * Portable Text embed block (YouTube, Twitter, etc.)
76
+ */
77
+ interface PortableTextEmbedBlock {
78
+ _type: "embed";
79
+ _key: string;
80
+ url: string;
81
+ provider?: string;
82
+ html?: string;
83
+ }
84
+ /**
85
+ * Portable Text gallery block
86
+ */
87
+ interface PortableTextGalleryBlock {
88
+ _type: "gallery";
89
+ _key: string;
90
+ images: Array<{
91
+ _type: "image";
92
+ _key: string;
93
+ asset: {
94
+ _type: "reference";
95
+ _ref: string;
96
+ url?: string;
97
+ };
98
+ alt?: string;
99
+ caption?: string;
100
+ }>;
101
+ columns?: number;
102
+ }
103
+ /**
104
+ * Portable Text columns block
105
+ */
106
+ interface PortableTextColumnsBlock {
107
+ _type: "columns";
108
+ _key: string;
109
+ columns: Array<{
110
+ _type: "column";
111
+ _key: string;
112
+ content: PortableTextBlock[];
113
+ }>;
114
+ }
115
+ /**
116
+ * Portable Text break/divider block
117
+ */
118
+ interface PortableTextBreakBlock {
119
+ _type: "break";
120
+ _key: string;
121
+ style: "lineBreak";
122
+ }
123
+ /**
124
+ * Portable Text table block
125
+ */
126
+ interface PortableTextTableBlock {
127
+ _type: "table";
128
+ _key: string;
129
+ rows: Array<{
130
+ _type: "tableRow";
131
+ _key: string;
132
+ cells: Array<{
133
+ _type: "tableCell";
134
+ _key: string;
135
+ content: PortableTextSpan[];
136
+ markDefs?: PortableTextMarkDef[];
137
+ isHeader?: boolean;
138
+ }>;
139
+ }>;
140
+ hasHeaderRow?: boolean;
141
+ }
142
+ /**
143
+ * Fallback HTML block for unconvertible content
144
+ */
145
+ interface PortableTextHtmlBlock {
146
+ _type: "htmlBlock";
147
+ _key: string;
148
+ html: string;
149
+ originalBlockName?: string | null;
150
+ originalAttrs?: Record<string, unknown>;
151
+ }
152
+ /**
153
+ * Portable Text button block
154
+ */
155
+ interface PortableTextButtonBlock {
156
+ _type: "button";
157
+ _key: string;
158
+ text: string;
159
+ url?: string;
160
+ style?: "default" | "outline" | "fill";
161
+ }
162
+ /**
163
+ * Portable Text buttons container block
164
+ */
165
+ interface PortableTextButtonsBlock {
166
+ _type: "buttons";
167
+ _key: string;
168
+ buttons: PortableTextButtonBlock[];
169
+ layout?: "horizontal" | "vertical";
170
+ }
171
+ /**
172
+ * Portable Text cover block (image/video with text overlay)
173
+ */
174
+ interface PortableTextCoverBlock {
175
+ _type: "cover";
176
+ _key: string;
177
+ backgroundImage?: string;
178
+ backgroundVideo?: string;
179
+ overlayColor?: string;
180
+ overlayOpacity?: number;
181
+ content: PortableTextBlock[];
182
+ minHeight?: string;
183
+ alignment?: "left" | "center" | "right";
184
+ }
185
+ /**
186
+ * Portable Text file download block
187
+ */
188
+ interface PortableTextFileBlock {
189
+ _type: "file";
190
+ _key: string;
191
+ url: string;
192
+ filename?: string;
193
+ showDownloadButton?: boolean;
194
+ }
195
+ /**
196
+ * Portable Text pullquote block
197
+ */
198
+ interface PortableTextPullquoteBlock {
199
+ _type: "pullquote";
200
+ _key: string;
201
+ text: string;
202
+ citation?: string;
203
+ }
204
+ /**
205
+ * Union of all Portable Text block types
206
+ */
207
+ type PortableTextBlock = PortableTextTextBlock | PortableTextImageBlock | PortableTextCodeBlock | PortableTextEmbedBlock | PortableTextGalleryBlock | PortableTextColumnsBlock | PortableTextBreakBlock | PortableTextTableBlock | PortableTextHtmlBlock | PortableTextButtonBlock | PortableTextButtonsBlock | PortableTextCoverBlock | PortableTextFileBlock | PortableTextPullquoteBlock;
208
+ /**
209
+ * Options for the conversion
210
+ */
211
+ interface ConvertOptions {
212
+ /** Map of WordPress media IDs to EmDash media IDs/URLs */
213
+ mediaMap?: Map<number, string>;
214
+ /** Custom block transformers */
215
+ customTransformers?: Record<string, BlockTransformer>;
216
+ /** Whether to generate keys (default: true) */
217
+ generateKeys?: boolean;
218
+ /** Custom key generator */
219
+ keyGenerator?: () => string;
220
+ }
221
+ /**
222
+ * Block transformer function
223
+ */
224
+ type BlockTransformer = (block: GutenbergBlock, options: ConvertOptions, context: TransformContext) => PortableTextBlock[];
225
+ /**
226
+ * Context passed to transformers
227
+ */
228
+ interface TransformContext {
229
+ /** Transform child blocks recursively */
230
+ transformBlocks: (blocks: GutenbergBlock[]) => PortableTextBlock[];
231
+ /** Parse inline HTML to spans */
232
+ parseInlineContent: (html: string) => {
233
+ children: PortableTextSpan[];
234
+ markDefs: PortableTextMarkDef[];
235
+ };
236
+ /** Generate a unique key */
237
+ generateKey: () => string;
238
+ }
239
+ //#endregion
240
+ //#region src/transformers/index.d.ts
241
+ /**
242
+ * Default block transformers for core WordPress blocks
243
+ */
244
+ declare const defaultTransformers: Record<string, BlockTransformer>;
245
+ /**
246
+ * Fallback transformer for unknown blocks
247
+ * Stores the original HTML for manual review
248
+ */
249
+ declare const fallbackTransformer: BlockTransformer;
250
+ declare namespace core_d_exports {
251
+ export { button, buttons, code, columns, cover, file, gallery, group, heading, html, image, list, mediaText, more, nextpage, paragraph, preformatted, pullquote, quote, separator, shortcode, table, verse };
252
+ }
253
+ /**
254
+ * core/paragraph → block with style "normal"
255
+ */
256
+ declare const paragraph: BlockTransformer;
257
+ /**
258
+ * core/heading → block with style "h1"-"h6"
259
+ */
260
+ declare const heading: BlockTransformer;
261
+ /**
262
+ * core/list → blocks with listItem
263
+ *
264
+ * Handles both old format (HTML list) and new format (innerBlocks with list-item)
265
+ */
266
+ declare const list: BlockTransformer;
267
+ /**
268
+ * core/quote → block with style "blockquote"
269
+ */
270
+ declare const quote: BlockTransformer;
271
+ /**
272
+ * core/image → image block
273
+ */
274
+ declare const image: BlockTransformer;
275
+ /**
276
+ * core/code → code block
277
+ */
278
+ declare const code: BlockTransformer;
279
+ /**
280
+ * core/preformatted → code block (no syntax highlighting)
281
+ */
282
+ declare const preformatted: BlockTransformer;
283
+ /**
284
+ * core/separator / core/spacer → break block
285
+ */
286
+ declare const separator: BlockTransformer;
287
+ /**
288
+ * core/gallery → gallery block
289
+ */
290
+ declare const gallery: BlockTransformer;
291
+ /**
292
+ * core/columns → columns block
293
+ */
294
+ declare const columns: BlockTransformer;
295
+ /**
296
+ * core/group → flatten children (no special container)
297
+ */
298
+ declare const group: BlockTransformer;
299
+ /**
300
+ * core/table → table block
301
+ */
302
+ declare const table: BlockTransformer;
303
+ /**
304
+ * core/button → button block
305
+ */
306
+ declare const button: BlockTransformer;
307
+ /**
308
+ * core/buttons → buttons container block
309
+ */
310
+ declare const buttons: BlockTransformer;
311
+ /**
312
+ * core/cover → cover block
313
+ */
314
+ declare const cover: BlockTransformer;
315
+ /**
316
+ * core/file → file block
317
+ */
318
+ declare const file: BlockTransformer;
319
+ /**
320
+ * core/pullquote → pullquote block
321
+ */
322
+ declare const pullquote: BlockTransformer;
323
+ /**
324
+ * core/html → htmlBlock (pass through)
325
+ */
326
+ declare const html: BlockTransformer;
327
+ /**
328
+ * core/verse → code block (preserves whitespace like preformatted)
329
+ */
330
+ declare const verse: BlockTransformer;
331
+ /**
332
+ * core/more → break block with "readMore" style
333
+ */
334
+ declare const more: BlockTransformer;
335
+ /**
336
+ * core/nextpage → break block with page break indicator
337
+ */
338
+ declare const nextpage: BlockTransformer;
339
+ /**
340
+ * core/shortcode → htmlBlock (preserve for manual handling)
341
+ */
342
+ declare const shortcode: BlockTransformer;
343
+ /**
344
+ * core/media-text → columns block with 2 columns
345
+ */
346
+ declare const mediaText: BlockTransformer;
347
+ declare namespace embed_d_exports {
348
+ export { audio, embed, twitter, video, vimeo, youtube };
349
+ }
350
+ /**
351
+ * core/embed and variants → embed block
352
+ */
353
+ declare const embed: BlockTransformer;
354
+ /**
355
+ * core-embed/youtube → embed block
356
+ */
357
+ declare const youtube: BlockTransformer;
358
+ /**
359
+ * core-embed/twitter → embed block
360
+ */
361
+ declare const twitter: BlockTransformer;
362
+ /**
363
+ * core-embed/vimeo → embed block
364
+ */
365
+ declare const vimeo: BlockTransformer;
366
+ /**
367
+ * core/video → embed block (self-hosted video)
368
+ */
369
+ declare const video: BlockTransformer;
370
+ /**
371
+ * core/audio → embed block (self-hosted audio)
372
+ */
373
+ declare const audio: BlockTransformer;
374
+ //#endregion
375
+ //#region src/inline.d.ts
376
+ interface ParseResult {
377
+ children: PortableTextSpan[];
378
+ markDefs: PortableTextMarkDef[];
379
+ }
380
+ /**
381
+ * Parse inline HTML content into Portable Text spans
382
+ */
383
+ declare function parseInlineContent(html: string, generateKey: () => string): ParseResult;
384
+ /**
385
+ * Extract plain text from HTML (for alt text, captions)
386
+ */
387
+ declare function extractText(html: string): string;
388
+ /**
389
+ * Extract alt text from an img element in HTML
390
+ */
391
+ declare function extractAlt(html: string): string | undefined;
392
+ /**
393
+ * Extract caption from a figcaption element
394
+ */
395
+ declare function extractCaption(html: string): string | undefined;
396
+ /**
397
+ * Extract src from an img element
398
+ */
399
+ declare function extractSrc(html: string): string | undefined;
400
+ //#endregion
401
+ //#region src/index.d.ts
402
+ /**
403
+ * Convert WordPress Gutenberg content to Portable Text
404
+ *
405
+ * @param content - WordPress post content (HTML with Gutenberg block comments)
406
+ * @param options - Conversion options
407
+ * @returns Array of Portable Text blocks
408
+ *
409
+ * @example
410
+ * ```ts
411
+ * const portableText = gutenbergToPortableText(`
412
+ * <!-- wp:paragraph -->
413
+ * <p>Hello <strong>world</strong>!</p>
414
+ * <!-- /wp:paragraph -->
415
+ * `);
416
+ * // → [{ _type: "block", style: "normal", children: [...] }]
417
+ * ```
418
+ */
419
+ declare function gutenbergToPortableText(content: string, options?: ConvertOptions): PortableTextBlock[];
420
+ /**
421
+ * Convert plain HTML (classic editor) to Portable Text
422
+ */
423
+ declare function htmlToPortableText(html: string, options?: ConvertOptions): PortableTextBlock[];
424
+ /**
425
+ * Parse Gutenberg blocks without converting to Portable Text
426
+ * Useful for inspection and debugging
427
+ */
428
+ declare function parseGutenbergBlocks(content: string): GutenbergBlock[];
429
+ //#endregion
430
+ export { type BlockTransformer, type ConvertOptions, type GutenbergBlock, type PortableTextBlock, type PortableTextBreakBlock, type PortableTextButtonBlock, type PortableTextButtonsBlock, type PortableTextCodeBlock, type PortableTextColumnsBlock, type PortableTextCoverBlock, type PortableTextEmbedBlock, type PortableTextFileBlock, type PortableTextGalleryBlock, type PortableTextHtmlBlock, type PortableTextImageBlock, type PortableTextMarkDef, type PortableTextPullquoteBlock, type PortableTextSpan, type PortableTextTextBlock, type TransformContext, core_d_exports as coreTransformers, defaultTransformers, embed_d_exports as embedTransformers, extractAlt, extractCaption, extractSrc, extractText, fallbackTransformer, gutenbergToPortableText, htmlToPortableText, parseGutenbergBlocks, parseInlineContent };
431
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/types.ts","../src/transformers/index.ts","../src/transformers/core.ts","../src/transformers/embed.ts","../src/inline.ts","../src/index.ts"],"mappings":";;;;AAOA;;;AAAA,UAAiB,cAAA;EAQH;EANb,SAAA;EAQmB;EANnB,KAAA,EAAO,MAAA;EAFP;EAIA,SAAA;EAFO;EAIP,WAAA,EAAa,cAAA;EAAb;EAEA,YAAA,EAAc,KAAA;AAAA;;;;UAME,gBAAA;EAChB,KAAA;EACA,IAAA;EACA,IAAA;EACA,KAAA;AAAA;;;;UAMgB,mBAAA;EAChB,KAAA;EACA,IAAA;EAAA,CACC,GAAA;AAAA;;;;UAMe,qBAAA;EAChB,KAAA;EACA,IAAA;EACA,KAAA;EACA,QAAA;EACA,KAAA;EACA,QAAA,EAAU,gBAAA;EACV,QAAA,GAAW,mBAAA;AAAA;;;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