@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.
- package/package.json +1 -1
- package/src/docx.ts +47 -28
package/package.json
CHANGED
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:
|
|
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
|
-
|
|
487
|
-
|
|
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
|
-
|
|
490
|
-
|
|
491
|
-
|
|
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
|
-
|
|
496
|
-
|
|
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
|
-
|
|
499
|
-
|
|
509
|
+
// 5. Process images
|
|
510
|
+
step = "process_images";
|
|
511
|
+
const imagesProcessed = await processImages(client, docToken, markdown, inserted);
|
|
500
512
|
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
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.
|
|
514
|
-
const
|
|
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
|
-
//
|
|
538
|
+
// 3. Insert blocks (unsupported types like Table are filtered)
|
|
520
539
|
const { children: inserted, skipped } = await insertBlocks(client, docToken, blocks);
|
|
521
540
|
|
|
522
|
-
//
|
|
541
|
+
// 4. Process images
|
|
523
542
|
const imagesProcessed = await processImages(client, docToken, markdown, inserted);
|
|
524
543
|
|
|
525
544
|
return {
|