@max1874/feishu 0.2.2 → 0.2.4
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 +72 -7
package/package.json
CHANGED
package/src/docx.ts
CHANGED
|
@@ -65,11 +65,9 @@ const BLOCK_TYPE_NAMES: Record<number, string> = {
|
|
|
65
65
|
};
|
|
66
66
|
|
|
67
67
|
// Block types that cannot be created via documentBlockChildren.create API
|
|
68
|
-
|
|
69
|
-
//
|
|
70
|
-
|
|
71
|
-
const UNSUPPORTED_CREATE_TYPES = new Set<number>([
|
|
72
|
-
// Empty for now - let's try creating tables directly
|
|
68
|
+
const UNSUPPORTED_CREATE_TYPES = new Set([
|
|
69
|
+
31, // Table - must use different API or workaround
|
|
70
|
+
32, // TableCell - child of Table
|
|
73
71
|
]);
|
|
74
72
|
|
|
75
73
|
/** Clean blocks for insertion (remove unsupported types and read-only fields) */
|
|
@@ -85,7 +83,7 @@ function cleanBlocksForInsert(blocks: any[]): { cleaned: any[]; skipped: string[
|
|
|
85
83
|
return true;
|
|
86
84
|
})
|
|
87
85
|
.map((block) => {
|
|
88
|
-
// Remove read-only fields
|
|
86
|
+
// Remove any read-only fields that might slip through
|
|
89
87
|
if (block.block_type === 31 && block.table?.merge_info) {
|
|
90
88
|
const { merge_info, ...tableRest } = block.table;
|
|
91
89
|
return { ...block, table: tableRest };
|
|
@@ -97,10 +95,77 @@ function cleanBlocksForInsert(blocks: any[]): { cleaned: any[]; skipped: string[
|
|
|
97
95
|
|
|
98
96
|
// ============ Core Functions ============
|
|
99
97
|
|
|
98
|
+
/**
|
|
99
|
+
* Convert Markdown tables to code blocks.
|
|
100
|
+
* Feishu API doesn't support creating Table blocks directly, so we preserve
|
|
101
|
+
* table formatting by wrapping them in code blocks.
|
|
102
|
+
*/
|
|
103
|
+
function convertTablesToCodeBlocks(markdown: string): string {
|
|
104
|
+
const lines = markdown.split("\n");
|
|
105
|
+
const result: string[] = [];
|
|
106
|
+
let tableLines: string[] = [];
|
|
107
|
+
let inTable = false;
|
|
108
|
+
|
|
109
|
+
const isTableLine = (line: string): boolean => {
|
|
110
|
+
const trimmed = line.trim();
|
|
111
|
+
// Table line starts with | or is a separator line like |---|---|
|
|
112
|
+
return trimmed.startsWith("|") && trimmed.endsWith("|");
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const isSeparatorLine = (line: string): boolean => {
|
|
116
|
+
const trimmed = line.trim();
|
|
117
|
+
// Separator line contains only |, -, :, and spaces
|
|
118
|
+
return /^\|[\s\-:|]+\|$/.test(trimmed);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const flushTable = () => {
|
|
122
|
+
if (tableLines.length >= 2) {
|
|
123
|
+
// Check if we have a valid table (at least header + separator)
|
|
124
|
+
const hasSeparator = tableLines.some(isSeparatorLine);
|
|
125
|
+
if (hasSeparator) {
|
|
126
|
+
// Wrap table in code block to preserve formatting
|
|
127
|
+
result.push("```");
|
|
128
|
+
result.push(...tableLines);
|
|
129
|
+
result.push("```");
|
|
130
|
+
} else {
|
|
131
|
+
// Not a valid table, keep as-is
|
|
132
|
+
result.push(...tableLines);
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
// Single line with |, keep as-is
|
|
136
|
+
result.push(...tableLines);
|
|
137
|
+
}
|
|
138
|
+
tableLines = [];
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
for (const line of lines) {
|
|
142
|
+
if (isTableLine(line)) {
|
|
143
|
+
inTable = true;
|
|
144
|
+
tableLines.push(line);
|
|
145
|
+
} else {
|
|
146
|
+
if (inTable) {
|
|
147
|
+
flushTable();
|
|
148
|
+
inTable = false;
|
|
149
|
+
}
|
|
150
|
+
result.push(line);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Flush remaining table if any
|
|
155
|
+
if (inTable) {
|
|
156
|
+
flushTable();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return result.join("\n");
|
|
160
|
+
}
|
|
161
|
+
|
|
100
162
|
/** Convert markdown to Feishu blocks using the Convert API */
|
|
101
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
|
+
|
|
102
167
|
const res = await client.docx.document.convert({
|
|
103
|
-
data: { content_type: "markdown", content:
|
|
168
|
+
data: { content_type: "markdown", content: processedMarkdown },
|
|
104
169
|
});
|
|
105
170
|
if (res.code !== 0) throw new Error(res.msg);
|
|
106
171
|
return {
|