@max1874/feishu 0.2.4 → 0.2.6
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 +60 -34
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 {
|
|
@@ -188,12 +185,19 @@ async function insertBlocks(
|
|
|
188
185
|
return { children: [], skipped };
|
|
189
186
|
}
|
|
190
187
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
188
|
+
try {
|
|
189
|
+
const res = await client.docx.documentBlockChildren.create({
|
|
190
|
+
path: { document_id: docToken, block_id: blockId },
|
|
191
|
+
data: { children: cleaned },
|
|
192
|
+
});
|
|
193
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
194
|
+
return { children: res.data?.children ?? [], skipped };
|
|
195
|
+
} catch (err) {
|
|
196
|
+
// Log block types for debugging
|
|
197
|
+
const blockTypes = cleaned.map((b) => BLOCK_TYPE_NAMES[b.block_type] || `type_${b.block_type}`);
|
|
198
|
+
const errMsg = err instanceof Error ? err.message : String(err);
|
|
199
|
+
throw new Error(`insertBlocks failed: ${errMsg}. Block types: [${blockTypes.join(", ")}]. Count: ${cleaned.length}`);
|
|
200
|
+
}
|
|
197
201
|
}
|
|
198
202
|
|
|
199
203
|
/** Delete all child blocks from a parent */
|
|
@@ -483,43 +487,65 @@ async function createDoc(
|
|
|
483
487
|
}
|
|
484
488
|
|
|
485
489
|
async function writeDoc(client: Lark.Client, docToken: string, markdown: string) {
|
|
486
|
-
|
|
487
|
-
|
|
490
|
+
let step = "init";
|
|
491
|
+
try {
|
|
492
|
+
// 1. Clear existing content
|
|
493
|
+
step = "clear_content";
|
|
494
|
+
const deleted = await clearDocumentContent(client, docToken);
|
|
495
|
+
|
|
496
|
+
// 2. Pre-process markdown (convert tables to code blocks)
|
|
497
|
+
step = "preprocess_markdown";
|
|
498
|
+
const processedMarkdown = convertTablesToCodeBlocks(markdown);
|
|
499
|
+
|
|
500
|
+
// 3. Convert markdown to blocks
|
|
501
|
+
step = "convert_markdown";
|
|
502
|
+
const res = await client.docx.document.convert({
|
|
503
|
+
data: { content_type: "markdown", content: processedMarkdown },
|
|
504
|
+
});
|
|
505
|
+
if (res.code !== 0) throw new Error(`convert failed: ${res.msg}`);
|
|
506
|
+
const blocks = res.data?.blocks ?? [];
|
|
488
507
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
return { success: true, blocks_deleted: deleted, blocks_added: 0, images_processed: 0 };
|
|
493
|
-
}
|
|
508
|
+
if (blocks.length === 0) {
|
|
509
|
+
return { success: true, blocks_deleted: deleted, blocks_added: 0, images_processed: 0 };
|
|
510
|
+
}
|
|
494
511
|
|
|
495
|
-
|
|
496
|
-
|
|
512
|
+
// 4. Insert new blocks (unsupported types like Table are filtered)
|
|
513
|
+
step = "insert_blocks";
|
|
514
|
+
const { children: inserted, skipped } = await insertBlocks(client, docToken, blocks);
|
|
497
515
|
|
|
498
|
-
|
|
499
|
-
|
|
516
|
+
// 5. Process images
|
|
517
|
+
step = "process_images";
|
|
518
|
+
const imagesProcessed = await processImages(client, docToken, markdown, inserted);
|
|
500
519
|
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
520
|
+
return {
|
|
521
|
+
success: true,
|
|
522
|
+
blocks_deleted: deleted,
|
|
523
|
+
blocks_added: inserted.length,
|
|
524
|
+
images_processed: imagesProcessed,
|
|
525
|
+
...(skipped.length > 0 && {
|
|
526
|
+
warning: `Skipped unsupported block types: ${skipped.join(", ")}. Tables are not supported via this API.`,
|
|
527
|
+
}),
|
|
528
|
+
};
|
|
529
|
+
} catch (err) {
|
|
530
|
+
const errMsg = err instanceof Error ? err.message : String(err);
|
|
531
|
+
throw new Error(`writeDoc failed at step '${step}': ${errMsg}`);
|
|
532
|
+
}
|
|
510
533
|
}
|
|
511
534
|
|
|
512
535
|
async function appendDoc(client: Lark.Client, docToken: string, markdown: string) {
|
|
513
|
-
// 1.
|
|
514
|
-
const
|
|
536
|
+
// 1. Pre-process markdown (convert tables to code blocks)
|
|
537
|
+
const processedMarkdown = convertTablesToCodeBlocks(markdown);
|
|
538
|
+
|
|
539
|
+
// 2. Convert markdown to blocks
|
|
540
|
+
const { blocks } = await convertMarkdown(client, processedMarkdown);
|
|
515
541
|
if (blocks.length === 0) {
|
|
516
542
|
throw new Error("Content is empty");
|
|
517
543
|
}
|
|
518
544
|
|
|
519
|
-
//
|
|
545
|
+
// 3. Insert blocks (unsupported types like Table are filtered)
|
|
520
546
|
const { children: inserted, skipped } = await insertBlocks(client, docToken, blocks);
|
|
521
547
|
|
|
522
|
-
//
|
|
548
|
+
// 4. Process images
|
|
523
549
|
const imagesProcessed = await processImages(client, docToken, markdown, inserted);
|
|
524
550
|
|
|
525
551
|
return {
|