@truto/truto-jsonata 1.0.44 → 1.0.46

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/esm/index.js CHANGED
@@ -3,9 +3,9 @@ import jsonata from "jsonata";
3
3
 
4
4
  // src/registerJsonataExtensions.ts
5
5
  import {
6
- castArray as castArray5,
6
+ castArray as castArray6,
7
7
  chunk as chunk2,
8
- compact as compact2,
8
+ compact as compact3,
9
9
  difference,
10
10
  filter as filter2,
11
11
  find as find2,
@@ -13,7 +13,7 @@ import {
13
13
  flattenDeep as flattenDeep3,
14
14
  flattenDepth,
15
15
  groupBy as groupBy3,
16
- join as join2,
16
+ join as join3,
17
17
  keyBy,
18
18
  omit as omit2,
19
19
  orderBy,
@@ -2192,8 +2192,98 @@ var convertMarkdownToSlack = (text) => {
2192
2192
  };
2193
2193
  var convertMarkdownToSlack_default = convertMarkdownToSlack;
2194
2194
 
2195
+ // src/functions/convertMdToPdf.ts
2196
+ import pRetry, { AbortError } from "p-retry";
2197
+ import { includes, split, trim } from "lodash-es";
2198
+ function getFilenameFromHeaders(resp, fallback) {
2199
+ const cd = resp.headers.get("content-disposition") || "";
2200
+ const matchStar = cd.match(/filename\*=([^;]+)/i);
2201
+ if (matchStar) {
2202
+ const value = trim(matchStar[1]);
2203
+ const parts = split(value, "''");
2204
+ if (parts.length === 2) {
2205
+ try {
2206
+ return decodeURIComponent(parts[1]);
2207
+ } catch {
2208
+ return parts[1];
2209
+ }
2210
+ }
2211
+ return trim(value, `'"`);
2212
+ }
2213
+ const match = cd.match(/filename="?([^";]+)"?/i);
2214
+ if (match)
2215
+ return match[1];
2216
+ return fallback || "document.pdf";
2217
+ }
2218
+ async function convertMdToPdf(markdown, options = {}, assetHeaders = {}) {
2219
+ const documentParserApiUrl = this.environment.lookup("documentParserApiUrl");
2220
+ const documentParserApiKey = this.environment.lookup("documentParserApiKey");
2221
+ if (!documentParserApiKey) {
2222
+ throw new Error("Document parser API key not found in environment");
2223
+ }
2224
+ if (!documentParserApiUrl) {
2225
+ throw new Error("Document parser API URL not found in environment");
2226
+ }
2227
+ if (!markdown || typeof markdown !== "string") {
2228
+ throw new AbortError("Markdown content is required and must be a string");
2229
+ }
2230
+ const requestBody = {
2231
+ markdown,
2232
+ options,
2233
+ assetHeaders
2234
+ };
2235
+ return await pRetry(async () => {
2236
+ const response = await fetch(`${documentParserApiUrl}/md-to-pdf`, {
2237
+ method: "POST",
2238
+ headers: {
2239
+ Authorization: `Bearer ${documentParserApiKey}`,
2240
+ "Content-Type": "application/json",
2241
+ Accept: "application/pdf",
2242
+ "User-Agent": "truto"
2243
+ },
2244
+ body: JSON.stringify(requestBody)
2245
+ });
2246
+ if (!response.ok) {
2247
+ if (response.status === 429) {
2248
+ throw new Error("Rate limit exceeded");
2249
+ }
2250
+ if (response.status >= 500) {
2251
+ throw new Error(`Server error: ${response.status}`);
2252
+ }
2253
+ const errorText = await response.text();
2254
+ let errorMessage = `PDF conversion failed (${response.status})`;
2255
+ try {
2256
+ const errorData = JSON.parse(errorText);
2257
+ errorMessage = errorData.error || errorMessage;
2258
+ if (errorData.details) {
2259
+ errorMessage += `: ${errorData.details}`;
2260
+ }
2261
+ } catch {
2262
+ errorMessage += `: ${errorText}`;
2263
+ }
2264
+ throw new AbortError(errorMessage);
2265
+ }
2266
+ const contentType = response.headers.get("content-type");
2267
+ if (!contentType || !includes(contentType, "application/pdf")) {
2268
+ throw new AbortError(`Expected PDF but received: ${contentType}`);
2269
+ }
2270
+ const arrayBuffer = await response.arrayBuffer();
2271
+ const filename = getFilenameFromHeaders(response, options.filename);
2272
+ const blob2 = new Blob([arrayBuffer], { type: "application/pdf" });
2273
+ blob2.name = filename;
2274
+ return blob2;
2275
+ }, {
2276
+ retries: 5,
2277
+ maxTimeout: 30000,
2278
+ minTimeout: 2500,
2279
+ onFailedAttempt: (error) => {
2280
+ console.warn(`PDF conversion attempt ${error.attemptNumber} failed:`, error.message);
2281
+ }
2282
+ });
2283
+ }
2284
+
2195
2285
  // src/functions/convertNotionToMarkdown.ts
2196
- import { castArray as castArray3, flatten, map as map3, reject, repeat } from "lodash-es";
2286
+ import { castArray as castArray3, flatten, join as join2, map as map3, reject, repeat } from "lodash-es";
2197
2287
  var formatPlainText = (x) => {
2198
2288
  if (x) {
2199
2289
  if (x.href) {
@@ -2223,7 +2313,7 @@ var formatPlainText = (x) => {
2223
2313
  return textToReturn;
2224
2314
  }
2225
2315
  };
2226
- var convertNotionToMd = function(block, level = 1) {
2316
+ var convertNotionToMd = function(block, level = 1, linkChildPages = false) {
2227
2317
  const n = `
2228
2318
 
2229
2319
  `;
@@ -2235,16 +2325,16 @@ var convertNotionToMd = function(block, level = 1) {
2235
2325
  case "bookmark":
2236
2326
  return `[${data.url}](data.url)${n}`;
2237
2327
  case "bulleted_list_item":
2238
- childData = map3(block.children, (child) => convertNotionToMd(child, level + 1)).join(repeat("\t", level));
2239
- return `- ${plainText.join("")}
2328
+ childData = join2(map3(block.children, (child) => convertNotionToMd(child, level + 1, linkChildPages)), repeat("\t", level));
2329
+ return `- ${join2(plainText, "")}
2240
2330
  ` + (childData ? `${repeat("\t", level)}${childData}` : "");
2241
2331
  case "numbered_list_item":
2242
- childData = map3(block.children, (child) => convertNotionToMd(child, level + 1)).join(repeat("\t", level));
2243
- return `${block.number}. ${plainText.join("")}
2332
+ childData = join2(map3(block.children, (child) => convertNotionToMd(child, level + 1, linkChildPages)), repeat("\t", level));
2333
+ return `${block.number}. ${join2(plainText, "")}
2244
2334
  ` + (childData ? `${repeat("\t", level)}${childData}` : "");
2245
2335
  case "quote":
2246
2336
  case "callout":
2247
- return `> ${plainText.join("")}${n}`;
2337
+ return `> ${join2(plainText, "")}${n}`;
2248
2338
  case "code":
2249
2339
  return `\`\`\`${data.language}
2250
2340
  ${plainText}
@@ -2256,51 +2346,60 @@ ${plainText}
2256
2346
  case "equation":
2257
2347
  return data.expression;
2258
2348
  case "paragraph":
2259
- return plainText.join("") + n;
2349
+ return join2(plainText, "") + n;
2260
2350
  case "video":
2261
2351
  case "pdf":
2262
2352
  case "file":
2263
2353
  return `[${caption.length ? caption : "File"}](${data.file ? data.file.url : data.external ? data.external.url : ""})${n}`;
2264
2354
  case "heading_1":
2265
- return `# ${plainText.join("")}${n}`;
2355
+ return `# ${join2(plainText, "")}${n}`;
2266
2356
  case "heading_2":
2267
- return `## ${plainText.join("")}${n}`;
2357
+ return `## ${join2(plainText, "")}${n}`;
2268
2358
  case "heading_3":
2269
- return `### ${plainText.join("")}${n}`;
2359
+ return `### ${join2(plainText, "")}${n}`;
2270
2360
  case "heading_4":
2271
- return `#### ${plainText.join("")}${n}`;
2361
+ return `#### ${join2(plainText, "")}${n}`;
2272
2362
  case "heading_5":
2273
- return `##### ${plainText.join("")}${n}`;
2363
+ return `##### ${join2(plainText, "")}${n}`;
2274
2364
  case "heading_6":
2275
- return `###### ${plainText.join("")}${n}`;
2365
+ return `###### ${join2(plainText, "")}${n}`;
2276
2366
  case "image":
2277
2367
  return `![${caption.length ? caption : "Image"}](${data.file ? data.file.url : data.external ? data.external.url : ""})${n}`;
2368
+ case "child_page":
2369
+ if (linkChildPages) {
2370
+ const pageTitle = data.title || "Untitled Page";
2371
+ const pageUrl = `https://www.notion.so/${block.id.replace(/-/g, "")}`;
2372
+ return `[${pageTitle}](${pageUrl})${n}`;
2373
+ } else {
2374
+ childData = join2(map3(block.children, (child) => convertNotionToMd(child, level, linkChildPages)), "");
2375
+ return (data.title || "Untitled Page") + n + childData;
2376
+ }
2278
2377
  case "table":
2279
2378
  if (block.children) {
2280
2379
  const firstChild = block.children[0];
2281
2380
  const remainingChildren = block.children.slice(1);
2282
- const header = `| ${firstChild.table_row.cells.map((x) => x.map((y) => formatPlainText(y)).join("")).join(" | ")} |
2381
+ const header = `| ${join2(firstChild.table_row.cells.map((x) => join2(x.map((y) => formatPlainText(y)), "")), " | ")} |
2283
2382
  `;
2284
2383
  const divider = `|${repeat("---|", data.table_width)}
2285
2384
  `;
2286
- const rows = remainingChildren.map((row) => {
2287
- return `| ${row.table_row.cells.map((x) => x.map((y) => formatPlainText(y)).join("")).join(" | ")} |`;
2288
- }).join(`
2385
+ const rows = join2(remainingChildren.map((row) => {
2386
+ return `| ${join2(row.table_row.cells.map((x) => join2(x.map((y) => formatPlainText(y)), "")), " | ")} |`;
2387
+ }), `
2289
2388
  `);
2290
2389
  return `${header}${divider}${rows}${n}`;
2291
2390
  }
2292
2391
  return `Table as CSV
2293
2392
  `;
2294
2393
  case "table_row":
2295
- return `| ${data.cells.map((x) => x.map((y) => formatPlainText(y)).join("")).join(" | ")} |
2394
+ return `| ${join2(data.cells.map((x) => join2(x.map((y) => formatPlainText(y)), "")), " | ")} |
2296
2395
  `;
2297
2396
  case "to_do":
2298
- childData = map3(block.children, (child) => convertNotionToMd(child, level + 1)).join(repeat("\t", level));
2299
- return `- [${data.checked ? "X" : " "}] ${plainText.join("")}
2397
+ childData = join2(map3(block.children, (child) => convertNotionToMd(child, level + 1, linkChildPages)), repeat("\t", level));
2398
+ return `- [${data.checked ? "X" : " "}] ${join2(plainText, "")}
2300
2399
  ` + (childData ? `${repeat("\t", level)}${childData}` : "");
2301
2400
  default:
2302
- childData = map3(block.children, (child) => convertNotionToMd(child)).join("");
2303
- return plainText.join("") + n + childData;
2401
+ childData = join2(map3(block.children, (child) => convertNotionToMd(child, level, linkChildPages)), "");
2402
+ return join2(plainText, "") + n + childData;
2304
2403
  }
2305
2404
  };
2306
2405
  var resolveChildren = function(block, blocks) {
@@ -2350,13 +2449,13 @@ var insertNewLinesBetweenLists = function(blocks) {
2350
2449
  return block;
2351
2450
  }));
2352
2451
  };
2353
- var convertNotionToMarkdown = function(blocks) {
2452
+ var convertNotionToMarkdown = function(blocks, linkChildPages = false) {
2354
2453
  const arrayBlocks = insertNewLinesBetweenLists(castArray3(blocks));
2355
2454
  const parentBlocks = reject(arrayBlocks, (block) => block.parent?.type === "block_id");
2356
2455
  const blocksWithChildren = numberOrderedLists(parentBlocks.map((block) => {
2357
2456
  return resolveChildren(block, arrayBlocks);
2358
2457
  }));
2359
- return blocksWithChildren.map((block) => convertNotionToMd(block)).join("");
2458
+ return join2(blocksWithChildren.map((block) => convertNotionToMd(block, 1, linkChildPages)), "");
2360
2459
  };
2361
2460
  var convertNotionToMarkdown_default = convertNotionToMarkdown;
2362
2461
 
@@ -2716,12 +2815,12 @@ var firstNonEmpty_default = firstNonEmpty;
2716
2815
  // src/functions/generateEmbeddingsCohere.ts
2717
2816
  import { castArray as castArray4, chunk, isEmpty as isEmpty4 } from "lodash-es";
2718
2817
  import pMap from "p-map";
2719
- import pRetry, { AbortError } from "p-retry";
2818
+ import pRetry2, { AbortError as AbortError2 } from "p-retry";
2720
2819
  async function generateEmbeddingsCohere(body, api_key) {
2721
2820
  if (!isEmpty4(body.texts)) {
2722
2821
  const chunks = chunk(castArray4(body.texts), 20);
2723
2822
  return await pMap(chunks, async (chunk2) => {
2724
- return await pRetry(async () => {
2823
+ return await pRetry2(async () => {
2725
2824
  const response = await fetch("https://api.cohere.com/v2/embed", {
2726
2825
  method: "POST",
2727
2826
  headers: {
@@ -2739,7 +2838,7 @@ async function generateEmbeddingsCohere(body, api_key) {
2739
2838
  if (response.status >= 500) {
2740
2839
  throw new Error("Server error");
2741
2840
  }
2742
- throw new AbortError(await response.text());
2841
+ throw new AbortError2(await response.text());
2743
2842
  }
2744
2843
  return await response.json();
2745
2844
  }, { retries: 5, maxTimeout: 5000, minTimeout: 2500 });
@@ -2747,7 +2846,7 @@ async function generateEmbeddingsCohere(body, api_key) {
2747
2846
  concurrency: 1
2748
2847
  });
2749
2848
  } else if (!isEmpty4(body.images)) {
2750
- return await pRetry(async () => {
2849
+ return await pRetry2(async () => {
2751
2850
  const response = await fetch("https://api.cohere.com/v2/embed", {
2752
2851
  method: "POST",
2753
2852
  headers: {
@@ -2765,7 +2864,7 @@ async function generateEmbeddingsCohere(body, api_key) {
2765
2864
  if (response.status >= 500) {
2766
2865
  throw new Error("Server error");
2767
2866
  }
2768
- throw new AbortError(await response.text());
2867
+ throw new AbortError2(await response.text());
2769
2868
  }
2770
2869
  return await response.json();
2771
2870
  }, { retries: 5, maxTimeout: 5000, minTimeout: 2500 });
@@ -2822,6 +2921,18 @@ function jsonParse(str) {
2822
2921
  }
2823
2922
  var jsonParse_default = jsonParse;
2824
2923
 
2924
+ // src/functions/jsonToCsv.ts
2925
+ import { Parser } from "@json2csv/plainjs";
2926
+ import { castArray as castArray5, compact as compact2, isEmpty as isEmpty5 } from "lodash-es";
2927
+ function jsonToCsv(json, options) {
2928
+ const jsonArray = compact2(castArray5(json));
2929
+ if (isEmpty5(jsonArray)) {
2930
+ return "";
2931
+ }
2932
+ const parser = new Parser(options);
2933
+ return parser.parse(jsonArray);
2934
+ }
2935
+
2825
2936
  // src/functions/jsToXml.ts
2826
2937
  import { js2xml } from "xml-js";
2827
2938
  function jsToXml_default(json, options = { compact: true, spaces: 4 }) {
@@ -2849,14 +2960,14 @@ function mostSimilar(value, possibleValues, threshold = 0.8) {
2849
2960
  var mostSimilar_default = mostSimilar;
2850
2961
 
2851
2962
  // src/functions/parseDocument.ts
2852
- import pRetry2, { AbortError as AbortError2 } from "p-retry";
2963
+ import pRetry3, { AbortError as AbortError3 } from "p-retry";
2853
2964
  async function parseDocument(file, fileType) {
2854
2965
  const documentParserApiUrl = this.environment.lookup("documentParserApiUrl");
2855
2966
  const documentParserApiKey = this.environment.lookup("documentParserApiKey");
2856
2967
  if (!documentParserApiKey) {
2857
2968
  throw new Error("API key not found in environment");
2858
2969
  }
2859
- return await pRetry2(async () => {
2970
+ return await pRetry3(async () => {
2860
2971
  const response = await fetch(`${documentParserApiUrl}/parse`, {
2861
2972
  method: "POST",
2862
2973
  headers: {
@@ -2874,7 +2985,7 @@ async function parseDocument(file, fileType) {
2874
2985
  if (response.status >= 500) {
2875
2986
  throw new Error("Server error");
2876
2987
  }
2877
- throw new AbortError2(await response.text());
2988
+ throw new AbortError3(await response.text());
2878
2989
  }
2879
2990
  const data = await response.json();
2880
2991
  return data.content;
@@ -3084,6 +3195,7 @@ function registerJsonataExtensions(expression) {
3084
3195
  expression.registerFunction("difference", function(arr1, arr2) {
3085
3196
  return difference(arr1, arr2);
3086
3197
  });
3198
+ expression.registerFunction("convertMdToPdf", convertMdToPdf);
3087
3199
  expression.registerFunction("toNumber", toNumber_default);
3088
3200
  expression.registerFunction("jsonParse", jsonParse_default);
3089
3201
  expression.registerFunction("getMimeType", getMimeType_default);
@@ -3100,10 +3212,10 @@ function registerJsonataExtensions(expression) {
3100
3212
  expression.registerFunction("jsToXml", jsToXml_default);
3101
3213
  expression.registerFunction("generateEmbeddingsCohere", generateEmbeddingsCohere_default);
3102
3214
  expression.registerFunction("groupBy", function(array, key) {
3103
- return groupBy3(castArray5(array), key);
3215
+ return groupBy3(castArray6(array), key);
3104
3216
  });
3105
3217
  expression.registerFunction("keyBy", function(array, key) {
3106
- return keyBy(castArray5(array), key);
3218
+ return keyBy(castArray6(array), key);
3107
3219
  });
3108
3220
  expression.registerFunction("pick", function(obj, keys) {
3109
3221
  return pick(obj, keys);
@@ -3112,28 +3224,28 @@ function registerJsonataExtensions(expression) {
3112
3224
  return omit2(obj, keys);
3113
3225
  });
3114
3226
  expression.registerFunction("compact", function(arr) {
3115
- return compact2(castArray5(arr));
3227
+ return compact3(castArray6(arr));
3116
3228
  });
3117
3229
  expression.registerFunction("join", function(arr, separator) {
3118
- return join2(castArray5(arr), separator);
3230
+ return join3(castArray6(arr), separator);
3119
3231
  });
3120
3232
  expression.registerFunction("orderBy", function(arr, attr, order) {
3121
- return orderBy(castArray5(arr), attr, order);
3233
+ return orderBy(castArray6(arr), attr, order);
3122
3234
  });
3123
3235
  expression.registerFunction("find", function(arr, attr) {
3124
- return find2(castArray5(arr), attr);
3236
+ return find2(castArray6(arr), attr);
3125
3237
  });
3126
3238
  expression.registerFunction("lofilter", function(arr, attr) {
3127
- return filter2(castArray5(arr), attr);
3239
+ return filter2(castArray6(arr), attr);
3128
3240
  });
3129
3241
  expression.registerFunction("values", function(obj) {
3130
3242
  return values(obj);
3131
3243
  });
3132
3244
  expression.registerFunction("chunk", function(arr, size) {
3133
- return chunk2(castArray5(arr), size);
3245
+ return chunk2(castArray6(arr), size);
3134
3246
  });
3135
3247
  expression.registerFunction("wrap", function(value, wrapper, endWrapper) {
3136
- return join2([wrapper, value, endWrapper || wrapper], "");
3248
+ return join3([wrapper, value, endWrapper || wrapper], "");
3137
3249
  });
3138
3250
  expression.registerFunction("parseDocument", parseDocument_default);
3139
3251
  expression.registerFunction("recursiveCharacterTextSplitter", recursiveCharacterTextSplitter);
@@ -3143,14 +3255,15 @@ function registerJsonataExtensions(expression) {
3143
3255
  expression.registerFunction("parseQuery", parseQuery);
3144
3256
  expression.registerFunction("stringifyQuery", stringifyQuery);
3145
3257
  expression.registerFunction("flatten", function(arr) {
3146
- return flatten2(castArray5(arr));
3258
+ return flatten2(castArray6(arr));
3147
3259
  });
3148
3260
  expression.registerFunction("flattenDeep", function(arr) {
3149
- return flattenDeep3(castArray5(arr));
3261
+ return flattenDeep3(castArray6(arr));
3150
3262
  });
3151
3263
  expression.registerFunction("flattenDepth", function(arr, depth) {
3152
- return flattenDepth(castArray5(arr), depth);
3264
+ return flattenDepth(castArray6(arr), depth);
3153
3265
  });
3266
+ expression.registerFunction("jsonToCsv", jsonToCsv);
3154
3267
  return expression;
3155
3268
  }
3156
3269
 
@@ -3162,4 +3275,4 @@ export {
3162
3275
  trutoJsonata as default
3163
3276
  };
3164
3277
 
3165
- //# debugId=EADA1C7ED032523764756E2164756E21
3278
+ //# debugId=555CAE470CE91F1064756E2164756E21