@marvalt/wadapter 2.0.0 → 2.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.
package/dist/index.js CHANGED
@@ -2107,6 +2107,205 @@ function getFormById(id) {
2107
2107
  return (gravityFormsStaticData?.forms ?? []).find(f => String(f.id) === key);
2108
2108
  }
2109
2109
 
2110
+ let document;
2111
+ let offset;
2112
+ let output;
2113
+ let stack;
2114
+ const tokenizer = /<!--\s+(\/)?wp:([a-z][a-z0-9_-]*\/)?([a-z][a-z0-9_-]*)\s+({(?:(?=([^}]+|}+(?=})|(?!}\s+\/?-->)[^])*)\5|[^]*?)}\s+)?(\/)?-->/g;
2115
+ function Block(blockName, attrs, innerBlocks, innerHTML, innerContent) {
2116
+ return {
2117
+ blockName,
2118
+ attrs,
2119
+ innerBlocks,
2120
+ innerHTML,
2121
+ innerContent
2122
+ };
2123
+ }
2124
+ function Freeform(innerHTML) {
2125
+ return Block(null, {}, [], innerHTML, [innerHTML]);
2126
+ }
2127
+ function Frame(block, tokenStart, tokenLength, prevOffset, leadingHtmlStart) {
2128
+ return {
2129
+ block,
2130
+ tokenStart,
2131
+ tokenLength,
2132
+ prevOffset: prevOffset || tokenStart + tokenLength,
2133
+ leadingHtmlStart
2134
+ };
2135
+ }
2136
+ const parse = (doc) => {
2137
+ document = doc;
2138
+ offset = 0;
2139
+ output = [];
2140
+ stack = [];
2141
+ tokenizer.lastIndex = 0;
2142
+ do {
2143
+ } while (proceed());
2144
+ return output;
2145
+ };
2146
+ function proceed() {
2147
+ const stackDepth = stack.length;
2148
+ const next = nextToken();
2149
+ const [tokenType, blockName, attrs, startOffset, tokenLength] = next;
2150
+ const leadingHtmlStart = startOffset > offset ? offset : null;
2151
+ switch (tokenType) {
2152
+ case "no-more-tokens":
2153
+ if (0 === stackDepth) {
2154
+ addFreeform();
2155
+ return false;
2156
+ }
2157
+ if (1 === stackDepth) {
2158
+ addBlockFromStack();
2159
+ return false;
2160
+ }
2161
+ while (0 < stack.length) {
2162
+ addBlockFromStack();
2163
+ }
2164
+ return false;
2165
+ case "void-block":
2166
+ if (0 === stackDepth) {
2167
+ if (null !== leadingHtmlStart) {
2168
+ output.push(
2169
+ Freeform(
2170
+ document.substr(
2171
+ leadingHtmlStart,
2172
+ startOffset - leadingHtmlStart
2173
+ )
2174
+ )
2175
+ );
2176
+ }
2177
+ output.push(Block(blockName, attrs, [], "", []));
2178
+ offset = startOffset + tokenLength;
2179
+ return true;
2180
+ }
2181
+ addInnerBlock(
2182
+ Block(blockName, attrs, [], "", []),
2183
+ startOffset,
2184
+ tokenLength
2185
+ );
2186
+ offset = startOffset + tokenLength;
2187
+ return true;
2188
+ case "block-opener":
2189
+ stack.push(
2190
+ Frame(
2191
+ Block(blockName, attrs, [], "", []),
2192
+ startOffset,
2193
+ tokenLength,
2194
+ startOffset + tokenLength,
2195
+ leadingHtmlStart
2196
+ )
2197
+ );
2198
+ offset = startOffset + tokenLength;
2199
+ return true;
2200
+ case "block-closer":
2201
+ if (0 === stackDepth) {
2202
+ addFreeform();
2203
+ return false;
2204
+ }
2205
+ if (1 === stackDepth) {
2206
+ addBlockFromStack(startOffset);
2207
+ offset = startOffset + tokenLength;
2208
+ return true;
2209
+ }
2210
+ const stackTop = stack.pop();
2211
+ const html = document.substr(
2212
+ stackTop.prevOffset,
2213
+ startOffset - stackTop.prevOffset
2214
+ );
2215
+ stackTop.block.innerHTML += html;
2216
+ stackTop.block.innerContent.push(html);
2217
+ stackTop.prevOffset = startOffset + tokenLength;
2218
+ addInnerBlock(
2219
+ stackTop.block,
2220
+ stackTop.tokenStart,
2221
+ stackTop.tokenLength,
2222
+ startOffset + tokenLength
2223
+ );
2224
+ offset = startOffset + tokenLength;
2225
+ return true;
2226
+ default:
2227
+ addFreeform();
2228
+ return false;
2229
+ }
2230
+ }
2231
+ function parseJSON(input) {
2232
+ try {
2233
+ return JSON.parse(input);
2234
+ } catch (e) {
2235
+ return null;
2236
+ }
2237
+ }
2238
+ function nextToken() {
2239
+ const matches = tokenizer.exec(document);
2240
+ if (null === matches) {
2241
+ return ["no-more-tokens", "", null, 0, 0];
2242
+ }
2243
+ const startedAt = matches.index;
2244
+ const [
2245
+ match,
2246
+ closerMatch,
2247
+ namespaceMatch,
2248
+ nameMatch,
2249
+ attrsMatch,
2250
+ ,
2251
+ voidMatch
2252
+ ] = matches;
2253
+ const length = match.length;
2254
+ const isCloser = !!closerMatch;
2255
+ const isVoid = !!voidMatch;
2256
+ const namespace = namespaceMatch || "core/";
2257
+ const name = namespace + nameMatch;
2258
+ const hasAttrs = !!attrsMatch;
2259
+ const attrs = hasAttrs ? parseJSON(attrsMatch) : {};
2260
+ if (isVoid) {
2261
+ return ["void-block", name, attrs, startedAt, length];
2262
+ }
2263
+ if (isCloser) {
2264
+ return ["block-closer", name, null, startedAt, length];
2265
+ }
2266
+ return ["block-opener", name, attrs, startedAt, length];
2267
+ }
2268
+ function addFreeform(rawLength) {
2269
+ const length = document.length - offset;
2270
+ if (0 === length) {
2271
+ return;
2272
+ }
2273
+ output.push(Freeform(document.substr(offset, length)));
2274
+ }
2275
+ function addInnerBlock(block, tokenStart, tokenLength, lastOffset) {
2276
+ const parent = stack[stack.length - 1];
2277
+ parent.block.innerBlocks.push(block);
2278
+ const html = document.substr(
2279
+ parent.prevOffset,
2280
+ tokenStart - parent.prevOffset
2281
+ );
2282
+ if (html) {
2283
+ parent.block.innerHTML += html;
2284
+ parent.block.innerContent.push(html);
2285
+ }
2286
+ parent.block.innerContent.push(null);
2287
+ parent.prevOffset = lastOffset ? lastOffset : tokenStart + tokenLength;
2288
+ }
2289
+ function addBlockFromStack(endOffset) {
2290
+ const { block, leadingHtmlStart, prevOffset, tokenStart } = stack.pop();
2291
+ const html = endOffset ? document.substr(prevOffset, endOffset - prevOffset) : document.substr(prevOffset);
2292
+ if (html) {
2293
+ block.innerHTML += html;
2294
+ block.innerContent.push(html);
2295
+ }
2296
+ if (null !== leadingHtmlStart) {
2297
+ output.push(
2298
+ Freeform(
2299
+ document.substr(
2300
+ leadingHtmlStart,
2301
+ tokenStart - leadingHtmlStart
2302
+ )
2303
+ )
2304
+ );
2305
+ }
2306
+ output.push(block);
2307
+ }
2308
+
2110
2309
  /**
2111
2310
  * @license GPL-3.0-or-later
2112
2311
  *
@@ -2137,6 +2336,8 @@ class WordPressGenerator {
2137
2336
  const data = await client.getAllData({
2138
2337
  per_page: this.config.maxItems || 100,
2139
2338
  _embed: this.config.includeEmbedded || false,
2339
+ // required for content.raw with Gutenberg comments
2340
+ context: 'edit',
2140
2341
  });
2141
2342
  // Create the static data structure matching the existing format
2142
2343
  const staticData = {
@@ -2158,7 +2359,17 @@ class WordPressGenerator {
2158
2359
  },
2159
2360
  // Map the data to the expected structure
2160
2361
  posts: data.posts,
2161
- pages: data.pages,
2362
+ pages: (data.pages || []).map((p) => {
2363
+ let blocks = [];
2364
+ try {
2365
+ const raw = p?.content?.raw || '';
2366
+ blocks = raw ? parse(raw) : [];
2367
+ }
2368
+ catch (e) {
2369
+ console.warn('⚠️ Failed to parse Gutenberg blocks for page', p?.id, e);
2370
+ }
2371
+ return { ...p, blocks };
2372
+ }),
2162
2373
  media: data.media,
2163
2374
  categories: data.categories,
2164
2375
  tags: data.tags,