@max1874/feishu 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/docx.ts +47 -28
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@max1874/feishu",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "type": "module",
5
5
  "description": "OpenClaw Feishu/Lark channel plugin",
6
6
  "license": "MIT",
package/src/docx.ts CHANGED
@@ -161,11 +161,8 @@ function convertTablesToCodeBlocks(markdown: string): string {
161
161
 
162
162
  /** Convert markdown to Feishu blocks using the Convert API */
163
163
  async function convertMarkdown(client: Lark.Client, markdown: string) {
164
- // Pre-process: convert tables to code blocks since Feishu API doesn't support Table blocks
165
- const processedMarkdown = convertTablesToCodeBlocks(markdown);
166
-
167
164
  const res = await client.docx.document.convert({
168
- data: { content_type: "markdown", content: processedMarkdown },
165
+ data: { content_type: "markdown", content: markdown },
169
166
  });
170
167
  if (res.code !== 0) throw new Error(res.msg);
171
168
  return {
@@ -483,43 +480,65 @@ async function createDoc(
483
480
  }
484
481
 
485
482
  async function writeDoc(client: Lark.Client, docToken: string, markdown: string) {
486
- // 1. Clear existing content
487
- const deleted = await clearDocumentContent(client, docToken);
483
+ let step = "init";
484
+ try {
485
+ // 1. Clear existing content
486
+ step = "clear_content";
487
+ const deleted = await clearDocumentContent(client, docToken);
488
+
489
+ // 2. Pre-process markdown (convert tables to code blocks)
490
+ step = "preprocess_markdown";
491
+ const processedMarkdown = convertTablesToCodeBlocks(markdown);
492
+
493
+ // 3. Convert markdown to blocks
494
+ step = "convert_markdown";
495
+ const res = await client.docx.document.convert({
496
+ data: { content_type: "markdown", content: processedMarkdown },
497
+ });
498
+ if (res.code !== 0) throw new Error(`convert failed: ${res.msg}`);
499
+ const blocks = res.data?.blocks ?? [];
488
500
 
489
- // 2. Convert markdown to blocks
490
- const { blocks } = await convertMarkdown(client, markdown);
491
- if (blocks.length === 0) {
492
- return { success: true, blocks_deleted: deleted, blocks_added: 0, images_processed: 0 };
493
- }
501
+ if (blocks.length === 0) {
502
+ return { success: true, blocks_deleted: deleted, blocks_added: 0, images_processed: 0 };
503
+ }
494
504
 
495
- // 3. Insert new blocks (unsupported types like Table are filtered)
496
- const { children: inserted, skipped } = await insertBlocks(client, docToken, blocks);
505
+ // 4. Insert new blocks (unsupported types like Table are filtered)
506
+ step = "insert_blocks";
507
+ const { children: inserted, skipped } = await insertBlocks(client, docToken, blocks);
497
508
 
498
- // 4. Process images
499
- const imagesProcessed = await processImages(client, docToken, markdown, inserted);
509
+ // 5. Process images
510
+ step = "process_images";
511
+ const imagesProcessed = await processImages(client, docToken, markdown, inserted);
500
512
 
501
- return {
502
- success: true,
503
- blocks_deleted: deleted,
504
- blocks_added: inserted.length,
505
- images_processed: imagesProcessed,
506
- ...(skipped.length > 0 && {
507
- warning: `Skipped unsupported block types: ${skipped.join(", ")}. Tables are not supported via this API.`,
508
- }),
509
- };
513
+ return {
514
+ success: true,
515
+ blocks_deleted: deleted,
516
+ blocks_added: inserted.length,
517
+ images_processed: imagesProcessed,
518
+ ...(skipped.length > 0 && {
519
+ warning: `Skipped unsupported block types: ${skipped.join(", ")}. Tables are not supported via this API.`,
520
+ }),
521
+ };
522
+ } catch (err) {
523
+ const errMsg = err instanceof Error ? err.message : String(err);
524
+ throw new Error(`writeDoc failed at step '${step}': ${errMsg}`);
525
+ }
510
526
  }
511
527
 
512
528
  async function appendDoc(client: Lark.Client, docToken: string, markdown: string) {
513
- // 1. Convert markdown to blocks
514
- const { blocks } = await convertMarkdown(client, markdown);
529
+ // 1. Pre-process markdown (convert tables to code blocks)
530
+ const processedMarkdown = convertTablesToCodeBlocks(markdown);
531
+
532
+ // 2. Convert markdown to blocks
533
+ const { blocks } = await convertMarkdown(client, processedMarkdown);
515
534
  if (blocks.length === 0) {
516
535
  throw new Error("Content is empty");
517
536
  }
518
537
 
519
- // 2. Insert blocks (unsupported types like Table are filtered)
538
+ // 3. Insert blocks (unsupported types like Table are filtered)
520
539
  const { children: inserted, skipped } = await insertBlocks(client, docToken, blocks);
521
540
 
522
- // 3. Process images
541
+ // 4. Process images
523
542
  const imagesProcessed = await processImages(client, docToken, markdown, inserted);
524
543
 
525
544
  return {